Skip to content

Commit b4f0afb

Browse files
committed
use kubectl's built-in kustomize when possible
1 parent 650e618 commit b4f0afb

File tree

7 files changed

+78
-37
lines changed

7 files changed

+78
-37
lines changed

deploy/skaffold/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
# This base image has to be updated manually after running `make build_deps`
16-
FROM gcr.io/k8s-skaffold/build_deps:d54e32f303a9bf5ce42978be0b04ae6cba235d37 as builder
16+
FROM gcr.io/k8s-skaffold/build_deps:73e3714b2c30ecedb4df789ed285e9151e1ed832 as builder
1717
WORKDIR /skaffold
1818
COPY . .
1919

deploy/skaffold/Dockerfile.deps

+1-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# Download kubectl
1616
FROM alpine:3.10 as download-kubectl
17-
ENV KUBECTL_VERSION v1.14.10
17+
ENV KUBECTL_VERSION v1.15.5
1818
ENV KUBECTL_URL https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
1919
RUN wget -O kubectl "${KUBECTL_URL}"
2020
RUN chmod +x kubectl
@@ -26,13 +26,6 @@ ENV HELM_URL https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz
2626
RUN wget -O helm.tar.gz "${HELM_URL}"
2727
RUN tar -xvf helm.tar.gz --strip-components 1
2828

29-
# Download kustomize
30-
FROM alpine:3.10 as download-kustomize
31-
ENV KUSTOMIZE_VERSION 3.5.4
32-
ENV KUSTOMIZE_URL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${KUSTOMIZE_VERSION}/kustomize_v${KUSTOMIZE_VERSION}_linux_amd64.tar.gz
33-
RUN wget -O kustomize.tar.gz "${KUSTOMIZE_URL}"
34-
RUN tar -xvf kustomize.tar.gz
35-
3629
# Download kompose
3730
FROM alpine:3.10 as download-kompose
3831
ENV KOMPOSE_VERSION v1.21.0
@@ -78,7 +71,6 @@ RUN apt-get update && \
7871
COPY --from=docker:18.09.6 /usr/local/bin/docker /usr/local/bin/
7972
COPY --from=download-kubectl kubectl /usr/local/bin/
8073
COPY --from=download-helm helm /usr/local/bin/
81-
COPY --from=download-kustomize kustomize /usr/local/bin/
8274
COPY --from=download-kompose kompose /usr/local/bin/
8375
COPY --from=download-container-structure-test container-structure-test /usr/local/bin/
8476
COPY --from=download-bazel bazel /usr/local/bin/

deploy/webhook/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ RUN wget -O- "${HUGO_URL}" | tar xz
3131

3232
# Download kubectl
3333
FROM alpine:3.10 as download-kubectl
34-
ENV KUBECTL_VERSION v1.12.0
34+
ENV KUBECTL_VERSION v1.15.5
3535
ENV KUBECTL_URL https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
3636
RUN wget -O kubectl "${KUBECTL_URL}"
3737
RUN chmod +x kubectl

integration/testdata/skaffold-in-cluster/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM gcr.io/gcp-runtimes/ubuntu_16_0_4
22

3-
ENV KUBECTL_VERSION v1.12.8
3+
ENV KUBECTL_VERSION v1.15.5
44
ENV KUBECTL_URL https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
55
RUN curl -O "${KUBECTL_URL}"
66
RUN chmod +x kubectl

pkg/skaffold/deploy/kubectl/cli.go

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func (c *CLI) Apply(ctx context.Context, out io.Writer, manifests ManifestList)
7373
return nil
7474
}
7575

76+
// Kustomize runs `kubectl kustomize` with the provided args
77+
func (c *CLI) Kustomize(ctx context.Context, args []string) ([]byte, error) {
78+
return c.RunOut(ctx, "kustomize", c.args(nil, args...)...)
79+
}
80+
7681
// ReadManifests reads a list of manifests in yaml format.
7782
func (c *CLI) ReadManifests(ctx context.Context, manifests []string) (ManifestList, error) {
7883
var list []string

pkg/skaffold/deploy/kustomize.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828

2929
"github.com/segmentio/textio"
30+
"github.com/sirupsen/logrus"
3031
yaml "gopkg.in/yaml.v2"
3132

3233
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
@@ -88,18 +89,32 @@ type KustomizeDeployer struct {
8889
*latest.KustomizeDeploy
8990

9091
kubectl deploy.CLI
92+
useKubectl bool
9193
insecureRegistries map[string]bool
9294
BuildArgs []string
9395
}
9496

9597
func NewKustomizeDeployer(runCtx *runcontext.RunContext) *KustomizeDeployer {
98+
kubectl := deploy.CLI{
99+
CLI: kubectl.NewFromRunContext(runCtx),
100+
Flags: runCtx.Cfg.Deploy.KustomizeDeploy.Flags,
101+
ForceDeploy: runCtx.Opts.Force,
102+
}
103+
104+
// if user's kubectl version is >1.14, we can use the built-in kustomize command
105+
var useKubectl bool
106+
gt, err := kubectl.CompareVersionTo(context.Background(), 1, 14)
107+
if err != nil {
108+
logrus.Warnf("could not retrieve kubectl version: relying on standalone kustomize binary")
109+
}
110+
if gt == 1 {
111+
useKubectl = true
112+
}
113+
96114
return &KustomizeDeployer{
97-
KustomizeDeploy: runCtx.Cfg.Deploy.KustomizeDeploy,
98-
kubectl: deploy.CLI{
99-
CLI: kubectl.NewFromRunContext(runCtx),
100-
Flags: runCtx.Cfg.Deploy.KustomizeDeploy.Flags,
101-
ForceDeploy: runCtx.Opts.Force,
102-
},
115+
KustomizeDeploy: runCtx.Cfg.Deploy.KustomizeDeploy,
116+
kubectl: kubectl,
117+
useKubectl: useKubectl,
103118
insecureRegistries: runCtx.InsecureRegistries,
104119
BuildArgs: runCtx.Cfg.Deploy.KustomizeDeploy.BuildArgs,
105120
}
@@ -339,8 +354,15 @@ func pathExistsLocally(filename string, workingDir string) (bool, os.FileMode) {
339354
func (k *KustomizeDeployer) readManifests(ctx context.Context) (deploy.ManifestList, error) {
340355
var manifests deploy.ManifestList
341356
for _, kustomizePath := range k.KustomizePaths {
342-
cmd := exec.CommandContext(ctx, "kustomize", buildCommandArgs(k.BuildArgs, kustomizePath)...)
343-
out, err := util.RunCmdOut(cmd)
357+
var out []byte
358+
var err error
359+
if k.useKubectl {
360+
out, err = k.kubectl.Kustomize(ctx, buildCommandArgs(k.BuildArgs, kustomizePath))
361+
} else {
362+
cmd := exec.CommandContext(ctx, "kustomize", append([]string{"build"}, buildCommandArgs(k.BuildArgs, kustomizePath)...)...)
363+
out, err = util.RunCmdOut(cmd)
364+
}
365+
344366
if err != nil {
345367
return nil, fmt.Errorf("kustomize build: %w", err)
346368
}
@@ -355,7 +377,6 @@ func (k *KustomizeDeployer) readManifests(ctx context.Context) (deploy.ManifestL
355377

356378
func buildCommandArgs(buildArgs []string, kustomizePath string) []string {
357379
var args []string
358-
args = append(args, "build")
359380

360381
if len(buildArgs) > 0 {
361382
for _, v := range buildArgs {

pkg/skaffold/deploy/kustomize_test.go

+39-16
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ func TestKustomizeDeploy(t *testing.T) {
8787
},
8888
forceDeploy: true,
8989
},
90+
{
91+
description: "built-in kubectl kustomize with newer version",
92+
cfg: &latest.KustomizeDeploy{
93+
KustomizePaths: []string{"a", "b"},
94+
},
95+
commands: testutil.
96+
CmdRunOut("kubectl version --client -ojson", kubectlVersion118).
97+
AndRunOut("kubectl --context kubecontext --namespace testNamespace kustomize a", deploymentWebYAML).
98+
AndRunOut("kubectl --context kubecontext --namespace testNamespace kustomize b", deploymentAppYAML).
99+
AndRun("kubectl --context kubecontext --namespace testNamespace apply -f - --force --grace-period=0"),
100+
builds: []build.Artifact{
101+
{
102+
ImageName: "leeroy-web",
103+
Tag: "leeroy-web:123",
104+
},
105+
{
106+
ImageName: "leeroy-app",
107+
Tag: "leeroy-app:123",
108+
},
109+
},
110+
forceDeploy: true,
111+
},
90112
}
91113
for _, test := range tests {
92114
testutil.Run(t, test.description, func(t *testutil.T) {
@@ -131,7 +153,8 @@ func TestKustomizeCleanup(t *testing.T) {
131153
KustomizePaths: []string{tmpDir.Root()},
132154
},
133155
commands: testutil.
134-
CmdRunOut("kustomize build "+tmpDir.Root(), deploymentWebYAML).
156+
CmdRunOut("kubectl version --client -ojson", kubectlVersion112).
157+
AndRunOut("kustomize build "+tmpDir.Root(), deploymentWebYAML).
135158
AndRun("kubectl --context kubecontext --namespace testNamespace delete --ignore-not-found=true -f -"),
136159
},
137160
{
@@ -140,7 +163,8 @@ func TestKustomizeCleanup(t *testing.T) {
140163
KustomizePaths: tmpDir.Paths("a", "b"),
141164
},
142165
commands: testutil.
143-
CmdRunOut("kustomize build "+tmpDir.Path("a"), deploymentWebYAML).
166+
CmdRunOut("kubectl version --client -ojson", kubectlVersion112).
167+
AndRunOut("kustomize build "+tmpDir.Path("a"), deploymentWebYAML).
144168
AndRunOut("kustomize build "+tmpDir.Path("b"), deploymentAppYAML).
145169
AndRun("kubectl --context kubecontext --namespace testNamespace delete --ignore-not-found=true -f -"),
146170
},
@@ -150,7 +174,8 @@ func TestKustomizeCleanup(t *testing.T) {
150174
KustomizePaths: []string{tmpDir.Root()},
151175
},
152176
commands: testutil.
153-
CmdRunOut("kustomize build "+tmpDir.Root(), deploymentWebYAML).
177+
CmdRunOut("kubectl version --client -ojson", kubectlVersion112).
178+
AndRunOut("kustomize build "+tmpDir.Root(), deploymentWebYAML).
154179
AndRunErr("kubectl --context kubecontext --namespace testNamespace delete --ignore-not-found=true -f -", errors.New("BUG")),
155180
shouldErr: true,
156181
},
@@ -159,11 +184,9 @@ func TestKustomizeCleanup(t *testing.T) {
159184
cfg: &latest.KustomizeDeploy{
160185
KustomizePaths: []string{tmpDir.Root()},
161186
},
162-
commands: testutil.CmdRunOutErr(
163-
"kustomize build "+tmpDir.Root(),
164-
"",
165-
errors.New("BUG"),
166-
),
187+
commands: testutil.
188+
CmdRunOut("kubectl version --client -ojson", kubectlVersion112).
189+
AndRunOutErr("kustomize build "+tmpDir.Root(), "", errors.New("BUG")),
167190
shouldErr: true,
168191
},
169192
}
@@ -399,49 +422,49 @@ func TestKustomizeBuildCommandArgs(t *testing.T) {
399422
description: "no BuildArgs, empty KustomizePaths ",
400423
buildArgs: []string{},
401424
kustomizePath: "",
402-
expectedArgs: []string{"build"},
425+
expectedArgs: nil,
403426
},
404427
{
405428
description: "One BuildArg, empty KustomizePaths",
406429
buildArgs: []string{"--foo"},
407430
kustomizePath: "",
408-
expectedArgs: []string{"build", "--foo"},
431+
expectedArgs: []string{"--foo"},
409432
},
410433
{
411434
description: "no BuildArgs, non-empty KustomizePaths",
412435
buildArgs: []string{},
413436
kustomizePath: "foo",
414-
expectedArgs: []string{"build", "foo"},
437+
expectedArgs: []string{"foo"},
415438
},
416439
{
417440
description: "One BuildArg, non-empty KustomizePaths",
418441
buildArgs: []string{"--foo"},
419442
kustomizePath: "bar",
420-
expectedArgs: []string{"build", "--foo", "bar"},
443+
expectedArgs: []string{"--foo", "bar"},
421444
},
422445
{
423446
description: "Multiple BuildArg, empty KustomizePaths",
424447
buildArgs: []string{"--foo", "--bar"},
425448
kustomizePath: "",
426-
expectedArgs: []string{"build", "--foo", "--bar"},
449+
expectedArgs: []string{"--foo", "--bar"},
427450
},
428451
{
429452
description: "Multiple BuildArg with spaces, empty KustomizePaths",
430453
buildArgs: []string{"--foo bar", "--baz"},
431454
kustomizePath: "",
432-
expectedArgs: []string{"build", "--foo", "bar", "--baz"},
455+
expectedArgs: []string{"--foo", "bar", "--baz"},
433456
},
434457
{
435458
description: "Multiple BuildArg with spaces, non-empty KustomizePaths",
436459
buildArgs: []string{"--foo bar", "--baz"},
437460
kustomizePath: "barfoo",
438-
expectedArgs: []string{"build", "--foo", "bar", "--baz", "barfoo"},
461+
expectedArgs: []string{"--foo", "bar", "--baz", "barfoo"},
439462
},
440463
{
441464
description: "Multiple BuildArg no spaces, non-empty KustomizePaths",
442465
buildArgs: []string{"--foo", "bar", "--baz"},
443466
kustomizePath: "barfoo",
444-
expectedArgs: []string{"build", "--foo", "bar", "--baz", "barfoo"},
467+
expectedArgs: []string{"--foo", "bar", "--baz", "barfoo"},
445468
},
446469
}
447470

0 commit comments

Comments
 (0)