diff --git a/cmd/kubeapply/subcmd/diff.go b/cmd/kubeapply/subcmd/diff.go index 6d976d7..d7ab800 100644 --- a/cmd/kubeapply/subcmd/diff.go +++ b/cmd/kubeapply/subcmd/diff.go @@ -223,6 +223,7 @@ func execDiff( ctx, clusterConfig.AbsSubpaths(), clusterConfig.ServerSideApply, + "", ) return results, "", err } diff --git a/data/data.go b/data/data.go index c22b5ff..7a02c74 100644 --- a/data/data.go +++ b/data/data.go @@ -9,10 +9,8 @@ // scripts/cluster-summary/cluster_summary.py (4.488kB) // scripts/cluster-summary/tabulate.py (57.091kB) // scripts/create-lambda-bundle.sh (791B) -// scripts/kdiff-wrapper.sh (247B) // scripts/kindctl.sh (1.76kB) // scripts/pull-deps.sh (1.797kB) -// scripts/raw-diff.sh (143B) package data @@ -196,7 +194,7 @@ func scriptsClusterSummary__init__Py() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "scripts/cluster-summary/__init__.py", size: 0, mode: os.FileMode(0644), modTime: time.Unix(1586803753, 0)} + info := bindataFileInfo{name: "scripts/cluster-summary/__init__.py", size: 0, mode: os.FileMode(0644), modTime: time.Unix(1620428515, 0)} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}} return a, nil } @@ -216,7 +214,7 @@ func scriptsClusterSummaryCluster_summaryPy() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "scripts/cluster-summary/cluster_summary.py", size: 4488, mode: os.FileMode(0755), modTime: time.Unix(1593205176, 0)} + info := bindataFileInfo{name: "scripts/cluster-summary/cluster_summary.py", size: 4488, mode: os.FileMode(0755), modTime: time.Unix(1620428515, 0)} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xb2, 0xfd, 0xc1, 0xd0, 0x9d, 0x3e, 0x14, 0x51, 0x1a, 0x84, 0x85, 0x49, 0x4a, 0x63, 0x5c, 0x4d, 0x3a, 0x6c, 0x28, 0x48, 0x9, 0xd0, 0x64, 0x2f, 0x7b, 0x5b, 0xa8, 0xe8, 0x1, 0xa, 0xa9, 0x99}} return a, nil } @@ -236,7 +234,7 @@ func scriptsClusterSummaryTabulatePy() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "scripts/cluster-summary/tabulate.py", size: 57091, mode: os.FileMode(0644), modTime: time.Unix(1586803753, 0)} + info := bindataFileInfo{name: "scripts/cluster-summary/tabulate.py", size: 57091, mode: os.FileMode(0644), modTime: time.Unix(1620428515, 0)} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe9, 0x68, 0x8, 0x2c, 0x92, 0x99, 0x98, 0x23, 0x29, 0xba, 0x60, 0xc3, 0xa4, 0x16, 0x4, 0xe3, 0x2, 0xdd, 0x43, 0xdd, 0x2d, 0x2d, 0x51, 0x1e, 0xa9, 0x2, 0xe9, 0x71, 0x54, 0xef, 0x3c, 0x1c}} return a, nil } @@ -261,26 +259,6 @@ func scriptsCreateLambdaBundleSh() (*asset, error) { return a, nil } -var _scriptsKdiffWrapperSh = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x5c\x8d\x31\x4e\xc4\x30\x10\x45\x7b\x9f\xe2\xa3\xac\xb4\xd0\xec\x0a\xce\x82\x44\x3d\xb6\x27\xc9\x28\xce\xd8\xf2\x8c\x15\x72\x7b\x14\x0a\x0a\xda\xaf\xff\xde\x9b\x5e\x9e\x51\xf4\x19\xc9\xd6\x10\x26\x7c\xae\x62\x10\xc3\x30\xce\x20\x83\xaf\x8c\x34\xcc\xeb\x8e\x2c\xf3\xcc\x1d\x73\xed\xd8\x46\xe4\xe4\xe5\x77\x7a\xe0\x8b\xa1\x7c\xdd\x71\x74\x6a\x8d\x3b\x2c\x75\x69\x0e\x51\x73\xa6\x1c\x26\xd4\x19\x89\x4a\x11\x5d\x70\xbf\x60\x6a\xad\x9c\xd8\x2e\xfe\x8e\x2c\xfd\xb2\x9d\x88\x9c\x68\x18\xff\xe9\x0f\x52\x37\x10\x4c\x74\x29\x0c\xfe\xe6\x34\x9c\x62\x61\xbc\x1e\xe2\x6b\x1d\x1e\x26\x90\x9e\xb0\x11\x53\xdd\x77\xd2\x6c\xa8\x1d\xd4\x97\xb1\xb3\xba\xbd\x3d\x42\xf8\x97\xc3\xed\x1d\xb7\x8f\xf0\x13\x00\x00\xff\xff\x1d\xf0\xaa\x59\xf7\x00\x00\x00") - -func scriptsKdiffWrapperShBytes() ([]byte, error) { - return bindataRead( - _scriptsKdiffWrapperSh, - "scripts/kdiff-wrapper.sh", - ) -} - -func scriptsKdiffWrapperSh() (*asset, error) { - bytes, err := scriptsKdiffWrapperShBytes() - if err != nil { - return nil, err - } - - info := bindataFileInfo{name: "scripts/kdiff-wrapper.sh", size: 247, mode: os.FileMode(0755), modTime: time.Unix(1611616180, 0)} - a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1c, 0x65, 0xd, 0x2, 0xe5, 0x67, 0x2e, 0xc4, 0x79, 0xc0, 0x40, 0x6, 0x5c, 0x67, 0x6b, 0xbf, 0x64, 0x8, 0x19, 0x33, 0x6c, 0xa0, 0x14, 0x2f, 0x62, 0x59, 0xfc, 0x7e, 0x5d, 0x1b, 0xc9, 0x27}} - return a, nil -} - var _scriptsKindctlSh = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x54\x5f\x6f\xdb\x36\x1c\x7c\x16\x3f\xc5\x45\x13\x92\x78\x00\xed\x25\x2f\x03\x9c\x39\x5b\x9b\xb9\x5b\x91\x2d\x0e\xec\xe6\x29\x0b\x0c\x5a\xa2\x24\xc2\x32\x29\x90\x54\x63\xcf\xd6\x77\x1f\xa8\x3f\x96\x6b\xaf\xc8\x80\xd5\x2f\xa2\x49\xde\xf1\x78\xbf\xdf\xf1\xbb\xb3\xc1\x42\xc8\xc1\x82\x99\x94\x10\xc3\x2d\x28\x27\x64\x3a\x7e\x9c\xcc\xa7\x93\xc9\xa7\x91\x1f\x5c\x22\x8c\xe0\x3e\x91\xd0\x92\xad\x38\xfc\x60\xfb\xfe\xdd\xec\xf7\xf9\x6c\xf2\x34\xbd\x1b\x3f\xff\xf0\x52\xfa\xe8\xf9\xb8\x1d\x44\xfc\xf3\x40\x16\x59\x86\xeb\xdb\xf3\x2b\x9c\x9f\x3b\x60\xbf\xef\x06\xf9\x6b\x84\x9e\x4f\xc8\xdd\x1f\x4f\xb3\x4f\xe3\xe9\xfc\xe1\xdd\x9f\xe3\x51\xb0\x3d\xfc\x3b\xa4\xcb\x62\xc1\x59\x9e\x67\x1b\x6a\xb9\xb1\x25\xb9\xff\xf8\xf0\xeb\xfc\xfe\xe9\xfd\xf8\x6e\xf2\xf0\xe1\xe3\x6f\xa3\x60\x7b\x34\x33\xa4\xc1\x76\xaf\xb4\x1c\xf4\x1d\xc1\x60\x29\x64\x44\xbf\xa4\x2e\xfb\x1b\xb6\xca\x4a\x42\xc2\x94\x87\xcb\xb9\xdb\x71\xd9\xdb\x12\x4f\xc4\x38\xc3\x33\xe8\xda\x5d\x2f\x54\xab\x15\x93\x11\xe8\x67\xb8\x0d\x3d\x1f\x2f\x37\xb0\x29\x97\xc4\xf3\x78\x98\x2a\xf8\xf7\x42\x46\x10\x06\x52\x59\x08\x69\x2c\xcb\x32\x1e\x9d\xe1\x31\xe3\xcc\xf0\x76\x06\x8e\xc3\xea\x0d\x58\xc2\x84\xec\xfb\x0e\xbd\x16\x16\x57\xc4\x8b\x05\x29\x09\x31\xc5\x62\x9e\xf2\x2c\xaf\x14\xd4\xc4\x4f\x86\x25\x7c\x88\xe0\x51\xab\xe4\xc1\x39\xfc\x93\x29\x16\x8d\x9e\x5b\x3c\xab\xdc\x0a\x25\xcd\xcb\x5f\xd2\x6f\x11\xb3\xfd\xba\x19\xee\x27\x01\xc0\x58\xa6\x2d\x30\xab\x3e\x36\xe5\xf8\xd2\xd4\xea\x6a\x08\xb3\xc2\x58\xae\x8f\x80\x2a\x87\x03\xaa\xfc\x6d\x9c\x03\x74\x50\xcd\x5d\xdb\xec\xa1\xb2\x11\xf1\x5f\x4f\xdf\x0f\x3e\x28\x0d\xe7\x0c\x5e\x85\x4d\xc1\x59\x98\xa2\xf3\x01\xba\x90\xdd\x55\xbf\x62\x15\x4d\x77\x94\x3a\x8a\x03\xf2\xc6\xf2\x4a\x93\xf3\x1c\xe8\xba\x80\x00\x7c\x2d\x8c\x15\x32\x99\x37\xb2\xcc\x28\xb8\xac\x64\x26\xdc\xb6\x52\x0d\x76\x48\x34\xcf\x71\xd4\x56\xd8\xed\x60\x75\xc1\x7b\x84\x00\x22\x76\xad\x24\xe1\x07\x27\x94\x5d\x2b\x75\xc6\xdd\xd5\x6b\x27\x94\x2c\xd3\x9c\x45\x9b\x5a\x97\x39\x6b\xbc\xae\x3b\x08\x88\x85\x3b\x6a\xb5\x8c\x84\x06\xcd\x5d\x12\x8f\xfb\xdf\x77\x1b\x6a\x9f\x35\x67\x96\xb7\x77\x00\xa5\x2e\xbc\xa3\xe3\xf3\x68\x15\xbb\x50\xc9\x58\x24\xa7\x09\x2b\x1d\x99\x5b\xb7\x59\x4b\x44\x85\x8c\x15\x28\x0d\x95\xb4\x7c\x5d\xd7\xf4\x38\x6f\x6f\xb2\xee\x8b\xa2\xf2\x93\x9a\x7c\xfb\xa2\xfc\xfd\x7f\x8b\xd2\x84\x5e\x17\x52\x0a\x99\xfc\x6b\x4d\x2a\x79\x11\xcf\xf8\x9b\x96\x13\x40\xaf\x40\xa7\x31\xbe\x6e\x4c\x15\xa9\xda\x99\xd6\xa7\xfd\x90\x69\xdb\x6c\x6b\xfa\x7e\x14\x5c\x91\xd0\x3d\x41\xc1\x41\x5c\x84\x24\x9e\xef\x63\x07\x9f\xa6\xf5\xa7\x0e\x46\x8f\x78\x5e\xfb\x04\x11\xcf\xbb\xb9\x21\xde\xf7\xd5\x5c\x2a\x62\xdb\xac\x05\xdb\x8e\xa8\x44\xf0\x0b\xf1\xbc\xca\xc8\xe0\x67\x8c\x70\x75\xfd\xe3\xc1\xcb\xd8\xc4\x6c\xac\xb5\xd2\x43\x5c\x1c\x28\xb8\x68\x5d\x63\x58\x4a\xf5\x2a\x0f\xb2\xdc\xf7\x71\x7b\x7e\xdd\xa1\x51\xff\xa6\x85\xc4\x45\x97\xec\x5a\xf0\x05\x62\xa5\xc1\x90\x09\x63\xa1\xe2\x13\x2e\x73\x40\xd6\xbc\xb3\xee\xa1\xad\x6e\xc6\x0d\x0b\xc9\x3f\x01\x00\x00\xff\xff\x2e\x64\xb3\xcb\xe0\x06\x00\x00") func scriptsKindctlShBytes() ([]byte, error) { @@ -321,26 +299,6 @@ func scriptsPullDepsSh() (*asset, error) { return a, nil } -var _scriptsRawDiffSh = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x24\xcc\x31\x8a\xc3\x30\x10\x40\xd1\x7e\x4e\xf1\x17\xbb\x15\xc6\xdb\x6e\xb1\x55\xda\x5c\xc0\xb8\x90\xc9\x28\x12\x18\x09\x46\x63\x9c\xe4\xf4\x21\xa4\x7f\xbc\xe1\x67\xda\x4a\x9d\xb6\xd8\xb3\xc8\xad\xa4\x44\x38\x08\x57\xc6\x99\xf1\x57\x64\xe0\x52\xfb\x61\x8a\xe7\xe8\x9c\x4a\xab\xfb\x13\x7d\x14\xe7\x2c\x9e\xa9\xad\x86\x97\x5a\xa3\x7b\xf4\xa3\x53\x3a\x9e\xd5\x94\x33\x76\x22\xa6\x71\x47\xcd\x9a\x49\x49\x2c\x0b\xe3\x3f\xe1\xee\xcc\xac\xeb\xdf\x47\x56\x01\xbe\xdf\x2c\xa9\xc8\x3b\x00\x00\xff\xff\x66\x89\xc3\x1f\x8f\x00\x00\x00") - -func scriptsRawDiffShBytes() ([]byte, error) { - return bindataRead( - _scriptsRawDiffSh, - "scripts/raw-diff.sh", - ) -} - -func scriptsRawDiffSh() (*asset, error) { - bytes, err := scriptsRawDiffShBytes() - if err != nil { - return nil, err - } - - info := bindataFileInfo{name: "scripts/raw-diff.sh", size: 143, mode: os.FileMode(0644), modTime: time.Unix(1612551594, 0)} - a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x9, 0x22, 0xae, 0xe8, 0xa3, 0x7b, 0xc9, 0xd1, 0x2a, 0x95, 0x3, 0x39, 0x38, 0x73, 0xa5, 0x4a, 0x8b, 0x2f, 0x10, 0x38, 0x1e, 0xc0, 0x41, 0x56, 0x9a, 0xde, 0x58, 0x2, 0xc9, 0x90, 0xa, 0x11}} - return a, nil -} - // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -450,13 +408,9 @@ var _bindata = map[string]func() (*asset, error){ "scripts/create-lambda-bundle.sh": scriptsCreateLambdaBundleSh, - "scripts/kdiff-wrapper.sh": scriptsKdiffWrapperSh, - "scripts/kindctl.sh": scriptsKindctlSh, "scripts/pull-deps.sh": scriptsPullDepsSh, - - "scripts/raw-diff.sh": scriptsRawDiffSh, } // AssetDir returns the file names below a certain @@ -518,10 +472,8 @@ var _bintree = &bintree{nil, map[string]*bintree{ "tabulate.py": &bintree{scriptsClusterSummaryTabulatePy, map[string]*bintree{}}, }}, "create-lambda-bundle.sh": &bintree{scriptsCreateLambdaBundleSh, map[string]*bintree{}}, - "kdiff-wrapper.sh": &bintree{scriptsKdiffWrapperSh, map[string]*bintree{}}, "kindctl.sh": &bintree{scriptsKindctlSh, map[string]*bintree{}}, "pull-deps.sh": &bintree{scriptsPullDepsSh, map[string]*bintree{}}, - "raw-diff.sh": &bintree{scriptsRawDiffSh, map[string]*bintree{}}, }}, }} diff --git a/pkg/cluster/client.go b/pkg/cluster/client.go index eade31f..2caceff 100644 --- a/pkg/cluster/client.go +++ b/pkg/cluster/client.go @@ -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) diff --git a/pkg/cluster/fake_client.go b/pkg/cluster/fake_client.go index dbebe3f..2fe6654 100644 --- a/pkg/cluster/fake_client.go +++ b/pkg/cluster/fake_client.go @@ -103,6 +103,7 @@ func (cc *FakeClusterClient) DiffStructured( ctx context.Context, paths []string, serverSide bool, + diffCommand string, ) ([]diff.Result, error) { return []diff.Result{ { diff --git a/pkg/cluster/kube/ordered_client.go b/pkg/cluster/kube/ordered_client.go index 11a2f02..165af0a 100644 --- a/pkg/cluster/kube/ordered_client.go +++ b/pkg/cluster/kube/ordered_client.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" "regexp" + "strings" "github.com/briandowns/spinner" "github.com/segmentio/kubeapply/data" @@ -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. @@ -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 @@ -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 { @@ -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( diff --git a/pkg/cluster/kube_client.go b/pkg/cluster/kube_client.go index 9e3722b..2f83d81 100644 --- a/pkg/cluster/kube_client.go +++ b/pkg/cluster/kube_client.go @@ -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)", @@ -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)", @@ -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) @@ -378,6 +380,7 @@ func (cc *KubeClusterClient) execDiff( ctx, paths, structured, + diffCommand, cc.spinnerObj, ) if err != nil || !cc.checkApplyConsistency { diff --git a/pkg/events/handler.go b/pkg/events/handler.go index 6ad82f6..b01312f 100644 --- a/pkg/events/handler.go +++ b/pkg/events/handler.go @@ -578,6 +578,7 @@ func (whh *WebhookHandler) runDiffs( diffCtx, clusterClient.Config().AbsSubpaths(), clusterClient.Config().ServerSideApply, + "", ) if err != nil { diffErr = fmt.Errorf( diff --git a/scripts/kdiff-wrapper.sh b/scripts/kdiff-wrapper.sh deleted file mode 100755 index 83ee588..0000000 --- a/scripts/kdiff-wrapper.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/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 diff --git a/scripts/raw-diff.sh b/scripts/raw-diff.sh deleted file mode 100644 index cdd0c64..0000000 --- a/scripts/raw-diff.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/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