Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
add kubectl command
Browse files Browse the repository at this point in the history
  • Loading branch information
cr1cr1 committed Oct 13, 2023
1 parent 48a2851 commit 3690c89
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/clusterKubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewClusterKubeconfig(parent *Cluster) *ClusterKubeconfig {
Short: "Fetch cluster kubeconfig",
Long: ``,
RunE: ck.RunClusterKubeconfigCommand,
Aliases: []string{"k"},
Aliases: []string{"kc"},
SilenceErrors: parent.Cmd().SilenceErrors,
SilenceUsage: parent.Cmd().SilenceUsage,
}
Expand Down
79 changes: 79 additions & 0 deletions cmd/clusterKubectl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright © 2023 Dataflows
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/thedataflows/go-commons/pkg/config"
)

type ClusterKubectl struct {
cmd *cobra.Command
parent *Cluster
}

var (
_ = NewClusterKubectl(mycluster)
)

func init() {

}

func NewClusterKubectl(parent *Cluster) *ClusterKubectl {
ck := &ClusterKubectl{
parent: parent,
}

ck.cmd = &cobra.Command{
Use: "kubectl",
Short: "Execute kubectl with a specified context",
Long: ``,
RunE: ck.RunClusterKubectlCommand,
Aliases: []string{"k"},
SilenceErrors: parent.Cmd().SilenceErrors,
SilenceUsage: parent.Cmd().SilenceUsage,
}

parent.Cmd().AddCommand(ck.cmd)

// Bind flags to config
config.ViperBindPFlagSet(ck.cmd, nil)

return ck
}

func (c *ClusterKubectl) RunClusterKubectlCommand(cmd *cobra.Command, args []string) error {
if err := c.CheckRequiredFlags(); err != nil {
return err
}

config.ViperSet(raw.Cmd(), c.parent.KeyTimeout(), c.parent.Timeout())
out, err := raw.RunRawCommandCaptureStdout(
raw.Cmd(),
append(
[]string{
"kubectl",
"--context",
c.parent.ClusterContext(),
},
args...),
)
if err != nil {
if len(out) == 0 {
return err
}
return fmt.Errorf("%v\n%s", err, out)
}

fmt.Println(out)

return nil
}

func (c *ClusterKubectl) CheckRequiredFlags() error {
return c.parent.CheckRequiredFlags()
}
2 changes: 1 addition & 1 deletion sample/myconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
log-level: info
log-level: debug
flux:
context: &context mycontext
bootstrap:
Expand Down

0 comments on commit 3690c89

Please sign in to comment.