jq set change to the object - json

I'm trying to update a secret with new key.
The issue is that when i call again this secret object, it don't reflect this change.
I get the secret data:
kubectl get secret mysecret --context dev -n dev-test -o jsonpath=\{$.data\} | jq
{
"example1": "Rlc3QuY29taW5hZG1pbn",
"example2": "NYXBiN3c3c1U3NFd",
"example3": "aW5hZG1pbnRlc3QsZGM9Y29tZGM9YWQsZGM9",
"example4": "jMDEuYWQuaW5hZG1pbnRlbGRhcDovL2luYmRc3QuY29tOjMyNjg=",
"example5": "YWxmcmVzY28YWRtaW50ZXN0LmNvbQ==uaW1wb3J0QGlu"
}
I aply the add command with jq :
kubectl get secret mysecret --context dev -n dev-test -o jsonpath=\{$.data\} | jq ' .test = "tests" '
{
"example1": "Rlc3QuY29taW5hZG1pbn",
"example2": "NYXBiN3c3c1U3NFd",
"example3": "aW5hZG1pbnRlc3QsZGM9Y29tZGM9YWQsZGM9",
"example4": "jMDEuYWQuaW5hZG1pbnRlbGRhcDovL2luYmRc3QuY29tOjMyNjg=",
"example5": "YWxmcmVzY28YWRtaW50ZXN0LmNvbQ==uaW1wb3J0QGlu",
"test": "tests"
}
Now, when i call the secret again, the output is like the original and not with the adding key
kubectl get secret mysecret --context dev -n dev-test -o jsonpath=\{$.data\} | jq
{
"example1": "Rlc3QuY29taW5hZG1pbn",
"example2": "NYXBiN3c3c1U3NFd",
"example3": "aW5hZG1pbnRlc3QsZGM9Y29tZGM9YWQsZGM9",
"example4": "jMDEuYWQuaW5hZG1pbnRlbGRhcDovL2luYmRc3QuY29tOjMyNjg=",
"example5": "YWxmcmVzY28YWRtaW50ZXN0LmNvbQ==uaW1wb3J0QGlu"
}
How can i set this new key ?
Obs: I'm not usin any yaml file.

Try
kubectl get secret mysecret -o json | jq --arg secret_base64 "$(echo -n tests | base64)" '.data.test=$secret_base64' | kubectl apply -f -
You can do it with kubectl edit secret mysecret and add a new secret key
test: dGVzdHM= # echo -n tests | base64

Related

Echo not outputting variable value

I am trying to print value of API_RESPONSE but it prints "response is: ". S3_RESPONSE value is set but API_RESPONSE shows blank in echo command.
- name: Check if certificate exists
id: check_certificate
run: |
API_RESPONSE=$(aws s3api head-object --bucket test-bucket-ssl --key fullchain.pem 2>&1 | tee true)
echo "::set-output name=S3_RESPONSE::$(echo $API_RESPONSE)"
echo "response is: ${API_RESPONSE}"
I think, instead of
response is: ${API_RESPONSE}
you should use
response is: ${{API_RESPONSE}}
You might need to use an environment file, as illustrate here.
In your case:
- name: Check if certificate exists (Set the value)
id: Check if certificate exists_set_value
run: |
echo "API_RESPONSE=$(aws s3api head-object --bucket test-bucket-ssl --key fullchain.pem 2>&1 | tee true)" >> $GITHUB_ENV
- name: Check if certificate exists (Use the value)
id: Check if certificate exists_use_value
run: |
echo "API_RESPONSE=${{ env.API_RESPONSE}}"

Get ca-cert.pem field of a kubernetes secret

In my istio-system namespace, I have the following secret
▶ k get secret istio-ca-secret -o yaml
apiVersion: v1
data:
ca-cert.pem: LS0tLS1CR...
ca-key.pem: LS0..
cert-chain.pem: ""
key.pem: ""
root-cert.pem: ""
While the following query works:
kubectl get secret istio-ca-secret -n istio-system -o jsonpath="{.data}"
{"ca-cert.pem":"LS0t=...","ca-key.pem":"LS0tLS1","cert-chain.pem":"","key.pem":"","root-cert.pem":""}%
the following, which I execute trying to get only the ca-cert.pem value returns nothing
kubectl get secret istio-ca-secret -n istio-system -o jsonpath="{.data.ca-cert.pem}"
why is that?
you need to escape the dot in "ca-cert.pem" to work.
like this
kubectl get secret istio-ca-secret -n istio-system -o jsonpath="{.data.ca-cert\.pem}"

How to patch container env variable in deployment with kubectl?

When I want to exctract the current value of some container env variabe I could use jsonpath with syntax like:
kubectl get pods -l component='somelabel' -n somenamespace -o \
jsonpath='{.items[*].spec.containers[*].env[?(#.name=="SOME_ENV_VARIABLE")].value}')
That will return me the value of env varialbe with the name SOME_ENV_VARIABLE. Pod section with container env variables in json will look like this:
"spec": {
"containers": [
{
"env": [
{
"name": "SOME_ENV_VARIABLE",
"value": "some_value"
},
{
"name": "ANOTHER_ENV_VARIABLE",
"value": "another_value"
}
],
When I want to patch some value in my deployment I'm using commands with syntax like:
kubectl -n kube-system patch svc kubernetes-dashboard --type='json' -p="[{'op': 'replace', 'path': '/spec/ports/0/nodePort', 'value': $PORT}]"
But how can I patch a variable with 'op': 'replace' in cases where I need to use expression like env[?(#.name=="SOME_ENV_VARIABLE")]? Which syntax I should use?
Rather than kubectl patch command, you can make use of kubectl set env to update environment variable of k8s deployment.
envvalue=$(kubectl get pods -l component='somelabel' -n somenamespace -o jsonpath='{.items[*].spec.containers[*].env[?(#.name=="SOME_ENV_VARIABLE")].value}')
kubectl set env deployment/my-app-deploy op=$envvalue
Hope this helps.
Most of them haven't provide proper commands just use as simple as it is =>
kubectl set env deployment/deploy_name APP_VERSION=value -n namespace
op: replace
path: /spec/template/spec/containers/0/env/0/name
value: YOUR_VARIABLE_NAME
op: replace
path: /spec/template/spec/containers/0/env/0/value
value: YOUR_VARIABLE_VALUE

Openshift - Correlation between confimap and pods

there's a quick way/oc command to get which deployement use one or more configmap ?
In my case, I need to know which deployment I need to rollout to apply configmap update.
No evidence on dc YAML file.
Thanks in advance!
From guide in OpenShift 3.9
In DeploymentConfig this is the Stanza to pull all environment variables from a ConfigMap.
spec:
containers:
...
envFrom:
- configMapRef:
name: env-config
...
In might be worth grepping all your deploymentConfigs for the keyword configMap in a particular namespace/project to check you've got configMaps attached
$ oc get dc -o json -n $(oc project -q) | grep -A2 configMap
"configMapRef": {
"name": "printenv-config"
}
Assuming injecting environment variables from configmaps, there might be an easier way of doing this? - but you could also pipe into jq and filter based on the envFrom array key being not null
oc get dc -o json | jq -r '[.items[] | select(.spec.template.spec.containers[].envFrom[]? != null)]' | jq -c '.[] | {namespace: .metadata.namespace, dcname: .metadata.name, configMap: .spec.template.spec.containers[].envFrom[].configMapRef.name}'
Results in:
{"namespace":"aps-env","dcname":"openshift-tasks","configMap":"another-config-map"}
{"namespace":"aps-env","dcname":"printenv","configMap":"printenv-config"}
Alternatively you might be mounting your configMap via a volume mount, in which case the Stanza is different, and you'd need to adjust the above accordingly
"volumes": [
...
{
"configMap": {
"defaultMode": 420,
"name": "gogs"
},
"name": "config-volume"
}
...
Different Query based on Volume mounted configMap
$ oc get dc -o json | jq -r '[.items[] | select(.spec.template.spec.volumes[]?.configMap != null)]' | jq -c '.[] | {dcname: .metadata.name, configMapName: .spec.template.spec.volumes[].configMap.name}' | grep -v null
Results in:
{"dcname":"gogs","configMapName":"gogs-configmap"}

I am trying to update the output of sudo knife node edit fqdn -c /etc/chef/client.rb using bash script

Here is the command that I run:
sudo knife node edit fqdn -c /etc/chef/client.rb . --> hit enter button then shows below output :
{
"name": "test",
"chef_environment": "standard_chef_environment",
"normal": {
"httpd": {
"fips_mode_enable": "false"
},
"enable_fips_mode": false,
"props": {
So i wanted to add few line under props using following command but its getting failed :
sudo knife node edit fqdn -c /etc/chef/client.rb |jq ‘.props |= . + { "ParameterKey": "Foo4", "ParameterValue": "Bar4" }'
The props key is nested under normal so you would need .normal.props or similar.