import ( "context" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/googlecloudplatform/k8s-cluster-bundle/pkg/kubeproto" "github.com/googlecloudplatform/k8s-cluster-bundle/pkg/kubeproto/kubeclient" "github.com/googlecloudplatform/kubernetes/pkg/client" "google.golang.org/protobuf/proto" ) // Create a new Kubernetes client c, err := client.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } // Define the namespace to list pods in namespace := "default" // List all pods in the namespace using the Kubernetes client pods, err := c.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{}) if err != nil { log.Fatalf("Failed to list pods: %v", err) } // Convert the list of pods to a protobuf message for serialization podList, err := kubeclient.MakePodList(pods) if err != nil { log.Fatalf("Failed to make pod list: %v", err) } // Serialize the protobuf message to JSON jsonList, err := proto.Marshal(podList) if err != nil { log.Fatalf("Failed to marshal pod list: %v", err) } // Print the JSON list of pods fmt.Println(string(jsonList))
import ( "context" "io/ioutil" "github.com/googlecloudplatform/kubernetes/pkg/client" v1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/kubernetes/scheme" ) // Create a new Kubernetes client c, err := client.NewClient() if err != nil { log.Fatalf("Failed to create client: %v", err) } // Read the YAML manifest file for the deployment data, err := ioutil.ReadFile("deployment.yaml") if err != nil { log.Fatalf("Failed to read YAML: %v", err) } // Parse the YAML file into a deployment object deployment := &v1.Deployment{} if err := yaml.Unmarshal(data, deployment); err != nil { log.Fatalf("Failed to unmarshal YAML: %v", err) } // Create the deployment if it does not exist, or update it if it does _, err = c.AppsV1().Deployments(deployment.Namespace).Create(context.Background(), deployment, metav1.CreateOptions{}) if errors.IsAlreadyExists(err) { _, err = c.AppsV1().Deployments(deployment.Namespace).Update(context.Background(), deployment, metav1.UpdateOptions{}) } if err != nil { log.Fatalf("Failed to create/update deployment: %v", err) }In both examples, the `Client` struct is used to interact with Kubernetes resources, such as pods and deployments. The first example shows how to list all pods in a given namespace using the `List()` function provided by the `CoreV1()` method on the client. The second example shows how to create or update a deployment using a YAML manifest file, by first parsing the YAML into a deployment object and then using the `Create()` or `Update()` functions provided by the `AppsV1()` method on the client. Overall, the `github.com.googlecloudplatform.kubernetes.pkg.client` package library provides a convenient and easy-to-use way for Go developers to interact with Kubernetes resources in their applications.