Skip to content

Commit 4e7ad68

Browse files
committed
List of Changes
1. Add kubectl apply command after setting labels 2. Integration tests for helms are back a. GoogleContainerTools#1823: Use env template in skaffold release name 3. During helm deploy, build is assumed and hence if no build-artifacts are supplied, it fails with following error "no build present for gcr.io/k8s-skaffold/skaffold-helm" Since Build and Deploy are now separate ( GoogleContainerTools#922 for notes) use the image value as is if not present as build artifacts Fix test fot this. TODO: 1. Add PR description 2. Figure out why we get 2 pods, 1 with valid labels which is running but another 1 with different labels.
1 parent 953c600 commit 4e7ad68

File tree

7 files changed

+119
-21
lines changed

7 files changed

+119
-21
lines changed

examples/helm-deployment/skaffold.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build:
88
deploy:
99
helm:
1010
releases:
11-
- name: skaffold-helm
11+
- name: skaffold-helm-{{.TEST_NS}}
1212
chartPath: skaffold-helm
1313
#wait: true
1414
#valuesFiles:

integration/examples/helm-deployment/skaffold.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build:
88
deploy:
99
helm:
1010
releases:
11-
- name: skaffold-helm
11+
- name: skaffold-helm-{{.TEST_NS}}
1212
chartPath: skaffold-helm
1313
#wait: true
1414
#valuesFiles:

integration/helm_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2019 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"testing"
23+
24+
"github.com/GoogleContainerTools/skaffold/integration/skaffold"
25+
"github.com/google/go-cmp/cmp"
26+
)
27+
28+
func TestHelmDeploy(t *testing.T) {
29+
if testing.Short() {
30+
t.Skip("skipping integration test")
31+
}
32+
if os.Getenv("REMOTE_INTEGRATION") != "true" {
33+
t.Skip("skipping remote only test")
34+
}
35+
36+
ns, client, deleteNs := SetupNamespace(t)
37+
defer deleteNs()
38+
39+
// To fix #1823, we make use of templating release name
40+
env := []string{fmt.Sprintf("TEST_NS=%s", ns.Name)}
41+
depName := fmt.Sprintf("skaffold-helm-%s", ns.Name)
42+
helmDir := "examples/helm-deployment"
43+
skaffold.Deploy().InDir(helmDir).InNs(ns.Name).WithEnv(env).RunOrFailOutput(t)
44+
45+
client.WaitForDeploymentsToStabilize(depName)
46+
47+
expectedLabels := map[string]string{
48+
"app.kubernetes.io/managed-by": "true",
49+
"release": depName,
50+
"skaffold.dev/deployer": "helm",
51+
}
52+
//check if labels are set correctly for deploument
53+
dep := client.GetDeployment(depName)
54+
extractLabels(t, fmt.Sprintf("Dep: %s", depName), expectedLabels, dep.ObjectMeta.Labels)
55+
56+
//check if labels are set correctly for pods
57+
pods := client.GetPods()
58+
// FIX ME the first pod dies.
59+
fmt.Println(pods.Items[1])
60+
po := pods.Items[0]
61+
extractLabels(t, fmt.Sprintf("Pod:%s", po.ObjectMeta.Name), expectedLabels, po.ObjectMeta.Labels)
62+
63+
skaffold.Delete().InDir(helmDir).InNs(ns.Name).WithEnv(env).RunOrFail(t)
64+
}
65+
66+
func extractLabels(t *testing.T, name string, expected map[string]string, actual map[string]string) {
67+
extracted := map[string]string{}
68+
for k, v := range actual {
69+
switch k {
70+
case "app.kubernetes.io/managed-by":
71+
extracted[k] = "true" // ignore version since its runtime
72+
case "release":
73+
extracted[k] = v
74+
case "skaffold.dev/deployer":
75+
extracted[k] = v
76+
default:
77+
continue
78+
}
79+
}
80+
if d := cmp.Diff(extracted, expected); d != "" {
81+
t.Errorf("expected to see %s labels for %s. Diff %s", expected, name, d)
82+
}
83+
}

integration/run_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ func TestRun(t *testing.T) {
9494
dir: "testdata/kaniko-microservices",
9595
deployments: []string{"leeroy-app", "leeroy-web"},
9696
remoteOnly: true,
97-
// }, {
98-
// description: "helm",
99-
// dir: "examples/helm-deployment",
100-
// deployments: []string{"skaffold-helm"},
101-
// remoteOnly: true,
10297
}, {
10398
description: "jib in googlecloudbuild",
10499
dir: "testdata/jib",

integration/util.go

+10
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ func (k *NSKubernetesClient) GetDeployment(depName string) *appsv1.Deployment {
130130
return dep
131131
}
132132

133+
// GetPods gets all pods in the client namespace
134+
func (k *NSKubernetesClient) GetPods() *v1.PodList {
135+
pods, err := k.client.CoreV1().Pods(k.ns).List(meta_v1.ListOptions{})
136+
if err != nil {
137+
k.t.Fatalf("Could not find pods for in namespace %s", k.ns)
138+
}
139+
return pods
140+
}
141+
142+
133143
// WaitForDeploymentsToStabilize waits for a list of deployments to become stable.
134144
func (k *NSKubernetesClient) WaitForDeploymentsToStabilize(depNames ...string) {
135145
if len(depNames) == 0 {

pkg/skaffold/deploy/helm.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type HelmDeployer struct {
4949
kubeContext string
5050
namespace string
5151
defaultRepo string
52+
kubectl kubectl.CLI
5253
forceDeploy bool
5354
}
5455

@@ -58,6 +59,10 @@ func NewHelmDeployer(runCtx *runcontext.RunContext) *HelmDeployer {
5859
return &HelmDeployer{
5960
HelmDeploy: runCtx.Cfg.Deploy.HelmDeploy,
6061
kubeContext: runCtx.KubeContext,
62+
kubectl: kubectl.CLI{
63+
Namespace: runCtx.Opts.Namespace,
64+
KubeContext: runCtx.KubeContext,
65+
},
6166
namespace: runCtx.Opts.Namespace,
6267
defaultRepo: runCtx.DefaultRepo,
6368
forceDeploy: runCtx.Opts.ForceDeploy(),
@@ -88,6 +93,13 @@ func (h *HelmDeployer) Deploy(ctx context.Context, out io.Writer, builds []build
8893
event.DeployFailed(err)
8994
return errors.Wrap(err, "setting labels in manifests")
9095
}
96+
97+
err = h.kubectl.Apply(ctx, out, manifests)
98+
if err != nil {
99+
event.DeployFailed(err)
100+
return errors.Wrap(err, "kubectl error while applying labels in manifests")
101+
}
102+
91103
event.DeployComplete()
92104
return nil
93105
}
@@ -163,11 +175,9 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
163175
color.Red.Fprintf(out, "Helm release %s not installed. Installing...\n", releaseName)
164176
isInstalled = false
165177
}
166-
params, err := h.joinTagsToBuildResult(builds, r.Values)
167-
if err != nil {
168-
return "", errors.Wrap(err, "matching build results to chart values")
169-
}
170178

179+
params := h.joinTagsToBuildResult(builds, r.Values)
180+
logrus.Warn(params)
171181
var setOpts []string
172182
for k, v := range params {
173183
setOpts = append(setOpts, "--set")
@@ -401,22 +411,23 @@ func (h *HelmDeployer) deleteRelease(ctx context.Context, out io.Writer, r lates
401411
return nil
402412
}
403413

404-
func (h *HelmDeployer) joinTagsToBuildResult(builds []build.Artifact, params map[string]string) (map[string]build.Artifact, error) {
414+
func (h *HelmDeployer) joinTagsToBuildResult(builds []build.Artifact, params map[string]string) map[string]build.Artifact {
405415
imageToBuildResult := map[string]build.Artifact{}
406-
for _, build := range builds {
407-
imageToBuildResult[build.ImageName] = build
416+
for _, b := range builds {
417+
imageToBuildResult[b.ImageName] = b
408418
}
409-
410419
paramToBuildResult := map[string]build.Artifact{}
411420
for param, imageName := range params {
412421
newImageName := util.SubstituteDefaultRepoIntoImage(h.defaultRepo, imageName)
413-
build, ok := imageToBuildResult[newImageName]
422+
b, ok := imageToBuildResult[newImageName]
414423
if !ok {
415-
return nil, fmt.Errorf("no build present for %s", imageName)
424+
logrus.Warnf("no build present for %s", imageName)
425+
logrus.Warnf("continuing with %s", imageName)
426+
b = build.Artifact{ImageName: imageName, Tag: imageName}
416427
}
417-
paramToBuildResult[param] = build
428+
paramToBuildResult[param] = b
418429
}
419-
return paramToBuildResult, nil
430+
return paramToBuildResult
420431
}
421432

422433
func evaluateReleaseName(nameTemplate string) (string, error) {

pkg/skaffold/deploy/helm_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,10 @@ func TestHelmDeploy(t *testing.T) {
242242
builds: testBuilds,
243243
},
244244
{
245-
description: "deploy error unmatched parameter",
245+
description: "deploy should not error for unmatched parameter",
246246
cmd: &MockHelm{t: t},
247247
runContext: makeRunContext(testDeployConfigParameterUnmatched, false),
248248
builds: testBuilds,
249-
shouldErr: true,
250249
},
251250
{
252251
description: "deploy success remote chart with skipBuildDependencies",

0 commit comments

Comments
 (0)