Skip to content

Commit

Permalink
Allow clusterSemver to be scoped to a profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckal777 committed Jun 8, 2022
1 parent 4285803 commit f6a5e82
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ __clusterSemver:__ Checks if a label containing a semantic version is less than
```yaml
clusterSemver:
key: a valid label key, required
profileScoped: do not check against the whole cluster, but against all nodes, which match the current profile, optional
```
__condition:__ Checks if a node condition has the defined status.
```yaml
Expand Down
12 changes: 12 additions & 0 deletions controllers/node_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,18 @@ var _ = Describe("The clusterSemver plugin", func() {
Expect(result).To(BeTrue())
})

It("returns false for a cluster-wide outdated node if scoped to a profile with no nodes", func() {
cs := impl.ClusterSemver{Key: "version", ProfileScoped: true}
result, err := cs.Check(plugin.Parameters{
Client: k8sClient,
Ctx: context.Background(),
Node: minnode,
Profile: "does-not-exist",
})
Expect(err).To(Succeed())
Expect(result).To(BeFalse())
})

It("fails for checked node with invalid version label", func() {
cs := impl.ClusterSemver{Key: "version"}
_, err := cs.Check(plugin.Parameters{Client: k8sClient, Ctx: context.Background(), Node: invalid})
Expand Down
27 changes: 24 additions & 3 deletions plugin/impl/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ package impl

import (
"fmt"
"strings"

"github.com/blang/semver/v4"
"github.com/elastic/go-ucfg"
"github.com/sapcc/maintenance-controller/constants"
"github.com/sapcc/maintenance-controller/plugin"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -34,17 +36,19 @@ import (
// than the clusters max value, which indicates
// that an update may be needed.
type ClusterSemver struct {
Key string
Key string
ProfileScoped bool
}

func (cs *ClusterSemver) New(config *ucfg.Config) (plugin.Checker, error) {
conf := struct {
Key string `config:"key" validate:"required"`
Key string `config:"key" validate:"required"`
ProfileScoped bool `config:"profileScoped"`
}{}
if err := config.Unpack(&conf); err != nil {
return nil, err
}
return &ClusterSemver{Key: conf.Key}, nil
return &ClusterSemver{Key: conf.Key, ProfileScoped: conf.ProfileScoped}, nil
}

func (cs *ClusterSemver) Check(params plugin.Parameters) (bool, error) {
Expand All @@ -62,6 +66,9 @@ func (cs *ClusterSemver) Check(params plugin.Parameters) (bool, error) {
return false, err
}
nodes := nodeList.Items
if cs.ProfileScoped {
nodes = filterByProfile(nodes, params.Profile)
}
maxVersion := semver.MustParse("0.1.0")
for _, node := range nodes {
versionStr, ok := node.Labels[cs.Key]
Expand All @@ -79,6 +86,20 @@ func (cs *ClusterSemver) Check(params plugin.Parameters) (bool, error) {
return ownVersion.LT(maxVersion), nil
}

func filterByProfile(nodes []v1.Node, profile string) []v1.Node {
filtered := make([]v1.Node, 0)
for _, node := range nodes {
nodeProfiles, ok := node.Labels[constants.ProfileLabelKey]
if !ok {
continue
}
if strings.Contains(nodeProfiles, profile) {
filtered = append(filtered, node)
}
}
return filtered
}

func (cs *ClusterSemver) AfterEval(chainResult bool, params plugin.Parameters) error {
return nil
}
3 changes: 2 additions & 1 deletion plugin/impl/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
var _ = Describe("The ClusterSemver plugin", func() {

It("can parse its configuration", func() {
configStr := "key: alge"
configStr := "key: alge\nprofileScoped: yes"
config, err := yaml.NewConfig([]byte(configStr))
Expect(err).To(Succeed())
var base ClusterSemver
plugin, err := base.New(config)
Expect(err).To(Succeed())
Expect(plugin.(*ClusterSemver).Key).To(Equal("alge"))
Expect(plugin.(*ClusterSemver).ProfileScoped).To(BeTrue())
})

})

0 comments on commit f6a5e82

Please sign in to comment.