Skip to content

Commit 4a5648e

Browse files
fix(server): use server side dry run in case if it is server side apply (#546)
* fix: use server side dry run in case if it is server side apply Signed-off-by: pashakostohrys <[email protected]> * fix: use server side dry run in case if it is server side apply Signed-off-by: pashakostohrys <[email protected]> --------- Signed-off-by: pashakostohrys <[email protected]>
1 parent f15cf61 commit 4a5648e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

pkg/sync/sync_context.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -903,16 +903,24 @@ func (sc *syncContext) ensureCRDReady(name string) error {
903903
})
904904
}
905905

906-
func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (common.ResultCode, string) {
907-
dryRunStrategy := cmdutil.DryRunNone
908-
if dryRun {
909-
dryRunStrategy = cmdutil.DryRunClient
906+
func getDryRunStrategy(serverSideApply, dryRun bool) cmdutil.DryRunStrategy {
907+
if !dryRun {
908+
return cmdutil.DryRunNone
909+
}
910+
if serverSideApply {
911+
return cmdutil.DryRunServer
910912
}
913+
return cmdutil.DryRunClient
914+
}
915+
916+
func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (common.ResultCode, string) {
917+
serverSideApply := sc.serverSideApply || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
918+
919+
dryRunStrategy := getDryRunStrategy(serverSideApply, dryRun)
911920

912921
var err error
913922
var message string
914923
shouldReplace := sc.replace || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionReplace)
915-
serverSideApply := sc.serverSideApply || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
916924
if shouldReplace {
917925
if t.liveObj != nil {
918926
// Avoid using `kubectl replace` for CRDs since 'replace' might recreate resource and so delete all CRD instances.

pkg/sync/sync_context_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"k8s.io/kubectl/pkg/cmd/util"
89
"net/http"
910
"net/http/httptest"
1011
"reflect"
@@ -539,6 +540,25 @@ func (s *APIServerMock) newHttpServer(t *testing.T, apiFailuresCount int) *httpt
539540
return server
540541
}
541542

543+
func TestGetDryRunStrategy(t *testing.T) {
544+
t.Run("no dry run", func(t *testing.T) {
545+
strategy := getDryRunStrategy(false, false)
546+
assert.Equal(t, util.DryRunNone, strategy)
547+
})
548+
t.Run("no dry run with server side apply", func(t *testing.T) {
549+
strategy := getDryRunStrategy(true, false)
550+
assert.Equal(t, util.DryRunNone, strategy)
551+
})
552+
t.Run("dry run with server side apply", func(t *testing.T) {
553+
strategy := getDryRunStrategy(true, true)
554+
assert.Equal(t, util.DryRunServer, strategy)
555+
})
556+
t.Run("dry run with client side apply", func(t *testing.T) {
557+
strategy := getDryRunStrategy(false, true)
558+
assert.Equal(t, util.DryRunClient, strategy)
559+
})
560+
}
561+
542562
func TestServerResourcesRetry(t *testing.T) {
543563
type fixture struct {
544564
apiServerMock *APIServerMock

0 commit comments

Comments
 (0)