@@ -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+
1726type 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-
10072func (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 (_ 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