Skip to content

Commit

Permalink
add context switching (#44)
Browse files Browse the repository at this point in the history
* rename context flag to align with kubectl

* rename context flag to align with kubectl, add an example for the context flag

* allow the user to provide a specific context to use

* update changelog
  • Loading branch information
glitchcrab authored Aug 28, 2021
1 parent 53339a9 commit 5708daf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Allow setting the cluster context via the --context flag.

## [0.5.0] - 2021-08-15

### Added
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ INFO[0000] resources deleted: deployment, podsecuritypolicy, clusterrolebinding,
Absolute path to the kubeconfig file to use.
--kube-context (default: current context in kube config)
--context (default: current context in kube config)
Name of the kube context to use to use. NOTE: not yet implemented.
Name of the kube context to use.
--name (default: 'debug')
Expand Down Expand Up @@ -118,6 +118,12 @@ sonar create --networkpolicy

- also creates a NetworkPolicy which allows all ingress and traffic to the Sonar pod.

```
sonar create --context foo-context --namespace bar
```

- create a deployment using context `foo-context` in namespace `bar`.

### Delete

#### Options
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ Global flags:
Absolute path to the kubeconfig file to use.
--kube-context (default: current context in kube config)
--context (default: current context in kube config)
Name of the kube context to use to use. NOTE: not yet implemented.
Name of the kubernetes context to use.
--name (default: 'debug')
Expand All @@ -83,7 +83,7 @@ func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&kubeConfig, "kube-config", "", "absolute path to kubeconfig file (default: '$HOME/.kube/config')")
rootCmd.PersistentFlags().StringVar(&kubeContext, "kube-context", "", "cluster context to use")
rootCmd.PersistentFlags().StringVar(&kubeContext, "context", "", "context to use")
rootCmd.PersistentFlags().StringVarP(&name, "name", "N", "debug", "resource name (max 50 characters) (automatically prepended with 'sonar-'")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "namespace to operate in")
}
Expand Down
16 changes: 12 additions & 4 deletions service/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,29 @@ import (
)

func New(kubeContext, kubeConfig string) (*kubernetes.Clientset, error) {
// Set the kubeconfig to the default location if the path
// wasn't provided.
// Set the kubeconfig to the default location if the path wasn't provided.
if kubeConfig == "" {
if home := homedir.HomeDir(); home != "" {
kubeConfig = filepath.Join(home, ".kube", "config")
}
}

// TODO: implement switching to provided context name
// Set defaults for creating a new ClientConfig.
loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig}
configOverrides := &clientcmd.ConfigOverrides{}

config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
// Set the context if it was provided.
if kubeContext != "" {
configOverrides = &clientcmd.ConfigOverrides{CurrentContext: kubeContext}
}

// Create the ClientConfig.
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides).ClientConfig()
if err != nil {
log.Fatal(err)
}

// Create a Clientset with the provided values.
k8sClientSet, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 5708daf

Please sign in to comment.