func main() {
	kube, err := kubernetes.NewClient("http://127.0.0.1:8080", &http.Client{
	/*Transport: &oauth2.Transport{
		Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "aCcessWbU3toKen"}),
	}*/})
	if err != nil {
		log.Fatalf("failed to create client: %v", err)
	}
	kube.Run(&api.Pod{
		TypeMeta: api.TypeMeta{
			APIVersion: "v1",
			Kind:       "Pod",
		},
		ObjectMeta: api.ObjectMeta{
			Name: "hello-world",
		},
		Spec: api.PodSpec{
			Containers: []api.Container{
				{
					Name:    "hello",
					Image:   "ubuntu:14.04",
					Command: []string{"/bin/echo", "hello", "world"},
				},
			},
		},
	})
}
Exemple #2
0
func ExampleRun() {
	kube, err := kubernetes.NewClient("example.com", &http.Client{
		Transport: &oauth2.Transport{
			Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "aCcessWbU3toKen"}),
		}})
	if err != nil {
		log.Fatalf("failed to create client: %v", err)
	}
	kube.Run(&api.Pod{
		TypeMeta: api.TypeMeta{
			APIVersion: "v1beta3",
			Kind:       "Pod",
		},
		ObjectMeta: api.ObjectMeta{
			Name: "my-nginx-pod",
			Labels: map[string]string{
				"tag": "prod",
			},
		},
		Spec: api.PodSpec{
			Containers: []api.Container{
				{
					Name:  "my-nginx-container",
					Image: "nginx:latest",
				},
			},
		},
	})
}
Exemple #3
0
// initGCE must be called before initKube
func initKube() error {
	initKubeCalled = true

	// projectID was set by initGCE
	registryPrefix += "/" + projectID
	if !hasCloudPlatformScope() {
		return errors.New("coordinator not running with access to the Cloud Platform scope.")
	}
	httpClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	var err error
	containerService, err = container.New(httpClient)
	if err != nil {
		return fmt.Errorf("could not create client for Google Container Engine: %v", err)
	}

	cluster, err := containerService.Projects.Zones.Clusters.Get(projectID, projectZone, clusterName).Do()
	if err != nil {
		return fmt.Errorf("cluster %q could not be found in project %q, zone %q: %v", clusterName, projectID, projectZone, err)
	}

	// Decode certs
	decode := func(which string, cert string) []byte {
		if err != nil {
			return nil
		}
		s, decErr := base64.StdEncoding.DecodeString(cert)
		if decErr != nil {
			err = fmt.Errorf("error decoding %s cert: %v", which, decErr)
		}
		return []byte(s)
	}
	clientCert := decode("client cert", cluster.MasterAuth.ClientCertificate)
	clientKey := decode("client key", cluster.MasterAuth.ClientKey)
	caCert := decode("cluster cert", cluster.MasterAuth.ClusterCaCertificate)
	if err != nil {
		return err
	}

	// HTTPS client
	cert, err := tls.X509KeyPair(clientCert, clientKey)
	if err != nil {
		return fmt.Errorf("x509 client key pair could not be generated: %v", err)
	}

	// CA Cert from kube master
	caCertPool := x509.NewCertPool()
	caCertPool.AppendCertsFromPEM([]byte(caCert))

	// Setup TLS config
	tlsConfig := &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      caCertPool,
	}
	tlsConfig.BuildNameToCertificate()

	kubeHTTPClient := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: tlsConfig,
		},
	}

	kubeClient, err = kubernetes.NewClient("https://"+cluster.Endpoint, kubeHTTPClient)
	if err != nil {
		return fmt.Errorf("kubernetes HTTP client could not be created: %v", err)
	}

	go kubePool.pollCapacityLoop()
	return nil
}