Skip to content

Commit 5123244

Browse files
authored
Propagate ResourceGroup labels and annotations (#4165)
* Propagate ResourceGroup labels and annotations When I declare a ResourceGroup on disk, I expect the labels and annotations defined on it to be propagated to the in-cluster inventory. That was previously not the case. kioutil annotations are removed (path, index, etc) since they are redundant for inventories that are typically defined in one place, and don't need to be reconstructed by a function pipeline. Signed-off-by: Fredrik Sommar <[email protected]> * Replace busybox with no-op function in test Signed-off-by: Fredrik Sommar <[email protected]> --------- Signed-off-by: Fredrik Sommar <[email protected]>
1 parent 55b74a5 commit 5123244

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

internal/pkg/pkg.go

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"sort"
25+
"strings"
2526

2627
"github.com/GoogleContainerTools/kpt/internal/errors"
2728
"github.com/GoogleContainerTools/kpt/internal/types"
@@ -736,9 +737,14 @@ func (p *Pkg) LocalInventory() (kptfilev1.Inventory, error) {
736737
if !hasKptfile {
737738
// Return the ResourceGroup object as inventory.
738739
if len(resources) == 1 {
740+
labels := resources[0].GetLabels()
741+
delete(labels, rgfilev1alpha1.RGInventoryIDLabel)
742+
739743
return kptfilev1.Inventory{
740744
Name: resources[0].GetName(),
741745
Namespace: resources[0].GetNamespace(),
746+
Annotations: resources[0].GetAnnotations(),
747+
Labels: labels,
742748
InventoryID: resources[0].GetLabels()[rgfilev1alpha1.RGInventoryIDLabel],
743749
}, nil
744750
}
@@ -764,9 +770,14 @@ func (p *Pkg) LocalInventory() (kptfilev1.Inventory, error) {
764770

765771
// ResourceGroup stores the inventory and Kptfile does not contain inventory.
766772
if len(resources) == 1 {
773+
labels := resources[0].GetLabels()
774+
delete(labels, rgfilev1alpha1.RGInventoryIDLabel)
775+
767776
return kptfilev1.Inventory{
768777
Name: resources[0].GetName(),
769778
Namespace: resources[0].GetNamespace(),
779+
Annotations: resources[0].GetAnnotations(),
780+
Labels: labels,
770781
InventoryID: resources[0].GetLabels()[rgfilev1alpha1.RGInventoryIDLabel],
771782
}, nil
772783
}
@@ -788,8 +799,21 @@ func filterResourceGroups(input []*yaml.RNode) (output []*yaml.RNode, err error)
788799
continue
789800
}
790801

802+
ClearInternalAnnotations(meta.Annotations)
803+
if err := r.SetAnnotations(meta.Annotations); err != nil {
804+
return nil, fmt.Errorf("failed to set annotations for resource %w", err)
805+
}
806+
791807
output = append(output, r)
792808
}
793809

794810
return output, nil
795811
}
812+
813+
func ClearInternalAnnotations(annotations map[string]string) {
814+
for k := range annotations {
815+
if strings.HasPrefix(k, "internal.config.kubernetes.io/") || strings.HasPrefix(k, "config.kubernetes.io/") {
816+
delete(annotations, k)
817+
}
818+
}
819+
}

internal/testutil/pkgbuilder/builder.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ func (sp *SubPkg) WithSubPackages(ps ...*SubPkg) *SubPkg {
309309
// RGFile represents a minimal resourcegroup.
310310
type RGFile struct {
311311
Name, Namespace, ID string
312+
Annotations, Labels map[string]string
312313
}
313314

314315
func NewRGFile() *RGFile {
@@ -547,8 +548,15 @@ func buildRGFile(pkg *pkg) string {
547548
tmp := rgfilev1alpha1.ResourceGroup{ResourceMeta: rgfilev1alpha1.DefaultMeta}
548549
tmp.ObjectMeta.Name = pkg.RGFile.Name
549550
tmp.ObjectMeta.Namespace = pkg.RGFile.Namespace
551+
tmp.ObjectMeta.Annotations = pkg.RGFile.Annotations
552+
tmp.ObjectMeta.Labels = pkg.RGFile.Labels
553+
554+
if tmp.ObjectMeta.Labels == nil {
555+
tmp.ObjectMeta.Labels = make(map[string]string)
556+
}
557+
550558
if pkg.RGFile.ID != "" {
551-
tmp.ObjectMeta.Labels = map[string]string{rgfilev1alpha1.RGInventoryIDLabel: pkg.RGFile.ID}
559+
tmp.ObjectMeta.Labels[rgfilev1alpha1.RGInventoryIDLabel] = pkg.RGFile.ID
552560
}
553561

554562
b, err := yaml.MarshalWithOptions(tmp, &yaml.EncoderOptions{SeqIndent: yaml.WideSequenceStyle})

pkg/live/load.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,18 @@ func readInvInfoFromStream(in io.Reader) (kptfilev1.Inventory, error) {
135135
}
136136
// Inventory found with ResourceGroup object.
137137
if len(rgFilter.Inventories) == 1 {
138-
invID := rgFilter.Inventories[0].Labels[rgfilev1alpha1.RGInventoryIDLabel]
138+
labels := rgFilter.Inventories[0].Labels
139+
invID := labels[rgfilev1alpha1.RGInventoryIDLabel]
140+
delete(labels, rgfilev1alpha1.RGInventoryIDLabel)
141+
142+
annotations := rgFilter.Inventories[0].Annotations
143+
pkg.ClearInternalAnnotations(annotations)
144+
139145
return kptfilev1.Inventory{
140146
Name: rgFilter.Inventories[0].Name,
141147
Namespace: rgFilter.Inventories[0].Namespace,
148+
Annotations: annotations,
149+
Labels: labels,
142150
InventoryID: invID,
143151
}, nil
144152
}

pkg/live/load_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,36 @@ func TestLoad_LocalDisk(t *testing.T) {
209209
},
210210
rgFile: "resourcegroup.yaml",
211211
},
212+
"Inventory information taken from resourcegroup in file": {
213+
pkg: pkgbuilder.NewRootPkg().
214+
WithKptfile(
215+
pkgbuilder.NewKptfile(),
216+
).WithRGFile(&pkgbuilder.RGFile{
217+
Name: "foo",
218+
Namespace: "bar",
219+
Annotations: map[string]string{
220+
"additional-annotation": "true",
221+
},
222+
Labels: map[string]string{
223+
"cli-utils.sigs.k8s.io/inventory-id": "foo-bar",
224+
"additional-label": "true",
225+
},
226+
}),
227+
namespace: "foo",
228+
expectedObjs: []object.ObjMetadata{},
229+
expectedInv: kptfile.Inventory{
230+
Name: "foo",
231+
Namespace: "bar",
232+
InventoryID: "foo-bar",
233+
Annotations: map[string]string{
234+
"additional-annotation": "true",
235+
},
236+
Labels: map[string]string{
237+
"additional-label": "true",
238+
},
239+
},
240+
rgFile: "inventory.yaml",
241+
},
212242
}
213243

214244
for tn, tc := range testCases {
@@ -239,6 +269,14 @@ func TestLoad_LocalDisk(t *testing.T) {
239269
})
240270
assert.Equal(t, tc.expectedObjs, objMetas)
241271

272+
// Simplify testing against empty labels and annotations
273+
if len(inv.Labels) == 0 {
274+
inv.Labels = nil
275+
}
276+
if len(inv.Annotations) == 0 {
277+
inv.Annotations = nil
278+
}
279+
242280
assert.Equal(t, tc.expectedInv, inv)
243281
})
244282
}
@@ -380,6 +418,39 @@ func TestLoad_StdIn(t *testing.T) {
380418
},
381419
},
382420
},
421+
"Inventory using STDIN resourcegroup file with annotations": {
422+
pkg: pkgbuilder.NewRootPkg().
423+
WithKptfile(
424+
pkgbuilder.NewKptfile(),
425+
).
426+
WithFile("cm.yaml", configMap).
427+
WithRGFile(&pkgbuilder.RGFile{
428+
Name: "foo",
429+
Namespace: "bar",
430+
ID: "foo-bar",
431+
Annotations: map[string]string{
432+
"extra-annotation": "hello-world",
433+
},
434+
}),
435+
namespace: "foo",
436+
expectedInv: kptfile.Inventory{
437+
Name: "foo",
438+
Namespace: "bar",
439+
InventoryID: "foo-bar",
440+
Annotations: map[string]string{
441+
"extra-annotation": "hello-world",
442+
},
443+
},
444+
expectedObjs: []object.ObjMetadata{
445+
{
446+
GroupKind: schema.GroupKind{
447+
Kind: "ConfigMap",
448+
},
449+
Name: "cm",
450+
Namespace: "foo",
451+
},
452+
},
453+
},
383454
"Multiple inventories using STDIN resourcegroup and Kptfile is error": {
384455
pkg: pkgbuilder.NewRootPkg().
385456
WithKptfile(
@@ -479,6 +550,13 @@ func TestLoad_StdIn(t *testing.T) {
479550
})
480551
assert.Equal(t, tc.expectedObjs, objMetas)
481552

553+
// Simplify testing against empty labels and annotations
554+
if len(inv.Annotations) == 0 {
555+
inv.Annotations = nil
556+
}
557+
if len(inv.Labels) == 0 {
558+
inv.Labels = nil
559+
}
482560
assert.Equal(t, tc.expectedInv, inv)
483561
})
484562
}

0 commit comments

Comments
 (0)