Skip to content

Commit

Permalink
Merge "Use the default namespace of the current context"
Browse files Browse the repository at this point in the history
  • Loading branch information
Microzuul CI authored and Gerrit Code Review committed Feb 12, 2025
2 parents cc955f7 + 2c8368e commit 71529dd
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions cli/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ import (
ctrlutils "github.com/softwarefactory-project/sf-operator/controllers/libs/utils"

"k8s.io/client-go/kubernetes"

"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// CLI config struct
Expand Down Expand Up @@ -159,14 +162,38 @@ func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error)
}
// Override with defaults
// We don't set a default namespace here so as not to interfere with rootcommand.

ns, _ := command.Flags().GetString("namespace")

kubeContext, _ := command.Flags().GetString("kube-context")
currentContext, contextName := GetKubeConfigContextByName(kubeContext)

defaultFunc := func(userProvided string, defaultValue string) string {
if userProvided != "" {
return userProvided
}
return defaultValue
}

// Default ladder: 1st what is in sf-operator cli config file passed as --config argument
// 2st what is in sf-operator cli passed as --namespace argumente
// 3rd what is defind default kubeconfig
if cliContext.Namespace == "" {
cliContext.Namespace = ns
// The user did not provide a --namespace argument, let's find it in the context
currentContextNamespace := ""
if currentContext != nil {
currentContextNamespace = currentContext.Namespace
}
cliContext.Namespace = defaultFunc(ns, currentContextNamespace)
}
kubeContext, _ := command.Flags().GetString("kube-context")

// Default ladder: 1st what is in sf-operator cli config file passed as --config argument
// 2st what is in sf-operator cli passed as --kube-context argumente
// 3rd what is defind default kubeconfig
if cliContext.KubeContext == "" {
cliContext.KubeContext = kubeContext
cliContext.KubeContext = defaultFunc(kubeContext, contextName)
}

fqdn, _ := command.Flags().GetString("fqdn")
if fqdn == "" {
fqdn = "sfop.me"
Expand Down Expand Up @@ -511,3 +538,35 @@ func ExecuteKubectlClient(ns string, podName string, containerName string, execu
}

}

func GetKubeConfig() *clientcmdapi.Config {
clientCfg, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
ctrlutils.LogE(err, "Could not find the kubeconfig")
os.Exit(1)
}
return clientCfg
}

func GetKubeConfigContextByName(contextName string) (*clientcmdapi.Context, string) {
clientCfg := GetKubeConfig()

// The user did not specify a context, let's pick the current one from the kubeconfig
if contextName == "" {
if clientCfg.CurrentContext == "" {
// Use the first available context
for k := range clientCfg.Contexts {
contextName = k
break
}
} else {
contextName = clientCfg.CurrentContext
}
}
// Load the context
context, err := clientCfg.Contexts[contextName]
if !err {
ctrlutils.LogD("could not find the context " + contextName)
}
return context, contextName
}

0 comments on commit 71529dd

Please sign in to comment.