Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement native kube cli #34

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .slsa-goreleaser/darwin-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ ldflags:
- "-X main.Commit={{ .Env.COMMIT }}"
- "-X main.CommitDate={{ .Env.COMMIT_DATE }}"
- "-X main.TreeState={{ .Env.TREE_STATE }}"
- "-X github.com/xentra-ai/advisor/pkg/k8s.Version=${VERSION}"
1 change: 1 addition & 0 deletions .slsa-goreleaser/darwin-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ ldflags:
- "-X main.Commit={{ .Env.COMMIT }}"
- "-X main.CommitDate={{ .Env.COMMIT_DATE }}"
- "-X main.TreeState={{ .Env.TREE_STATE }}"
- "-X github.com/xentra-ai/advisor/pkg/k8s.Version=${VERSION}"
1 change: 1 addition & 0 deletions .slsa-goreleaser/linux-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ ldflags:
- "-X main.Commit={{ .Env.COMMIT }}"
- "-X main.CommitDate={{ .Env.COMMIT_DATE }}"
- "-X main.TreeState={{ .Env.TREE_STATE }}"
- "-X github.com/xentra-ai/advisor/pkg/k8s.Version=${VERSION}"
1 change: 1 addition & 0 deletions .slsa-goreleaser/linux-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ ldflags:
- "-X main.Commit={{ .Env.COMMIT }}"
- "-X main.CommitDate={{ .Env.COMMIT_DATE }}"
- "-X main.TreeState={{ .Env.TREE_STATE }}"
- "-X github.com/xentra-ai/advisor/pkg/k8s.Version=${VERSION}"
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ RUN go mod download
COPY . .

RUN go build -a -installsuffix cgo \
-ldflags="-w -extldflags '-static' -X 'main.ApplicationName=${APPLICATION_NAME}}' -X 'main.Version=${VERSION}' -X 'main.SHA=${SHA}'" \
-ldflags="-w -extldflags '-static' \
-X 'main.ApplicationName=${APPLICATION_NAME}}' \
-X 'main.Version=${VERSION}' -X 'main.SHA=${SHA}' \
-X 'github.com/xentra-ai/advisor/pkg/k8s.Version=${VERSION}'" \
-o advisor .

FROM gcr.io/distroless/static:nonroot
Expand Down
25 changes: 4 additions & 21 deletions advisor/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ var networkPolicyCmd = &cobra.Command{
Short: "Generate network policy",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

kubeconfig, _ := cmd.Flags().GetString("kubeconfig")
namespace, _ := cmd.Flags().GetString("namespace")

config, err := k8s.NewConfig(kubeconfig, namespace)
if err != nil {
log.Fatal().Err(err).Msg("Error initializing Kubernetes client")
// Retrieve the config from the command context
config, ok := cmd.Context().Value(k8s.ConfigKey).(*k8s.Config)
if !ok {
log.Fatal().Msg("Failed to retrieve Kubernetes configuration")
}

log.Info().Msgf("Using kubeconfig file: %s", config.Kubeconfig)
log.Info().Msgf("Using namespace: %s", config.Namespace)
podName := args[0]

stopChan, errChan, done := k8s.PortForward(config)
Expand All @@ -42,14 +36,3 @@ var networkPolicyCmd = &cobra.Command{
close(stopChan)
},
}

var seccompCmd = &cobra.Command{
Use: "seccomp [pod-name]",
Aliases: []string{"sc"},
Short: "Generate seccomp profile",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
podName := args[0]
log.Info().Msgf("Generating seccomp profile for pod: %s", podName)
},
}
41 changes: 36 additions & 5 deletions advisor/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package cmd

import (
"context"
"os"

"github.com/rs/zerolog"
log "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/xentra-ai/advisor/pkg/k8s"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var debug bool // To store the value of the --debug flag
var (
kubeConfigFlags *genericclioptions.ConfigFlags
debug bool // To store the value of the --debug flag
)

func init() {
// Set up logging to console
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel)

// Add your sub-commands
genCmd.AddCommand(networkPolicyCmd, seccompCmd)
genCmd.AddCommand(networkPolicyCmd)

// Initialize kubeConfigFlags
kubeConfigFlags = genericclioptions.NewConfigFlags(true)

// Add flags
genCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
genCmd.PersistentFlags().String("namespace", "", "If present, the namespace scope for this CLI request")
// Add global flags from kubeConfigFlags to rootCmd
kubeConfigFlags.AddFlags(rootCmd.PersistentFlags())

// Add debug flag to rootCmd so it's available for all sub-commands
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "sets log level to debug")
Expand All @@ -45,6 +53,29 @@ var rootCmd = &cobra.Command{
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

// Initialize Kubernetes config and logging
config, err := k8s.NewConfig(kubeConfigFlags)
if err != nil {
log.Fatal().Err(err).Msg("Error initializing Kubernetes client")
}

kubeconfigPath := kubeConfigFlags.ToRawKubeConfigLoader().ConfigAccess().GetDefaultFilename()
if err != nil {
log.Fatal().Err(err).Msg("Error initializing Kubernetes client")
}

namespace, _, err := kubeConfigFlags.ToRawKubeConfigLoader().Namespace()
if err != nil {
log.Fatal().Err(err).Msg("Failed to get namespace")
}

log.Info().Msgf("Using kubeconfig file: %s", kubeconfigPath)
log.Info().Msgf("Using namespace: %s", namespace)

// Create a new context with the config and assign it to the command
ctx := context.WithValue(cmd.Context(), k8s.ConfigKey, config)
cmd.SetContext(ctx)
},
}

Expand Down
15 changes: 15 additions & 0 deletions advisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,51 @@ require (
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.3
k8s.io/apimachinery v0.28.3
k8s.io/cli-runtime v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand All @@ -53,5 +66,7 @@ require (
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading