简体   繁体   中英

How to compare a Kubernetes custom resource spec with expected spec in a GO controller?

I am trying to implement my first Kubernetes operator. I want the operator controller to be able to compare the config in a running pod vs the expected config defined in a custom resource definition.

Eg: Custom Resource

apiVersion: test.com/v1alpha1
kind: TEST
metadata::
  name: example-test
spec:
  replicas: 3
  version: 20:03
  config:
    valueA: true
    valueB: 123

The above custom resource is deployed and 3 pods are running. A change is made such that the config "valueA" is changed to false.

In the GO controller reconcile function I can get the TEST instance and see the "new" version of the config:

instance := &testv1alpha1.TEST{}
log.Info("New value : " + instance.Spec.Config.valueA)

I am wondering how I can access what the value of "valueA" is in my running pods so that I can compare and recreate the pods if it has changed?

Also a secondary question, do I need to loop through all running pods in the reconcile function to check each or can I do this as a single operation?

What is this config exactly? If it's Pod's spec config, i would suggest to you to update not individual Pods, but spec in Deployment, it will restart it's pods automatically. If it's environment variables for apps in this pod, i would recommend to use ConfigMap for storing them, and update it. Answering your second question, in both cases -- it will be a single operation.

To get Deployment, or ConfigMap you need to have it's name and namespace, usually, with custom resource, it should be derived from it's name. Here is example how you can get deployment instance and update it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM