Skip to content

Adds ability to use kubeconfig context #351

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

Closed
Closed
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
9 changes: 7 additions & 2 deletions commands/operator-sdk/cmd/up/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ kubernetes cluster using a kubeconfig file.
Run: upLocalFunc,
}

upLocalCmd.Flags().StringVar(&kubeConfig, "kubeconfig", "", "The file path to kubernetes configuration file; defaults to $HOME/.kube/config")
upLocalCmd.Flags().StringVar(&kubeConfig, "kubeconfig", "", "The file path to Kubernetes configuration file; defaults to $HOME/.kube/config")
upLocalCmd.Flags().StringVar(&kubeContext, "context", "", "The kubeconfig context to use")
upLocalCmd.Flags().StringVar(&operatorFlags, "operator-flags", "", "The flags that the operator needs. Example: \"--flag1 value1 --flag2=value2\"")
upLocalCmd.Flags().StringVar(&namespace, "namespace", "default", "The namespace where the operator watches for changes.")

Expand All @@ -35,6 +36,7 @@ kubernetes cluster using a kubeconfig file.

var (
kubeConfig string
kubeContext string
operatorFlags string
namespace string
)
Expand Down Expand Up @@ -80,7 +82,10 @@ func upLocal(projectName string) {
dc := exec.Command(gocmd, args...)
dc.Stdout = os.Stdout
dc.Stderr = os.Stderr
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig), fmt.Sprintf("%v=%v", k8sutil.WatchNamespaceEnvVar, namespace))
dc.Env = append(os.Environ(),
fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig),
fmt.Sprintf("%v=%v", k8sutil.KubeContextEnvVar, kubeContext),
fmt.Sprintf("%v=%v", k8sutil.WatchNamespaceEnvVar, namespace))
err := dc.Run()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to run operator locally: %v", err))
Expand Down
16 changes: 14 additions & 2 deletions pkg/k8sclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
"github.com/sirupsen/logrus"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -133,7 +134,10 @@ func mustNewKubeClientAndConfig() (kubernetes.Interface, *rest.Config) {
cfg, err = inClusterConfig()
}
if err != nil {
panic(err)
logrus.Fatalf("failed to get a valid kubeconfig for the provided parameters, %v", err)
}
if cfg != nil {
logrus.Infof("API server URL: %v", cfg.Host)
}
return kubernetes.NewForConfigOrDie(cfg), cfg
}
Expand All @@ -157,10 +161,18 @@ func inClusterConfig() (*rest.Config, error) {

func outOfClusterConfig() (*rest.Config, error) {
kubeconfig := os.Getenv(k8sutil.KubeConfigEnvVar)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
kubecontext := os.Getenv(k8sutil.KubeContextEnvVar)
config, err := buildConfigWithContext(kubeconfig, kubecontext)
return config, err
}

// see clientcmd.BuildConfigFromFlags()
func buildConfigWithContext(kubeconfigPath string, context string) (*rest.Config, error) {
Copy link
Author

@pawelprazak pawelprazak Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: this is the most important change that allows me to implement what I need, the rest is just supporting it

Copy link
Author

@pawelprazak pawelprazak Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the looks of it, decorating GetConfig function https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/config/config.go#L59 should give me the same result (after controller-runtime refactoring is merged)

Copy link

@juanvallejo juanvallejo Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, the first link gives me 404

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configRules := clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}
overrides := clientcmd.ConfigOverrides{CurrentContext: context}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&configRules, &overrides).ClientConfig()
}

// runBackgroundCacheReset - Starts the rest mapper cache reseting
// at a duration given.
func (c *resourceClientFactory) runBackgroundCacheReset(duration time.Duration) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/k8sutil/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const (
// contains the kubeconfig file path.
KubeConfigEnvVar = "KUBERNETES_CONFIG"

// KubeContextEnvVar defines the env variable KUBERNETES_CONTEXT which
// contains the kubeconfig context.
KubeContextEnvVar = "KUBERNETES_CONTEXT"

// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which is the namespace that the pod is currently running in.
WatchNamespaceEnvVar = "WATCH_NAMESPACE"
Expand Down