Skip to content

Commit d65e9d9

Browse files
pasha-codefreshpasha
andauthored
feat: Enable SkipDryRunOnMissingResource sync option on Application level (#712)
* fix: go mod tidy is not working due to k8s.io/externaljwt dependency Signed-off-by: pasha <pasha.k@fyxt.com> * feat: Enable SkipDryRunOnMissingResource sync option on Application level Signed-off-by: pasha <pasha.k@fyxt.com> * feat: Enable SkipDryRunOnMissingResource sync option on Application level * feat: add support for skipping dry run on missing resources in sync context - Introduced a new option to skip dry run verification for missing resources at the application level. - Updated the sync context to include a flag for this feature. - Enhanced tests to cover scenarios where the skip dry run annotation is applied to all resources. --------- Signed-off-by: pasha <pasha.k@fyxt.com> Co-authored-by: pasha <pasha.k@fyxt.com>
1 parent 5f90e7b commit d65e9d9

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

pkg/sync/sync_context.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ func WithSkipDryRun(skipDryRun bool) SyncOpt {
194194
}
195195
}
196196

197+
func WithSkipDryRunOnMissingResource(skipDryRunOnMissingResource bool) SyncOpt {
198+
return func(ctx *syncContext) {
199+
ctx.skipDryRunOnMissingResource = skipDryRunOnMissingResource
200+
}
201+
}
202+
197203
func WithServerSideApply(serverSideApply bool) SyncOpt {
198204
return func(ctx *syncContext) {
199205
ctx.serverSideApply = serverSideApply
@@ -340,19 +346,20 @@ type syncContext struct {
340346
resourceOps kubeutil.ResourceOperations
341347
namespace string
342348

343-
dryRun bool
344-
skipDryRun bool
345-
force bool
346-
validate bool
347-
skipHooks bool
348-
resourcesFilter func(key kubeutil.ResourceKey, target *unstructured.Unstructured, live *unstructured.Unstructured) bool
349-
prune bool
350-
replace bool
351-
serverSideApply bool
352-
serverSideApplyManager string
353-
pruneLast bool
354-
prunePropagationPolicy *metav1.DeletionPropagation
355-
pruneConfirmed bool
349+
dryRun bool
350+
skipDryRun bool
351+
skipDryRunOnMissingResource bool
352+
force bool
353+
validate bool
354+
skipHooks bool
355+
resourcesFilter func(key kubeutil.ResourceKey, target *unstructured.Unstructured, live *unstructured.Unstructured) bool
356+
prune bool
357+
replace bool
358+
serverSideApply bool
359+
serverSideApplyManager string
360+
pruneLast bool
361+
prunePropagationPolicy *metav1.DeletionPropagation
362+
pruneConfirmed bool
356363

357364
syncRes map[string]common.ResourceSyncResult
358365
startedAt time.Time
@@ -818,11 +825,18 @@ func (sc *syncContext) getSyncTasks() (_ syncTasks, successful bool) {
818825
}
819826
}
820827

828+
shouldSkipDryRunOnMissingResource := func() bool {
829+
// skip dry run on missing resource error for all application resources
830+
if sc.skipDryRunOnMissingResource {
831+
return true
832+
}
833+
return (task.targetObj != nil && resourceutil.HasAnnotationOption(task.targetObj, common.AnnotationSyncOptions, common.SyncOptionSkipDryRunOnMissingResource)) ||
834+
sc.hasCRDOfGroupKind(task.group(), task.kind())
835+
}
836+
821837
if err != nil {
822838
switch {
823-
case apierrors.IsNotFound(err) &&
824-
((task.targetObj != nil && resourceutil.HasAnnotationOption(task.targetObj, common.AnnotationSyncOptions, common.SyncOptionSkipDryRunOnMissingResource)) ||
825-
sc.hasCRDOfGroupKind(task.group(), task.kind())):
839+
case apierrors.IsNotFound(err) && shouldSkipDryRunOnMissingResource():
826840
// Special case for custom resources: if CRD is not yet known by the K8s API server,
827841
// and the CRD is part of this sync or the resource is annotated with SkipDryRunOnMissingResource=true,
828842
// then skip verification during `kubectl apply --dry-run` since we expect the CRD

pkg/sync/sync_context_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ func TestSyncCreateInSortedOrder(t *testing.T) {
148148

149149
func TestSyncCustomResources(t *testing.T) {
150150
type fields struct {
151-
skipDryRunAnnotationPresent bool
152-
crdAlreadyPresent bool
153-
crdInSameSync bool
151+
skipDryRunAnnotationPresent bool
152+
skipDryRunAnnotationPresentForAllResources bool
153+
crdAlreadyPresent bool
154+
crdInSameSync bool
154155
}
155156

156157
tests := []struct {
@@ -174,6 +175,9 @@ func TestSyncCustomResources(t *testing.T) {
174175
{"unknown crd, skip dry run annotated", fields{
175176
skipDryRunAnnotationPresent: true, crdAlreadyPresent: false, crdInSameSync: false,
176177
}, false, true},
178+
{"unknown crd, skip dry run annotated on app level", fields{
179+
skipDryRunAnnotationPresentForAllResources: true, crdAlreadyPresent: false, crdInSameSync: false,
180+
}, false, true},
177181
}
178182
for _, tt := range tests {
179183
t.Run(tt.name, func(t *testing.T) {
@@ -211,6 +215,10 @@ func TestSyncCustomResources(t *testing.T) {
211215
cr.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "SkipDryRunOnMissingResource=true"})
212216
}
213217

218+
if tt.fields.skipDryRunAnnotationPresentForAllResources {
219+
syncCtx.skipDryRunOnMissingResource = true
220+
}
221+
214222
resources := []*unstructured.Unstructured{cr}
215223
if tt.fields.crdInSameSync {
216224
resources = append(resources, testingutils.NewCRD())
@@ -2151,6 +2159,7 @@ func BenchmarkSync(b *testing.B) {
21512159
maxContainers := 10
21522160
for i := 0; i < b.N; i++ {
21532161
b.StopTimer()
2162+
21542163
containerCount := min(i+1, maxContainers)
21552164

21562165
containerStr := strings.Repeat(container+",", containerCount)

0 commit comments

Comments
 (0)