首页 > 解决方案 > Openshift“oc apply”是覆盖整个配置还是只覆盖包含的参数?

问题描述

我们目前正在使用以下命令来更新 Openshift 中的 configmap 设置(之后我们重新启动 pod 以使设置生效):

oc apply -f configmap.yml

我的问题是:

此命令会删除现有的配置映射并将其替换为此文件的内容,还是仅从文件中导入设置而保持任何其他设置不变?

基本上,如果 live configmap 包含一个设置mytest: true并且新文件不包含 parameter mytest,那么该参数是保留在 Openshift 中的 live configmap 中,还是因为它没有在导入的文件中列出而被删除?

标签: kubernetesopenshift

解决方案


我已经复制了您的案例,并且在应用具有不同 configmap 设置的新 yaml 后,新版本正在生成。所以 OpenShift 不是在合并 configmap,而是在替换。

让我们一起度过难关...

kind: ConfigMap
apiVersion: v1
metadata:
  name: example-config
data: 
  mytest0: "HELLO"
  mytest1: "STACK"
  mytest2: "COMMUNITY"
  mytest3: "!!!"

oc apply -f configmap_lab.yaml

如我们所见,我们按预期包含了所有内容:

$ oc get configmap/example-config -o yaml
apiVersion: v1
data:
  mytest0: HELLO
  mytest1: STACK
  mytest2: COMMUNITY
  mytest3: '!!!'
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"mytest0":"HELLO","mytest1":"STACK","mytest2":"COMMUNITY","mytest3":"!!!"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"example-config","namespace":"myproject"}}
  creationTimestamp: 2020-01-09T10:42:11Z
  name: example-config
  namespace: myproject
  resourceVersion: "7987774"
  selfLink: /api/v1/namespaces/myproject/configmaps/example-config
  uid: b148dbef-32cc-11ea-9339-525400d653ae

现在让我们在这个上部署一个新的 yaml:

kind: ConfigMap
apiVersion: v1
metadata:
  name: example-config
data: 
  mytest0: "THANKS"
  mytest1: "STACK"
  newmytest0: "COMMUNITY"
  newmytest1: "!!!"

在这里,我们正在更改值,删除 2 并添加 2 个参数。让我们检查一下 OC 将如何处理它:

oc apply -f configmap_lab_new.yaml
$ oc get configmap/example-config -o yaml
apiVersion: v1
data:
  mytest0: THANKS
  mytest1: STACK
  newmytest0: COMMUNITY
  newmytest1: '!!!'
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"mytest0":"THANKS","mytest1":"STACK","newmytest0":"COMMUNITY","newmytest1":"!!!"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"example-config","namespace":"myproject"}}
  creationTimestamp: 2020-01-09T10:42:11Z
  name: example-config
  namespace: myproject
  resourceVersion: "7988585"
  selfLink: /api/v1/namespaces/myproject/configmaps/example-config
  uid: b148dbef-32cc-11ea-9339-525400d653ae

我们可以注意到,所有更改都被接受并处于活动状态。

尽管如果您想以更可控的方式进行操作,您可能需要使用oc patch. 文档在这里


推荐阅读