Skip to content

Commit

Permalink
added support for creating a KubeStellar BindingPolicy with --create-…
Browse files Browse the repository at this point in the history
…bp switch - next - brew install formula and demos - home stretch
  • Loading branch information
clubanderson committed Apr 7, 2024
1 parent 983475a commit 8a01c34
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
69 changes: 69 additions & 0 deletions labeler-bp-creator.go
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 10 additions & 0 deletions labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ParamsStruct struct {
debugMode bool
templateMode bool
installMode bool
createBindingPolicy bool
}

type resultsStruct struct {
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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:]...)
}
}

Expand Down Expand Up @@ -208,6 +213,11 @@ func (p ParamsStruct) aliasRun(args []string) error {
log.Printf(cmd)
}
}
if p.createBindingPolicy {
log.Println()
p.createBP()
}

}
return nil
}
Expand Down

0 comments on commit 8a01c34

Please sign in to comment.