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