Skip to content

Commit a9b1631

Browse files
author
Amit Kumar Das
authored
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 <[email protected]>
1 parent 15a9957 commit a9b1631

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pkg/recipe/labeling.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ func (l *Labeling) labelOrUnset(
128128
obj *unstructured.Unstructured,
129129
) error {
130130
var isInclude bool
131-
for _, name := range l.Label.FilterByNames {
131+
if len(l.Label.IncludeByNames) == 0 {
132+
isInclude = true
133+
}
134+
for _, name := range l.Label.IncludeByNames {
132135
if name == obj.GetName() {
133136
isInclude = true
134137
break
@@ -195,5 +198,10 @@ func (l *Labeling) labelAll() (*types.LabelResult, error) {
195198
// Run applyies the desired labels or unsets them
196199
// against the resource(s)
197200
func (l *Labeling) Run() (*types.LabelResult, error) {
201+
if len(l.Label.ApplyLabels) == 0 {
202+
return nil, errors.Errorf(
203+
"Invalid label operation: Missing ApplyLabels",
204+
)
205+
}
198206
return l.labelAll()
199207
}

types/recipe/label.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ type Label struct {
2929
// labeled
3030
State *unstructured.Unstructured `json:"state"`
3131

32-
// Filter the resources by these names
32+
// Include the resources by these names
3333
//
3434
// Optional
35-
FilterByNames []string `json:"filterByNames,omitempty"`
35+
IncludeByNames []string `json:"includeByNames,omitempty"`
3636

3737
// ApplyLabels represents the labels that need to be
38-
// applied
38+
// applied against the selected resources
39+
//
40+
// This is mandatory field
3941
ApplyLabels map[string]string `json:"applyLabels"`
4042

4143
// AutoUnset removes the labels from the resources if

0 commit comments

Comments
 (0)