@@ -151,9 +151,9 @@ func TestUpdateNodeObject(t *testing.T) {
151
151
sort .Strings (fakeExtResourceNames )
152
152
153
153
// Create a list of expected node status patches
154
- statusPatches := []apihelper .JsonPatch {}
154
+ statusPatches := []utils .JsonPatch {}
155
155
for k , v := range fakeExtResources {
156
- statusPatches = append (statusPatches , apihelper .NewJsonPatch ("add" , "/status/capacity" , k , v ))
156
+ statusPatches = append (statusPatches , utils .NewJsonPatch ("add" , "/status/capacity" , k , v ))
157
157
}
158
158
159
159
mockAPIHelper := new (apihelper.MockAPIHelpers )
@@ -166,17 +166,17 @@ func TestUpdateNodeObject(t *testing.T) {
166
166
167
167
Convey ("When I successfully update the node with feature labels" , func () {
168
168
// Create a list of expected node metadata patches
169
- metadataPatches := []apihelper .JsonPatch {
170
- apihelper .NewJsonPatch ("replace" , "/metadata/annotations" , nfdv1alpha1 .AnnotationNs + "/feature-labels" , strings .Join (fakeFeatureLabelNames , "," )),
171
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureAnnotationsTrackingAnnotation , strings .Join (fakeFeatureAnnotationsNames , "," )),
172
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .AnnotationNs + "/extended-resources" , strings .Join (fakeExtResourceNames , "," )),
173
- apihelper .NewJsonPatch ("remove" , "/metadata/labels" , nfdv1alpha1 .FeatureLabelNs + "/old-feature" , "" ),
169
+ metadataPatches := []utils .JsonPatch {
170
+ utils .NewJsonPatch ("replace" , "/metadata/annotations" , nfdv1alpha1 .AnnotationNs + "/feature-labels" , strings .Join (fakeFeatureLabelNames , "," )),
171
+ utils .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureAnnotationsTrackingAnnotation , strings .Join (fakeFeatureAnnotationsNames , "," )),
172
+ utils .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .AnnotationNs + "/extended-resources" , strings .Join (fakeExtResourceNames , "," )),
173
+ utils .NewJsonPatch ("remove" , "/metadata/labels" , nfdv1alpha1 .FeatureLabelNs + "/old-feature" , "" ),
174
174
}
175
175
for k , v := range fakeFeatureLabels {
176
- metadataPatches = append (metadataPatches , apihelper .NewJsonPatch ("add" , "/metadata/labels" , k , v ))
176
+ metadataPatches = append (metadataPatches , utils .NewJsonPatch ("add" , "/metadata/labels" , k , v ))
177
177
}
178
178
for k , v := range fakeAnnotations {
179
- metadataPatches = append (metadataPatches , apihelper .NewJsonPatch ("add" , "/metadata/annotations" , k , v ))
179
+ metadataPatches = append (metadataPatches , utils .NewJsonPatch ("add" , "/metadata/annotations" , k , v ))
180
180
}
181
181
182
182
mockAPIHelper .On ("GetClient" ).Return (mockClient , nil )
@@ -244,7 +244,7 @@ func TestUpdateMasterNode(t *testing.T) {
244
244
mockClient := & k8sclient.Clientset {}
245
245
mockNode := newMockNode ()
246
246
Convey ("When update operation succeeds" , func () {
247
- expectedPatches := []apihelper .JsonPatch {}
247
+ expectedPatches := []utils .JsonPatch {}
248
248
mockHelper .On ("GetClient" ).Return (mockClient , nil )
249
249
mockHelper .On ("GetNode" , mockClient , mockNodeName ).Return (mockNode , nil )
250
250
mockHelper .On ("PatchNode" , mockClient , mockNodeName , mock .MatchedBy (jsonPatchMatcher (expectedPatches ))).Return (nil )
@@ -297,9 +297,9 @@ func TestAddingExtResources(t *testing.T) {
297
297
Convey ("When there are matching labels" , func () {
298
298
mockNode := newMockNode ()
299
299
mockResourceLabels := ExtendedResources {"feature-1" : "1" , "feature-2" : "2" }
300
- expectedPatches := []apihelper .JsonPatch {
301
- apihelper .NewJsonPatch ("add" , "/status/capacity" , "feature-1" , "1" ),
302
- apihelper .NewJsonPatch ("add" , "/status/capacity" , "feature-2" , "2" ),
300
+ expectedPatches := []utils .JsonPatch {
301
+ utils .NewJsonPatch ("add" , "/status/capacity" , "feature-1" , "1" ),
302
+ utils .NewJsonPatch ("add" , "/status/capacity" , "feature-2" , "2" ),
303
303
}
304
304
patches := mockMaster .createExtendedResourcePatches (mockNode , mockResourceLabels )
305
305
So (sortJsonPatches (patches ), ShouldResemble , sortJsonPatches (expectedPatches ))
@@ -317,9 +317,9 @@ func TestAddingExtResources(t *testing.T) {
317
317
mockNode := newMockNode ()
318
318
mockNode .Status .Capacity [corev1 .ResourceName ("feature-1" )] = * resource .NewQuantity (2 , resource .BinarySI )
319
319
mockResourceLabels := ExtendedResources {"feature-1" : "1" }
320
- expectedPatches := []apihelper .JsonPatch {
321
- apihelper .NewJsonPatch ("replace" , "/status/capacity" , "feature-1" , "1" ),
322
- apihelper .NewJsonPatch ("replace" , "/status/allocatable" , "feature-1" , "1" ),
320
+ expectedPatches := []utils .JsonPatch {
321
+ utils .NewJsonPatch ("replace" , "/status/capacity" , "feature-1" , "1" ),
322
+ utils .NewJsonPatch ("replace" , "/status/allocatable" , "feature-1" , "1" ),
323
323
}
324
324
patches := mockMaster .createExtendedResourcePatches (mockNode , mockResourceLabels )
325
325
So (sortJsonPatches (patches ), ShouldResemble , sortJsonPatches (expectedPatches ))
@@ -379,15 +379,15 @@ func TestSetLabels(t *testing.T) {
379
379
}
380
380
sort .Strings (mockLabelNames )
381
381
382
- expectedStatusPatches := []apihelper .JsonPatch {}
382
+ expectedStatusPatches := []utils .JsonPatch {}
383
383
384
384
Convey ("When node update succeeds" , func () {
385
385
mockMaster .config .ExtraLabelNs = map [string ]struct {}{"example.io" : {}}
386
- expectedPatches := []apihelper .JsonPatch {
387
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureLabelsAnnotation , strings .Join (mockLabelNames , "," )),
386
+ expectedPatches := []utils .JsonPatch {
387
+ utils .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureLabelsAnnotation , strings .Join (mockLabelNames , "," )),
388
388
}
389
389
for k , v := range mockLabels {
390
- expectedPatches = append (expectedPatches , apihelper .NewJsonPatch ("add" , "/metadata/labels" , k , v ))
390
+ expectedPatches = append (expectedPatches , utils .NewJsonPatch ("add" , "/metadata/labels" , k , v ))
391
391
}
392
392
393
393
mockHelper .On ("GetClient" ).Return (mockClient , nil )
@@ -402,9 +402,9 @@ func TestSetLabels(t *testing.T) {
402
402
403
403
Convey ("When -label-whitelist is specified" , func () {
404
404
mockMaster .config .ExtraLabelNs = map [string ]struct {}{"example.io" : {}}
405
- expectedPatches := []apihelper .JsonPatch {
406
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureLabelsAnnotation , "example.io/feature-2" ),
407
- apihelper .NewJsonPatch ("add" , "/metadata/labels" , "example.io/feature-2" , mockLabels ["example.io/feature-2" ]),
405
+ expectedPatches := []utils .JsonPatch {
406
+ utils .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureLabelsAnnotation , "example.io/feature-2" ),
407
+ utils .NewJsonPatch ("add" , "/metadata/labels" , "example.io/feature-2" , mockLabels ["example.io/feature-2" ]),
408
408
}
409
409
410
410
mockMaster .config .LabelWhiteList .Regexp = * regexp .MustCompile ("^f.*2$" )
@@ -434,14 +434,14 @@ func TestSetLabels(t *testing.T) {
434
434
vendorProfileLabel : "val-7" ,
435
435
"--invalid-name--" : "valid-val" ,
436
436
"valid-name" : "--invalid-val--" }
437
- expectedPatches := []apihelper .JsonPatch {
438
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" ,
437
+ expectedPatches := []utils .JsonPatch {
438
+ utils .NewJsonPatch ("add" , "/metadata/annotations" ,
439
439
instance + "." + nfdv1alpha1 .FeatureLabelsAnnotation ,
440
440
"feature-1,valid.ns/feature-2," + vendorFeatureLabel + "," + vendorProfileLabel ),
441
- apihelper .NewJsonPatch ("add" , "/metadata/labels" , "feature.node.kubernetes.io/feature-1" , mockLabels ["feature.node.kubernetes.io/feature-1" ]),
442
- apihelper .NewJsonPatch ("add" , "/metadata/labels" , "valid.ns/feature-2" , mockLabels ["valid.ns/feature-2" ]),
443
- apihelper .NewJsonPatch ("add" , "/metadata/labels" , vendorFeatureLabel , mockLabels [vendorFeatureLabel ]),
444
- apihelper .NewJsonPatch ("add" , "/metadata/labels" , vendorProfileLabel , mockLabels [vendorProfileLabel ]),
441
+ utils .NewJsonPatch ("add" , "/metadata/labels" , "feature.node.kubernetes.io/feature-1" , mockLabels ["feature.node.kubernetes.io/feature-1" ]),
442
+ utils .NewJsonPatch ("add" , "/metadata/labels" , "valid.ns/feature-2" , mockLabels ["valid.ns/feature-2" ]),
443
+ utils .NewJsonPatch ("add" , "/metadata/labels" , vendorFeatureLabel , mockLabels [vendorFeatureLabel ]),
444
+ utils .NewJsonPatch ("add" , "/metadata/labels" , vendorProfileLabel , mockLabels [vendorProfileLabel ]),
445
445
}
446
446
447
447
mockMaster .deniedNs .normal = map [string ]struct {}{"random.denied.ns" : {}}
@@ -461,14 +461,14 @@ func TestSetLabels(t *testing.T) {
461
461
462
462
Convey ("When -resource-labels is specified" , func () {
463
463
mockMaster .config .ExtraLabelNs = map [string ]struct {}{"example.io" : {}}
464
- expectedPatches := []apihelper .JsonPatch {
465
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureLabelsAnnotation , "example.io/feature-2" ),
466
- apihelper .NewJsonPatch ("add" , "/metadata/labels" , "example.io/feature-2" , mockLabels ["example.io/feature-2" ]),
467
- apihelper .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .ExtendedResourceAnnotation , "feature-1,feature-3" ),
464
+ expectedPatches := []utils .JsonPatch {
465
+ utils .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .FeatureLabelsAnnotation , "example.io/feature-2" ),
466
+ utils .NewJsonPatch ("add" , "/metadata/labels" , "example.io/feature-2" , mockLabels ["example.io/feature-2" ]),
467
+ utils .NewJsonPatch ("add" , "/metadata/annotations" , nfdv1alpha1 .ExtendedResourceAnnotation , "feature-1,feature-3" ),
468
468
}
469
- expectedStatusPatches := []apihelper .JsonPatch {
470
- apihelper .NewJsonPatch ("add" , "/status/capacity" , "feature.node.kubernetes.io/feature-1" , mockLabels ["feature.node.kubernetes.io/feature-1" ]),
471
- apihelper .NewJsonPatch ("add" , "/status/capacity" , "feature.node.kubernetes.io/feature-3" , mockLabels ["feature.node.kubernetes.io/feature-3" ]),
469
+ expectedStatusPatches := []utils .JsonPatch {
470
+ utils .NewJsonPatch ("add" , "/status/capacity" , "feature.node.kubernetes.io/feature-1" , mockLabels ["feature.node.kubernetes.io/feature-1" ]),
471
+ utils .NewJsonPatch ("add" , "/status/capacity" , "feature.node.kubernetes.io/feature-3" , mockLabels ["feature.node.kubernetes.io/feature-3" ]),
472
472
}
473
473
474
474
mockMaster .config .ResourceLabels = map [string ]struct {}{"feature.node.kubernetes.io/feature-3" : {}, "feature-1" : {}}
@@ -605,32 +605,32 @@ func TestCreatePatches(t *testing.T) {
605
605
606
606
Convey ("When when there are itmes to remoe but none to add or update" , func () {
607
607
p := createPatches ([]string {"key-2" , "key-3" , "foo" }, existingItems , map [string ]string {}, jsonPath )
608
- expected := []apihelper .JsonPatch {
609
- apihelper .NewJsonPatch ("remove" , jsonPath , "key-2" , "" ),
610
- apihelper .NewJsonPatch ("remove" , jsonPath , "key-3" , "" ),
608
+ expected := []utils .JsonPatch {
609
+ utils .NewJsonPatch ("remove" , jsonPath , "key-2" , "" ),
610
+ utils .NewJsonPatch ("remove" , jsonPath , "key-3" , "" ),
611
611
}
612
612
So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
613
613
})
614
614
615
615
Convey ("When when there are no itmes to remove but new items to add" , func () {
616
616
newItems := map [string ]string {"new-key" : "new-val" , "key-1" : "new-1" }
617
617
p := createPatches ([]string {"key-1" }, existingItems , newItems , jsonPath )
618
- expected := []apihelper .JsonPatch {
619
- apihelper .NewJsonPatch ("add" , jsonPath , "new-key" , newItems ["new-key" ]),
620
- apihelper .NewJsonPatch ("replace" , jsonPath , "key-1" , newItems ["key-1" ]),
618
+ expected := []utils .JsonPatch {
619
+ utils .NewJsonPatch ("add" , jsonPath , "new-key" , newItems ["new-key" ]),
620
+ utils .NewJsonPatch ("replace" , jsonPath , "key-1" , newItems ["key-1" ]),
621
621
}
622
622
So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
623
623
})
624
624
625
625
Convey ("When when there are items to remove add and update" , func () {
626
626
newItems := map [string ]string {"new-key" : "new-val" , "key-2" : "new-2" , "key-4" : "val-4" }
627
627
p := createPatches ([]string {"key-1" , "key-2" , "key-3" , "foo" }, existingItems , newItems , jsonPath )
628
- expected := []apihelper .JsonPatch {
629
- apihelper .NewJsonPatch ("add" , jsonPath , "new-key" , newItems ["new-key" ]),
630
- apihelper .NewJsonPatch ("add" , jsonPath , "key-4" , newItems ["key-4" ]),
631
- apihelper .NewJsonPatch ("replace" , jsonPath , "key-2" , newItems ["key-2" ]),
632
- apihelper .NewJsonPatch ("remove" , jsonPath , "key-1" , "" ),
633
- apihelper .NewJsonPatch ("remove" , jsonPath , "key-3" , "" ),
628
+ expected := []utils .JsonPatch {
629
+ utils .NewJsonPatch ("add" , jsonPath , "new-key" , newItems ["new-key" ]),
630
+ utils .NewJsonPatch ("add" , jsonPath , "key-4" , newItems ["key-4" ]),
631
+ utils .NewJsonPatch ("replace" , jsonPath , "key-2" , newItems ["key-2" ]),
632
+ utils .NewJsonPatch ("remove" , jsonPath , "key-1" , "" ),
633
+ utils .NewJsonPatch ("remove" , jsonPath , "key-3" , "" ),
634
634
}
635
635
So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
636
636
})
@@ -651,14 +651,14 @@ func TestRemoveLabelsWithPrefix(t *testing.T) {
651
651
652
652
Convey ("a unique label should be removed" , func () {
653
653
p := removeLabelsWithPrefix (n , "single" )
654
- So (p , ShouldResemble , []apihelper .JsonPatch {apihelper .NewJsonPatch ("remove" , "/metadata/labels" , "single-label" , "" )})
654
+ So (p , ShouldResemble , []utils .JsonPatch {utils .NewJsonPatch ("remove" , "/metadata/labels" , "single-label" , "" )})
655
655
})
656
656
657
657
Convey ("a non-unique search string should remove all matching keys" , func () {
658
658
p := removeLabelsWithPrefix (n , "multiple" )
659
- So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches ([]apihelper .JsonPatch {
660
- apihelper .NewJsonPatch ("remove" , "/metadata/labels" , "multiple_A" , "" ),
661
- apihelper .NewJsonPatch ("remove" , "/metadata/labels" , "multiple_B" , "" ),
659
+ So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches ([]utils .JsonPatch {
660
+ utils .NewJsonPatch ("remove" , "/metadata/labels" , "multiple_A" , "" ),
661
+ utils .NewJsonPatch ("remove" , "/metadata/labels" , "multiple_B" , "" ),
662
662
}))
663
663
})
664
664
@@ -851,8 +851,8 @@ func BenchmarkNfdAPIUpdateAllNodes(b *testing.B) {
851
851
852
852
mockClient := & k8sclient.Clientset {}
853
853
854
- statusPatches := []apihelper .JsonPatch {}
855
- metadataPatches := []apihelper .JsonPatch {
854
+ statusPatches := []utils .JsonPatch {}
855
+ metadataPatches := []utils .JsonPatch {
856
856
{Op : "add" , Path : "/metadata/annotations/nfd.node.kubernetes.io~1feature-labels" , Value : "" }, {Op : "add" , Path : "/metadata/annotations/nfd.node.kubernetes.io~1extended-resources" , Value : "" },
857
857
}
858
858
@@ -909,8 +909,8 @@ func withTimeout(actual interface{}, expected ...interface{}) string {
909
909
}
910
910
}
911
911
912
- func jsonPatchMatcher (expected []apihelper .JsonPatch ) func ([]apihelper .JsonPatch ) bool {
913
- return func (actual []apihelper .JsonPatch ) bool {
912
+ func jsonPatchMatcher (expected []utils .JsonPatch ) func ([]utils .JsonPatch ) bool {
913
+ return func (actual []utils .JsonPatch ) bool {
914
914
// We don't care about modifying the original slices
915
915
ok , msg := assertions .So (sortJsonPatches (actual ), ShouldResemble , sortJsonPatches (expected ))
916
916
if ! ok {
@@ -926,18 +926,18 @@ func jsonPatchMatcher(expected []apihelper.JsonPatch) func([]apihelper.JsonPatch
926
926
}
927
927
}
928
928
929
- func sortJsonPatches (p []apihelper .JsonPatch ) []apihelper .JsonPatch {
929
+ func sortJsonPatches (p []utils .JsonPatch ) []utils .JsonPatch {
930
930
sort .Slice (p , func (i , j int ) bool { return p [i ].Path < p [j ].Path })
931
931
return p
932
932
}
933
933
934
934
// Remove any labels having the given prefix
935
- func removeLabelsWithPrefix (n * corev1.Node , search string ) []apihelper .JsonPatch {
936
- var p []apihelper .JsonPatch
935
+ func removeLabelsWithPrefix (n * corev1.Node , search string ) []utils .JsonPatch {
936
+ var p []utils .JsonPatch
937
937
938
938
for k := range n .Labels {
939
939
if strings .HasPrefix (k , search ) {
940
- p = append (p , apihelper .NewJsonPatch ("remove" , "/metadata/labels" , k , "" ))
940
+ p = append (p , utils .NewJsonPatch ("remove" , "/metadata/labels" , k , "" ))
941
941
}
942
942
}
943
943
0 commit comments