Skip to content

Commit d67ffa9

Browse files
author
Diego Alfonso
committed
chore: Ignore subpath when image is used
Signed-off-by: Diego Alfonso <[email protected]>
1 parent 081a2d7 commit d67ffa9

File tree

4 files changed

+211
-11
lines changed

4 files changed

+211
-11
lines changed

pkg/apis/cartographer/v1alpha1/workload_helpers.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ func (w *WorkloadSpec) Merge(updates *WorkloadSpec) {
219219
for _, p := range updates.Params {
220220
w.MergeParams(p.Name, p.Value)
221221
}
222+
sp := ""
223+
if w.Source != nil {
224+
sp = w.Source.Subpath
225+
}
226+
if updates.Image != "" {
227+
w.MergeImage(updates.Image)
228+
}
229+
if updates.Source != nil && (updates.Source.Git != nil || updates.Source.Image != "") && sp != "" {
230+
if w.Source == nil {
231+
w.Source = &Source{}
232+
}
233+
w.Source.Subpath = sp
234+
}
222235
if updates.Source != nil {
223236
s := updates.Source.DeepCopy()
224237
if s.Git != nil {
@@ -227,13 +240,10 @@ func (w *WorkloadSpec) Merge(updates *WorkloadSpec) {
227240
if s.Image != "" {
228241
w.MergeSourceImage(s.Image)
229242
}
230-
if s.Subpath != "" {
243+
if s.Subpath != "" && w.Source != nil && (w.Source.Git != nil || w.Source.Image != "") {
231244
w.MergeSubPath(s.Subpath)
232245
}
233246
}
234-
if updates.Image != "" {
235-
w.MergeImage(updates.Image)
236-
}
237247
for _, e := range updates.Env {
238248
w.MergeEnv(e)
239249
}

pkg/apis/cartographer/v1alpha1/workload_test.go

+96-7
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,101 @@ func TestWorkload_Merge(t *testing.T) {
10341034
Image: "ubuntu:bionic",
10351035
},
10361036
},
1037+
}, {
1038+
name: "image with sources nill",
1039+
seed: &Workload{
1040+
Spec: WorkloadSpec{
1041+
Image: "alpine:latest",
1042+
},
1043+
},
1044+
update: &Workload{
1045+
Spec: WorkloadSpec{
1046+
Image: "ubuntu:bionic",
1047+
Source: &Source{Subpath: "my-subpath"},
1048+
},
1049+
},
1050+
want: &Workload{
1051+
Spec: WorkloadSpec{
1052+
Image: "ubuntu:bionic",
1053+
},
1054+
},
1055+
}, {
1056+
name: "image with sources source image and subpath",
1057+
seed: &Workload{
1058+
Spec: WorkloadSpec{
1059+
Image: "alpine:latest",
1060+
},
1061+
},
1062+
update: &Workload{
1063+
Spec: WorkloadSpec{
1064+
Image: "ubuntu:bionic",
1065+
Source: &Source{
1066+
Image: "ubuntu:bionic",
1067+
Subpath: "my-subpath",
1068+
},
1069+
},
1070+
},
1071+
want: &Workload{
1072+
Spec: WorkloadSpec{
1073+
Source: &Source{
1074+
Image: "ubuntu:bionic",
1075+
Subpath: "my-subpath",
1076+
},
1077+
},
1078+
},
1079+
}, {
1080+
name: "image update sources source image and subpath",
1081+
seed: &Workload{
1082+
Spec: WorkloadSpec{
1083+
Image: "alpine:latest",
1084+
Source: &Source{
1085+
Subpath: "new-subpath",
1086+
},
1087+
},
1088+
},
1089+
update: &Workload{
1090+
Spec: WorkloadSpec{
1091+
Image: "ubuntu:bionic",
1092+
Source: &Source{
1093+
Image: "ubuntu:bionic",
1094+
Subpath: "my-subpath",
1095+
},
1096+
},
1097+
},
1098+
want: &Workload{
1099+
Spec: WorkloadSpec{
1100+
Source: &Source{
1101+
Image: "ubuntu:bionic",
1102+
Subpath: "my-subpath",
1103+
},
1104+
},
1105+
},
1106+
}, {
1107+
name: "local source image with subpath update source image and subpath",
1108+
seed: &Workload{
1109+
Spec: WorkloadSpec{
1110+
Source: &Source{
1111+
Image: "ubuntu:bionic",
1112+
Subpath: "/opt",
1113+
},
1114+
},
1115+
},
1116+
update: &Workload{
1117+
Spec: WorkloadSpec{
1118+
Source: &Source{
1119+
Image: "ubuntu:bionic",
1120+
Subpath: "/sys",
1121+
},
1122+
},
1123+
},
1124+
want: &Workload{
1125+
Spec: WorkloadSpec{
1126+
Source: &Source{
1127+
Image: "ubuntu:bionic",
1128+
Subpath: "/sys",
1129+
},
1130+
},
1131+
},
10371132
}, {
10381133
name: "git",
10391134
seed: &Workload{
@@ -1092,13 +1187,7 @@ func TestWorkload_Merge(t *testing.T) {
10921187
},
10931188
},
10941189
},
1095-
want: &Workload{
1096-
Spec: WorkloadSpec{
1097-
Source: &Source{
1098-
Subpath: "test-path",
1099-
},
1100-
},
1101-
},
1190+
want: &Workload{},
11021191
}, {
11031192
name: "env",
11041193
seed: &Workload{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
image: my-registry/default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
30+
source:
31+
image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69

pkg/commands/workload_apply_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -9046,6 +9046,76 @@ No source code is changed
90469046
To see logs: "tanzu apps workload tail my-workload --timestamp --since 1h"
90479047
To get status: "tanzu apps workload get my-workload"
90489048
9049+
`, localSource),
9050+
},
9051+
9052+
{
9053+
Name: "update from local source using lsp from file with image",
9054+
Skip: runtm.GOOS == "windows",
9055+
Args: []string{workloadName, flags.LocalPathFlagName, localSource, flags.FilePathFlagName, "./testdata/workload-lsp-image-non-subPath.yaml", flags.YesFlagName},
9056+
GivenObjects: []client.Object{
9057+
parent.
9058+
MetadataDie(func(d *diemetav1.ObjectMetaDie) {
9059+
d.Annotations(map[string]string{apis.LocalSourceProxyAnnotationName: ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69"})
9060+
d.Labels(map[string]string{apis.WorkloadTypeLabelName: "web"})
9061+
}).SpecDie(func(d *diecartov1alpha1.WorkloadSpecDie) {
9062+
d.Source(&cartov1alpha1.Source{
9063+
Image: ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69",
9064+
Subpath: "current-subpath",
9065+
})
9066+
}),
9067+
},
9068+
KubeConfigTransport: clitesting.NewFakeTransportFromResponse(respCreator(http.StatusOK, `{"statuscode": "200", "message": "any ignored message"}`, myWorkloadHeader)),
9069+
ExpectUpdates: []client.Object{
9070+
&cartov1alpha1.Workload{
9071+
ObjectMeta: metav1.ObjectMeta{
9072+
Namespace: defaultNamespace,
9073+
Name: workloadName,
9074+
Labels: map[string]string{
9075+
apis.WorkloadTypeLabelName: "web",
9076+
},
9077+
Annotations: map[string]string{
9078+
"local-source-proxy.apps.tanzu.vmware.com": ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69",
9079+
},
9080+
},
9081+
Spec: cartov1alpha1.WorkloadSpec{
9082+
Source: &cartov1alpha1.Source{
9083+
Image: ":default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69",
9084+
Subpath: "current-subpath",
9085+
},
9086+
Params: []cartov1alpha1.Param{
9087+
{
9088+
Name: "annotations",
9089+
Value: apiextensionsv1.JSON{Raw: []byte(`{"autoscaling.knative.dev/minScale":"2"}`)},
9090+
},
9091+
},
9092+
},
9093+
},
9094+
},
9095+
ExpectOutput: fmt.Sprintf(`
9096+
❗ 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).
9097+
9098+
Publishing source in "%s" to "local-source-proxy.tap-local-source-system.svc.cluster.local/source:default-my-workload"...
9099+
No source code is changed
9100+
9101+
🔎 Update workload:
9102+
...
9103+
8, 8 | apps.tanzu.vmware.com/workload-type: web
9104+
9, 9 | name: my-workload
9105+
10, 10 | namespace: default
9106+
11, 11 |spec:
9107+
12 + | params:
9108+
13 + | - name: annotations
9109+
14 + | value:
9110+
15 + | autoscaling.knative.dev/minScale: "2"
9111+
12, 16 | source:
9112+
13, 17 | image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
9113+
14, 18 | subPath: current-subpath
9114+
👍 Updated workload "my-workload"
9115+
9116+
To see logs: "tanzu apps workload tail my-workload --timestamp --since 1h"
9117+
To get status: "tanzu apps workload get my-workload"
9118+
90499119
`, localSource),
90509120
},
90519121
}

0 commit comments

Comments
 (0)