import ( "log" "k8s.io/client-go/kubernetes" "k8s.io/client-go/pkg/api/v1" "k8s.io/client-go/pkg/apis/apps/v1beta1" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) func main() { config, err := clientcmd.BuildConfigFromFlags("", "/path/to/kubeconfig") if err != nil { log.Fatalf("Error building config: %s", err.Error()) } clientset, err := kubernetes.NewForConfig(config) if err != nil { log.Fatalf("Error building clientset: %s", err.Error()) } deployment := &v1beta1.Deployment{ ObjectMeta: v1.ObjectMeta{ Name: "nginx-deployment", }, Spec: v1beta1.DeploymentSpec{ Replicas: int32Ptr(2), Template: v1.PodTemplateSpec{ ObjectMeta: v1.ObjectMeta{ Labels: map[string]string{ "app": "nginx", }, }, Spec: v1.PodSpec{ Containers: []v1.Container{ { Name: "nginx", Image: "nginx", Ports: []v1.ContainerPort{ { ContainerPort: 80, }, }, }, }, }, }, }, } deploymentsClient := clientset.Apps().Deployments("default") _, err = deploymentsClient.Create(deployment) if err != nil { log.Fatalf("Error creating deployment: %s", err.Error()) } } func int32Ptr(i int32) *int32 { return &i }
import ( "log" "k8s.io/client-go/kubernetes" "k8s.io/client-go/pkg/api/v1" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) func main() { config, err := clientcmd.BuildConfigFromFlags("", "/path/to/kubeconfig") if err != nil { log.Fatalf("Error building config: %s", err.Error()) } clientset, err := kubernetes.NewForConfig(config) if err != nil { log.Fatalf("Error building clientset: %s", err.Error()) } service, err := clientset.CoreV1().Services("default").Get("my-service", v1.GetOptions{}) if err != nil { log.Fatalf("Error getting service: %s", err.Error()) } log.Printf("Service %s has IP %s", service.Name, service.Spec.ClusterIP) }This example uses the clientset object to get information about a service named "my-service" in the "default" namespace. It then logs the service's name and IP address.