Appending strings to output of jsonpath expression when listing pod labels using kubectl - kubectl

I have a json path command to get all the labels of my pods.
kubectl get pods -o jsonpath="{.items[*].metadata.labels}"
This will output:
{
"app": "api-dogs-v1",
"release": "0.0.119"
} {
"app": "api-cats-v1",
"release": "0.0.16"
}
I want to do some simple manipulation of the command so that it outputs valid json and surround inside a json object.
I'm trying the below:
kubectl get pods -o jsonpath='{"{"}{{range .items[*]}{.metadata.labels}{"}"}{end}'
But this gives me back:
unrecognized character in action: U+007B '\'
Can anyone help me with this? Desired output below:
{
{
"app": "api-dogs-v1",
"release": "0.0.119"
} {
"app": "api-cats-v1",
"release": "0.0.16"
}
}
thanks!
...........................................
Update
kubectl get pods -o jsonpath='"richard"{range .items[*]}{.metadata.labels}{end}"}"'
Almost gets me there but I get an error when i change to add curly braces at the start of the json path expression (instead of my name) i believe it thinks i'm starting the function...
kubectl get pods -o jsonpath='"{"{range .items[*]}{.metadata.labels}{end}"}"'
error: error parsing jsonpath {{range .items[*]}{.metadata.labels}{end}}, unrecognized character in action: U+007B '{'

i have remove extra { infront of {range .items[*]} and moved {"}"} after {end} as following :
kubectl get pods -o jsonpath='{"{"}{range .items[*]}{.metadata.labels}{end}{"}"}'
Output(your desired output format):
{{"app":"test-multi-pv","pod-template-hash":"55665bc94c"}{"app":"nginx","controller-revision-hash":"web1-774fbdcb49","statefulset.kubernetes.io/pod-name":"web1-0"}{"app":"nginx","controller-revision-hash":"web2-6b59d76fc6","statefulset.kubernetes.io/pod-name":"web2-0"}{"app":"nginx","controller-revision-hash":"web3-7c65fbbcdc","statefulset.kubernetes.io/pod-name":"web3-0"}}

I found that you have opened one extra open brace in your command. Kindly recheck the command and try once.
I have reproduced the use case and successfully got the expected output.while running the following jsonpath command, I have got all the labels of my pods.
kubectl get pods -o jsonpath="{.items[*].metadata.labels}"
Output:
{
"app":"hello-server",
"Pod-template-hash":"5bd6b6875f"
}
I have removed the curly braces of the jsonpath expression “{“ before the {range .items[*]} and added a newline expression before the {end}
So, the final range function for the json object that surrounds the inside json object is
kubectl get pods -o jsonpath='{"{"}{range .items[*]}{.metadata.labels}{"}"}{"\n"}{end}'
Expected Output:
{
{
"app":"hello-server",
"Pod-template-hash":"5bd6b6875f"
}
}

Related

Cisco CSR user-data bootstrap to allow multiline in banner

The user data format for the IOS appliance uses the following:
ios-config-0001="hostname test-csr-deployment-001"
ios-config-0002="banner exec |Hostname: test-csr-deployment-001\r\nRegion: eu-west-2|"
The commands are accepted, but the returned value is not split to multiline
show banner exec
Hostname: test-csr-deployment-001rnRegion: eu-west-2
How do I split the multiline?
I've also tried:
ios-config-0002="banner exec |Hostname: test-csr-deployment-001\015\012Region: eu-west-2|"
output:
Hostname: test-csr-deployment-001015012Region: eu-west-2
ios-config-000x="set NEWL "\0""
ios-config-000x="set NEWL1 "12""
ios-config-000x="set NEWL $NEWL$NEWL1"
ios-config-000x="banner exec |Hostname: test-csr-deployment-001$NEWLRegion: eu-west-2|"
error:
Invalid input detected at '^' marker
output:
Hostname: test-csr-deployment-001$NEWLRegion: eu-west-2
ios-config-0002="banner exec |Hostname: test-csr-deployment-001"
ios-config-0003="Region: eu-west-2|"
error:
Invalid input detected at '^' marker
output:
Hostname: test-csr-deployment-001
I also tried double escapes, but the config then doesn't load at all.
Ideally output should be:
show banner exec
Hostname: test-csr-deployment-001
Region: eu-west-2
While configuring via CLI, we use the delimiter marks the beginning and the end of the message and new lines are accepted as far they are within the delimiter.
I don't know what language you are using in typical python way we could do something like this
ios-config-0002='''banner exec # Line 1
Line 2 # '''

AWS CLI Error parsing parameter '--container-definitions': Invalid JSON: Invalid \e

I'm trying to update a task though AWS CLI, but in the moment of register the task, i have a problem with the container definition i'm passing to it.
aws ecs register-task-definition --container-definitions "$task_def" --family $TASK_FAMILY | $JQ '.taskDefinition.taskDefinitionArn'
Error parsing parameter '--container-definitions': Invalid JSON: Invalid \escape: line 130 column 23 (char 3202)
the problem in that line is an environment variable with a special character () a backslash
{
"name": "FTP_USER",
"value": "xxx\xxxxx"
}
If i try to scape the character with double back slash \, it doesn´t response to me an error, but the environment variable is setup with double \ and the connection to the FTP of course doesn´t work.
{
"name": "FTP_USER",
"value": "xxx\\xxxxx"
}
Someone can help me with this issue.
Thanks

Implementing a 'for loop' within user_data provisioner file

I am currently using a template_file to provision user_data into an aws_launch_configuration, like so:
resource "aws_launch_configuration" "launch_config" {
...
user_data = "${data.template_file.init.rendered}"
}
data "template_file" "init" {
template = "${file("router-init.sh.tpl")}"
vars {
hub_ip_addresses = "${join(",", aws_instance.gridHub.*.private_ip)}"
}
}
I am feeding in a variable (i.e. hub_ip_addresses) into the router-init.sh.tpl file, and in this file I am making use of the argument like so:
`#!/bin/sh
...
IFS=',' read -r -a array <<< "$hub_ip_addresses"
for element in "${array[#]}"
do
#do stuff with $element
done
Basically, I am splitting the string based on a delimiter, and then looping through each ip address in the array.
This bash script works fine when I run it on my local machine -- however, when terraform executes it, it throws a error:
* data.template_file.init: data.template_file.init: failed to render : parse error at 13:25: expected expression but found invalid sequence "#"
I'm supposing the '#' symbol is causing an issue. Is there a reason why this is so? Do I need to escape it with a '\' ?
EDIT: Not sure if related to this issue, but in the preceeding line in the bash script, IFS=',' read -r -a array <<< "$hub_ip_addresses", the <<< seems to be causing everything else that follows to look as if they are inside a comment (i.e. greyed out as if it was within a quotation mark ').)
You need to escape the $ characters in your template by doubling them up or Terraform will attempt to interpolate them as the input variables to the template.
The template docs cover this briefly although the example given is for inline templates rather than for all templates, including those that are loaded with the file() function.
So something like:
#!/bin/sh
...
IFS=',' read -r -a array <<< "$hub_ip_addresses"
for element in "$${array[#]}"
do
#do stuff with $$element
done

Error (InvalidChangeBatch) in adding multiple DNS Records from aws command line

I am trying to add multiple DNS records using this script add_multipleDNSrecord.sh and i am getting this error
A client error (InvalidChangeBatch) occurred when calling the ChangeResourceRecordSets operation: FATAL problem: UnsupportedCharacter (Value contains unsupported characters) encountered with ' '
But i am able to add single record without any issue from aws cli. can anyone please tell me what went wrong in this script?
#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING
# Hosted Zone ID
ZONEID="Z24*************"
#Comment
COMMENT="Add new entry to the zone"
# The Time-To-Live of this recordset
TTL=300
# Type
TYPE="A"
# Input File Name
FILENAME=/home/ec2-user/awscli/route53/scripts/test.json
cat >> $FILENAME << EOF
{
"Comment":"$COMMENT",
"Changes":[
{
"Action":"CREATE",
"ResourceRecordSet":{
"ResourceRecords":[
{
"Value":"$IP"
}
],
"Name":"$RECORDSET",
"Type":"$TYPE",
"TTL":$TTL
}
}
]
}
EOF
echo $FILENAME
After Replacing the space and using dot instead of space solves the problem.
Now,The script works fine and its able to add multiple records to the hosted zone.

"YYYYMMDD": Invalid identifier error while trying through SQOOP

Please help me out from the below error.It works fine when checked in oracle but fails when trying through SQOOP import.
version : Hadoop 0.20.2-cdh3u4 and Sqoop 1.3.0-cdh3u5
sqoop import $SQOOP_CONNECTION_STRING
--query 'SELECT st.reference,u.unit,st.reading,st.code,st.read_id,st.avg FROM reading st,tunit `tu,unit u
WHERE st.reference=tu.reference and st.number IN ('218730','123456') and tu.unit_id = u.unit_id
and u.enrolled='Y' AND st.reading <= latest_off and st.reading >= To_Date('20120701','yyyymmdd')
and st.type_id is null and $CONDITIONS'
--split-by u.unit
--target-dir /sample/input
Error:
12/10/10 09:33:21 ERROR manager.SqlManager: Error executing statement:
java.sql.SQLSyntaxErrorException: ORA-00904: "YYYYMMDD": invalid identifier
followed by....
12/10/10 09:33:21 ERROR sqoop.Sqoop: Got exception running Sqoop:
java.lang.NullPointerException
Thanks & Regards,
Tamil
I believe that the problem is actually on Bash side (or your command line interpret). Your query contains for example following fragment u.enrolled='Y'. Please notice that you're escaping character constants with single quotes. You seem to be putting entire query into additional single quotes: --query 'YOUR QUERY'. Which results in something like --query '...u.enrolled='Y'...'. However such string is stripped by bash to '...u.enrolled=Y...'. You can verify that by using "echo" to see what exactly will bash do with your string before it will be passed to Sqoop.
jarcec#jarcec-thinkpad ~ % echo '...u.enrolled='Y'...'
...u.enrolled=Y..
.
I would recommend to either escape all single quotes (\') inside your query or choose double quotes for entire query. Please note that the later option will require escaping $ characters with backslash (\$).