Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to include labels for every configmap in a configmapList? #3108

Closed
KR411-prog opened this issue Oct 16, 2020 · 2 comments
Closed
Labels
kind/support Categorizes issue or PR as a support question.

Comments

@KR411-prog
Copy link

KR411-prog commented Oct 16, 2020

I have a yaml file like below.This yaml file needs to be patched with a new label under each configmap.

apiVersion: v1
kind: ConfigMapList
items:
  - apiVersion: v1
    data:
      test: 1
    kind: ConfigMap
    metadata:
      name: grafana-dashboard-apiserver1
      namespace: monitoring   
  - apiVersion: v1
    data:
      test: 2
    kind: ConfigMap
    metadata:
      name: grafana-dashboard-apiserver2
      namespace: monitoring   
  - apiVersion: v1
    data:
      test: 3
    kind: ConfigMap
    metadata:
      name: grafana-dashboard-apiserver3
      namespace: monitoring    

I want to insert labels under every configmap in this configmaplist as below,

apiVersion: v1
kind: ConfigMapList
items:
  - apiVersion: v1
    data:
      test: 1
    kind: ConfigMap
    metadata:
      name: grafana-dashboard-apiserver1
      namespace: monitoring
      labels:
        grafana_dashboard: "1"   
  - apiVersion: v1
    data:
      test: 2
    kind: ConfigMap
    metadata:
      name: grafana-dashboard-apiserver2
      namespace: monitoring   
      labels:
        grafana_dashboard: "1" 
  - apiVersion: v1
    data:
      test: 3
    kind: ConfigMap
    metadata:
      name: grafana-dashboard-apiserver3
      namespace: monitoring    
      labels:
        grafana_dashboard: "1" 

I tried to do this using Kustomize by following https://kubectl.docs.kubernetes.io/pages/app_customization/customizing_arbitrary_fields.html. But its not working.

Here is my file,

overlays/patch.yaml
- op: add
  path: /items/?/metadata/labels/testlabel
  value: test

overlays/kustomization.yaml

bases:
- ../base
patchesJson6902:
- target:
    kind: ConfigMapList
  path: patch.yaml

Since ConfigMapList does not contain a name, its not working in target,
but it reports error as , `: must specify the target name
How to do patching in ConfigMapList?
Also for the patch.yaml file,I have used "?" here in this example..but I am not sure what to use to replace it..because it is around 100 different configmaps under items.Should I use wildcard * ?

@pantaoran
Copy link

I have the exact same question/problem. I need to add an annotation to every item in a list, and the annotation should be different for every environment.

@Shell32-Natsu Shell32-Natsu added the kind/support Categorizes issue or PR as a support question. label Oct 16, 2020
@Shell32-Natsu
Copy link
Contributor

  1. Please use patches instead of patchesJson6902.
  2. *List resources will be parsed to a list of resources in kustomize instead of a single resource. So there is no ConfigMapList in kustomize but a list of ConfigMap. In target.kind you should use ConfigMap.
  3. JSON patch requires the path except last item does exist. If you want to add a label /metadata/labels/testlabel, /metadata/labels must exist.
  4. If you just want to add labels, you can use LabelTransformer and custom fieldSpec to add labels to a list. doc

Transformer YAML:

apiVersion: builtin
kind: LabelTransformer
metadata:
  name: notImportantHere
labels:
  app: hello
fieldSpecs:
- kind: ConfigMap
  path: /metadata/labels
  create: true

Then add

transformers:
- label_transformer.yaml

to the kustomization.yaml file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question.
Projects
None yet
Development

No branches or pull requests

3 participants