How to set EXTERNAL-IP for kubernetes Loadbalancer Service by Client-go?

11/29/2019

I want to custom a loadbalancer-controller by client-go, now I just try to set a address for the service, but it not work. This is my code:

func (c *Controller) syncToStdout(key string) error {
    obj, exists, err := c.indexer.GetByKey(key)
    if err != nil {
        klog.Errorf("Fetching object with key %s from store failed with %v", key, err)
        return err
    }

    if !exists {
        fmt.Printf("Pod %s does not exist anymore%s\n", key)
    } else {
        service := obj.(*v1.Service)
        if service.ObjectMeta.Annotations["service.beta.kubernetes.io/netease-cloud"] == "nlb" {
            klog.Info("service belong to NLB, name = ", service.GetName())
            // [0].IP = "10.123.234.213";
            service.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{IP: "10.123.234.213"}}
            _, _ = clientset.CoreV1().Services(v1.NamespaceDefault).Update(service)
            klog.Info("service belong to NLB, service = ", service)
        } else {
            klog.Info("service not belong to NLB, name = ", service.GetName())
        }
    }
    return nil
}

Then I get the service config, the ip not be set.

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/netease-cloud: nlb
  creationTimestamp: "2019-11-29T07:39:41Z"
  name: httpd-service
  namespace: default
  resourceVersion: "480971"
  selfLink: /api/v1/namespaces/default/services/httpd-service
  uid: 3000dddd-3d56-4989-af41-6d6a9bab7565
spec:
  clusterIP: 10.108.100.92
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30000
    port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: httpd
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer: {}

How can I set a ip for LoadBalancer Service?

-- Bill Jay
client-go
kubernetes

1 Answer

12/3/2019

use clientset.CoreV1().Services(v1.NamespaceDefault).UpdateStatus(service) can solve it.

-- Bill Jay
Source: StackOverflow