diff --git a/changes/30481-gitops-manual-label-no-hosts b/changes/30481-gitops-manual-label-no-hosts new file mode 100644 index 0000000000..fae8e40bb4 --- /dev/null +++ b/changes/30481-gitops-manual-label-no-hosts @@ -0,0 +1 @@ +- Fixed missing empty host lists on manual labels in gitops diff --git a/pkg/spec/gitops.go b/pkg/spec/gitops.go index 8ca17f4f57..35d2d7261f 100644 --- a/pkg/spec/gitops.go +++ b/pkg/spec/gitops.go @@ -675,8 +675,14 @@ func parseLabels(top map[string]json.RawMessage, result *GitOps, baseDir string, if l.Name == "" { multiError = multierror.Append(multiError, errors.New("name is required for each label")) } - if l.Query == "" && len(l.Hosts) == 0 && l.HostVitalsCriteria == nil { - multiError = multierror.Append(multiError, errors.New("a SQL query, hosts list or host vitals criteria is required for each label")) + + if l.LabelMembershipType != fleet.LabelMembershipTypeManual && l.Query == "" && l.HostVitalsCriteria == nil { + multiError = multierror.Append(multiError, errors.New("a SQL query or host vitals criteria is required for each non-manual label")) + } + + // Manual labels can have empty hosts lists, just make sure we initialize the empty list + if l.LabelMembershipType == fleet.LabelMembershipTypeManual && l.Hosts == nil { + l.Hosts = []string{} } // Don't use non-ASCII if !isASCII(l.Name) { diff --git a/pkg/spec/gitops_test.go b/pkg/spec/gitops_test.go index f60cbbb695..c232d8a40b 100644 --- a/pkg/spec/gitops_test.go +++ b/pkg/spec/gitops_test.go @@ -366,6 +366,21 @@ policies: assert.ErrorContains(t, err, "duplicate policy names") } +func TestManualLabelEmptyHostList(t *testing.T) { + t.Parallel() + config := getGlobalConfig([]string{}) + config += ` +labels: + - name: TestLabel + description: Label for testing + hosts: + label_membership_type: manual` + + gitops, err := gitOpsFromString(t, config) + require.NoError(t, err) + assert.NotNil(t, gitops.Labels[0].Hosts) +} + func TestDuplicateQueryNames(t *testing.T) { t.Parallel() config := getGlobalConfig([]string{"queries"})