From a9b16316df6ed554d92310dd573db30289f881ae Mon Sep 17 00:00:00 2001 From: Amit Kumar Das Date: Thu, 19 Nov 2020 10:42:00 +0530 Subject: [PATCH] fix(recipe): update labeling operation with fixes (#182) This handles the case when ApplyLabels field is not set. Code results in error when ApplyLabels is empty. All listed resources are included to be labeled if IncludeByNames field is empty, since this is an optional field. Signed-off-by: AmitKumarDas --- pkg/recipe/labeling.go | 10 +++++++++- types/recipe/label.go | 8 +++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/recipe/labeling.go b/pkg/recipe/labeling.go index 2cd2e3c..494c8f2 100644 --- a/pkg/recipe/labeling.go +++ b/pkg/recipe/labeling.go @@ -128,7 +128,10 @@ func (l *Labeling) labelOrUnset( obj *unstructured.Unstructured, ) error { var isInclude bool - for _, name := range l.Label.FilterByNames { + if len(l.Label.IncludeByNames) == 0 { + isInclude = true + } + for _, name := range l.Label.IncludeByNames { if name == obj.GetName() { isInclude = true break @@ -195,5 +198,10 @@ func (l *Labeling) labelAll() (*types.LabelResult, error) { // Run applyies the desired labels or unsets them // against the resource(s) func (l *Labeling) Run() (*types.LabelResult, error) { + if len(l.Label.ApplyLabels) == 0 { + return nil, errors.Errorf( + "Invalid label operation: Missing ApplyLabels", + ) + } return l.labelAll() } diff --git a/types/recipe/label.go b/types/recipe/label.go index e1ed9ed..cfab781 100644 --- a/types/recipe/label.go +++ b/types/recipe/label.go @@ -29,13 +29,15 @@ type Label struct { // labeled State *unstructured.Unstructured `json:"state"` - // Filter the resources by these names + // Include the resources by these names // // Optional - FilterByNames []string `json:"filterByNames,omitempty"` + IncludeByNames []string `json:"includeByNames,omitempty"` // ApplyLabels represents the labels that need to be - // applied + // applied against the selected resources + // + // This is mandatory field ApplyLabels map[string]string `json:"applyLabels"` // AutoUnset removes the labels from the resources if