From d6ef1a86ce95903e8cfee90145e9b5217fd68d64 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Mon, 9 Oct 2023 16:46:24 +0200 Subject: [PATCH] prpfiles can be exluded with ~ prefix Used the ~ as prefix as ! will mess up with yaml syntax Signed-off-by: Christian Goll --- CHANGELOG.md | 3 +- internal/pkg/node/constuctors_test.go | 24 ++++++++++++ internal/pkg/node/methods.go | 54 +++++++++++++++++++-------- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73d389f5..ae2be947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -134,7 +134,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 warewulf.grubboot: true in warewulf.conf. For unknown nodes, grub.efi and shim.efi are extracted from the Warewulf host. If the booted node has a container these binaries are extracted from the container image. -- overlays from different profiles for one node are now merged #885 +- overlays from different profiles for one node are now merged, overlays can be + excluded with ~ prefix and in listings are listed as !{excluded_profile} #885 ## [4.4.0] 2023-01-18 diff --git a/internal/pkg/node/constuctors_test.go b/internal/pkg/node/constuctors_test.go index 1e5a3b42..dc452c20 100644 --- a/internal/pkg/node/constuctors_test.go +++ b/internal/pkg/node/constuctors_test.go @@ -171,6 +171,13 @@ nodes: profiles: - profile1 - profile2 + node5: + runtime overlay: + - n1o1 + - ~p1o2 + profiles: + - profile1 + - profile2 ` assert := assert.New(t) var ymlSrc NodeYaml @@ -194,4 +201,21 @@ nodes: assert.Contains(nodemap, "node4") assert.ElementsMatch(nodemap["node4"].RuntimeOverlay.GetSlice(), []string{"p1o1", "p1o2", "p2o1", "p2o2", "n1o1", "n1o2"}) assert.Equal(nodemap["node4"].RuntimeOverlay.Print(), "n1o1,n1o2,p1o1,p1o2,p2o1,p2o2") + assert.Contains(nodemap, "node5") + assert.ElementsMatch(nodemap["node5"].RuntimeOverlay.GetSlice(), []string{"p1o1", "p2o1", "p2o2", "n1o1"}) + assert.Equal(nodemap["node5"].RuntimeOverlay.Print(), "n1o1,p1o1,p2o1,p2o2 ~{p1o2}") +} + +func Test_negated_list(t *testing.T) { + assert := assert.New(t) + list := []string{"tok1", "tok2"} + list2 := []string{"tok1", "tok2", "~tok3"} + list3 := []string{"tok1", "tok2", "~tok3", "~tok3"} + list4 := []string{"tok1", "tok2", "~tok3", "~tok4"} + list5 := []string{"tok1", "tok3", "~tok3", "tok2"} + assert.Equal([]string{"tok3"}, negList(list2)) + assert.Equal(list, cleanList(list2)) + assert.Equal(list, cleanList(list3)) + assert.Equal(list, cleanList(list4)) + assert.Equal(list, cleanList(list5)) } diff --git a/internal/pkg/node/methods.go b/internal/pkg/node/methods.go index 3414a44e..3e401c25 100644 --- a/internal/pkg/node/methods.go +++ b/internal/pkg/node/methods.go @@ -238,24 +238,46 @@ func (ent *Entry) GetB() bool { } } +// returns all negated elemets which are marked with ! as prefix +// from a list +func negList(list []string) (ret []string) { + for _, tok := range list { + if strings.HasPrefix(tok, "~") { + ret = append(ret, tok[1:]) + } + } + return +} + +// clean a list from negated tokens +func cleanList(list []string) (ret []string) { + neg := negList(list) + for _, listTok := range list { + notNegate := true + for _, negTok := range neg { + if listTok == negTok || listTok == "~"+negTok { + notNegate = false + } + } + if notNegate { + ret = append(ret, listTok) + } + } + return ret +} + /* -Returns a string slice created from a comma seperated list of the value. +Returns a string slice created from a comma separated list of the value. */ func (ent *Entry) GetSlice() []string { - var retval []string - if len(ent.value) != 0 { - retval = ent.value - } - if len(ent.altvalue) != 0 { - retval = append(retval, ent.altvalue...) - } + retval := append(ent.value, ent.altvalue...) if len(retval) != 0 { - return retval + return cleanList(retval) } if len(ent.def) != 0 { return ent.def } - return retval + return []string{} } /* @@ -328,11 +350,13 @@ alternative is presend. Default value is in '()'. If nothing is defined '--' is returned. */ func (ent *Entry) Print() (ret string) { - if len(ent.value) != 0 { - ret = strings.Join(ent.value, ",") - } - if len(ent.altvalue) != 0 { - ret = strings.Join(append(ent.value, ent.altvalue...), ",") + if len(ent.value) != 0 || len(ent.altvalue) != 0 { + combList := append(ent.value, ent.altvalue...) + ret = strings.Join(cleanList(combList), ",") + if len(negList(combList)) > 0 { + ret += " ~{" + strings.Join(negList(combList), ",") + "}" + } + } if ret != "" { return ret