Skip to content

Commit

Permalink
commands/.../up/local: fix kubeconfig and namespace handling (#983)
Browse files Browse the repository at this point in the history
* commands/.../up/local: fix kubeconfig and namespace handling
  • Loading branch information
AlexNPavel authored Jan 24, 2019
1 parent d39ab6c commit 770a5b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

### Bug Fixes

- Make `up local` subcommand respect `KUBECONFIG` env var ([#983](https://github.com/operator-framework/operator-sdk/pull/983))
- Make `up local` subcommand use default namespace set in kubeconfig instead of hardcoded `default` and also add ability to watch all namespaces for ansible and helm type operators ([#983](https://github.com/operator-framework/operator-sdk/pull/983))

## v0.4.0

### Added
Expand Down
67 changes: 32 additions & 35 deletions commands/operator-sdk/cmd/up/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"strings"
"syscall"

k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/ansible"
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"
Expand All @@ -51,9 +51,9 @@ kubernetes cluster using a kubeconfig file.
RunE: 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 location specified by $KUBECONFIG with a fallback to $HOME/.kube/config if not set")
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.")
upLocalCmd.Flags().StringVar(&namespace, "namespace", "", "The namespace where the operator watches for changes.")
upLocalCmd.Flags().StringVar(&ldFlags, "go-ldflags", "", "Set Go linker options")
switch projutil.GetOperatorType() {
case projutil.OperatorTypeAnsible:
Expand All @@ -73,15 +73,19 @@ var (
helmOperatorFlags *hoflags.HelmOperatorFlags
)

const (
defaultConfigPath = ".kube/config"
)

func upLocalFunc(cmd *cobra.Command, args []string) error {
mustKubeConfig()

log.Info("Running the operator locally.")

// get default namespace to watch if unset
if !cmd.Flags().Changed("namespace") {
_, defaultNamespace, err := k8sInternal.GetKubeconfigAndNamespace(kubeConfig)
if err != nil {
return fmt.Errorf("failed to get kubeconfig and default namespace: %v", err)
}
namespace = defaultNamespace
}
log.Infof("Using namespace %s.", namespace)

t := projutil.GetOperatorType()
switch t {
case projutil.OperatorTypeGo:
Expand All @@ -95,24 +99,6 @@ func upLocalFunc(cmd *cobra.Command, args []string) error {
return fmt.Errorf("unknown operator type '%v'", t)
}

// mustKubeConfig exits if the kubeconfig file does not exist.
func mustKubeConfig() {
// If kubeConfig is not specified, search for the default kubeconfig file
// under the $HOME/.kube/config.
if len(kubeConfig) == 0 {
usr, err := user.Current()
if err != nil {
log.Fatalf("Failed to determine user's home dir: (%v)", err)
}
kubeConfig = filepath.Join(usr.HomeDir, defaultConfigPath)
}

_, err := os.Stat(kubeConfig)
if err != nil && os.IsNotExist(err) {
log.Fatalf("Failed to find the kubeconfig file (%v): (%v)", kubeConfig, err)
}
}

func upLocal() error {
args := []string{"run"}
if ldFlags != "" {
Expand All @@ -134,7 +120,13 @@ func upLocal() error {
}
os.Exit(0)
}()
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig))
dc.Stdout = os.Stdout
dc.Stderr = os.Stderr
dc.Env = os.Environ()
// only set env var if user explicitly specified a kubeconfig path
if kubeConfig != "" {
dc.Env = append(dc.Env, fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig))
}
dc.Env = append(dc.Env, fmt.Sprintf("%v=%v", k8sutil.WatchNamespaceEnvVar, namespace))
if err := projutil.ExecCmd(dc); err != nil {
return fmt.Errorf("failed to run operator locally: (%v)", err)
Expand All @@ -146,10 +138,13 @@ func upLocalAnsible() error {
logf.SetLogger(logf.ZapLogger(false))

// Set the kubeconfig that the manager will be able to grab
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
return fmt.Errorf("failed to set %s environment variable: (%v)", k8sutil.KubeConfigEnvVar, err)
// only set env var if user explicitly specified a kubeconfig path
if kubeConfig != "" {
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
return fmt.Errorf("failed to set %s environment variable: (%v)", k8sutil.KubeConfigEnvVar, err)
}
}
// Set the kubeconfig that the manager will be able to grab
// Set the namespace that the manager will be able to grab
if namespace != "" {
if err := os.Setenv(k8sutil.WatchNamespaceEnvVar, namespace); err != nil {
return fmt.Errorf("failed to set %s environment variable: (%v)", k8sutil.WatchNamespaceEnvVar, err)
Expand All @@ -163,11 +158,13 @@ func upLocalHelm() error {
logf.SetLogger(logf.ZapLogger(false))

// Set the kubeconfig that the manager will be able to grab
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
return fmt.Errorf("failed to set %s environment variable: (%v)", k8sutil.KubeConfigEnvVar, err)
// only set env var if user explicitly specified a kubeconfig path
if kubeConfig != "" {
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
return fmt.Errorf("failed to set %s environment variable: (%v)", k8sutil.KubeConfigEnvVar, err)
}
}

// Set the kubeconfig that the manager will be able to grab
// Set the namespace that the manager will be able to grab
if namespace != "" {
if err := os.Setenv(k8sutil.WatchNamespaceEnvVar, namespace); err != nil {
return fmt.Errorf("failed to set %s environment variable: (%v)", k8sutil.WatchNamespaceEnvVar, err)
Expand Down

0 comments on commit 770a5b7

Please sign in to comment.