kubectl create pod using override return error: Invalid JSON Patch - json

I am trying to run my pod using below command but keep getting error:
error: Invalid JSON Patch
kubectl run -i tmp-pod --rm -n=my-scripts --image=placeholder --restart=Never --overrides= "$(cat pod.json)"
Here is my pod.json file:
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "test",
"namespace": "my-ns",
"labels": {
"app": "test"
}
},
"spec": {
"containers": [
{
"name": "test",
"image": "myimage",
"command": [
"python",
"/usr/bin/cma/excute.py"
]
}
]
}
}
What am I doing wrong here?

I did a bit of testing and it seems there is an issue with Cmder not executing $() properly - either not working at all, or treating newlines as Enter, and thus executing a commant before entire JSON is passed.
You may want to try running your commands in PowerShell:
kubectl run -i tmp-pod --rm -n=my-scripts --image=placeholder --restart=Never --overrides=$(Get-Content pod.json -Raw)
There is a similar issue on GitHub [Windows] kubectl run not accepting valid JSON as --override on Windows (same JSON works on Mac) #519. Unfortunately, there is no clear solution for this.
Possible solutions are:
Passing JSON as a string directly
kubectl run -i tmp-pod --rm -n=my-scripts --image=placeholder --restart=Never --overrides='{"apiVersion":"v1","kind":"Pod","metadata":{...}}'
Using ' instead of " around overrides.
Using triple quotes """ instead of single quotes " in JSON file.

Related

Print specific element of json object of an array using jq

I am describing a AWS security group and passing the output by jq in order to get all the CIDRs of the inbound rules.
I have reached so far:
▶ aws ec2 describe-security-groups --group-ids sg-123456789 | jq '.SecurityGroups[0].IpPermissions[0].IpRanges'
[
{
"CidrIp": "11.22.33.44/32",
"Description": "Something"
},
{
"CidrIp": "22.33.44.12/32",
"Description": "Something else"
},
{
"CidrIp": "22.11.33.55/32",
"Description": "Something different"
},
]
I know I can grep but is there a way to get just the CidrIp from each json element of this array?
Sure, change your pipeline to
jq -r '.SecurityGroups[0].IpPermissions[0].IpRanges[].CidrIp'
Demo
Note that I also added the -r flag which makes the output raw text instead of JSON.
jq '.SecurityGroups[0].IpPermissions[0].IpRanges | values[].CidrIp'
this seemed to work as well.

Get JSON value from curl request

I have a Nexus Repository server where my artifacts are stored. I want to write a shell script to download artifacts from here. When using the curl request curl --user username:password -X GET "http://your_ip:your_port/service/rest/v1/search?repository=your_repository" -H "accept: application/json" I get a list of the items in my repository which looks like this:
{
"items": [
{
"id": "dGVzdC1hcHA6ZDM1MTBiN2FkMThkODJjZGU1NjNhMWVlMWFmOWIwMGQ",
"repository": "test-app",
"format": "maven2",
"group": "no.ahj",
"name": "test-app",
"version": "1.0-20190715.130341-2",
"assets": [
{
"downloadUrl": "http://192.168.56.2:8081/repository/test-app/no/ahj/test-app/1.0-SNAPSHOT/test-app-1.0-20190715.130341-2.pom",
"path": "no/ahj/test-app/1.0-SNAPSHOT/test-app-1.0-20190715.130341-2.pom",
"id": "dGVzdC1hcHA6Yzc3MDE2OWMwYjJlM2VkODU0MGMyOGEwOWQ0Njk4ZTQ",
"repository": "test-app",
"format": "maven2",
"checksum": {
"sha1": "5fd032774dd3ae6fbbd6484b3dc6ef2582d9b397",
"md5": "3a6aa8e295a734fdb8a8df782c0a14d5"
}
},
I would like my shell script to run this curl request, extract the value from the downloadURL field, store it in some variable and then use wget with this variable to download the file. So my question is this: How can I take the URL from downloadURL and store/use it in my shell script?
A way to parse it with likely no external dependencies (as python is installed by default on most Linux distributions) is just to use python:
user#host ~ % JSON=$(curl --user username:password -X GET "http://your_ip:your_port/service/rest/v1/search?repository=your_repository" -H "accept: application/json")
user#host ~ % echo $JSON | python -c 'import sys, json; print(json.load(sys.stdin)["items"][0]["assets"][0]["downloadUrl"])'
http://192.168.56.2:8081/repository/test-app/no/ahj/test-app/1.0-SNAPSHOT/test-app-1.0-20190715.130341-2.pom
If you are going to do a lot of JSON parsing in this script, it may be worth considering writing the entire script in Python, too, instead of shell script.

how to patch a route with an alternate backend

I have been trying to patch a route in openshift that has an alternate backend.
I have tried:
oc patch route/image-mirror-poc --patch '{"spec":{"alternateBackends": "kind:Service" "name:image-mirror-poc-blue" "weight:75"}}'
Error: Error from server: invalid character '"' after object key:value pair
oc patch route/image-mirror-poc --patch '{"spec":{"alternateBackends": "kind:Service", "name:image-mirror-poc-blue", "weight:75"}}'
Error: Error from server: invalid character ',' after object key
oc patch route/image-mirror-poc --patch '{"spec":{"alternateBackends": ["kind:Service", "name:image-mirror-poc-blue", "weight:75"]}}'
Error: Error from server: cannot restore struct from: string
I pulled the current spec of my route and it looks like:
"spec": {
"alternateBackends": [
{
"kind": "Service",
"name": "image-mirror-poc-blue",
"weight": 75
}
],
"host": "image-mirror-poc.sbx1apps.ocp.delta.com",
"port": {
"targetPort": "8080-tcp"
},
"to": {
"kind": "Service",
"name": "image-mirror-poc-green",
"weight": 25
},
"wildcardPolicy": "None"
}
I have been trying to following the documentation but unsuccessful.
Openshift info:
$ oc version
oc v3.9.0+191fece
kubernetes v1.9.1+a0ce1bc657
features: Basic-Auth
Server https://mycluster.ocp.mycompany.com
openshift v3.7.23
kubernetes v1.7.6+a08f5eeb6
How about this format ? If the changes are nothing, then the patch command finish with no changes.
oc patch route/image-mirror-poc --patch'{"spec": {"alternateBackends": [{"kind": "Service","name": "image-mirror-poc-blue","weight": 75}]}}'

Get ami_name from inside a script in packer

I have a script in provisioner's section of my Packer template, which requires the variable $ami_name. Is there any way that I can reliably get this information?
Provisioners section
"provisioners": [
{
"type": "shell",
"execute_command": "sudo {{.Vars}} bash '{{.Path}}'",
"scripts": [
"./scripts/packer/myscript.sh"
]
}
]
./scripts/packer/myscript.sh:
#!/usr/bin/env bash
aminame=$ami_name
make_an_external_call "$aminame"
Here I need to get the ami_name from Packer. As this provisioners stage happens after Packer does a pre-validation of AMI Name, there should be a way I can get this right?

Parsing JSON "Pretty" format to one liner

I run a command on one of my systems and it spits out JSON "pretty" format like so:
[
{
"server": "servename1",
"i.p": 127.0.0.1,
"domain": "generic",
"OS": "RHEL",
"Version": 7.0
},
{
"server": "servename2",
"i.p": 127.0.0.1,
"domain": "generic",
"OS": "RHEL",
"Version": 7.0
},
{
"server": "servename3",
"i.p": 127.0.0.1,
"domain": "generic",
"OS": "RHEL",
"Version": 7.0
}
]
I need to parse these paragraphs in one liners like so:
[{"server":"servename1","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename2","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename3","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},]
What is the easiest way to do this? I am tried using SED and JQ but couldn't get it.
You can try this in jq if you're OK with newline-delimited JSON:
$ jq -c ".[]" test.json
{"server":"servename1","i.p":"127.0.0.1","domain":"generic","OS":"RHEL","Version":7}
{"server":"servename2","i.p":"127.0.0.1","domain":"generic","OS":"RHEL","Version":7}
{"server":"servename3","i.p":"127.0.0.1","domain":"generic","OS":"RHEL","Version":7}
Note that I had to quote the IP addresses from your sample, since the JSON you posted is not valid JSON.
If your input is always that regular:
$ awk '{ORS=(/},|\]/?RS:""); gsub(/[[:blank:]]+/,""); sub(/}$/,"},")}1' file
[{"server":"servename1","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename2","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename3","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},]
If that doesn't work for your real input then edit your question to include more truly representative sample input.
To skip the [ and ] lines:
$ awk '!/^[][]/{ORS=(/},/?RS:""); gsub(/[[:blank:]]+/,""); sub(/}$/,"},\n"); print}' file
{"server":"servename1","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename2","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename3","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},