Skip to content

Commit

Permalink
feat: add support for --context flag
Browse files Browse the repository at this point in the history
- add support for --context flag so that Kubernetes context can be passed as a parameter
  • Loading branch information
prit342 committed Sep 18, 2023
1 parent 6122399 commit 54ae590
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 11 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
serializer *kjson.Serializer
scanTimeOut int
scanURL string
kubeContext string // kubernetes context to use that is present in the kubeconfig
)

func Execute() {
Expand All @@ -33,8 +34,11 @@ func Execute() {
rootCmd.PersistentFlags().IntVarP(&scanTimeOut, "timeout", "t", 120, "Scan timeout in seconds")
rootCmd.PersistentFlags().StringVarP(&scanURL, "url", "u", "https://v2.kubesec.io", "URL to send the request for scanning")
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to kubeconfig, overrides KUBECONFIG environment variable")
rootCmd.PersistentFlags().StringVarP(&kubeContext, "context", "c", "", "kubernetes context to use in kubeconfig")

serializer = kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
serializer = kjson.NewSerializerWithOptions(kjson.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, kjson.SerializerOptions{
Yaml: true,
})

// commands
rootCmd.AddCommand(versionCmd)
Expand All @@ -56,6 +60,7 @@ func newKubeClient(kubeconfig string) (*kubernetes.Clientset, error) {
}

clientset, err := kubernetes.NewForConfig(config)

if err != nil {
return nil, fmt.Errorf("Unable to create a new Clienset: %w", err)
}
Expand All @@ -73,6 +78,11 @@ func newClientConfig(kubeconfig string) (*rest.Config, error) {

configOverrides := &clientcmd.ConfigOverrides{}

// if kubeContext is not empty, it will override the current context
if kubeContext != "" {
configOverrides.CurrentContext = kubeContext
}

kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)

config, err := kubeConfig.ClientConfig()
Expand Down
7 changes: 1 addition & 6 deletions pkg/kubesec/kubesec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
Expand All @@ -29,15 +28,13 @@ func NewClient(url string, timeOutSec int) *KubesecClient {

// ScanDefinition scans the provided resource definition.
func (kc *KubesecClient) ScanDefinition(def bytes.Buffer) (KubeSecResults, error) {

ctx := context.Background()

ctx, cancel := context.WithTimeout(ctx, time.Duration(kc.TimeOutSec)*time.Second)

defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodPost, kc.URL, &def)

if err != nil {
return nil, err
}
Expand All @@ -49,7 +46,6 @@ func (kc *KubesecClient) ScanDefinition(def bytes.Buffer) (KubeSecResults, error
client := &http.Client{}

resp, err := client.Do(req.WithContext(ctx))

if err != nil {
return nil, err
}
Expand All @@ -60,8 +56,7 @@ func (kc *KubesecClient) ScanDefinition(def bytes.Buffer) (KubeSecResults, error
return nil, fmt.Errorf("got %v response from %v instead of 200 OK", resp.StatusCode, kc.URL)
}

body, err := ioutil.ReadAll(resp.Body)

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 54ae590

Please sign in to comment.