Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DelusionalOptimist/7e870dc17bf80277b48cd840aaa0ca9d to your computer and use it in GitHub Desktop.
Save DelusionalOptimist/7e870dc17bf80277b48cd840aaa0ca9d to your computer and use it in GitHub Desktop.
#!/usr/env bash
# change these vars according to needs
# do run `helm repo add hashicorp https://helm.releases.hashicorp.com` before using this
if [[ -z "${CHART_VERSION:-}" ]]; then
CHART_VERSION=$(helm show chart hashicorp/consul | grep 'version' | cut -d ' ' -f2)
fi
NEW_CONSUL_VERSION=$(helm show chart hashicorp/consul --version $CHART_VERSION | grep 'appVersion' | cut -d ' ' -f2)
echo $CHART_VERSION $NEW_CONSUL_VERSION
UTILPATH=~/dev/kubeopenapi-jsonschema
meshName=Consul
smpMeshName=CONSUL
resourceType=CustomResourceDefinition
dirName=./templates/oam/workloads/$NEW_CONSUL_VERSION
mkdir consul-$CHART_VERSION; cd consul-$CHART_VERSION
# use this for newer charts
curl --silent --remote-name-all `curl --silent -H "Accept: application/vnd.github.VERSION.raw" https://api.github.com/repos/hashicorp/consul-k8s/contents/control-plane/config/crd/bases\?ref\=v${CHART_VERSION} | jq 'map(.download_url) | .[]' | tr -d '"'`;
rm -rf ~/consul.yaml
awk 'FNR==1 && NR>1 { printf("\n%s\n\n","---") } 1' ./*.yaml > ~/consul.yaml;
# for older charts
#curl --silent --remote-name-all `curl -H "Accept: application/vnd.github.VERSION.raw" https://api.github.com/repos/hashicorp/consul-helm/contents/templates\?ref\=v${CHART_VERSION} | jq 'map(.download_url) | .[]' | tr -d '"' | grep crd`;
#rm -rf ~/consul.yaml
#for file in ./*.yaml; do sed '1d;$d;1s/^/---/' ./$file >> ~/consul.yaml; done
cd ..
echo "generating components..."
template='{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkloadDefinition","metadata":{},"spec":{"definitionRef":{},"metadata":{"@type":"pattern.meshery.io/mesh/workload","meshVersion":"'v$NEW_CONSUL_VERSION'","meshName":"'$smpMeshName'","k8sAPIVersion":null,"k8sKind":""}}}'
crds=$($UTILPATH/kubeopenapi-jsonschema --location ~/consul.yaml -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.consul.meshery.layer5.io $nameLower)
apiVersion=$($UTILPATH/kubeopenapi-jsonschema --location ~/consul.yaml -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 ~/consul.yaml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..spec' --o-filter "$[].group" -o json | jq '.[]' | tr -d '"')
$UTILPATH/kubeopenapi-jsonschema --location ~/consul.yaml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..openAPIV3Schema.properties.spec' --o-filter "$[]" -o json |\
jq '.[]' > $dirName/$nameLower.consul.meshery.layer5io.schema.json
echo $template |\
jq '
."metadata"."name" = "'$(printf %s.consul $nameUpper)'"
| ."spec"."metadata"."k8sAPIVersion" = "'$(printf $apiGroup/$apiVersion $apiGroup $apiVersion)'"
| ."spec"."metadata"."k8sKind" = "'$nameUpper'"
| ."spec"."definitionRef"."name"="'$definitionRef'"' > $dirName/$nameLower.consul_definition.json
done
rm -rf ~/consul.yaml
@DelusionalOptimist
Copy link
Author

  • do run the below command before using this script
     helm repo add hashicorp https://helm.releases.hashicorp.com
    
  • for running the script
     CHART_VERSION=0.34.1 bash consul-pattern-components-gen.sh 
    
  • this should work well with newer versions however older ones will need a little commenting and un commenting in the script.

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