kubectl create configmap - options to remove file names from output configmap when generateing using --file-name option - kubernetes

I am using kubectl create configmap command as follows:
kubectl create configmap config-multi-yaml-files --from-file=templates/1template.yaml --from-file=apps/app1.yaml --from-file=app2/app2.yaml --dry-run=true -o yaml > output.yaml
The resultant configmap do have file names (app1.yaml, app2.yaml) like this:
apiVersion: v1
data:
app1.yaml: |-
groups:
- name: sample
rules:
- alert: alert
How can I use this command so that I do have a configmap from multiple yamls, but do not have the respective file names in the resultant configmap.
Any pointers are appreciated.
Thanks.

Unfortunately, you will have to use keys in order to make it work. But here is an example of what you could do:
kubectl create configmap config-multi-yaml-files --from-file <(cat templates/1template.yaml) --from-file <(cat apps/app1.yaml) --dry-run=client -o yaml
That way the keys would be numbers: 11, 12, 13...
So you will not have to use file names in your ConfigMap but keys will have to be used.

Related

Programatically apply a single resource from a multi resource Kubernetes YAML file

I have a file with three configmaps in it, like the one below.
apiVersion: v1
data:
TEST: "one"
kind: ConfigMap
metadata:
name: test-config-one
---
apiVersion: v1
data:
TEST: "two"
kind: ConfigMap
metadata:
name: test-config-two
---
apiVersion: v1
data:
TEST: "three"
kind: ConfigMap
metadata:
name: test-config-three
I'm trying to apply only test-config-three to the cluster. I know I can break that out into its own file and run kubectl apply -f test-config-three.yaml, but is there a way to do that without having to create a new file?
I was hoping to be able to do something like:
cat file.yml | yq <get only test-config-three> | kubectl apply -f -
But yq doesn't seem to support finding a single resource in a file. I also looked at tools like kubesplit but they tend to output all resources to separate files.
Is there a way to isolate and output a single resource from a yaml file containing multiple resources without creating a new file?
Update
Thanks to #Inian's answer below, I was able to get this full command working.
cat file.yml | yq e 'select(.data.TEST == "three")' - | kubectl apply -f -
There are two versions of yq implemented, one in Python and one in Go as I've highlighted in my answer at How can I parse a YAML file from a Linux shell script?
Using the Python version - kislyuk/yq
yq -y 'select(.data.TEST == "three")' yaml
Go version - mikefarah/yq
yq e 'select(.data.TEST == "three")' yaml
If you have python you can try following
export MYFILE=file.yml
export DOC_NUMBER=2
python3 -c "import yaml; print(yaml.dump(list(yaml.safe_load_all(open('"$MYFILE"')))["$DOC_NUMBER"]))" | kubectl apply -f -
yaml.safe_load_all(open('"$MYFILE"')) loads all the documents in the yaml file into a list. Then you are selecting "$DOC_NUMBER" document with ["$DOC_NUMBER"].
yaml.dump would dump the loaded object back into yaml format which is then printed.

Can we specify --from-file option when creating a configmap using yaml [duplicate]

This question already has answers here:
Kubernetes - How to define ConfigMap built using a file in a yaml?
(3 answers)
Closed 2 years ago.
I am creating a config map as below
kubectl create configmap testconfigmap --from-file=testkey=testfile.txt
As I am using helm charts, I would like to create the config map using YAML file instead of running kubectl, so something like:
apiVersion: v1
kind: ConfigMap
metadata:
name: testconfigmap
data:
fromfile: testkey=testfile.txt
The --from-file allows me to pass the key and the value I read from the testfile.txt. But the fromfile doesn't work if I create configMap as YAML file.
The following command will generate the Yaml file you need to apply
kubectl create configmap testconfigmap —from-file=testkey=test file.txt —dry-run=client -o yaml > myconfigmap.yaml
Then you can just do
kubectl apply -f myconfigmap.yaml

Delete a configmap itself from Kubernetes

I am trying to delete a configmap from a k8s namespace .. i created the configmap using the command below
kubectl -n namespacename create -f configmap.yaml
checking the k8s cheat sheet https://kubernetes.io/docs/reference/kubectl/cheatsheet/ i didn't find anything related .. kindly advise how to do that ?
To delete configmap using configmap name:
# kubectl delete configmap <configmap-name> -n <namespace-name>
$ kubectl delete configmap my-cofigmap -n namespacename
To delete configmap using configmap yaml file:
# kubectl delete -f <file-directory> -n <namespace-name>
$ kubectl delete -f configmap.yaml -n namespacename
You can delete a configMap by it's name. If you are unsure you can check the configMaps within a namespace by using:
kubectl get configmap -n namespacename`
once you have them you can run a delete command:
kubectl delete configmap <configmapname> -n namespacename
Should work this way:
kubectl delete configmap <configmap-name> -n <namespace-name>
Your configmap's name should be defined in your configmap.yaml file.
Easiest way if you created the ConfigMap with a YAML file is to delete it by referencing the YAML file as well:
kubectl delete -n <namespacename> -f configmap.yaml

How to edit configmap in kubernetes and override the values from a different yaml file?

I want to edit the configmap and replace the values. But it should be done using a different YAML in I ll specify overriding values as part of that file.
I was trying using kubectl edit cm -f replace.yaml but this didn't work so i want to know the structure in which the new file should be.
apiVersion: v1
kind: ConfigMap
metadata:
name: int-change-change-management-service-configurations
data:
should_retain_native_dn: "False"
NADC_IP: "10.11.12.13"
NADC_USER: "omc"
NADC_PASSWORD: "hello"
NADC_PORT: "991"
plan_compare_wait_time: "1"
plan_prefix: ""
ingress_ip: "http://10.12.13.14"
Now lets us assume NADC_IP should be changed and So I would like to know how should be structure of the YAML file and using which command it can be served?
The override taking place should only be during helm test for example when i run
helm test <release-name>?
kubectl replace -f replace.yaml
If you have a configmap in place like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
should_retain_native_dn: "False"
NADC_IP: "10.11.12.13"
and you want to change the value of NADC_IP create a manifest file like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
should_retain_native_dn: "False"
NADC_IP: "12.34.56.78" # the new IP
and run kubectl replace -f replace.yaml
To update variable in configmap you need to take two steps:
First, update the value of variable:
kubectl create configmap <name_of_configmap> --from-literal=<var_name>=<new_value> -o yaml --dry-run | kubectl replace -f -
So in your case it will looks like this:
kubectl create configmap int-change-change-management-service-configurations --from-literal=NADC_IP=<new_value> -o yaml --dry-run | kubectl replace -f -
Second step, restart the pod:
kubectl delete pod <pod_name>
App will use new value from now. Let me know, if it works for you.
kubectl get cm {configmap name} -o=yaml --export > filename.yaml
You can try this it will give you yaml format
kubectl get configmap
int-change-change-management-service-configurations -o yaml
You can copy the content and replace it inside new yaml file and apply the changes
EDIT : 1
If you want to edit over terminal you can run
kubectl edit configmap {configmap name}
It will use vim editor and you can replace value from terminal using edit command.
EDIT : 2
kubectl get cm {configmap name} -o=yaml --export > filename.yaml

kubectl create yml config doesn't create with headers/generator v1

I am trying to create a config map yml file from a file:
kubectl create configmap my-config --from-file=my-file.json -o yaml --dry-run
I even try with generator:
kubectl create configmap my-config --from-file=my-file.json --generator="configmap/v1" -o yaml --dry-run
but the output, doesn't contain apiVersion / kind but just data / metadata.
solved by installing kubectl v1.9.2 rather than older version I had.