From 92d69bdb2ae7975f501c7b9dcf4771da8b52dbac Mon Sep 17 00:00:00 2001 From: Shuting Zhao Date: Mon, 5 Aug 2019 16:24:59 -0700 Subject: [PATCH] add script to generate N number of resources/policies --- examples/policy_generate_networkPolicy.yaml | 6 +-- test/generate-resource/generate.sh | 41 +++++++++++++++++++++ test/generate-resource/main.go | 9 ++--- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 test/generate-resource/generate.sh diff --git a/examples/policy_generate_networkPolicy.yaml b/examples/policy_generate_networkPolicy.yaml index 390d8fc07c..6c4ea2bf55 100644 --- a/examples/policy_generate_networkPolicy.yaml +++ b/examples/policy_generate_networkPolicy.yaml @@ -1,10 +1,10 @@ apiVersion: kyverno.io/v1alpha1 kind: Policy metadata: - name: "default-networkPolicy" + name: "default-networkpolicy" spec: rules: - - name: "default-networkPolicy" + - name: "default-networkpolicy" match: resources: kinds: @@ -12,7 +12,7 @@ spec: name: "devtest" generate: kind: NetworkPolicy - name: default-networkPolicy + name: default-networkpolicy data: spec: # select all pods in the namespace diff --git a/test/generate-resource/generate.sh b/test/generate-resource/generate.sh new file mode 100644 index 0000000000..76dfe5a463 --- /dev/null +++ b/test/generate-resource/generate.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +### To use this script to generate resource: +### ./resource.sh --file=resource.yaml --replica=10 + +for i in "$@" +do +case $i in + --file=*) + file="${i#*=}" + shift + ;; + --replica=*) + replica="${i#*=}" + shift + ;; +esac +done + +if [ -z "${file}" ]; then + echo -e "Please specify '--file' where resource is located." + exit 1 +fi + +if [ -z "${replica}" ]; then + echo -e "Please specify '--replica' of the number of replicas you want to create." + exit 1 +fi + +echo "loading resource from ${file}" +RESOURCE=$(cat ${file} | sed -n -e 's/^ name: //p') + +echo "generating ${replica} replicas from resource $RESOURCE" + +for i in $(seq 1 ${replica}) +do + # echo `cat ${file} | sed "s/name: ${RESOURCE}/name: ${RESOURCE}-${i}/"` + dstfile=`sed 's/.\{5\}$/-$i&/' <<< "${file}"` + cat ${file} | sed "s/name: ${RESOURCE}/name: ${RESOURCE}-${i}/" > ${dstfile} +done + diff --git a/test/generate-resource/main.go b/test/generate-resource/main.go index 22e2238201..ee5a66b579 100644 --- a/test/generate-resource/main.go +++ b/test/generate-resource/main.go @@ -14,7 +14,7 @@ import ( yaml "k8s.io/apimachinery/pkg/util/yaml" ) -var policyPath, resourcePath string +var policyPath, replica string func main() { generatePolicies() @@ -22,7 +22,6 @@ func main() { func generatePolicies() error { var policy *kubepolicy.Policy - numbers := 10 file, err := ioutil.ReadFile(policyPath) if err != nil { @@ -41,8 +40,8 @@ func generatePolicies() error { } oldName := policy.Name - - for i := 0; i < numbers; i++ { + repl, _ := strconv.Atoi(replica) + for i := 0; i < repl; i++ { newName := oldName + "-" + strconv.Itoa(i) data := bytes.Replace(file, []byte(oldName), []byte(newName), -1) @@ -68,7 +67,7 @@ func writeToFile(data []byte, filename string) { func init() { flag.StringVar(&policyPath, "policyPath", "", "Path to a policy") - flag.StringVar(&resourcePath, "resourcePath", "", "Path to a resource") + flag.StringVar(&replica, "replica", "10", "the number of replicas to generate") flag.Parse() }