@@ -13,16 +13,21 @@ import (
1313 cmdutil "k8s.io/kubectl/pkg/cmd/util"
1414)
1515
16+ type RegisteredCommand struct {
17+ Command string
18+ Validate bool
19+ ServerSideApply bool
20+ ServerSideApplyManager string
21+ Force bool
22+ DryRunStrategy cmdutil.DryRunStrategy
23+ }
24+
1625type MockResourceOps struct {
1726 Commands map [string ]KubectlOutput
1827 Events chan watch.Event
1928 DynamicClient dynamic.Interface
2029
21- lastCommandPerResource map [kube.ResourceKey ]string
22- lastValidate bool
23- serverSideApply bool
24- serverSideApplyManager string
25- lastForce bool
30+ commandsPerResource map [kube.ResourceKey ][]RegisteredCommand
2631
2732 recordLock sync.RWMutex
2833
@@ -35,82 +40,59 @@ func (r *MockResourceOps) WithGetResourceFunc(getResourcefunc func(context.Conte
3540 return r
3641}
3742
38- func (r * MockResourceOps ) SetLastValidate (validate bool ) {
39- r .recordLock .Lock ()
40- r .lastValidate = validate
41- r .recordLock .Unlock ()
42- }
43-
44- func (r * MockResourceOps ) GetLastValidate () bool {
43+ func (r * MockResourceOps ) GetLastValidate (key kube.ResourceKey ) bool {
4544 r .recordLock .RLock ()
46- validate := r .lastValidate
45+ validate := r .lastCommand ( key ). Validate
4746 r .recordLock .RUnlock ()
4847 return validate
4948}
5049
51- func (r * MockResourceOps ) SetLastServerSideApply (serverSideApply bool ) {
52- r .recordLock .Lock ()
53- r .serverSideApply = serverSideApply
54- r .recordLock .Unlock ()
55- }
56-
57- func (r * MockResourceOps ) GetLastServerSideApplyManager () string {
50+ func (r * MockResourceOps ) GetLastServerSideApplyManager (key kube.ResourceKey ) string {
5851 r .recordLock .Lock ()
59- manager := r .serverSideApplyManager
52+ manager := r .lastCommand ( key ). ServerSideApplyManager
6053 r .recordLock .Unlock ()
6154 return manager
6255}
6356
64- func (r * MockResourceOps ) GetLastServerSideApply () bool {
57+ func (r * MockResourceOps ) GetLastServerSideApply (key kube. ResourceKey ) bool {
6558 r .recordLock .RLock ()
66- serverSideApply := r .serverSideApply
59+ serverSideApply := r .lastCommand ( key ). ServerSideApply
6760 r .recordLock .RUnlock ()
6861 return serverSideApply
6962}
7063
71- func (r * MockResourceOps ) SetLastServerSideApplyManager (manager string ) {
72- r .recordLock .Lock ()
73- r .serverSideApplyManager = manager
74- r .recordLock .Unlock ()
75- }
76-
77- func (r * MockResourceOps ) SetLastForce (force bool ) {
78- r .recordLock .Lock ()
79- r .lastForce = force
80- r .recordLock .Unlock ()
81- }
82-
83- func (r * MockResourceOps ) GetLastForce () bool {
64+ func (r * MockResourceOps ) GetLastForce (key kube.ResourceKey ) bool {
8465 r .recordLock .RLock ()
85- force := r .lastForce
66+ force := r .lastCommand ( key ). Force
8667 r .recordLock .RUnlock ()
8768 return force
8869}
8970
90- func (r * MockResourceOps ) SetLastResourceCommand (key kube.ResourceKey , cmd string ) {
91- r .recordLock .Lock ()
92- if r .lastCommandPerResource == nil {
93- r .lastCommandPerResource = map [kube.ResourceKey ]string {}
94- }
95- r .lastCommandPerResource [key ] = cmd
96- r .recordLock .Unlock ()
97- }
98-
9971func (r * MockResourceOps ) GetLastResourceCommand (key kube.ResourceKey ) string {
10072 r .recordLock .Lock ()
10173 defer r .recordLock .Unlock ()
102- if r .lastCommandPerResource == nil {
74+ if r .commandsPerResource == nil {
10375 return ""
10476 }
105- return r .lastCommandPerResource [key ]
77+ return r .lastCommand (key ).Command
78+ }
79+
80+ func (r * MockResourceOps ) RegisteredCommands (key kube.ResourceKey ) []RegisteredCommand {
81+ r .recordLock .RLock ()
82+ registeredCommands := r .commandsPerResource [key ]
83+ r .recordLock .RUnlock ()
84+ return registeredCommands
10685}
10786
10887func (r * MockResourceOps ) ApplyResource (ctx context.Context , obj * unstructured.Unstructured , dryRunStrategy cmdutil.DryRunStrategy , force , validate , serverSideApply bool , manager string , serverSideDiff bool ) (string , error ) {
109- r .SetLastValidate (validate )
110- r .SetLastServerSideApply (serverSideApply )
111- r .SetLastServerSideApplyManager (manager )
112- r .SetLastForce (force )
113- r .SetLastResourceCommand (kube .GetResourceKey (obj ), "apply" )
88+ r .registerCommand (kube .GetResourceKey (obj ), RegisteredCommand {
89+ Command : "apply" ,
90+ Validate : validate ,
91+ ServerSideApply : serverSideApply ,
92+ ServerSideApplyManager : manager ,
93+ Force : force ,
94+ DryRunStrategy : dryRunStrategy ,
95+ })
11496 command , ok := r .Commands [obj .GetName ()]
11597 if ! ok {
11698 return "" , nil
@@ -120,9 +102,12 @@ func (r *MockResourceOps) ApplyResource(ctx context.Context, obj *unstructured.U
120102}
121103
122104func (r * MockResourceOps ) ReplaceResource (ctx context.Context , obj * unstructured.Unstructured , dryRunStrategy cmdutil.DryRunStrategy , force bool ) (string , error ) {
123- r .SetLastForce (force )
105+ r .registerCommand (kube .GetResourceKey (obj ), RegisteredCommand {
106+ Command : "replace" ,
107+ Force : force ,
108+ DryRunStrategy : dryRunStrategy ,
109+ })
124110 command , ok := r .Commands [obj .GetName ()]
125- r .SetLastResourceCommand (kube .GetResourceKey (obj ), "replace" )
126111 if ! ok {
127112 return "" , nil
128113 }
@@ -131,7 +116,10 @@ func (r *MockResourceOps) ReplaceResource(ctx context.Context, obj *unstructured
131116}
132117
133118func (r * MockResourceOps ) UpdateResource (ctx context.Context , obj * unstructured.Unstructured , dryRunStrategy cmdutil.DryRunStrategy ) (* unstructured.Unstructured , error ) {
134- r .SetLastResourceCommand (kube .GetResourceKey (obj ), "update" )
119+ r .registerCommand (kube .GetResourceKey (obj ), RegisteredCommand {
120+ Command : "update" ,
121+ DryRunStrategy : dryRunStrategy ,
122+ })
135123 command , ok := r .Commands [obj .GetName ()]
136124 if ! ok {
137125 return obj , nil
@@ -141,15 +129,31 @@ func (r *MockResourceOps) UpdateResource(ctx context.Context, obj *unstructured.
141129}
142130
143131func (r * MockResourceOps ) CreateResource (ctx context.Context , obj * unstructured.Unstructured , dryRunStrategy cmdutil.DryRunStrategy , validate bool ) (string , error ) {
144-
145- r .SetLastResourceCommand (kube .GetResourceKey (obj ), "create" )
132+ r .registerCommand (kube .GetResourceKey (obj ), RegisteredCommand {
133+ Command : "create" ,
134+ Validate : validate ,
135+ DryRunStrategy : dryRunStrategy ,
136+ })
146137 command , ok := r .Commands [obj .GetName ()]
147138 if ! ok {
148139 return "" , nil
149140 }
150141 return command .Output , command .Err
151142}
152143
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+
153157/*func (r *MockResourceOps) ConvertToVersion(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error) {
154158 if r.convertToVersionFunc != nil {
155159 return (*r.convertToVersionFunc)(obj, group, version)
0 commit comments