Kubernetes client-go creating services and enpdoints

12/20/2018

I am looking for pointers on how to create/update services and endpoints using the client-go API. Can't seem to find any examples or documentation on how to do it.

Thanks! Satish

-- Satish Mohan
client-go
kubernetes

2 Answers

12/20/2018

How about this. Something like this (picked up from the file):

deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)

deployment := &appsv1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name: "demo-deployment",
    },
    Spec: appsv1.DeploymentSpec{
        Replicas: int32Ptr(2),
        Selector: &metav1.LabelSelector{
            MatchLabels: map[string]string{
                "app": "demo",
            },
        },
        Template: apiv1.PodTemplateSpec{
            ObjectMeta: metav1.ObjectMeta{
                Labels: map[string]string{
                    "app": "demo",
                },
            },
            Spec: apiv1.PodSpec{
                Containers: []apiv1.Container{
                    {
                        Name:  "web",
                        Image: "nginx:1.12",
                        Ports: []apiv1.ContainerPort{
                            {
                                Name:          "http",
                                Protocol:      apiv1.ProtocolTCP,
                                ContainerPort: 80,
                            },
                        },
                    },
                },
            },
        },
    },
}

// Create Deployment
fmt.Println("Creating deployment...")
result, err := deploymentsClient.Create(deployment)
if err != nil {
    panic(err)
}
-- Rico
Source: StackOverflow

9/13/2019
   clientset.CoreV1().Services("kube-system").Create(&corev1.Service{
            ObjectMeta: metav1.ObjectMeta{
                Name:                       controllerSVCName,
                Namespace:                  "kube-system",
                Labels: map[string]string{
                    "k8s-app": "kube-controller-manager",
                },
            },
            Spec: corev1.ServiceSpec{
                Ports:                    nil,
                Selector:                 nil,
                ClusterIP:                "",

            },
   })
-- 张馆长
Source: StackOverflow