From 152efb6fce7c8971fd003c9407ef14e84f49ae86 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 17 Apr 2024 21:31:40 -0400 Subject: [PATCH] using compile-time plugin for labeler --- pkg/helpers/labeler-helpers.go | 2 + .../labeler-plugin-labeler.old | 124 ------------------ pkg/plugin-labeler/labeler-plugin-labeler.go | 55 ++++++++ 3 files changed, 57 insertions(+), 124 deletions(-) delete mode 100644 pkg/plugin-labeler-old/labeler-plugin-labeler.old create mode 100644 pkg/plugin-labeler/labeler-plugin-labeler.go diff --git a/pkg/helpers/labeler-helpers.go b/pkg/helpers/labeler-helpers.go index b25e94e..5825e9c 100644 --- a/pkg/helpers/labeler-helpers.go +++ b/pkg/helpers/labeler-helpers.go @@ -19,6 +19,7 @@ import ( pluginBPcreator "github.com/clubanderson/labeler/pkg/plugin-bp-creator" pluginHelp "github.com/clubanderson/labeler/pkg/plugin-help" + pluginLabeler "github.com/clubanderson/labeler/pkg/plugin-labeler" pluginOCMcreator "github.com/clubanderson/labeler/pkg/plugin-ocm-creator" pluginRemoteDeploy "github.com/clubanderson/labeler/pkg/plugin-remote-deploy" @@ -48,6 +49,7 @@ var pluginFunctions = []interface{}{ pluginBPcreator.PluginCreateBP, pluginOCMcreator.PluginCreateMW, pluginRemoteDeploy.PluginRemoteDeployTo, + pluginLabeler.PluginLabeler, // add other plugin functions here as needed } diff --git a/pkg/plugin-labeler-old/labeler-plugin-labeler.old b/pkg/plugin-labeler-old/labeler-plugin-labeler.old deleted file mode 100644 index 653c37b..0000000 --- a/pkg/plugin-labeler-old/labeler-plugin-labeler.old +++ /dev/null @@ -1,124 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "log" - "strings" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/types" -) - -type PluginImpl struct{} - -func (pi PluginImpl) Run() []string { - log.Println("Plugin is running") - return []string{"label,string,label key and value to be applied to objects (usage: --label=app.kubernetes.io/part-of=sample)"} -} - -func (p ParamsStruct) PluginLabeler(reflect bool) []string { - // function must be exportable (capitalize first letter of function name) to be discovered by labeler - if reflect { - return []string{"label,string,label key and value to be applied to objects (usage: --label=app.kubernetes.io/part-of=sample)"} - } - - if p.params["labelKey"] != "" && p.params["labelVal"] != "" && (p.flags["upgrade"] || p.flags["install"] || p.flags["apply"] || p.flags["create"] || p.flags["replace"]) { - for r, v := range p.resources { - _ = v - gvr := schema.GroupVersionResource{ - Group: r.Group, - Version: r.Version, - Resource: r.Resource, - } - var err error - if gvr.Resource == "namespaces" { - if r.ObjectName == "" || r.ObjectName == "default" { - labelCmd := fmt.Sprintf("kubectl label %v %v %v=%v\n", gvr.Resource, r.ObjectName, p.params["labelKey"], p.params["labelVal"]) - runResults.didNotLabel = append(runResults.didNotLabel, labelCmd) - } else { - err = p.setLabel(r.Namespace, r.ObjectName, gvr) - } - } else { - err = p.setLabel(r.Namespace, r.ObjectName, gvr) - } - if err != nil { - if p.flags["l-debug"] { - log.Println("labeler.go: error (setLabel):", err) - } - // return err - } - } - } - if len(runResults.didNotLabel) > 0 { - log.Printf("\nlabeler.go: The following resources can be labeled at a later time:\n\n") - for _, cmd := range runResults.didNotLabel { - log.Printf(cmd) - } - } - log.Println() - - return []string{} -} - -func (p ParamsStruct) setLabel(namespace, objectName string, gvr schema.GroupVersionResource) error { - - if flags.label == "" && p.params["labelKey"] == "" { - if p.flags["l-debug"] { - log.Println("labeler.go: no label provided") - } - return nil - } - if flags.label != "" { - p.params["labelKey"], p.params["labelVal"] = strings.Split(flags.label, "=")[0], strings.Split(flags.label, "=")[1] - } - - labels := map[string]string{ - p.params["labelKey"]: p.params["labelVal"], - } - - // serialize labels to JSON - patch, err := json.Marshal(map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": labels, - }, - }) - if err != nil { - return err - } - - if p.flags["l-debug"] { - log.Printf("labeler.go: patching object %v/%v/%v %q in namespace %q with %v=%v %q %q %q %v\n", gvr.Group, gvr.Version, gvr.Resource, objectName, namespace, p.params["labelKey"], p.params["labelVal"], gvr.Resource, gvr.Version, gvr.Group, string(patch)) - } - if namespace == "" { - _, err = p.DynamicClient.Resource(gvr).Patch(context.TODO(), objectName, types.MergePatchType, patch, metav1.PatchOptions{}) - if err != nil { - if p.flags["l-debug"] { - log.Printf("labeler.go: error patching object %v/%v/%v %q in namespace %q: %v\n", gvr.Group, gvr.Version, gvr.Resource, objectName, namespace, err) - } - } - } else { - _, err = p.DynamicClient.Resource(gvr).Namespace(namespace).Patch(context.TODO(), objectName, types.MergePatchType, patch, metav1.PatchOptions{}) - if err != nil { - if p.flags["l-debug"] { - log.Printf("labeler.go: error patching object %v/%v/%v %q in namespace %q: %v\n", gvr.Group, gvr.Version, gvr.Resource, objectName, namespace, err) - } - } - } - - if err != nil { - if namespace != "" { - labelCmd := fmt.Sprintf("kubectl label %v %v %v=%v -n %q\n", gvr.Resource, objectName, p.params["labelKey"], p.params["labelVal"], namespace) - runResults.didNotLabel = append(runResults.didNotLabel, labelCmd) - } else { - labelCmd := fmt.Sprintf("kubectl label %v %v %v=%v\n", gvr.Resource, objectName, p.params["labelKey"], p.params["labelVal"]) - runResults.didNotLabel = append(runResults.didNotLabel, labelCmd) - } - return err - } - - log.Printf(" 🏷️ labeled object %v/%v/%v %q in namespace %q with %v=%v\n", gvr.Group, gvr.Version, gvr.Resource, objectName, namespace, p.params["labelKey"], p.params["labelVal"]) - return nil -} diff --git a/pkg/plugin-labeler/labeler-plugin-labeler.go b/pkg/plugin-labeler/labeler-plugin-labeler.go new file mode 100644 index 0000000..64e1fbe --- /dev/null +++ b/pkg/plugin-labeler/labeler-plugin-labeler.go @@ -0,0 +1,55 @@ +package pluginLabeler + +import ( + "fmt" + "log" + + c "github.com/clubanderson/labeler/pkg/common" + k "github.com/clubanderson/labeler/pkg/kube-helpers" + + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func PluginLabeler(p c.ParamsStruct, reflect bool) []string { + // function must be exportable (capitalize first letter of function name) to be discovered by labeler + if reflect { + return []string{"label,string,label key and value to be applied to objects (usage: --label=app.kubernetes.io/part-of=sample)"} + } + + if p.Params["labelKey"] != "" && p.Params["labelVal"] != "" && (p.Flags["upgrade"] || p.Flags["install"] || p.Flags["apply"] || p.Flags["create"] || p.Flags["replace"]) { + for r, v := range p.Resources { + _ = v + gvr := schema.GroupVersionResource{ + Group: r.Group, + Version: r.Version, + Resource: r.Resource, + } + var err error + if gvr.Resource == "namespaces" { + if r.ObjectName == "" || r.ObjectName == "default" { + labelCmd := fmt.Sprintf("kubectl label %v %v %v=%v\n", gvr.Resource, r.ObjectName, p.Params["labelKey"], p.Params["labelVal"]) + c.RunResults.DidNotLabel = append(c.RunResults.DidNotLabel, labelCmd) + } else { + err = k.SetLabel(r.Namespace, r.ObjectName, gvr, p) + } + } else { + err = k.SetLabel(r.Namespace, r.ObjectName, gvr, p) + } + if err != nil { + if p.Flags["l-debug"] { + log.Println("labeler.go: error (setLabel):", err) + } + // return err + } + } + } + if len(c.RunResults.DidNotLabel) > 0 { + log.Printf("\nlabeler.go: The following resources can be labeled at a later time:\n\n") + for _, cmd := range c.RunResults.DidNotLabel { + log.Printf("%v", cmd) + } + } + log.Println() + + return []string{} +}