config files for mock provider (#247)
This commit is contained in:
committed by
Robbie Zhang
parent
5d39a2c292
commit
3e8a1b9bb5
@@ -1,16 +1,25 @@
|
|||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"fmt"
|
|
||||||
"github.com/virtual-kubelet/virtual-kubelet/providers"
|
"github.com/virtual-kubelet/virtual-kubelet/providers"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Provider configuration defaults.
|
||||||
|
defaultCPUCapacity = "20"
|
||||||
|
defaultMemoryCapacity = "100Gi"
|
||||||
|
defaultPodCapacity = "20"
|
||||||
|
)
|
||||||
|
|
||||||
// MockProvider implements the virtual-kubelet provider interface and stores pods in memory.
|
// MockProvider implements the virtual-kubelet provider interface and stores pods in memory.
|
||||||
type MockProvider struct {
|
type MockProvider struct {
|
||||||
nodeName string
|
nodeName string
|
||||||
@@ -18,19 +27,61 @@ type MockProvider struct {
|
|||||||
internalIP string
|
internalIP string
|
||||||
daemonEndpointPort int32
|
daemonEndpointPort int32
|
||||||
pods map[string]*v1.Pod
|
pods map[string]*v1.Pod
|
||||||
|
config MockConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockConfig contains a mock virtual-kubelet's configurable parameters.
|
||||||
|
type MockConfig struct {
|
||||||
|
CPU string `json:"cpu,omitempty"`
|
||||||
|
Memory string `json:"memory,omitempty"`
|
||||||
|
Pods string `json:"pods,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMockProvider creates a new MockProvider
|
// NewMockProvider creates a new MockProvider
|
||||||
func NewMockProvider(nodeName, operatingSystem string, internalIP string, daemonEndpointPort int32) (*MockProvider, error) {
|
func NewMockProvider(providerConfig, nodeName, operatingSystem string, internalIP string, daemonEndpointPort int32) (*MockProvider, error) {
|
||||||
|
config, err := loadConfig(providerConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
provider := MockProvider{
|
provider := MockProvider{
|
||||||
nodeName: nodeName,
|
nodeName: nodeName,
|
||||||
operatingSystem: operatingSystem,
|
operatingSystem: operatingSystem,
|
||||||
internalIP: internalIP,
|
internalIP: internalIP,
|
||||||
daemonEndpointPort: daemonEndpointPort,
|
daemonEndpointPort: daemonEndpointPort,
|
||||||
pods: make(map[string]*v1.Pod),
|
pods: make(map[string]*v1.Pod),
|
||||||
|
config: config,
|
||||||
|
}
|
||||||
|
return &provider, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadConfig loads the given json configuration files.
|
||||||
|
func loadConfig(providerConfig string) (config MockConfig, err error) {
|
||||||
|
data, err := ioutil.ReadFile(providerConfig)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(data, &config)
|
||||||
|
if config.CPU == "" {
|
||||||
|
config.CPU = defaultCPUCapacity
|
||||||
|
}
|
||||||
|
if config.Memory == "" {
|
||||||
|
config.Memory = defaultMemoryCapacity
|
||||||
|
}
|
||||||
|
if config.Pods == "" {
|
||||||
|
config.Memory = defaultPodCapacity
|
||||||
}
|
}
|
||||||
|
|
||||||
return &provider, nil
|
if _, err = resource.ParseQuantity(config.CPU); err != nil {
|
||||||
|
return config, fmt.Errorf("Invalid CPU value %v", config.CPU)
|
||||||
|
}
|
||||||
|
if _, err = resource.ParseQuantity(config.Memory); err != nil {
|
||||||
|
return config, fmt.Errorf("Invalid memory value %v", config.Memory)
|
||||||
|
}
|
||||||
|
if _, err = resource.ParseQuantity(config.Pods); err != nil {
|
||||||
|
return config, fmt.Errorf("Invalid pods value %v", config.Pods)
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreatePod accepts a Pod definition and stores it in memory.
|
// CreatePod accepts a Pod definition and stores it in memory.
|
||||||
@@ -162,11 +213,10 @@ func (p *MockProvider) GetPods() ([]*v1.Pod, error) {
|
|||||||
|
|
||||||
// Capacity returns a resource list containing the capacity limits.
|
// Capacity returns a resource list containing the capacity limits.
|
||||||
func (p *MockProvider) Capacity() v1.ResourceList {
|
func (p *MockProvider) Capacity() v1.ResourceList {
|
||||||
// TODO: These should be configurable
|
|
||||||
return v1.ResourceList{
|
return v1.ResourceList{
|
||||||
"cpu": resource.MustParse("20"),
|
"cpu": resource.MustParse(p.config.CPU),
|
||||||
"memory": resource.MustParse("100Gi"),
|
"memory": resource.MustParse(p.config.Memory),
|
||||||
"pods": resource.MustParse("20"),
|
"pods": resource.MustParse(p.config.Pods),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func New(nodeName, operatingSystem, namespace, kubeConfig, taint, provider, prov
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case "mock":
|
case "mock":
|
||||||
p, err = mock.NewMockProvider(nodeName, operatingSystem, internalIP, daemonEndpointPort)
|
p, err = mock.NewMockProvider(providerConfig, nodeName, operatingSystem, internalIP, daemonEndpointPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user