Skip to content

Commit 20fdf7b

Browse files
author
Diego Alfonso
committed
fix: Update subpath only if provided when merge
Signed-off-by: Diego Alfonso <[email protected]>
1 parent 570beca commit 20fdf7b

File tree

4 files changed

+146
-35
lines changed

4 files changed

+146
-35
lines changed

pkg/apis/cartographer/v1alpha1/workload_helpers.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -342,36 +342,36 @@ func (w *WorkloadSpec) MergeGit(git GitSource) {
342342
w.Source = &Source{
343343
Git: &git,
344344
}
345-
if stash != nil && stash.Git != nil {
345+
if stash != nil && stash.Subpath != "" {
346346
w.Source.Subpath = stash.Subpath
347347
}
348348
}
349349
}
350350

351351
func (w *WorkloadSpec) MergeSourceImage(image string) {
352-
stash := w.Source.DeepCopy()
353-
w.ResetSource()
354-
355-
w.Source = &Source{
356-
Image: image,
357-
}
358-
if stash != nil {
359-
w.Source.Subpath = stash.Subpath
352+
src := &Source{Image: image}
353+
if w.Source != nil {
354+
src.Subpath = w.Source.Subpath
360355
}
356+
w.ResetSource()
357+
w.Source = src
361358
}
362359

363360
func (w *WorkloadSpec) MergeSubPath(subPath string) {
364361
if w.Source == nil {
365362
w.Source = &Source{}
366363
}
367-
368364
w.Source.Subpath = subPath
369365
}
370366

371367
func (w *WorkloadSpec) MergeImage(image string) {
368+
var src *Source
369+
if w.Source != nil && w.Source.Subpath != "" {
370+
src = &Source{Subpath: w.Source.Subpath}
371+
}
372372
w.ResetSource()
373-
374373
w.Image = image
374+
w.Source = src
375375
}
376376

377377
func (w *WorkloadSpec) MergeEnv(env corev1.EnvVar) {

pkg/apis/cartographer/v1alpha1/workload_test.go

+36-24
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,42 @@ func TestWorkload_Merge(t *testing.T) {
10011001
Image: "ubuntu:bionic",
10021002
},
10031003
},
1004+
}, {
1005+
name: "image without source",
1006+
seed: &Workload{},
1007+
update: &Workload{
1008+
Spec: WorkloadSpec{
1009+
Image: "ubuntu:bionic",
1010+
},
1011+
},
1012+
want: &Workload{
1013+
Spec: WorkloadSpec{
1014+
Image: "ubuntu:bionic",
1015+
},
1016+
},
1017+
}, {
1018+
name: "image with subpath",
1019+
seed: &Workload{
1020+
Spec: WorkloadSpec{
1021+
Image: "alpine:latest",
1022+
Source: &Source{
1023+
Subpath: "/sys",
1024+
},
1025+
},
1026+
},
1027+
update: &Workload{
1028+
Spec: WorkloadSpec{
1029+
Image: "ubuntu:bionic",
1030+
},
1031+
},
1032+
want: &Workload{
1033+
Spec: WorkloadSpec{
1034+
Image: "ubuntu:bionic",
1035+
Source: &Source{
1036+
Subpath: "/sys",
1037+
},
1038+
},
1039+
},
10041040
}, {
10051041
name: "git",
10061042
seed: &Workload{
@@ -1767,30 +1803,6 @@ func TestWorkloadSpec_MergeGit(t *testing.T) {
17671803
Subpath: "my-subpath",
17681804
},
17691805
},
1770-
}, {
1771-
name: "update to git source deleting subpath",
1772-
seed: &WorkloadSpec{
1773-
Source: &Source{
1774-
Image: "my-registry.nip.io/my-folder/my-image:latest@sha:my-sha1234567890",
1775-
Subpath: "my-subpath",
1776-
},
1777-
},
1778-
git: GitSource{
1779-
URL: "[email protected]:example/repo.git",
1780-
Ref: GitRef{
1781-
Branch: "main",
1782-
},
1783-
},
1784-
want: &WorkloadSpec{
1785-
Source: &Source{
1786-
Git: &GitSource{
1787-
URL: "[email protected]:example/repo.git",
1788-
Ref: GitRef{
1789-
Branch: "main",
1790-
},
1791-
},
1792-
},
1793-
},
17941806
}, {
17951807
name: "delete source when setting repo to empty string",
17961808
seed: &WorkloadSpec{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2023 VMware, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: carto.run/v1alpha1
16+
kind: Workload
17+
metadata:
18+
annotations:
19+
local-source-proxy.apps.tanzu.vmware.com: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
20+
labels:
21+
apps.tanzu.vmware.com/workload-type: web
22+
name: my-workload
23+
namespace: default
24+
spec:
25+
params:
26+
- name: annotations
27+
value:
28+
autoscaling.knative.dev/minScale: "2"
29+
source:
30+
image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69

pkg/commands/workload_apply_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -8977,6 +8977,75 @@ No source code is changed
89778977
To see logs: "tanzu apps workload tail my-workload --timestamp --since 1h"
89788978
To get status: "tanzu apps workload get my-workload"
89798979
8980+
`, localSource),
8981+
},
8982+
{
8983+
Name: "update from local source using lsp from file without subpath",
8984+
Skip: runtm.GOOS == "windows",
8985+
Args: []string{workloadName, flags.LocalPathFlagName, localSource, flags.FilePathFlagName, "./testdata/workload-lsp-non-subPath.yaml", flags.YesFlagName},
8986+
GivenObjects: []client.Object{
8987+
parent.
8988+
MetadataDie(func(d *diemetav1.ObjectMetaDie) {
8989+
d.Annotations(map[string]string{apis.LocalSourceProxyAnnotationName: ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69"})
8990+
d.Labels(map[string]string{apis.WorkloadTypeLabelName: "web"})
8991+
}).SpecDie(func(d *diecartov1alpha1.WorkloadSpecDie) {
8992+
d.Source(&cartov1alpha1.Source{
8993+
Image: ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69",
8994+
Subpath: "current-subpath",
8995+
})
8996+
}),
8997+
},
8998+
KubeConfigTransport: clitesting.NewFakeTransportFromResponse(respCreator(http.StatusOK, `{"statuscode": "200", "message": "any ignored message"}`, myWorkloadHeader)),
8999+
ExpectUpdates: []client.Object{
9000+
&cartov1alpha1.Workload{
9001+
ObjectMeta: metav1.ObjectMeta{
9002+
Namespace: defaultNamespace,
9003+
Name: workloadName,
9004+
Labels: map[string]string{
9005+
apis.WorkloadTypeLabelName: "web",
9006+
},
9007+
Annotations: map[string]string{
9008+
"local-source-proxy.apps.tanzu.vmware.com": ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69",
9009+
},
9010+
},
9011+
Spec: cartov1alpha1.WorkloadSpec{
9012+
Source: &cartov1alpha1.Source{
9013+
Image: ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69",
9014+
Subpath: "current-subpath",
9015+
},
9016+
Params: []cartov1alpha1.Param{
9017+
{
9018+
Name: "annotations",
9019+
Value: apiextensionsv1.JSON{Raw: []byte(`{"autoscaling.knative.dev/minScale":"2"}`)},
9020+
},
9021+
},
9022+
},
9023+
},
9024+
},
9025+
ExpectOutput: fmt.Sprintf(`
9026+
❗ WARNING: Configuration file update strategy is changing. By default, provided configuration files will replace rather than merge existing configuration. The change will take place in the January 2024 TAP release (use "--update-strategy" to control strategy explicitly).
9027+
9028+
Publishing source in "%s" to "local-source-proxy.tap-local-source-system.svc.cluster.local/source:default-my-workload"...
9029+
No source code is changed
9030+
9031+
🔎 Update workload:
9032+
...
9033+
8, 8 | apps.tanzu.vmware.com/workload-type: web
9034+
9, 9 | name: my-workload
9035+
10, 10 | namespace: default
9036+
11, 11 |spec:
9037+
12 + | params:
9038+
13 + | - name: annotations
9039+
14 + | value:
9040+
15 + | autoscaling.knative.dev/minScale: "2"
9041+
12, 16 | source:
9042+
13, 17 | image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
9043+
14, 18 | subPath: current-subpath
9044+
👍 Updated workload "my-workload"
9045+
9046+
To see logs: "tanzu apps workload tail my-workload --timestamp --since 1h"
9047+
To get status: "tanzu apps workload get my-workload"
9048+
89809049
`, localSource),
89819050
},
89829051
}

0 commit comments

Comments
 (0)