mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
add function to generate N policies
This commit is contained in:
parent
70f5592933
commit
912c99a88f
1 changed files with 74 additions and 0 deletions
74
test/generate-resource/main.go
Normal file
74
test/generate-resource/main.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
ioutil "io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
kubepolicy "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
|
||||
yaml "k8s.io/apimachinery/pkg/util/yaml"
|
||||
)
|
||||
|
||||
var policyPath, resourcePath string
|
||||
|
||||
func main() {
|
||||
generatePolicies()
|
||||
}
|
||||
|
||||
func generatePolicies() error {
|
||||
var policy *kubepolicy.Policy
|
||||
numbers := 10
|
||||
|
||||
file, err := ioutil.ReadFile(policyPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load file: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Generating policies from %s\n", policyPath)
|
||||
|
||||
rawPolicy, err := yaml.ToJSON(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(rawPolicy, &policy); err != nil {
|
||||
return fmt.Errorf("failed to decode policy %s, err: %v", policy.Name, err)
|
||||
}
|
||||
|
||||
oldName := policy.Name
|
||||
|
||||
for i := 0; i < numbers; i++ {
|
||||
newName := oldName + "-" + strconv.Itoa(i)
|
||||
data := bytes.Replace(file, []byte(oldName), []byte(newName), -1)
|
||||
|
||||
writeToFile(data, "./.policy/"+newName+".yaml")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeToFile(data []byte, filename string) {
|
||||
|
||||
dir := filepath.Dir(filename)
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
if err = os.MkdirAll(dir, 0755); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(filename, data, 0755); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&policyPath, "policyPath", "", "Path to a policy")
|
||||
flag.StringVar(&resourcePath, "resourcePath", "", "Path to a resource")
|
||||
|
||||
flag.Parse()
|
||||
}
|
Loading…
Add table
Reference in a new issue