Skip to content

Commit

Permalink
fix(server): use server side dry run in case if it is server side app…
Browse files Browse the repository at this point in the history
…ly (#546)

* fix: use server side dry run in case if it is server side apply

Signed-off-by: pashakostohrys <pavel@codefresh.io>

* fix: use server side dry run in case if it is server side apply

Signed-off-by: pashakostohrys <pavel@codefresh.io>

---------

Signed-off-by: pashakostohrys <pavel@codefresh.io>
  • Loading branch information
pasha-codefresh authored Oct 31, 2023
1 parent f15cf61 commit 4a5648e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,16 +903,24 @@ func (sc *syncContext) ensureCRDReady(name string) error {
})
}

func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (common.ResultCode, string) {
dryRunStrategy := cmdutil.DryRunNone
if dryRun {
dryRunStrategy = cmdutil.DryRunClient
func getDryRunStrategy(serverSideApply, dryRun bool) cmdutil.DryRunStrategy {
if !dryRun {
return cmdutil.DryRunNone
}
if serverSideApply {
return cmdutil.DryRunServer
}
return cmdutil.DryRunClient
}

func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (common.ResultCode, string) {
serverSideApply := sc.serverSideApply || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)

dryRunStrategy := getDryRunStrategy(serverSideApply, dryRun)

var err error
var message string
shouldReplace := sc.replace || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionReplace)
serverSideApply := sc.serverSideApply || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
if shouldReplace {
if t.liveObj != nil {
// Avoid using `kubectl replace` for CRDs since 'replace' might recreate resource and so delete all CRD instances.
Expand Down
20 changes: 20 additions & 0 deletions pkg/sync/sync_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"k8s.io/kubectl/pkg/cmd/util"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -539,6 +540,25 @@ func (s *APIServerMock) newHttpServer(t *testing.T, apiFailuresCount int) *httpt
return server
}

func TestGetDryRunStrategy(t *testing.T) {
t.Run("no dry run", func(t *testing.T) {
strategy := getDryRunStrategy(false, false)
assert.Equal(t, util.DryRunNone, strategy)
})
t.Run("no dry run with server side apply", func(t *testing.T) {
strategy := getDryRunStrategy(true, false)
assert.Equal(t, util.DryRunNone, strategy)
})
t.Run("dry run with server side apply", func(t *testing.T) {
strategy := getDryRunStrategy(true, true)
assert.Equal(t, util.DryRunServer, strategy)
})
t.Run("dry run with client side apply", func(t *testing.T) {
strategy := getDryRunStrategy(false, true)
assert.Equal(t, util.DryRunClient, strategy)
})
}

func TestServerResourcesRetry(t *testing.T) {
type fixture struct {
apiServerMock *APIServerMock
Expand Down

0 comments on commit 4a5648e

Please sign in to comment.