diff --git a/README.md b/README.md index 8b2d124..b2dc9f3 100644 --- a/README.md +++ b/README.md @@ -311,3 +311,22 @@ run kl with any kubectl command line arguments, and labeler will label all appli 🏷️ labeled object apps/v1/deployments "sealed-secrets" in namespace "sealed-secrets" with app.kubernetes.io/part-of=sample-app 🏷️ labeled object /v1/namespaces "sealed-secrets" with app.kubernetes.io/part-of=sample-app + +# Labeler with a sample KubeStellar BindingPolicy as output + + kl apply -k examples/kustomize -l app.kubernetes.io/part-of=sample --context=kind-kind --namespace=default --overwrite --create-bp  kind-kind/default ⎈ + service/my-app-service already has label app.kubernetes.io/part-of=sample + deployment.apps/my-app-deployment already has label app.kubernetes.io/part-of=sample + + apiVersion: control.kubestellar.io/v1alpha1 + kind: BindingPolicy + metadata: + name: wec-kwasm-bindingpolicy + wantSingletonReportedState: true + clusterSelectors: + - matchLabels: + location-group: edge + downsync: + - objectSelectors: + - matchLabels: + app.kubernetes.io/part-of: sample \ No newline at end of file diff --git a/labeler-bp-creator.go b/labeler-bp-creator.go new file mode 100644 index 0000000..448ebb3 --- /dev/null +++ b/labeler-bp-creator.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + + "gopkg.in/yaml.v2" +) + +type BindingPolicy struct { + APIVersion string `yaml:"apiVersion"` + Kind string `yaml:"kind"` + Metadata Metadata `yaml:"metadata"` + WantSingletonReportedState bool `yaml:"wantSingletonReportedState"` + ClusterSelectors []ClusterSelector `yaml:"clusterSelectors"` + Downsync []Downsync `yaml:"downsync"` +} + +type Metadata struct { + Name string `yaml:"name"` +} + +type ClusterSelector struct { + MatchLabels map[string]string `yaml:"matchLabels"` +} + +type Downsync struct { + ObjectSelectors []ObjectSelector `yaml:"objectSelectors"` +} + +type ObjectSelector struct { + MatchLabels map[string]string `yaml:"matchLabels"` +} + +func (p ParamsStruct) createBP() { + bindingPolicy := BindingPolicy{ + APIVersion: "control.kubestellar.io/v1alpha1", + Kind: "BindingPolicy", + Metadata: Metadata{ + Name: "wec-kwasm-bindingpolicy", + }, + WantSingletonReportedState: true, + ClusterSelectors: []ClusterSelector{ + { + MatchLabels: map[string]string{ + "location-group": "edge", + }, + }, + }, + Downsync: []Downsync{ + { + ObjectSelectors: []ObjectSelector{ + { + MatchLabels: map[string]string{ + p.labelKey: p.labelVal, + }, + }, + }, + }, + }, + } + + yamlData, err := yaml.Marshal(bindingPolicy) + if err != nil { + fmt.Println("Error marshaling YAML:", err) + return + } + + fmt.Println(string(yamlData)) +} diff --git a/labeler.go b/labeler.go index 01269ae..70105cd 100755 --- a/labeler.go +++ b/labeler.go @@ -46,6 +46,7 @@ type ParamsStruct struct { debugMode bool templateMode bool installMode bool + createBindingPolicy bool } type resultsStruct struct { @@ -100,6 +101,7 @@ func (p ParamsStruct) aliasRun(args []string) error { p.dryrunMode = false p.templateMode = false p.namespace = "" + p.createBindingPolicy = false if args[0] == "k" || args[0] == "kubectl" || args[0] == "helm" { for i := 0; i < len(args); i++ { // log.Printf("arg: %v\n", args[i]) @@ -136,6 +138,9 @@ func (p ParamsStruct) aliasRun(args []string) error { p.labelKey = strings.Split(args[i], "=")[1] p.labelVal = strings.Split(args[i], "=")[2] args = append(args[:i], args[i+1:]...) + } else if args[i] == "--create-bp" { + p.createBindingPolicy = true + args = append(args[:i], args[i+1:]...) } } @@ -208,6 +213,11 @@ func (p ParamsStruct) aliasRun(args []string) error { log.Printf(cmd) } } + if p.createBindingPolicy { + log.Println() + p.createBP() + } + } return nil }