Home > Mobile >  How to access metadata.labels.{custom_field} in service.yaml with client-go?
How to access metadata.labels.{custom_field} in service.yaml with client-go?

Time:04-14

I am currently working on a side project with Go. I'm trying to get the information about the pods running on the cluster.

I can reach the pods according to the namespace value, but in order to reach the working pods with the metadata.labels.applicationGroup value in the service.yaml file, I need to obtain this value first.

I added below a part of my service.yaml file.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: metadata-name
  labels:
    service: service-name
    applicationGroup: beta --> this field
spec:
  replicas: 1
  selector:
    matchLabels:
      service: service-name
  template:
    metadata:
      labels:
        service: service-name
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.8
        ports:
        - containerPort: 80
...

Right now, I can access information pods with "default" namespaces.

func getPodsInfo() (string, error) {

  var kubeConfig *string

  if home := homedir.HomeDir(); home != "" {
    kubeConfig = flag.String("kubeConfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeConfig file")
  } else {
    kubeConfig = flag.String("kubeConfig", "", "absolute path to the kubeConfig file")
  }
  flag.Parse()

  config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
  if err != nil {
    err = fmt.Errorf("error occured when build config from flags: %s", err.Error())
    return "", err
  }

  clientSet, err := kubernetes.NewForConfig(config)
  if err != nil {
    err = fmt.Errorf("error occured when build client set from config: %s", err.Error())
    return "", err
  }
  /*
    listOptions := meta.ListOptions{
      FieldSelector: "metadata.labels.applicationGroup=alpha",
    }
  */
  pods, err := clientSet.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
  if err != nil {
    err = fmt.Errorf("error occured when get pods info from k8s: %s", err.Error())
    return "", err
  }

  podsInfo, err := json.Marshal(&pods)
  if err != nil {
    err = fmt.Errorf("error occured when json marshal: %s", err.Error())
    return "", err
  }

  return string(podsInfo), nil
}

I tried to FieldSelector, but FieldSelector has supported some fields.

listOptions := meta.ListOptions{
    FieldSelector: "metadata.labels.applicationGroup=alpha",
}

And I got the below error.

field label not supported: metadata.labels.applicationGroup

You can check this link for the supported fields.

To my question, how can I reach the metadata.labels.applicationGroup value with cilent_go, and which function should I use to reach pods working with this value?

CodePudding user response:

Just use LabelSelector instead of FieldSelector

listOptions := meta.ListOptions{
    LabelSelector: "applicationGroup=alpha",
}

UPD: If you want to use it for pods you should set applicationGroup on pod template

apiVersion: apps/v1
kind: Deployment
metadata:
  name: metadata-name
  labels:
    service: service-name
spec:
  replicas: 1
  selector:
    matchLabels:
      service: service-name
  template:
    metadata:
      labels:
        service: service-name
        applicationGroup: beta
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.8
        ports:
        - containerPort: 80
  • Related