简体   繁体   English

如何将 Kubernetes 自定义资源规范与 GO controller 中的预期规范进行比较?

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

I am trying to implement my first Kubernetes operator.我正在尝试实现我的第一个 Kubernetes 运算符。 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.我希望操作员 controller 能够将正在运行的 pod 中的配置与自定义资源定义中定义的预期配置进行比较。

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.上面的自定义资源已部署,3 个 pod 正在运行。 A change is made such that the config "valueA" is changed to false.进行了更改,以使配置“valueA”更改为 false。

In the GO controller reconcile function I can get the TEST instance and see the "new" version of the config:在 GO controller 协调 function 中,我可以获得 TEST 实例并查看配置的“新”版本:

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?我想知道如何访问正在运行的 Pod 中的“valueA”的值,以便我可以比较和重新创建 Pod(如果它已更改)?

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?还有一个次要问题,我是否需要遍历协调 function 中的所有正在运行的 Pod 来检查每个 Pod,或者我可以将其作为单个操作执行吗?

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.如果是 Pod 的规范配置,我建议您更新的不是单个 Pod,而是 Deployment 中的规范,它会自动重新启动它的 Pod。 If it's environment variables for apps in this pod, i would recommend to use ConfigMap for storing them, and update it.如果它是这个 pod 中应用程序的环境变量,我建议使用ConfigMap来存储它们并更新它。 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.要获得 Deployment 或 ConfigMap,您需要拥有它的名称和命名空间,通常,对于自定义资源,它应该从它的名称派生而来。 Here is example how you can get deployment instance and update it. 这是如何获取部署实例并对其进行更新的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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