-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull out command/deviations to separate packages Move prevalidation checks to package cmd Restructure registring flags to sub-command run/all
- Loading branch information
1 parent
ab60f9f
commit 9fc8734
Showing
18 changed files
with
502 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apiVersion: "apiextensions.k8s.io/v1beta1" | ||
kind: "CustomResourceDefinition" | ||
metadata: | ||
name: "projects.example.martin-helmich.de" | ||
spec: | ||
group: "example.martin-helmich.de" | ||
version: "v1alpha1" | ||
scope: "Namespaced" | ||
names: | ||
plural: "projects" | ||
singular: "project" | ||
kind: "Project" | ||
validation: | ||
openAPIV3Schema: | ||
required: ["spec"] | ||
properties: | ||
spec: | ||
required: ["replicas"] | ||
properties: | ||
replicas: | ||
type: "integer" | ||
minimum: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
|
||
"github.com/nikhilsbhat/helm-drift/pkg/deviation" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
HelmContext = "HELM_KUBECONTEXT" | ||
HelmNamespace = "HELM_NAMESPACE" | ||
KubeConfig = "KUBECONFIG" | ||
) | ||
|
||
type Exec interface { | ||
SetKubeCmd(namespace string, args ...string) | ||
RunKubeCmd(deviation deviation.Deviation) (deviation.Deviation, error) | ||
} | ||
|
||
type command struct { | ||
baseCmd *exec.Cmd | ||
log *log.Logger | ||
} | ||
|
||
func NewCommand(cmd string, logger *log.Logger) Exec { | ||
commandClient := command{ | ||
baseCmd: exec.CommandContext(context.Background(), cmd), | ||
log: logger, | ||
} | ||
|
||
return &commandClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/nikhilsbhat/helm-drift/pkg/deviation" | ||
) | ||
|
||
func (cmd *command) RunKubeCmd(deviation deviation.Deviation) (deviation.Deviation, error) { | ||
cmd.log.Debugf("envionment variables that would be used: %v", cmd.baseCmd.Environ()) | ||
|
||
out, err := cmd.baseCmd.CombinedOutput() | ||
if err != nil { | ||
var exerr *exec.ExitError | ||
if errors.As(err, &exerr) { | ||
switch exerr.ExitCode() { | ||
case 1: | ||
deviation.HasDrift = true | ||
deviation.Deviations = string(out) | ||
cmd.log.Debugf("found diffs for '%s' with name '%s'", deviation.Kind, deviation.Kind) | ||
default: | ||
return deviation, fmt.Errorf("running kubectl diff errored with exit code: %w ,with message: %s", err, string(out)) | ||
} | ||
} | ||
} else { | ||
cmd.log.Debugf("no diffs found for '%s' with name '%s'", deviation.Kind, deviation.Kind) | ||
} | ||
|
||
return deviation, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"k8s.io/client-go/util/homedir" | ||
) | ||
|
||
func (cmd *command) SetKubeCmd(namespace string, args ...string) { | ||
cmd.baseCmd.Env = cmd.prepareKubeEnvironments() | ||
cmd.baseCmd.Args = append(cmd.baseCmd.Args, "diff") | ||
cmd.baseCmd.Args = append(cmd.baseCmd.Args, args...) | ||
cmd.baseCmd.Args = append(cmd.baseCmd.Args, cmd.getNamespace(namespace)) | ||
|
||
if len(setContext()) != 0 { | ||
cmd.baseCmd.Args = append(cmd.baseCmd.Args, setContext()) | ||
} | ||
|
||
cmd.log.Debugf("running command '%s' to find diff", cmd.baseCmd.String()) | ||
} | ||
|
||
func (cmd *command) prepareKubeEnvironments() []string { | ||
config := os.Getenv(KubeConfig) | ||
|
||
os.Environ() | ||
var envs []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 | ||
} | ||
|
||
func (cmd *command) getNamespace(nameSpace string) string { | ||
return fmt.Sprintf("-n=%s", nameSpace) | ||
} | ||
|
||
func constructEnv(key, value string) string { | ||
return fmt.Sprintf("%s=%s", key, value) | ||
} | ||
|
||
func setContext() string { | ||
kubeContext := os.Getenv(HelmContext) | ||
if len(kubeContext) != 0 { | ||
return fmt.Sprintf("--context=%s", kubeContext) | ||
} | ||
|
||
return kubeContext | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package deviation | ||
|
||
import ( | ||
"github.com/thoas/go-funk" | ||
) | ||
|
||
const ( | ||
Failed = "FAILED" | ||
Success = "SUCCESS" | ||
) | ||
|
||
type DriftedReleases struct { | ||
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` | ||
Deviations []Deviation `json:"deviations,omitempty" yaml:"deviations,omitempty"` | ||
} | ||
|
||
type Deviation struct { | ||
Deviations string `json:"deviations,omitempty" yaml:"deviations,omitempty"` | ||
HasDrift bool `json:"has_drift,omitempty" yaml:"has_drift,omitempty"` | ||
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` | ||
Resource string `json:"resource,omitempty" yaml:"resource,omitempty"` | ||
TemplatePath string `json:"template_path,omitempty" yaml:"template_path,omitempty"` | ||
ManifestPath string `json:"manifest_path,omitempty" yaml:"manifest_path,omitempty"` | ||
} | ||
|
||
type Deviations []Deviation | ||
|
||
func (dvn *Deviation) Drifted() string { | ||
if dvn.HasDrift { | ||
return "YES" | ||
} | ||
|
||
return "NO" | ||
} | ||
|
||
func (dvn Deviations) GetDriftAsMap(chart, release, time string) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"drifts": dvn, | ||
"total_drifts": dvn.Count(), | ||
"time": time, | ||
"release": release, | ||
"chart": chart, | ||
"status": dvn.Status(), | ||
} | ||
} | ||
|
||
func (dvn Deviations) Status() string { | ||
hasDrift := funk.Contains(dvn, func(dft Deviation) bool { | ||
return dft.HasDrift | ||
}) | ||
|
||
if hasDrift { | ||
return Failed | ||
} | ||
|
||
return Success | ||
} | ||
|
||
func (dvn Deviations) Count() int { | ||
var count int | ||
for _, dft := range dvn { | ||
if dft.HasDrift { | ||
count++ | ||
} | ||
} | ||
|
||
return count | ||
} |
Oops, something went wrong.