Skip to content

Commit 1486290

Browse files
t-kikucpipecd-bot
authored andcommitted
Lambda: clone manifests not to modify original manifests (#5308)
Signed-off-by: t-kikuc <[email protected]> Signed-off-by: pipecd-bot <[email protected]>
1 parent 6f442d6 commit 1486290

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

pkg/app/piped/driftdetector/lambda/detector.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,
207207
}
208208
d.logger.Info(fmt.Sprintf("application %s has a live function manifest", app.Id))
209209

210-
ignoreAndSortParameters(&headManifest.Spec)
210+
clonedSpec := ignoreAndSortParameters(headManifest.Spec)
211+
head := provider.FunctionManifest{
212+
Kind: headManifest.Kind,
213+
APIVersion: headManifest.APIVersion,
214+
Spec: clonedSpec,
215+
}
211216

212217
// WithIgnoreAddingMapKeys option ignores all of followings:
213218
// - default value of Architecture
@@ -216,7 +221,7 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,
216221
// - tags added in live states, including pipecd managed tags
217222
result, err := provider.Diff(
218223
liveManifest,
219-
headManifest,
224+
head,
220225
diff.WithEquateEmpty(),
221226
diff.WithIgnoreAddingMapKeys(),
222227
diff.WithCompareNumberAndNumericString(),
@@ -238,22 +243,31 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,
238243
// sorts: (Lambda sorts them in liveSpec)
239244
// - Architectures in headSpec
240245
// - SubnetIDs in headSpec
241-
func ignoreAndSortParameters(headSpec *provider.FunctionManifestSpec) {
246+
func ignoreAndSortParameters(headSpec provider.FunctionManifestSpec) provider.FunctionManifestSpec {
247+
cloneSpec := headSpec
242248
// We cannot compare SourceCode and S3 packaging because live states do not have them.
243-
headSpec.SourceCode = provider.SourceCode{}
244-
headSpec.S3Bucket = ""
245-
headSpec.S3Key = ""
246-
headSpec.S3ObjectVersion = ""
249+
cloneSpec.SourceCode = provider.SourceCode{}
250+
cloneSpec.S3Bucket = ""
251+
cloneSpec.S3Key = ""
252+
cloneSpec.S3ObjectVersion = ""
247253

248254
// Architectures, Environments, SubnetIDs, and Tags are sorted in live states.
249255
if len(headSpec.Architectures) > 1 {
250-
sort.Slice(headSpec.Architectures, func(i, j int) bool {
251-
return strings.Compare(headSpec.Architectures[i].Name, headSpec.Architectures[j].Name) < 0
256+
cloneSpec.Architectures = slices.Clone(headSpec.Architectures)
257+
sort.Slice(cloneSpec.Architectures, func(i, j int) bool {
258+
return strings.Compare(cloneSpec.Architectures[i].Name, cloneSpec.Architectures[j].Name) < 0
252259
})
253260
}
254261
if headSpec.VPCConfig != nil && len(headSpec.VPCConfig.SubnetIDs) > 1 {
255-
slices.Sort(headSpec.VPCConfig.SubnetIDs)
262+
cloneSubnets := slices.Clone(headSpec.VPCConfig.SubnetIDs)
263+
slices.Sort(cloneSubnets)
264+
cloneSpec.VPCConfig = &provider.VPCConfig{
265+
SecurityGroupIDs: headSpec.VPCConfig.SecurityGroupIDs,
266+
SubnetIDs: cloneSubnets,
267+
}
256268
}
269+
270+
return cloneSpec
257271
}
258272

259273
func (d *detector) loadHeadFunctionManifest(app *model.Application, repo git.Repo, headCommit git.Commit) (provider.FunctionManifest, error) {

pkg/app/piped/driftdetector/lambda/detector_test.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package lambda
1616

1717
import (
18-
"fmt"
1918
"testing"
2019

2120
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
@@ -124,8 +123,7 @@ func TestIgnoreAndSortParameters(t *testing.T) {
124123
expectDiff: false,
125124
},
126125
{
127-
128-
name: "Not ignore added fields in headstate",
126+
name: "Not ignore added fields in headspec",
129127
liveSpec: provider.FunctionManifestSpec{
130128
Tags: map[string]string{
131129
"key1": "value1",
@@ -149,17 +147,50 @@ func TestIgnoreAndSortParameters(t *testing.T) {
149147
tc := tc
150148
t.Run(tc.name, func(t *testing.T) {
151149
t.Parallel()
152-
ignoreAndSortParameters(&tc.headSpec)
150+
ignored := ignoreAndSortParameters(tc.headSpec)
153151
result, err := provider.Diff(
154152
provider.FunctionManifest{Spec: tc.liveSpec},
155-
provider.FunctionManifest{Spec: tc.headSpec},
153+
provider.FunctionManifest{Spec: ignored},
156154
diff.WithEquateEmpty(),
157155
diff.WithIgnoreAddingMapKeys(),
158156
diff.WithCompareNumberAndNumericString(),
159157
)
160158
assert.NoError(t, err)
161-
fmt.Println(result.Render(provider.DiffRenderOptions{}))
162159
assert.Equal(t, tc.expectDiff, result.Diff.HasDiff())
163160
})
164161
}
165162
}
163+
164+
func TestIgnoreAndSortParametersNotChangeOriginal(t *testing.T) {
165+
t.Parallel()
166+
167+
headSpec := provider.FunctionManifestSpec{
168+
S3Bucket: "test-bucket",
169+
SourceCode: provider.SourceCode{
170+
Git: "https://test-repo.git",
171+
Ref: "test-ref",
172+
Path: "test-path",
173+
},
174+
Architectures: []provider.Architecture{
175+
{Name: string(types.ArchitectureX8664)},
176+
{Name: string(types.ArchitectureArm64)},
177+
},
178+
VPCConfig: &provider.VPCConfig{
179+
SubnetIDs: []string{"subnet-2", "subnet-1"},
180+
},
181+
}
182+
183+
_ = ignoreAndSortParameters(headSpec)
184+
185+
assert.Equal(t, "test-bucket", headSpec.S3Bucket)
186+
assert.Equal(t, provider.SourceCode{
187+
Git: "https://test-repo.git",
188+
Ref: "test-ref",
189+
Path: "test-path",
190+
}, headSpec.SourceCode)
191+
assert.Equal(t, []provider.Architecture{
192+
{Name: string(types.ArchitectureX8664)},
193+
{Name: string(types.ArchitectureArm64)}},
194+
headSpec.Architectures)
195+
assert.Equal(t, []string{"subnet-2", "subnet-1"}, headSpec.VPCConfig.SubnetIDs)
196+
}

0 commit comments

Comments
 (0)