Skip to content

Commit

Permalink
Add support for skipping tests for identifying drift
Browse files Browse the repository at this point in the history
Fix kubeCmd
  • Loading branch information
nikhilsbhat committed Mar 24, 2023
1 parent c554f1b commit 1041630
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
5 changes: 3 additions & 2 deletions cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func versionConfig(cmd *cobra.Command, args []string) error {
return nil
}

//nolint:goerr113
func minimumArgError(cmd *cobra.Command, args []string) error {
minArgError := errors.New("[RELEASE] or [CHART] cannot be empty")
oneOfThemError := errors.New("when '--from-release' is enabled, valid input is [RELEASE] and not both [RELEASE] [CHART]")
Expand All @@ -136,14 +137,14 @@ func minimumArgError(cmd *cobra.Command, args []string) error {
if len(args) != getArgumentCountLocal {
log.Println(minArgError)

return minArgError
return fmt.Errorf("%w", minArgError)
}

return nil
}

if len(args) > getArgumentCountRelease {
log.Fatalln(oneOfThemError)
log.Fatalln(fmt.Errorf("%w", oneOfThemError))
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions completion.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: drift
flags:
- log-level
- set
- set-file
- set-string
- values
- log-level
- skip-tests
- values
commands:
- name: run
flags:
Expand Down
14 changes: 6 additions & 8 deletions pkg/diff.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"bytes"
"errors"
"fmt"
"os"
Expand All @@ -24,25 +23,24 @@ func (drift *Drift) Diff() (map[string]string, error) {

drift.log.Debugf("calculating diff for %s", manifestPath)

arguments := []string{"diff", "-f", manifestPath}
arguments := []string{fmt.Sprintf("-f=%s", manifestPath)}
cmd := drift.kubeCmd(arguments...)

drift.log.Debugf("envionment variables that would be used: %v", cmd.Environ())

var output bytes.Buffer
cmd.Stdout = &output
if err = cmd.Run(); err != nil {
out, err := cmd.CombinedOutput()
if err != nil {
var exerr *exec.ExitError
if errors.As(err, &exerr) {
if exerr.ExitCode() != 1 {
return nil, fmt.Errorf("running kubectl diff errored with exit code: %w ,with message: %s", err, output.String())
return nil, fmt.Errorf("running kubectl diff errored with exit code: %w ,with message: %s", err, string(out))
}
}
}

if output.Len() != 0 {
if len(out) != 0 {
drift.log.Debugf("found diffs for %s", manifest.Name())
diffs[manifestPath] = output.String()
diffs[manifestPath] = string(out)
}
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

"k8s.io/client-go/util/homedir"
)

const (
Expand All @@ -16,8 +19,11 @@ const (
)

func (drift *Drift) kubeCmd(args ...string) *exec.Cmd {
cmd := exec.CommandContext(context.Background(), "kubectl", args...)
cmd := exec.CommandContext(context.Background(), "kubectl")
cmd.Args = append(cmd.Args, "diff")
cmd.Args = append(cmd.Args, args...)
cmd.Env = drift.getKubeEnvironments()

drift.log.Debugf("running command '%s' to find diff", cmd.String())

return cmd
Expand All @@ -28,6 +34,7 @@ func (drift *Drift) getKubeEnvironments() []string {
namespace := os.Getenv(helmNamespace)
config := os.Getenv(kubeConfig)

os.Environ()
var envs []string
if len(contexts) != 0 {
envs = append(envs, constructEnv(kubeContext, contexts))
Expand All @@ -37,8 +44,12 @@ func (drift *Drift) getKubeEnvironments() []string {
}
if len(config) != 0 {
envs = append(envs, constructEnv(kubeConfig, config))
} else {
envs = append(envs, constructEnv(kubeConfig, filepath.Join(homedir.HomeDir(), ".kube", "config")))
}

envs = append(envs, os.Environ()...)

return envs
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/from_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (drift *Drift) getChartFromTemplate() ([]byte, error) {
flags = append(flags, "--debug")
}

if drift.SkipTests {
flags = append(flags, "--skip-tests")
}

args := []string{"template", drift.release, drift.chart}
args = append(args, flags...)

Expand Down
1 change: 1 addition & 0 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type NameInterface interface {
Get(dataMap string) (string, error)
}

//nolint:goerr113
func (name *Name) Get(dataMap string) (string, error) {
var kindYaml map[string]interface{}
if err := yaml.Unmarshal([]byte(dataMap), &kindYaml); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/value_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func (v *ValueFiles) String() string {
return fmt.Sprint(*v)
}

//nolint:goerr113
func (v *ValueFiles) Valid() error {
errStr := ""
for _, valuesFile := range *v {
Expand Down

0 comments on commit 1041630

Please sign in to comment.