@@ -14,16 +14,21 @@ import (
14
14
"github.com/argoproj/gitops-engine/pkg/utils/kube"
15
15
)
16
16
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
+
17
26
type MockResourceOps struct {
18
27
Commands map [string ]KubectlOutput
19
28
Events chan watch.Event
20
29
DynamicClient dynamic.Interface
21
30
22
- lastCommandPerResource map [kube.ResourceKey ]string
23
- lastValidate bool
24
- serverSideApply bool
25
- serverSideApplyManager string
26
- lastForce bool
31
+ commandsPerResource map [kube.ResourceKey ][]RegisteredCommand
27
32
28
33
recordLock sync.RWMutex
29
34
@@ -36,82 +41,59 @@ func (r *MockResourceOps) WithGetResourceFunc(getResourcefunc func(context.Conte
36
41
return r
37
42
}
38
43
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 {
46
45
r .recordLock .RLock ()
47
- validate := r .lastValidate
46
+ validate := r .lastCommand ( key ). Validate
48
47
r .recordLock .RUnlock ()
49
48
return validate
50
49
}
51
50
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 {
59
52
r .recordLock .Lock ()
60
- manager := r .serverSideApplyManager
53
+ manager := r .lastCommand ( key ). ServerSideApplyManager
61
54
r .recordLock .Unlock ()
62
55
return manager
63
56
}
64
57
65
- func (r * MockResourceOps ) GetLastServerSideApply () bool {
58
+ func (r * MockResourceOps ) GetLastServerSideApply (key kube. ResourceKey ) bool {
66
59
r .recordLock .RLock ()
67
- serverSideApply := r .serverSideApply
60
+ serverSideApply := r .lastCommand ( key ). ServerSideApply
68
61
r .recordLock .RUnlock ()
69
62
return serverSideApply
70
63
}
71
64
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 {
85
66
r .recordLock .RLock ()
86
- force := r .lastForce
67
+ force := r .lastCommand ( key ). Force
87
68
r .recordLock .RUnlock ()
88
69
return force
89
70
}
90
71
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
-
100
72
func (r * MockResourceOps ) GetLastResourceCommand (key kube.ResourceKey ) string {
101
73
r .recordLock .Lock ()
102
74
defer r .recordLock .Unlock ()
103
- if r .lastCommandPerResource == nil {
75
+ if r .commandsPerResource == nil {
104
76
return ""
105
77
}
106
- return r .lastCommandPerResource [ key ]
78
+ return r .lastCommand ( key ). Command
107
79
}
108
80
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
+ })
115
97
command , ok := r .Commands [obj .GetName ()]
116
98
if ! ok {
117
99
return "" , nil
@@ -120,35 +102,58 @@ func (r *MockResourceOps) ApplyResource(_ context.Context, obj *unstructured.Uns
120
102
return command .Output , command .Err
121
103
}
122
104
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
+ })
125
111
command , ok := r .Commands [obj .GetName ()]
126
- r .SetLastResourceCommand (kube .GetResourceKey (obj ), "replace" )
127
112
if ! ok {
128
113
return "" , nil
129
114
}
130
115
131
116
return command .Output , command .Err
132
117
}
133
118
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
+ })
136
124
command , ok := r .Commands [obj .GetName ()]
137
125
if ! ok {
138
126
return obj , nil
139
127
}
140
128
return obj , command .Err
141
129
}
142
130
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 (_ 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
+ })
145
137
command , ok := r .Commands [obj .GetName ()]
146
138
if ! ok {
147
139
return "" , nil
148
140
}
149
141
return command .Output , command .Err
150
142
}
151
143
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
+
152
157
/*func (r *MockResourceOps) ConvertToVersion(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error) {
153
158
if r.convertToVersionFunc != nil {
154
159
return (*r.convertToVersionFunc)(obj, group, version)
0 commit comments