Skip to content

Commit caa404a

Browse files
committed
Do not use --force with --dry-run
Signed-off-by: rafal-jan <[email protected]>
1 parent cebed7e commit caa404a

File tree

3 files changed

+85
-69
lines changed

3 files changed

+85
-69
lines changed

pkg/sync/sync_context.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,8 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
10861086
var err error
10871087
var message string
10881088
shouldReplace := sc.replace || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionReplace)
1089-
force := sc.force || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionForce)
1089+
// force is not required when running dry-run in client mode
1090+
shouldForce := !dryRun && (sc.force || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionForce))
10901091
serverSideApply := sc.shouldUseServerSideApply(t.targetObj, dryRun)
10911092
if shouldReplace {
10921093
if t.liveObj != nil {
@@ -1103,13 +1104,13 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
11031104
message = fmt.Sprintf("error when updating: %v", err.Error())
11041105
}
11051106
} else {
1106-
message, err = sc.resourceOps.ReplaceResource(context.TODO(), t.targetObj, dryRunStrategy, force)
1107+
message, err = sc.resourceOps.ReplaceResource(context.TODO(), t.targetObj, dryRunStrategy, shouldForce)
11071108
}
11081109
} else {
11091110
message, err = sc.resourceOps.CreateResource(context.TODO(), t.targetObj, dryRunStrategy, validate)
11101111
}
11111112
} else {
1112-
message, err = sc.resourceOps.ApplyResource(context.TODO(), t.targetObj, dryRunStrategy, force, validate, serverSideApply, sc.serverSideApplyManager)
1113+
message, err = sc.resourceOps.ApplyResource(context.TODO(), t.targetObj, dryRunStrategy, shouldForce, validate, serverSideApply, sc.serverSideApplyManager)
11131114
}
11141115
if err != nil {
11151116
return common.ResultCodeSyncFailed, err.Error()

pkg/sync/sync_context_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/client-go/rest"
2929
testcore "k8s.io/client-go/testing"
3030
"k8s.io/klog/v2/textlogger"
31+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
3132

3233
"github.com/argoproj/gitops-engine/pkg/diff"
3334
"github.com/argoproj/gitops-engine/pkg/health"
@@ -102,7 +103,7 @@ func TestSyncValidate(t *testing.T) {
102103

103104
// kubectl := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
104105
resourceOps, _ := syncCtx.resourceOps.(*kubetest.MockResourceOps)
105-
assert.False(t, resourceOps.GetLastValidate())
106+
assert.False(t, resourceOps.GetLastValidate(kube.GetResourceKey(pod)))
106107
}
107108

108109
func TestSyncNotPermittedNamespace(t *testing.T) {
@@ -749,7 +750,7 @@ func TestSyncOptionValidate(t *testing.T) {
749750

750751
// kubectl, _ := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
751752
resourceOps, _ := syncCtx.resourceOps.(*kubetest.MockResourceOps)
752-
assert.Equal(t, tt.want, resourceOps.GetLastValidate())
753+
assert.Equal(t, tt.want, resourceOps.GetLastValidate(kube.GetResourceKey(pod)))
753754
})
754755
}
755756
}
@@ -846,8 +847,8 @@ func TestSync_ServerSideApply(t *testing.T) {
846847
// kubectl, _ := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
847848
resourceOps, _ := syncCtx.resourceOps.(*kubetest.MockResourceOps)
848849
assert.Equal(t, tc.commandUsed, resourceOps.GetLastResourceCommand(kube.GetResourceKey(tc.target)))
849-
assert.Equal(t, tc.serverSideApply, resourceOps.GetLastServerSideApply())
850-
assert.Equal(t, tc.manager, resourceOps.GetLastServerSideApplyManager())
850+
assert.Equal(t, tc.serverSideApply, resourceOps.GetLastServerSideApply(kube.GetResourceKey(tc.target)))
851+
assert.Equal(t, tc.manager, resourceOps.GetLastServerSideApplyManager(kube.GetResourceKey(tc.target)))
851852
})
852853
}
853854
}
@@ -920,8 +921,17 @@ func TestSync_Force(t *testing.T) {
920921
syncCtx.Sync()
921922

922923
resourceOps, _ := syncCtx.resourceOps.(*kubetest.MockResourceOps)
924+
registeredCommands := resourceOps.RegisteredCommands(kube.GetResourceKey(tc.target))
925+
assert.Len(t, registeredCommands, 2)
926+
dryRunCommand := registeredCommands[0]
927+
command := registeredCommands[1]
923928
assert.Equal(t, tc.commandUsed, resourceOps.GetLastResourceCommand(kube.GetResourceKey(tc.target)))
924-
assert.Equal(t, tc.force, resourceOps.GetLastForce())
929+
930+
assert.Equal(t, false, dryRunCommand.Force)
931+
assert.Equal(t, cmdutil.DryRunClient, dryRunCommand.DryRunStrategy)
932+
933+
assert.Equal(t, tc.force, command.Force)
934+
assert.Equal(t, cmdutil.DryRunNone, command.DryRunStrategy)
925935
})
926936
}
927937
}

pkg/utils/kube/kubetest/mock_resource_operations.go

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ import (
1414
"github.com/argoproj/gitops-engine/pkg/utils/kube"
1515
)
1616

17+
type RegisteredCommand struct {
18+
Command string
19+
Validate bool
20+
ServerSideApply bool
21+
ServerSideApplyManager string
22+
Force bool
23+
DryRunStrategy cmdutil.DryRunStrategy
24+
}
25+
1726
type MockResourceOps struct {
1827
Commands map[string]KubectlOutput
1928
Events chan watch.Event
2029
DynamicClient dynamic.Interface
2130

22-
lastCommandPerResource map[kube.ResourceKey]string
23-
lastValidate bool
24-
serverSideApply bool
25-
serverSideApplyManager string
26-
lastForce bool
31+
commandsPerResource map[kube.ResourceKey][]RegisteredCommand
2732

2833
recordLock sync.RWMutex
2934

@@ -36,82 +41,59 @@ func (r *MockResourceOps) WithGetResourceFunc(getResourcefunc func(context.Conte
3641
return r
3742
}
3843

39-
func (r *MockResourceOps) SetLastValidate(validate bool) {
40-
r.recordLock.Lock()
41-
r.lastValidate = validate
42-
r.recordLock.Unlock()
43-
}
44-
45-
func (r *MockResourceOps) GetLastValidate() bool {
44+
func (r *MockResourceOps) GetLastValidate(key kube.ResourceKey) bool {
4645
r.recordLock.RLock()
47-
validate := r.lastValidate
46+
validate := r.lastCommand(key).Validate
4847
r.recordLock.RUnlock()
4948
return validate
5049
}
5150

52-
func (r *MockResourceOps) SetLastServerSideApply(serverSideApply bool) {
53-
r.recordLock.Lock()
54-
r.serverSideApply = serverSideApply
55-
r.recordLock.Unlock()
56-
}
57-
58-
func (r *MockResourceOps) GetLastServerSideApplyManager() string {
51+
func (r *MockResourceOps) GetLastServerSideApplyManager(key kube.ResourceKey) string {
5952
r.recordLock.Lock()
60-
manager := r.serverSideApplyManager
53+
manager := r.lastCommand(key).ServerSideApplyManager
6154
r.recordLock.Unlock()
6255
return manager
6356
}
6457

65-
func (r *MockResourceOps) GetLastServerSideApply() bool {
58+
func (r *MockResourceOps) GetLastServerSideApply(key kube.ResourceKey) bool {
6659
r.recordLock.RLock()
67-
serverSideApply := r.serverSideApply
60+
serverSideApply := r.lastCommand(key).ServerSideApply
6861
r.recordLock.RUnlock()
6962
return serverSideApply
7063
}
7164

72-
func (r *MockResourceOps) SetLastServerSideApplyManager(manager string) {
73-
r.recordLock.Lock()
74-
r.serverSideApplyManager = manager
75-
r.recordLock.Unlock()
76-
}
77-
78-
func (r *MockResourceOps) SetLastForce(force bool) {
79-
r.recordLock.Lock()
80-
r.lastForce = force
81-
r.recordLock.Unlock()
82-
}
83-
84-
func (r *MockResourceOps) GetLastForce() bool {
65+
func (r *MockResourceOps) GetLastForce(key kube.ResourceKey) bool {
8566
r.recordLock.RLock()
86-
force := r.lastForce
67+
force := r.lastCommand(key).Force
8768
r.recordLock.RUnlock()
8869
return force
8970
}
9071

91-
func (r *MockResourceOps) SetLastResourceCommand(key kube.ResourceKey, cmd string) {
92-
r.recordLock.Lock()
93-
if r.lastCommandPerResource == nil {
94-
r.lastCommandPerResource = map[kube.ResourceKey]string{}
95-
}
96-
r.lastCommandPerResource[key] = cmd
97-
r.recordLock.Unlock()
98-
}
99-
10072
func (r *MockResourceOps) GetLastResourceCommand(key kube.ResourceKey) string {
10173
r.recordLock.Lock()
10274
defer r.recordLock.Unlock()
103-
if r.lastCommandPerResource == nil {
75+
if r.commandsPerResource == nil {
10476
return ""
10577
}
106-
return r.lastCommandPerResource[key]
78+
return r.lastCommand(key).Command
10779
}
10880

109-
func (r *MockResourceOps) ApplyResource(_ context.Context, obj *unstructured.Unstructured, _ cmdutil.DryRunStrategy, force bool, validate bool, serverSideApply bool, manager string) (string, error) {
110-
r.SetLastValidate(validate)
111-
r.SetLastServerSideApply(serverSideApply)
112-
r.SetLastServerSideApplyManager(manager)
113-
r.SetLastForce(force)
114-
r.SetLastResourceCommand(kube.GetResourceKey(obj), "apply")
81+
func (r *MockResourceOps) RegisteredCommands(key kube.ResourceKey) []RegisteredCommand {
82+
r.recordLock.RLock()
83+
registeredCommands := r.commandsPerResource[key]
84+
r.recordLock.RUnlock()
85+
return registeredCommands
86+
}
87+
88+
func (r *MockResourceOps) ApplyResource(_ context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force bool, validate bool, serverSideApply bool, manager string) (string, error) {
89+
r.registerCommand(kube.GetResourceKey(obj), RegisteredCommand{
90+
Command: "apply",
91+
Validate: validate,
92+
ServerSideApply: serverSideApply,
93+
ServerSideApplyManager: manager,
94+
Force: force,
95+
DryRunStrategy: dryRunStrategy,
96+
})
11597
command, ok := r.Commands[obj.GetName()]
11698
if !ok {
11799
return "", nil
@@ -120,35 +102,58 @@ func (r *MockResourceOps) ApplyResource(_ context.Context, obj *unstructured.Uns
120102
return command.Output, command.Err
121103
}
122104

123-
func (r *MockResourceOps) ReplaceResource(_ context.Context, obj *unstructured.Unstructured, _ cmdutil.DryRunStrategy, force bool) (string, error) {
124-
r.SetLastForce(force)
105+
func (r *MockResourceOps) ReplaceResource(_ context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force bool) (string, error) {
106+
r.registerCommand(kube.GetResourceKey(obj), RegisteredCommand{
107+
Command: "replace",
108+
Force: force,
109+
DryRunStrategy: dryRunStrategy,
110+
})
125111
command, ok := r.Commands[obj.GetName()]
126-
r.SetLastResourceCommand(kube.GetResourceKey(obj), "replace")
127112
if !ok {
128113
return "", nil
129114
}
130115

131116
return command.Output, command.Err
132117
}
133118

134-
func (r *MockResourceOps) UpdateResource(_ context.Context, obj *unstructured.Unstructured, _ cmdutil.DryRunStrategy) (*unstructured.Unstructured, error) {
135-
r.SetLastResourceCommand(kube.GetResourceKey(obj), "update")
119+
func (r *MockResourceOps) UpdateResource(_ context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy) (*unstructured.Unstructured, error) {
120+
r.registerCommand(kube.GetResourceKey(obj), RegisteredCommand{
121+
Command: "update",
122+
DryRunStrategy: dryRunStrategy,
123+
})
136124
command, ok := r.Commands[obj.GetName()]
137125
if !ok {
138126
return obj, nil
139127
}
140128
return obj, command.Err
141129
}
142130

143-
func (r *MockResourceOps) CreateResource(_ context.Context, obj *unstructured.Unstructured, _ cmdutil.DryRunStrategy, _ bool) (string, error) {
144-
r.SetLastResourceCommand(kube.GetResourceKey(obj), "create")
131+
func (r *MockResourceOps) CreateResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, validate bool) (string, error) {
132+
r.registerCommand(kube.GetResourceKey(obj), RegisteredCommand{
133+
Command: "create",
134+
Validate: validate,
135+
DryRunStrategy: dryRunStrategy,
136+
})
145137
command, ok := r.Commands[obj.GetName()]
146138
if !ok {
147139
return "", nil
148140
}
149141
return command.Output, command.Err
150142
}
151143

144+
func (r *MockResourceOps) registerCommand(key kube.ResourceKey, cmd RegisteredCommand) {
145+
r.recordLock.Lock()
146+
if r.commandsPerResource == nil {
147+
r.commandsPerResource = map[kube.ResourceKey][]RegisteredCommand{}
148+
}
149+
r.commandsPerResource[key] = append(r.commandsPerResource[key], cmd)
150+
r.recordLock.Unlock()
151+
}
152+
153+
func (r *MockResourceOps) lastCommand(key kube.ResourceKey) RegisteredCommand {
154+
return r.commandsPerResource[key][len(r.commandsPerResource[key])-1]
155+
}
156+
152157
/*func (r *MockResourceOps) ConvertToVersion(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error) {
153158
if r.convertToVersionFunc != nil {
154159
return (*r.convertToVersionFunc)(obj, group, version)

0 commit comments

Comments
 (0)