Skip to content

Commit

Permalink
commands/.../up/local: fix kubeconfig and namespace handling (operato…
Browse files Browse the repository at this point in the history
…r-framework#983) (operator-framework#996)

**Description of the change:** Cherry pick commit from operator-framework#983


**Motivation for the change:** It is a bug fix that applies to `v0.4.0`
  • Loading branch information
AlexNPavel authored and Shawn Hurley committed Mar 8, 2019
1 parent a827ea2 commit 1d7a5c1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## Unreleased

### Added

### Changed

### Deprecated

### Removed

### Bug Fixes

- Make `up local` subcommand respect `KUBECONFIG` env var ([#996](https://github.com/operator-framework/operator-sdk/pull/996))
- 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 ([#996](https://github.com/operator-framework/operator-sdk/pull/996))

## v0.4.0

### Added
Expand Down
64 changes: 30 additions & 34 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 @@ -50,9 +50,9 @@ 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 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 @@ -72,15 +72,19 @@ var (
helmOperatorFlags *hoflags.HelmOperatorFlags
)

const (
defaultConfigPath = ".kube/config"
)

func upLocalFunc(cmd *cobra.Command, args []string) {
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 {
log.Fatalf("Failed to get kubeconfig and default namespace: %v", err)
}
namespace = defaultNamespace
}
log.Infof("Using namespace %s.", namespace)

switch projutil.GetOperatorType() {
case projutil.OperatorTypeGo:
projutil.MustInProjectRoot()
Expand All @@ -94,23 +98,6 @@ func upLocalFunc(cmd *cobra.Command, args []string) {
}
}

// mustKubeConfig checks if the kubeconfig file exists.
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() {
args := []string{"run"}
if ldFlags != "" {
Expand All @@ -134,7 +121,11 @@ func upLocal() {
}()
dc.Stdout = os.Stdout
dc.Stderr = os.Stderr
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig))
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))
err := dc.Run()
if err != nil {
Expand All @@ -144,10 +135,13 @@ func upLocal() {

func upLocalAnsible() {
// Set the kubeconfig that the manager will be able to grab
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
log.Fatalf("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 {
log.Fatalf("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 {
log.Fatalf("Failed to set %s environment variable: (%v)", k8sutil.WatchNamespaceEnvVar, err)
Expand All @@ -159,11 +153,13 @@ func upLocalAnsible() {

func upLocalHelm() {
// Set the kubeconfig that the manager will be able to grab
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
log.Fatalf("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 {
log.Fatalf("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 {
log.Fatalf("Failed to set %s environment variable: (%v)", k8sutil.WatchNamespaceEnvVar, err)
Expand Down

0 comments on commit 1d7a5c1

Please sign in to comment.