Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/kubeapply/subcmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func execDiff(
ctx,
clusterConfig.AbsSubpaths(),
clusterConfig.ServerSideApply,
"",
)
return results, "", err
}
54 changes: 3 additions & 51 deletions data/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion pkg/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ type ClusterClient interface {

// Diff gets the diffs between the configs at the given path and the actual state of resources
// in the cluster.
DiffStructured(ctx context.Context, paths []string, serverSide bool) ([]diff.Result, error)
//
// The diffCommand argument can be set to use a custom diff command in place of the default
// (kubeapply kdiff).
DiffStructured(
ctx context.Context,
paths []string,
serverSide bool,
diffCommand string,
) ([]diff.Result, error)

// Summary returns a summary of all workloads in the cluster.
Summary(ctx context.Context) (string, error)
Expand Down
1 change: 1 addition & 0 deletions pkg/cluster/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (cc *FakeClusterClient) DiffStructured(
ctx context.Context,
paths []string,
serverSide bool,
diffCommand string,
) ([]diff.Result, error) {
return []diff.Result{
{
Expand Down
54 changes: 43 additions & 11 deletions pkg/cluster/kube/ordered_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/briandowns/spinner"
"github.com/segmentio/kubeapply/data"
Expand All @@ -18,8 +19,22 @@ import (
)

const (
structuredDiffScript = "kdiff-wrapper.sh"
rawDiffScript = "raw-diff.sh"
rawDiffScript = `#!/bin/bash

diff -u -N $1 $2

# Ensure that we only exit with non-zero status is there was a real error
if [[ $? -gt 1 ]]; then
exit 1
fi`

structuredDiffScript = `#!/bin/bash

# This is used as the custom differ for kubectl diff. We need a wrapper script instead
# of calling 'kubeapply kdiff' directly because kubectl wants a single executable (without
# any subcommands or arguments).

kubeapply kdiff $1 $2`
)

// TODO: Switch to a YAML library that supports doing this splitting for us.
Expand Down Expand Up @@ -149,10 +164,9 @@ func (k *OrderedClient) Diff(
ctx context.Context,
configPaths []string,
structured bool,
diffCommand string,
spinner *spinner.Spinner,
) ([]byte, error) {
var diffCmd string

tempDir, err := ioutil.TempDir("", "diff")
if err != nil {
return nil, err
Expand Down Expand Up @@ -184,18 +198,36 @@ func (k *OrderedClient) Diff(
}

envVars := []string{}
var diffScript string
var diffScriptBody string

if structured {
diffScript = structuredDiffScript
if diffCommand == "" {
diffScriptBody = structuredDiffScript
} else {
diffScriptBody = strings.Replace(
structuredDiffScript,
"kubeapply kdiff",
diffCommand,
-1,
)
}
} else {
diffScript = rawDiffScript
if diffCommand == "" {
diffScriptBody = rawDiffScript
} else {
diffScriptBody = strings.Replace(
rawDiffScript,
"diff",
diffCommand,
-1,
)
}
}

diffCmd = filepath.Join(tempDir, diffScript)
kubectlDiffCmd := filepath.Join(tempDir, "diff.sh")
err = ioutil.WriteFile(
diffCmd,
data.MustAsset(fmt.Sprintf("scripts/%s", diffScript)),
kubectlDiffCmd,
[]byte(diffScriptBody),
0755,
)
if err != nil {
Expand All @@ -204,7 +236,7 @@ func (k *OrderedClient) Diff(

envVars = append(
envVars,
fmt.Sprintf("KUBECTL_EXTERNAL_DIFF=%s", diffCmd),
fmt.Sprintf("KUBECTL_EXTERNAL_DIFF=%s", kubectlDiffCmd),
)

return runKubectlOutput(
Expand Down
7 changes: 5 additions & 2 deletions pkg/cluster/kube_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (cc *KubeClusterClient) Diff(
paths []string,
serverSide bool,
) ([]byte, error) {
rawResults, err := cc.execDiff(ctx, paths, false)
rawResults, err := cc.execDiff(ctx, paths, false, "")
if err != nil {
return nil, fmt.Errorf(
"Error running diff: %+v (output: %s)",
Expand All @@ -229,8 +229,9 @@ func (cc *KubeClusterClient) DiffStructured(
ctx context.Context,
paths []string,
serverSide bool,
diffCommand string,
) ([]diff.Result, error) {
rawResults, err := cc.execDiff(ctx, paths, true)
rawResults, err := cc.execDiff(ctx, paths, true, diffCommand)
if err != nil {
return nil, fmt.Errorf(
"Error running diff: %+v (output: %s)",
Expand Down Expand Up @@ -351,6 +352,7 @@ func (cc *KubeClusterClient) execDiff(
ctx context.Context,
paths []string,
structured bool,
diffCommand string,
) ([]byte, error) {
if cc.useLocks {
acquireCtx, cancel := context.WithTimeout(ctx, lockAcquistionTimeout)
Expand Down Expand Up @@ -378,6 +380,7 @@ func (cc *KubeClusterClient) execDiff(
ctx,
paths,
structured,
diffCommand,
cc.spinnerObj,
)
if err != nil || !cc.checkApplyConsistency {
Expand Down
1 change: 1 addition & 0 deletions pkg/events/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ func (whh *WebhookHandler) runDiffs(
diffCtx,
clusterClient.Config().AbsSubpaths(),
clusterClient.Config().ServerSideApply,
"",
)
if err != nil {
diffErr = fmt.Errorf(
Expand Down
7 changes: 0 additions & 7 deletions scripts/kdiff-wrapper.sh

This file was deleted.

8 changes: 0 additions & 8 deletions scripts/raw-diff.sh

This file was deleted.