import ( "log" "fmt" "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/client/unversioned" ) func main() { config := unversioned.NewInClusterConfig() client, err := unversioned.New(config) if err != nil { log.Fatal(err) } pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "my-pod", }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "my-container", Image: "nginx", }, }, }, } createdPod, err := client.Pods("default").Create(pod) if err != nil { log.Fatal(err) } fmt.Printf("Created Pod: %v\n", createdPod.ObjectMeta.Name) }
import ( "log" "fmt" "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/client/unversioned" ) func main() { config := unversioned.NewInClusterConfig() client, err := unversioned.New(config) if err != nil { log.Fatal(err) } pods, err := client.Pods("").List(api.ListOptions{}) if err != nil { log.Fatal(err) } fmt.Printf("Found %v Pods\n", len(pods.Items)) for _, pod := range pods.Items { fmt.Printf("Pod: %v\n", pod.ObjectMeta.Name) } }This example reads all Pods in the cluster using the `client.Pods("").List(api.ListOptions{})` function. The `api.ListOptions{}` object can be used to filter the list of Pods. In this example, an empty object is used to retrieve all Pods. The number of Pods found is printed along with their names. In both examples, the k8s.io/kubernetes/pkg/client/unversioned package is used to create a Kubernetes client and interact with Pods.