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

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

Related

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

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.

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.

Terraform kubernetes_config_map --from-env-file

I am creating a kubernetes configMap using '--from-env-file' option to store the file contents as environment variables.
kubectl create configmap env --from-env-file=env.properties -n namespace
When I create a terraform resource as below, the created configMap contains a file, not environment variables.
resource "kubernetes_config_map" "env" {
metadata {
name = "env-config"
namespace = var.namespace
}
data = {
"env.properties" = "${file("${path.module}/env.properties")}"
}
}
How to create configMap with file content as environment variables using terraform-kubernetes-provider resource ?
If env.properties looks like this:
$ cat env.properties
enemies=aliens
lives=3
allowed="true"
Then kubectl create configmap env --from-env-file=env.properties -n namespace would result in something like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: env
namespace: namespace
data:
allowed: '"true"'
enemies: aliens
lives: "3"
But what you're doing with Terraform would result in something more like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: env
namespace: namespace
data:
env.properties: |
enemies=aliens
lives=3
allowed="true"
Based on the Terraform docs it appears that what you're looking for, i.e. some native support for --from-env-file behaviour within the Terraform provider, is not possible.
The ConfigMap format that you get doing it the Terraform way could still be useful, you might just have to change how you're pulling the data from the ConfigMap into your pods/deployments. If you can share more details, and even a simplified/sanitized example of your pods/deployments where you're consuming the config map, it may be possible to describe how to change those to make use of the different style of ConfigMap. See more here.

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.