Skip to content

Instantly share code, notes, and snippets.

@DelusionalOptimist
Last active October 10, 2021 17:43
Show Gist options
  • Save DelusionalOptimist/36e639fcf0e68cbeef85386198985791 to your computer and use it in GitHub Desktop.
Save DelusionalOptimist/36e639fcf0e68cbeef85386198985791 to your computer and use it in GitHub Desktop.
Shell script for extracting JSON schema and generating OAM workloads from service mesh manifests
#!/usr/env bash
# change these vars according to needs
NEW_ISTIO_VERSION=1.10.3
UTILPATH=~/dev/kubeopenapi-jsonschema
meshName=Istio
smpMeshName=ISTIO
resourceType=CustomResourceDefinition
dirName=./templates/oam/workloads/$NEW_ISTIO_VERSION
# change this according to the link for fetching manifests
curl https://raw.githubusercontent.com/istio/istio/$NEW_ISTIO_VERSION/manifests/charts/base/crds/crd-all.gen.yaml > ~/istio.yml
template='{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkloadDefinition","metadata":{},"spec":{"definitionRef":{},"metadata":{"@type":"pattern.meshery.io/mesh/workload","meshVersion":"'$NEW_ISTIO_VERSION'","meshName":"'$smpMeshName'","k8sAPIVersion":null,"k8sKind":""}}}'
crds=$($UTILPATH/kubeopenapi-jsonschema --location ~/istio.yml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition")]' -o json --o-filter '$..["spec"]["names"]["kind"]' | jq '.[]')
mkdir -p $dirName
for t in ${crds[@]}; do
nameUpper=`echo $t | tr -d '"'`
nameLower=`echo $t | tr -d '"' | tr '[:upper:]' '[:lower:]'`
definitionRef=$(printf %s.istio.meshery.layer5.io $nameLower)
apiVersion=$($UTILPATH/kubeopenapi-jsonschema --location ~/istio.yml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..spec.versions[0]' --o-filter "$[].name" -o json | jq '.[]' | tr -d '"')
apiGroup=$($UTILPATH/kubeopenapi-jsonschema --location ~/istio.yml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..spec' --o-filter "$[].group" -o json | jq '.[]' | tr -d '"')
$UTILPATH/kubeopenapi-jsonschema --location ~/istio.yml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..openAPIV3Schema.properties.spec' --o-filter "$[]" -o json |\
jq '
.[]
| .version = "'$NEW_ISTIO_VERSION'"
| ."object-type" = "'$nameUpper'"
| ."service-mesh" = "'$meshName'" ' > $dirName/$nameLower.istio.meshery.layer5io.schema.json
echo $template |\
jq '
."metadata"."name" = "'$(printf %s.Istio $nameUpper)'"
| ."spec"."metadata"."k8sAPIVersion" = "'$(printf $apiGroup/$apiVersion $apiGroup $apiVersion)'"
| ."spec"."metadata"."k8sKind" = "'$nameUpper'"
| ."spec"."definitionRef"."name"="'$definitionRef'"' > $dirName/$nameLower.istio_definition.json
done
@Shreyas220
Copy link

Shreyas220 commented Aug 20, 2021

How to use

-> Building kubeopenapi-jsonschema

  • nvm install v14.15.3
  • nvm install-latest-npm
  • git clone https://github.com/layer5io/kubeopenapi-jsonschema
  • cd kubeopenapi-jsonschema
  • sudo npm i
  • sudo npm i nexe -g
  • make linux

->Using the script

get the script in adapter repo

few things you need to keep in mind before running the script

  • Build the kubeopenapi-jsonschema before
  • set the UTILPATH variable to where the kubeopenapi-jsonschema is , for example if it is on desktop it should be UTILPATH=~/Desktop/kubeopenapi-jsonschema
  • make sure the crds link is valid

Now run the script using bash generator.sh and go brrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

@DelusionalOptimist
Copy link
Author

Thanks @Shreyas220 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment