1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

add script to generate N number of resources/policies

This commit is contained in:
Shuting Zhao 2019-08-05 16:24:59 -07:00
parent 912c99a88f
commit 92d69bdb2a
3 changed files with 48 additions and 8 deletions

View file

@ -1,10 +1,10 @@
apiVersion: kyverno.io/v1alpha1 apiVersion: kyverno.io/v1alpha1
kind: Policy kind: Policy
metadata: metadata:
name: "default-networkPolicy" name: "default-networkpolicy"
spec: spec:
rules: rules:
- name: "default-networkPolicy" - name: "default-networkpolicy"
match: match:
resources: resources:
kinds: kinds:
@ -12,7 +12,7 @@ spec:
name: "devtest" name: "devtest"
generate: generate:
kind: NetworkPolicy kind: NetworkPolicy
name: default-networkPolicy name: default-networkpolicy
data: data:
spec: spec:
# select all pods in the namespace # select all pods in the namespace

View file

@ -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

View file

@ -14,7 +14,7 @@ import (
yaml "k8s.io/apimachinery/pkg/util/yaml" yaml "k8s.io/apimachinery/pkg/util/yaml"
) )
var policyPath, resourcePath string var policyPath, replica string
func main() { func main() {
generatePolicies() generatePolicies()
@ -22,7 +22,6 @@ func main() {
func generatePolicies() error { func generatePolicies() error {
var policy *kubepolicy.Policy var policy *kubepolicy.Policy
numbers := 10
file, err := ioutil.ReadFile(policyPath) file, err := ioutil.ReadFile(policyPath)
if err != nil { if err != nil {
@ -41,8 +40,8 @@ func generatePolicies() error {
} }
oldName := policy.Name oldName := policy.Name
repl, _ := strconv.Atoi(replica)
for i := 0; i < numbers; i++ { for i := 0; i < repl; i++ {
newName := oldName + "-" + strconv.Itoa(i) newName := oldName + "-" + strconv.Itoa(i)
data := bytes.Replace(file, []byte(oldName), []byte(newName), -1) data := bytes.Replace(file, []byte(oldName), []byte(newName), -1)
@ -68,7 +67,7 @@ func writeToFile(data []byte, filename string) {
func init() { func init() {
flag.StringVar(&policyPath, "policyPath", "", "Path to a policy") 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() flag.Parse()
} }