import ( "fmt" "k8s.io/kubernetes/pkg/kubectl/cmd/util/factory" ) func createConfigMap() { // Get the factory object f := factory.NewFactory() // Create a new ConfigMap object cm, err := f.Core().V1().ConfigMaps().Create(&corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "my-configmap", Namespace: "default", }, Data: map[string]string{ "key1": "value1", "key2": "value2", }, }) if err != nil { fmt.Printf("Error creating ConfigMap: %v", err) return } fmt.Printf("Successfully created ConfigMap: %v", cm) }
import ( "fmt" "k8s.io/kubernetes/pkg/kubectl/cmd/util/factory" "k8s.io/kubernetes/pkg/apis/apps/v1" ) func createDeployment() { // Get the factory object f := factory.NewFactory() // Create a new Deployment object dep, err := f.Apps().V1().Deployments().Create(&v1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "my-deployment", Namespace: "default", }, Spec: v1.DeploymentSpec{ Replicas: int32(2), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ "app": "my-app", }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ "app": "my-app", }, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ { Name: "my-container", Image: "my-image", }, }, }, }, }, }) if err != nil { fmt.Printf("Error creating Deployment: %v", err) return } fmt.Printf("Successfully created Deployment: %v", dep) }This code creates a new Deployment object in the "default" namespace with the name "my-deployment", two replicas, and a Pod template that includes a single Container with the name "my-container" and the image "my-image".