Skip to content

Commit

Permalink
using compile-time plugin for labeler
Browse files Browse the repository at this point in the history
  • Loading branch information
clubanderson committed Apr 18, 2024
1 parent 323d577 commit 152efb6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 124 deletions.
2 changes: 2 additions & 0 deletions pkg/helpers/labeler-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -48,6 +49,7 @@ var pluginFunctions = []interface{}{
pluginBPcreator.PluginCreateBP,
pluginOCMcreator.PluginCreateMW,
pluginRemoteDeploy.PluginRemoteDeployTo,
pluginLabeler.PluginLabeler,
// add other plugin functions here as needed
}

Expand Down
124 changes: 0 additions & 124 deletions pkg/plugin-labeler-old/labeler-plugin-labeler.old

This file was deleted.

55 changes: 55 additions & 0 deletions pkg/plugin-labeler/labeler-plugin-labeler.go
Original file line number Diff line number Diff line change
@@ -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{}
}

0 comments on commit 152efb6

Please sign in to comment.