Skip to content

Commit fd39030

Browse files
committed
inventory client replace skipped in dry-run
1 parent 902de23 commit fd39030

File tree

3 files changed

+68
-58
lines changed

3 files changed

+68
-58
lines changed

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,6 @@ sigs.k8s.io/controller-runtime v0.6.0 h1:Fzna3DY7c4BIP6KwfSlrfnj20DJ+SeMBK8HSFvO
703703
sigs.k8s.io/controller-runtime v0.6.0/go.mod h1:CpYf5pdNY/B352A1TFLAS2JVSlnGQ5O2cftPHndTroo=
704704
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
705705
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
706-
sigs.k8s.io/kustomize/kyaml v0.9.3 h1:kZ5HnNmmnbndSXFivrAVM6u3Ik1dwu4kbpaV8KNwy8I=
707-
sigs.k8s.io/kustomize/kyaml v0.9.3/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
708706
sigs.k8s.io/kustomize/kyaml v0.9.4 h1:DDuzZtjIzFqp2IPy4DTyCI69Cl3bDgcJODjI6sjF9NY=
709707
sigs.k8s.io/kustomize/kyaml v0.9.4/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
710708
sigs.k8s.io/structured-merge-diff/v3 v3.0.0-20200116222232-67a7b8c61874/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=

pkg/inventory/inventory-client.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func (cic *ClusterInventoryClient) Merge(localInv InventoryInfo, objs []object.O
151151
// Replace stores the passed objects in the cluster inventory object, or
152152
// an error if one occurred.
153153
func (cic *ClusterInventoryClient) Replace(localInv InventoryInfo, objs []object.ObjMetadata) error {
154+
// Skip entire function for dry-run.
155+
if cic.dryRunStrategy.ClientOrServerDryRun() {
156+
klog.V(4).Infoln("dry-run replace inventory object: not applied")
157+
return nil
158+
}
154159
clusterObjs, err := cic.GetClusterObjs(localInv)
155160
if err != nil {
156161
return err
@@ -163,24 +168,31 @@ func (cic *ClusterInventoryClient) Replace(localInv InventoryInfo, objs []object
163168
if err != nil {
164169
return err
165170
}
166-
wrappedInv := cic.InventoryFactoryFunc(clusterInv)
167-
if err = wrappedInv.Store(objs); err != nil {
171+
clusterInv, err = cic.replaceInventory(clusterInv, objs)
172+
if err != nil {
168173
return err
169174
}
170-
if !cic.dryRunStrategy.ClientOrServerDryRun() {
171-
clusterInv, err = wrappedInv.GetObject()
172-
if err != nil {
173-
return err
174-
}
175-
klog.V(4).Infof("replace cluster inventory: %s/%s", clusterInv.GetNamespace(), clusterInv.GetName())
176-
klog.V(4).Infof("replace cluster inventory %d objects", len(objs))
177-
if err := cic.applyInventoryObj(clusterInv); err != nil {
178-
return err
179-
}
175+
klog.V(4).Infof("replace cluster inventory: %s/%s", clusterInv.GetNamespace(), clusterInv.GetName())
176+
klog.V(4).Infof("replace cluster inventory %d objects", len(objs))
177+
if err := cic.applyInventoryObj(clusterInv); err != nil {
178+
return err
180179
}
181180
return nil
182181
}
183182

183+
// replaceInventory stores the passed objects into the passed inventory object.
184+
func (cic *ClusterInventoryClient) replaceInventory(inv *unstructured.Unstructured, objs []object.ObjMetadata) (*unstructured.Unstructured, error) {
185+
wrappedInv := cic.InventoryFactoryFunc(inv)
186+
if err := wrappedInv.Store(objs); err != nil {
187+
return nil, err
188+
}
189+
clusterInv, err := wrappedInv.GetObject()
190+
if err != nil {
191+
return nil, err
192+
}
193+
return clusterInv, nil
194+
}
195+
184196
// DeleteInventoryObj deletes the inventory object from the cluster.
185197
func (cic *ClusterInventoryClient) DeleteInventoryObj(localInv InventoryInfo) error {
186198
return cic.deleteInventoryObj(cic.invToUnstructuredFunc(localInv))

pkg/inventory/inventory-client_test.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -268,85 +268,85 @@ func TestCreateInventory(t *testing.T) {
268268

269269
func TestReplace(t *testing.T) {
270270
tests := map[string]struct {
271-
localInv InventoryInfo
272271
localObjs []object.ObjMetadata
273272
clusterObjs []object.ObjMetadata
274-
isError bool
275273
}{
276-
"Local inventory nil is error": {
277-
localInv: nil,
274+
"Cluster and local inventories empty": {
278275
localObjs: []object.ObjMetadata{},
279276
clusterObjs: []object.ObjMetadata{},
280-
isError: true,
281277
},
282-
"Cluster and local inventories empty: no error": {
283-
localInv: copyInventory(),
284-
localObjs: []object.ObjMetadata{},
285-
clusterObjs: []object.ObjMetadata{},
286-
isError: false,
287-
},
288-
"Cluster and local inventories same: no error": {
289-
localInv: copyInventory(),
278+
"Cluster and local inventories same": {
290279
localObjs: []object.ObjMetadata{
291280
ignoreErrInfoToObjMeta(pod1Info),
292281
},
293282
clusterObjs: []object.ObjMetadata{
294283
ignoreErrInfoToObjMeta(pod1Info),
295284
},
296-
isError: false,
297285
},
298-
"Cluster two obj, local one: no error": {
299-
localInv: copyInventory(),
286+
"Cluster two obj, local one": {
300287
localObjs: []object.ObjMetadata{
301288
ignoreErrInfoToObjMeta(pod1Info),
302289
},
303290
clusterObjs: []object.ObjMetadata{
304291
ignoreErrInfoToObjMeta(pod1Info),
305292
ignoreErrInfoToObjMeta(pod3Info),
306293
},
307-
isError: false,
308294
},
309-
"Cluster multiple objs, local multiple different objs: no error": {
310-
localInv: copyInventory(),
295+
"Cluster multiple objs, local multiple different objs": {
311296
localObjs: []object.ObjMetadata{
312297
ignoreErrInfoToObjMeta(pod2Info),
313298
},
314299
clusterObjs: []object.ObjMetadata{
315300
ignoreErrInfoToObjMeta(pod1Info),
316301
ignoreErrInfoToObjMeta(pod2Info),
317302
ignoreErrInfoToObjMeta(pod3Info)},
318-
isError: false,
319303
},
320304
}
321305

322306
tf := cmdtesting.NewTestFactory().WithNamespace(testNamespace)
323307
defer tf.Cleanup()
324308

309+
// Client and server dry-run do not throw errors.
310+
invClient, _ := NewInventoryClient(tf, WrapInventoryObj, InvInfoToConfigMap)
311+
invClient.SetDryRunStrategy(common.DryRunClient)
312+
err := invClient.Replace(copyInventory(), []object.ObjMetadata{})
313+
if err != nil {
314+
t.Fatalf("unexpected error received: %s", err)
315+
}
316+
invClient.SetDryRunStrategy(common.DryRunServer)
317+
err = invClient.Replace(copyInventory(), []object.ObjMetadata{})
318+
if err != nil {
319+
t.Fatalf("unexpected error received: %s", err)
320+
}
321+
325322
for name, tc := range tests {
326-
for i := range common.Strategies {
327-
drs := common.Strategies[i]
328-
t.Run(name, func(t *testing.T) {
329-
invClient, _ := NewInventoryClient(tf,
330-
WrapInventoryObj, InvInfoToConfigMap)
331-
invClient.SetDryRunStrategy(drs)
332-
// Create fake builder returning the cluster inventory object
333-
// storing the "tc.clusterObjs" objects.
334-
fakeBuilder := FakeBuilder{}
335-
fakeBuilder.SetInventoryObjs(tc.clusterObjs)
336-
invClient.builderFunc = fakeBuilder.GetBuilder()
337-
// Call "Replace", passing in the new local inventory objects
338-
err := invClient.Replace(tc.localInv, tc.localObjs)
339-
if tc.isError {
340-
if err == nil {
341-
t.Fatalf("expected error but received none")
342-
}
343-
return
344-
}
345-
if !tc.isError && err != nil {
346-
t.Fatalf("unexpected error received: %s", err)
347-
}
348-
})
349-
}
323+
t.Run(name, func(t *testing.T) {
324+
// Create inventory client, and store the cluster objs in the inventory object.
325+
invClient, _ := NewInventoryClient(tf,
326+
WrapInventoryObj, InvInfoToConfigMap)
327+
wrappedInv := invClient.InventoryFactoryFunc(inventoryObj)
328+
if err := wrappedInv.Store(tc.clusterObjs); err != nil {
329+
t.Fatalf("unexpected error storing inventory objects: %s", err)
330+
}
331+
inv, err := wrappedInv.GetObject()
332+
if err != nil {
333+
t.Fatalf("unexpected error storing inventory objects: %s", err)
334+
}
335+
// Call replaceInventory with the new set of "localObjs"
336+
inv, err = invClient.replaceInventory(inv, tc.localObjs)
337+
if err != nil {
338+
t.Fatalf("unexpected error received: %s", err)
339+
}
340+
wrappedInv = invClient.InventoryFactoryFunc(inv)
341+
// Validate that the stored objects are now the "localObjs".
342+
actualObjs, err := wrappedInv.Load()
343+
if err != nil {
344+
t.Fatalf("unexpected error received: %s", err)
345+
}
346+
if !object.SetEquals(tc.localObjs, actualObjs) {
347+
t.Errorf("expected objects (%s), got (%s)", tc.localObjs, actualObjs)
348+
}
349+
})
350350
}
351351
}
352352

0 commit comments

Comments
 (0)