Skip to content

Commit

Permalink
now working with kubeconfig from environment and working with kubectl…
Browse files Browse the repository at this point in the history
… edit, logs and other interactive commands, updated brew and version number
  • Loading branch information
clubanderson committed Apr 12, 2024
1 parent 1d2ed29 commit 1c6acc0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Formula/labeler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Labeler < Formula
desc "Utility that automates the labeling of resources output from kubectl, kustomize, and helm"
homepage "https://github.com/clubanderson/labeler"
url "https://github.com/clubanderson/labeler/releases/download/v0.15.0/labeler"
url "https://github.com/clubanderson/labeler/releases/download/v0.16.0/labeler"
# sha256 "26c5d47adbd0ed7d0a0d9f8a33a25bc242f7cdff2a661d8d6211f5279ca995d4"

def install
Expand Down
45 changes: 34 additions & 11 deletions pkg/src/labeler-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"regexp"
Expand Down Expand Up @@ -197,7 +198,7 @@ func (p ParamsStruct) runHelmInTemplateMode(args []string) []byte {
log.Printf("labeler.go: [debug] modified command components: %v\n", modifiedCommandComponents)
}

output, err := p.runCmd("helm", modifiedCommandComponents)
output, err := p.runCmd("helm", modifiedCommandComponents, true)
if err != nil {
// log.Println("labeler.go: error (run helm):", err)
os.Exit(1)
Expand Down Expand Up @@ -395,7 +396,7 @@ func (p ParamsStruct) createCachedDiscoveryClient(restConfigCoreOrWds rest.Confi

func (p ParamsStruct) useContext(contextName string) {
setContext := []string{"config", "use-context", contextName}
_, err := p.runCmd("kubectl", setContext)
_, err := p.runCmd("kubectl", setContext, false)
if err != nil {
// log.Printf(" 🔴 error setting kubeconfig's current context: %v\n", err)
} else {
Expand Down Expand Up @@ -429,26 +430,48 @@ func DecodeYAML(yamlBytes []byte) (*unstructured.Unstructured, error) {
return obj, nil
}

func (p ParamsStruct) runCmd(cmdToRun string, cmdArgs []string) ([]byte, error) {
func expandTilde(args []string) []string {
for i, arg := range args {
if strings.Contains(arg, "~") {
usr, err := user.Current()
if err != nil {
log.Printf("Error getting current user: %v\n", err)
return args
}
args[i] = strings.ReplaceAll(args[i], "~", usr.HomeDir)
}
}
return args
}

func (p ParamsStruct) runCmd(cmdToRun string, cmdArgs []string, suppressOutput bool) ([]byte, error) {
cmdArgs = expandTilde(cmdArgs)

cmd := exec.Command(cmdToRun, cmdArgs...)
cmd.Env = append(cmd.Env, "PATH="+p.path)
cmd.Env = append(cmd.Env, "HOME="+p.homeDir)
cmd.Env = append(cmd.Env, "KUBECONFIG="+os.Getenv("KUBECONFIG"))

var outputBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(&outputBuf)
cmd.Stderr = io.MultiWriter(&outputBuf)
if suppressOutput {
cmd.Stdout = io.MultiWriter(&outputBuf)
} else {
cmd.Stdout = io.MultiWriter(&outputBuf, os.Stdout)
}
cmd.Stderr = io.MultiWriter(&outputBuf, os.Stderr)
cmd.Stdin = os.Stdin

err := cmd.Start()
if err != nil {
log.Println("labeler.go: error starting command:", err)
return nil, err
}

// err = cmd.Wait()
// if err != nil {
// // log.Println("labeler.go: error waiting for command to complete:", err)
// log.Printf(string(outputBuf.Bytes()))
// return nil, err
// }
err = cmd.Wait()
if err != nil {
// log.Println("labeler.go: error waiting for command to complete:", err)
// log.Printf(string(outputBuf.Bytes()))
return nil, err
}
return outputBuf.Bytes(), nil
}
2 changes: 1 addition & 1 deletion pkg/src/labeler-piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (p ParamsStruct) helmOrKubectl(r io.Reader, w io.Writer, input []string) er
modifiedCommandComponents := append(strings.Split(modifiedCommand, " ")[1:])
// log.Printf("labeler.go: modified command: %q\n", modifiedCommand)
// log.Printf("labeler.go: modified command components: %q\n", modifiedCommandComponents)
output, err := p.runCmd("helm", modifiedCommandComponents)
output, err := p.runCmd("helm", modifiedCommandComponents, true)
if err != nil {
// log.Println("labeler.go: error (running helm):", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/src/labeler-plugin-bp-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (p ParamsStruct) PluginCreateBP(reflect bool) []string {
log.Printf(" 🚀 Attempting to create %v object %q in WDS namespace %q", k, n, p.params[nsArg])
p.createObjForPlugin(gvk, yamlData, n, r, p.params["namespaceArg"])
} else {
fmt.Println(string(yamlData))
fmt.Printf("%v", string(yamlData))
}
return []string{}
}
2 changes: 1 addition & 1 deletion pkg/src/labeler-plugin-ocm-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (p ParamsStruct) PluginCreateMW(reflect bool) []string {
log.Printf(" 🚀 Attempting to create %v object %q in namespace %q", k, n, p.params["namespaceArg"])
p.createObjForPlugin(gvk, yamlData, n, r, p.params["namespaceArg"])
} else {
fmt.Println(string(yamlData))
fmt.Printf("%v", string(yamlData))
}
return []string{}
}
4 changes: 2 additions & 2 deletions pkg/src/labeler-plugin-remote-deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p ParamsStruct) PluginRemoteDeployTo(reflect bool) []string {
log.Printf("labeler.go: [debug] modified command components: %v\n", modifiedCommand)
}

output, err := p.runCmd("kubectl", modifiedCommand[1:])
output, err := p.runCmd("kubectl", modifiedCommand[1:], false)
if err != nil {
log.Println(err)
} else {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (p ParamsStruct) PluginRemoteDeployTo(reflect bool) []string {
log.Printf("labeler.go: [debug] modified command components: %v\n", modifiedCommand)
}

output, err := p.runCmd("helm", modifiedCommand[1:])
output, err := p.runCmd("helm", modifiedCommand[1:], false)
if err != nil {
log.Println(err)
} else {
Expand Down
22 changes: 10 additions & 12 deletions pkg/src/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"os"
"os/exec"
"os/user"
"reflect"
"strings"
Expand All @@ -16,7 +15,7 @@ import (
"k8s.io/client-go/rest"
)

var version = "0.15.0"
var version = "0.16.0"

type ResourceStruct struct {
Group string
Expand All @@ -30,6 +29,7 @@ type ParamsStruct struct {
homeDir string
path string
originalCmd string
kubeconfig string
ClientSet *kubernetes.Clientset
RestConfig *rest.Config
DynamicClient *dynamic.DynamicClient
Expand Down Expand Up @@ -209,8 +209,8 @@ func (p ParamsStruct) aliasRun(args []string) error {
if p.flags["l-debug"] {
log.Println("labeler.go: [debug] before args: ", args)
}
// Run the command with the parsed flags

// Run the command with the parsed flags
if args[0] == "k" || args[0] == "kubectl" {
if p.flags["l-debug"] {
log.Println("labeler.go: [debug] after args: ", args)
Expand All @@ -219,13 +219,12 @@ func (p ParamsStruct) aliasRun(args []string) error {
originalCommand := strings.Join(args, " ")
p.originalCmd = originalCommand

cmd := exec.Command(args[0], args[1:]...)
out, err := cmd.CombinedOutput()
// cmd := exec.Command(args[0], args[1:]...)
out, err := p.runCmd(args[0], args[1:], false)
// out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%v", string(out))
// fmt.Printf("%v", string(out))
os.Exit(1)
} else {
fmt.Printf("%v", string(out))
}

p.ClientSet, p.RestConfig, p.DynamicClient = p.switchContext()
Expand All @@ -235,12 +234,10 @@ func (p ParamsStruct) aliasRun(args []string) error {

} else if args[0] == "helm" {
// run the original helm command without the extra labeler flags
output, err := p.runCmd("helm", args[1:])
_, err := p.runCmd("helm", args[1:], false)
if err != nil {
log.Println(err)
// log.Println(err)
os.Exit(1)
} else {
fmt.Printf("%v", string(output))
}

// now run helm as template and label the output
Expand Down Expand Up @@ -277,6 +274,7 @@ func (p ParamsStruct) aliasRun(args []string) error {
v := strings.Split(vCSV, ",")
if key == v[0] {
if p.pluginPtrs[pkey].IsValid() {
log.Printf("\nlabeler plugin: %q:\n\n", pkey)
p.pluginPtrs[pkey].Call(fnArgs)
}
}
Expand Down

0 comments on commit 1c6acc0

Please sign in to comment.