Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update subpath from DeepCopy instead of pointer #580

Merged
merged 7 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions pkg/apis/cartographer/v1alpha1/workload_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,28 @@ func (w *WorkloadSpec) Merge(updates *WorkloadSpec) {
for _, p := range updates.Params {
w.MergeParams(p.Name, p.Value)
}
sp := ""
if w.Source != nil {
sp = w.Source.Subpath
}
if updates.Image != "" {
w.MergeImage(updates.Image)
}
if s := updates.Source; s != nil {
if updates.Source != nil && (updates.Source.Git != nil || updates.Source.Image != "") && sp != "" {
if w.Source == nil {
w.Source = &Source{}
}
w.Source.Subpath = sp
}
if updates.Source != nil {
s := updates.Source.DeepCopy()
if s.Git != nil {
w.MergeGit(*s.Git)
}
if s.Image != "" {
w.MergeSourceImage(s.Image)
}
if s.Subpath != "" {
if s.Subpath != "" && w.Source != nil && (w.Source.Git != nil || w.Source.Image != "") {
w.MergeSubPath(s.Subpath)
}
}
Expand Down Expand Up @@ -326,7 +337,7 @@ func (w *WorkloadSpec) ResetSource() {
}

func (w *WorkloadSpec) MergeGit(git GitSource) {
stash := w.Source
stash := w.Source.DeepCopy()
image := w.Image
w.ResetSource()

Expand All @@ -341,35 +352,30 @@ func (w *WorkloadSpec) MergeGit(git GitSource) {
w.Source = &Source{
Git: &git,
}
if stash != nil && stash.Git != nil {
if stash != nil && stash.Subpath != "" {
w.Source.Subpath = stash.Subpath
}
}
}

func (w *WorkloadSpec) MergeSourceImage(image string) {
stash := w.Source
w.ResetSource()

w.Source = &Source{
Image: image,
}
if stash != nil && stash.Image != "" {
w.Source.Subpath = stash.Subpath
src := &Source{Image: image}
if w.Source != nil {
src.Subpath = w.Source.Subpath
}
w.ResetSource()
w.Source = src
}

func (w *WorkloadSpec) MergeSubPath(subPath string) {
if w.Source == nil {
w.Source = &Source{}
}

w.Source.Subpath = subPath
}

func (w *WorkloadSpec) MergeImage(image string) {
w.ResetSource()

w.Image = image
}

Expand Down
179 changes: 129 additions & 50 deletions pkg/apis/cartographer/v1alpha1/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,134 @@ func TestWorkload_Merge(t *testing.T) {
Image: "ubuntu:bionic",
},
},
}, {
name: "image without source",
seed: &Workload{},
update: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
},
},
want: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
},
},
}, {
name: "image with subpath",
seed: &Workload{
Spec: WorkloadSpec{
Image: "alpine:latest",
Source: &Source{
Subpath: "/sys",
},
},
},
update: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
},
},
want: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
},
},
}, {
name: "image with sources nil",
seed: &Workload{
Spec: WorkloadSpec{
Image: "alpine:latest",
},
},
update: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
Source: &Source{Subpath: "my-subpath"},
},
},
want: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
},
},
}, {
name: "workload with multiple sources and subpath",
seed: &Workload{
Spec: WorkloadSpec{
Image: "alpine:latest",
},
},
update: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "my-subpath",
},
},
},
want: &Workload{
Spec: WorkloadSpec{
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "my-subpath",
},
},
},
}, {
name: "image update sources source image and subpath",
seed: &Workload{
Spec: WorkloadSpec{
Image: "alpine:latest",
Source: &Source{
Subpath: "new-subpath",
},
},
},
update: &Workload{
Spec: WorkloadSpec{
Image: "ubuntu:bionic",
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "my-subpath",
},
},
},
want: &Workload{
Spec: WorkloadSpec{
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "my-subpath",
},
},
},
}, {
name: "local source image with subpath update source image and subpath",
seed: &Workload{
Spec: WorkloadSpec{
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "/opt",
},
},
},
update: &Workload{
Spec: WorkloadSpec{
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "/sys",
},
},
},
want: &Workload{
Spec: WorkloadSpec{
Source: &Source{
Image: "ubuntu:bionic",
Subpath: "/sys",
},
},
},
}, {
name: "git",
seed: &Workload{
Expand Down Expand Up @@ -1059,13 +1187,7 @@ func TestWorkload_Merge(t *testing.T) {
},
},
},
want: &Workload{
Spec: WorkloadSpec{
Source: &Source{
Subpath: "test-path",
},
},
},
want: &Workload{},
}, {
name: "env",
seed: &Workload{
Expand Down Expand Up @@ -1767,30 +1889,6 @@ func TestWorkloadSpec_MergeGit(t *testing.T) {
Subpath: "my-subpath",
},
},
}, {
name: "update to git source deleting subpath",
odinnordico marked this conversation as resolved.
Show resolved Hide resolved
seed: &WorkloadSpec{
Source: &Source{
Image: "my-registry.nip.io/my-folder/my-image:latest@sha:my-sha1234567890",
Subpath: "my-subpath",
},
},
git: GitSource{
URL: "[email protected]:example/repo.git",
Ref: GitRef{
Branch: "main",
},
},
want: &WorkloadSpec{
Source: &Source{
Git: &GitSource{
URL: "[email protected]:example/repo.git",
Ref: GitRef{
Branch: "main",
},
},
},
},
}, {
name: "delete source when setting repo to empty string",
seed: &WorkloadSpec{
Expand Down Expand Up @@ -1884,25 +1982,6 @@ func TestWorkloadSpec_MergeSourceImage(t *testing.T) {
Subpath: "my-subpath",
},
},
}, {
name: "update deleting subpath",
seed: &WorkloadSpec{
Source: &Source{
Git: &GitSource{
URL: "[email protected]:example/repo.git",
Ref: GitRef{
Branch: "main",
},
},
Subpath: "my-subpath",
},
},
sourceImage: "my-registry.nip.io/my-folder/my-image:latest@sha:my-sha1234567890",
want: &WorkloadSpec{
Source: &Source{
Image: "my-registry.nip.io/my-folder/my-image:latest@sha:my-sha1234567890",
},
},
}}

for _, test := range tests {
Expand Down
31 changes: 31 additions & 0 deletions pkg/commands/testdata/workload-lsp-image-non-subPath.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: carto.run/v1alpha1
kind: Workload
metadata:
annotations:
local-source-proxy.apps.tanzu.vmware.com: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
labels:
apps.tanzu.vmware.com/workload-type: web
name: my-workload
namespace: default
spec:
params:
- name: annotations
value:
autoscaling.knative.dev/minScale: "2"
image: my-registry/default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
source:
image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
30 changes: 30 additions & 0 deletions pkg/commands/testdata/workload-lsp-non-subPath.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2023 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: carto.run/v1alpha1
kind: Workload
metadata:
annotations:
local-source-proxy.apps.tanzu.vmware.com: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
labels:
apps.tanzu.vmware.com/workload-type: web
name: my-workload
namespace: default
spec:
params:
- name: annotations
value:
autoscaling.knative.dev/minScale: "2"
source:
image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
31 changes: 31 additions & 0 deletions pkg/commands/testdata/workload-lsp-subPath.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: carto.run/v1alpha1
kind: Workload
metadata:
annotations:
local-source-proxy.apps.tanzu.vmware.com: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
labels:
apps.tanzu.vmware.com/workload-type: web
name: my-workload
namespace: default
spec:
params:
- name: annotations
value:
autoscaling.knative.dev/minScale: "2"
source:
image: :default-my-workload@sha256:978be33a7f0cbe89bf48fbb438846047a28e1298d6d10d0de2d64bdc102a9e69
subPath: new-subpath
Loading