From b870ac2eaa1cf79e7db2fdf473df9f66c194c255 Mon Sep 17 00:00:00 2001 From: Jimmy Xu Date: Sun, 17 Dec 2017 22:48:29 +0800 Subject: [PATCH] [hyper-provider] 1.fix hyper.NewClient 2.add instanceType 3.fix incrementRefCounters and decrementRefCounters --- manager/resource.go | 8 ++--- providers/hypersh/config.go | 8 ++++- providers/hypersh/hypersh.go | 60 +++++++++++++++++++++++++++++++----- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/manager/resource.go b/manager/resource.go index 55512fd77..801a13dd5 100644 --- a/manager/resource.go +++ b/manager/resource.go @@ -257,11 +257,11 @@ func (rm *ResourceManager) watchSecrets() { func (rm *ResourceManager) incrementRefCounters(p *v1.Pod) { for _, c := range p.Spec.Containers { for _, e := range c.Env { - if e.ValueFrom.ConfigMapKeyRef != nil { + if e.ValueFrom != nil && e.ValueFrom.ConfigMapKeyRef != nil { rm.configMapRef[e.ValueFrom.ConfigMapKeyRef.Name]++ } - if e.ValueFrom.SecretKeyRef != nil { + if e.ValueFrom != nil && e.ValueFrom.SecretKeyRef != nil { rm.secretRef[e.ValueFrom.SecretKeyRef.Name]++ } } @@ -277,11 +277,11 @@ func (rm *ResourceManager) incrementRefCounters(p *v1.Pod) { func (rm *ResourceManager) decrementRefCounters(p *v1.Pod) { for _, c := range p.Spec.Containers { for _, e := range c.Env { - if e.ValueFrom.ConfigMapKeyRef != nil { + if e.ValueFrom != nil && e.ValueFrom.ConfigMapKeyRef != nil { rm.configMapRef[e.ValueFrom.ConfigMapKeyRef.Name]-- } - if e.ValueFrom.SecretKeyRef != nil { + if e.ValueFrom != nil && e.ValueFrom.SecretKeyRef != nil { rm.secretRef[e.ValueFrom.SecretKeyRef.Name]-- } } diff --git a/providers/hypersh/config.go b/providers/hypersh/config.go index 59f8872c6..52f61b075 100644 --- a/providers/hypersh/config.go +++ b/providers/hypersh/config.go @@ -4,8 +4,8 @@ import ( "fmt" "io" - "github.com/virtual-kubelet/virtual-kubelet/providers" "github.com/BurntSushi/toml" + "github.com/virtual-kubelet/virtual-kubelet/providers" ) type providerConfig struct { @@ -15,6 +15,7 @@ type providerConfig struct { OperatingSystem string CPU string Memory string + InstanceType string Pods string } @@ -27,6 +28,11 @@ func (p *HyperProvider) loadConfig(r io.Reader) error { p.accessKey = config.AccessKey p.secretKey = config.SecretKey + p.instanceType = "s4" + if config.InstanceType != "" { + p.instanceType = config.InstanceType + } + // Default to 20 mcpu p.cpu = "20" if config.CPU != "" { diff --git a/providers/hypersh/hypersh.go b/providers/hypersh/hypersh.go index 838da8732..15007fa8a 100644 --- a/providers/hypersh/hypersh.go +++ b/providers/hypersh/hypersh.go @@ -6,8 +6,11 @@ import ( "log" "net/http" "os" + "runtime" "github.com/docker/go-connections/nat" + "github.com/docker/go-connections/sockets" + "github.com/docker/go-connections/tlsconfig" hyper "github.com/hyperhq/hyper-api/client" "github.com/hyperhq/hyper-api/types" "github.com/hyperhq/hyper-api/types/container" @@ -20,11 +23,13 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +var host = "tcp://*.hyper.sh:443" + const ( - host = "https://us-west-1.hyper.sh" - verStr = "v1.23" - containerLabel = "hyper-virtual-kubelet" - nodeLabel = containerLabel + "-node" + verStr = "v1.23" + containerLabel = "hyper-virtual-kubelet" + nodeLabel = containerLabel + "-node" + instanceTypeLabel = "sh_hyper_instancetype" ) // HyperProvider implements the virtual-kubelet provider interface and communicates with hyper.sh APIs. @@ -38,6 +43,7 @@ type HyperProvider struct { secretKey string cpu string memory string + instanceType string pods string } @@ -72,10 +78,21 @@ func NewHyperProvider(config string, rm *manager.ResourceManager, nodeName, oper p.region = r } + if it := os.Getenv("HYPERSH_INSTANCE_TYPE"); it != "" { + p.instanceType = it + } + + host = fmt.Sprintf("tcp://%v.hyper.sh:443", p.region) + httpClient, err := newHTTPClient(host, &tlsconfig.Options{InsecureSkipVerify: false}) + + customHeaders := map[string]string{} + ver := "0.1" + customHeaders["User-Agent"] = fmt.Sprintf("Virtual-Kubelet-Client/%v (%v)", ver, runtime.GOOS) + p.operatingSystem = operatingSystem p.nodeName = nodeName - p.hyperClient, err = hyper.NewClient(host, verStr, http.DefaultClient, nil, p.accessKey, p.secretKey, p.region) + p.hyperClient, err = hyper.NewClient(host, verStr, httpClient, customHeaders, p.accessKey, p.secretKey, p.region) if err != nil { return nil, err } @@ -83,6 +100,31 @@ func NewHyperProvider(config string, rm *manager.ResourceManager, nodeName, oper return &p, nil } +func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) { + if tlsOptions == nil { + // let the api client configure the default transport. + return nil, nil + } + + config, err := tlsconfig.Client(*tlsOptions) + if err != nil { + return nil, err + } + tr := &http.Transport{ + TLSClientConfig: config, + } + proto, addr, _, err := hyper.ParseHost(host) + if err != nil { + return nil, err + } + + sockets.ConfigureTransport(tr, proto, addr) + + return &http.Client{ + Transport: tr, + }, nil +} + // CreatePod accepts a Pod definition and creates // a hyper.sh deployment func (p *HyperProvider) CreatePod(pod *v1.Pod) error { @@ -98,10 +140,12 @@ func (p *HyperProvider) CreatePod(pod *v1.Pod) error { // Iterate over the containers to create and start them. for k, ctr := range containers { containerName := fmt.Sprintf("pod-%s-%s", pod.Name, pod.Spec.Containers[k].Name) + // Add labels to the pod containers. ctr.Labels = map[string]string{ - containerLabel: pod.Name, - nodeLabel: p.nodeName, + containerLabel: pod.Name, + nodeLabel: p.nodeName, + instanceTypeLabel: p.instanceType, } // Create the container. @@ -147,7 +191,7 @@ func (p *HyperProvider) GetPodStatus(namespace, name string) (*v1.PodStatus, err // GetPods returns a list of all pods known to be running within hyper.sh. func (p *HyperProvider) GetPods() ([]*v1.Pod, error) { - filter, err := filters.FromParam(nodeLabel + "=" + p.nodeName) + filter, err := filters.FromParam(fmt.Sprintf("{\"%v\":{\"%v\":true}}", nodeLabel, p.nodeName)) if err != nil { return nil, fmt.Errorf("Creating filter to get containers by node name failed: %v", err) }