示例#1
0
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: "my-nginx-pod",
			Labels: map[string]string{
				"tag": "prod",
			},
		},
		Spec: api.PodSpec{
			Containers: []api.Container{
				{
					Name:  "my-nginx-container",
					Image: "nginx:latest",
				},
			},
		},
	})
}
func NewClientWrapper(apiHost string, userName string, passWord string) (*ClientWrapper, error) {
	conf := &unversionedclient.Config{
		Host:   apiHost,
		Prefix: "",
		//Version:    "v1",
		//GroupVersion: &unversionedapi.GroupVersion{"", "v1"},
		QPS:      5.0,
		Burst:    10,
		Username: userName,
		Password: passWord,
		Insecure: true,
	}

	wrapper := &ClientWrapper{
		k8sConfig: conf,
	}

	if client, err := unversionedclient.New(conf); err == nil {
		wrapper.k8sClient = client
		return wrapper, nil
	}

	wrapper.httpClient = &http.Client{
	//Transport: &oauth2.Transport{
	//    Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "aCcessWbU3toKen"}),
	//}
	}

	baseUrl := apiHost
	if m, _ := regexp.MatchString(`(?i)https?://`, apiHost); !m {
		baseUrl = "http://" + apiHost
	}

	if c, err := kubernetes.NewClient(baseUrl, wrapper.httpClient); err == nil {
		return nil, err
	} else {
		wrapper.Client = c
	}

	wrapper.apiUrl = strings.TrimSuffix(baseUrl, "/") + "/api/" + apiVersion

	return wrapper, nil
}