Allow manual label with empty host list in gitops (#30756)

#30481
This commit is contained in:
Dante Catalfamo
2025-07-18 11:07:19 -04:00
committed by GitHub
parent 4948325892
commit 587e21aef5
3 changed files with 24 additions and 2 deletions
@@ -0,0 +1 @@
- Fixed missing empty host lists on manual labels in gitops
+8 -2
View File
@@ -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) {
+15
View File
@@ -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"})