Skip to content

Commit

Permalink
Fail if multiple kubeconfig files are set.
Browse files Browse the repository at this point in the history
The environment variable 'KUBECONFIG' supports declaring multiple kubeconfig files but not the flag '--kubeconfig'.
Marking the plugin validation as failure due to the above limitation since plugins rely on falg '--kubeconfig' over 'KUBECONFIG'
  • Loading branch information
nikhilsbhat committed Mar 9, 2024
1 parent 95eeaac commit 06032ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getRunCommand() *cobra.Command {

if !drifts.SkipValidation {
if !drifts.ValidatePrerequisite() {
return &errors.PreValidationError{Message: "validation failed, please install prerequisites to identify drifts"}
return &errors.PreValidationError{Message: "validation failed, please address the prerequisite errors to identify drifts"}
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ Do note that this is expensive operation since multiple kubectl command would be

if !drifts.SkipValidation {
if !drifts.ValidatePrerequisite() {
return &errors.PreValidationError{Message: "validation failed, please install prerequisites to identify drifts"}
return &errors.PreValidationError{Message: "validation failed, please address the prerequisite errors to identify drifts"}
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/from_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/release"

// Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
Expand All @@ -24,9 +25,8 @@ func (drift *Drift) getChartFromRelease() ([]byte, error) {
settings.KubeContext = kubeContext
}

kubeConfig := drift.kubeConfig //nolint:ifshort
if len(kubeConfig) != 0 {
settings.KubeConfig = kubeConfig
if len(drift.kubeConfig) != 0 {
settings.KubeConfig = drift.kubeConfig
}

if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions pkg/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package pkg

import (
"errors"
"fmt"
"os/exec"
"strings"
)

const (
docLink = "https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" +
"#append-home-kube-config-to-your-kubeconfig-environment-variable"
)

func (drift *Drift) ValidatePrerequisite() bool {
success := true

if len(strings.Split(drift.kubeConfig, ":")) > 1 {
drift.log.Info("since drift uses kubectl underneath with arg '--kubeconfig' " +
"drift cannot run with multiple kubeconfig files set under environment var 'KUBECONFIG', " +
"like mentioned in below document")
fmt.Printf("%s\n", docLink)

success = false
}

if goPath := exec.Command("kubectl"); goPath.Err != nil {
if !errors.Is(goPath.Err, exec.ErrDot) {
drift.log.Infof("%v", goPath.Err.Error())
Expand Down

0 comments on commit 06032ff

Please sign in to comment.