Skip to content

Commit

Permalink
Merge pull request k8stopologyawareschedwg#83 from fromanirh/update-v…
Browse files Browse the repository at this point in the history
…alidation

validate: extend to check and store the cluster version
  • Loading branch information
ffromani authored Feb 14, 2022
2 parents f6d4dc9 + 2a97f27 commit ea4e77b
Show file tree
Hide file tree
Showing 38 changed files with 2,233 additions and 43 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/k8stopologyawareschedwg/deployer
go 1.16

require (
github.com/aquasecurity/go-version v0.0.0-20210121072130-637058cfe492
github.com/coreos/ignition/v2 v2.7.0
github.com/hashicorp/go-version v1.2.0
github.com/k8stopologyawareschedwg/noderesourcetopology-api v0.0.12
Expand All @@ -16,6 +17,7 @@ require (
k8s.io/apiextensions-apiserver v0.22.3
k8s.io/apimachinery v0.22.3
k8s.io/client-go v0.22.3
k8s.io/klog/v2 v2.10.0
k8s.io/kube-scheduler v0.22.3
k8s.io/kubelet v0.22.3
k8s.io/kubernetes v1.22.3 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ github.com/aokoli/goutils v1.0.1/go.mod h1:SijmP0QR8LtwsmDs8Yii5Z/S4trXFGFC2oO5g
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apparentlymart/go-cidr v1.0.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/aquasecurity/go-version v0.0.0-20210121072130-637058cfe492 h1:rcEG5HI490FF0a7zuvxOxen52ddygCfNVjP0XOCMl+M=
github.com/aquasecurity/go-version v0.0.0-20210121072130-637058cfe492/go.mod h1:9Beu8XsUNNfzml7WBf3QmyPToP1wm1Gj/Vc5UJKqTzU=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -1339,6 +1341,7 @@ github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/uudashr/gocognit v1.0.5/go.mod h1:wgYz0mitoKOTysqxTDMOUXg+Jb5SvtihkfmugIZYpEA=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand Down
11 changes: 11 additions & 0 deletions hack/kind-config-e2e-positive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ kubeadmConfigPatches:
cpuManagerPolicy: "static"
topologyManagerPolicy: "single-numa-node"
reservedSystemCPUs: "0"
memoryManagerPolicy: "Static"
evictionHard:
memory.available: "100Mi"
kubeReserved:
memory: "256Mi"
reservedMemory:
- numaNode: 0
limits:
memory: "612Mi"
systemReserved:
memory: "256Mi"
featureGates:
KubeletPodResourcesGetAllocatable: true
nodes:
Expand Down
14 changes: 14 additions & 0 deletions pkg/clientutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package clientutil

import (
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"

Expand Down Expand Up @@ -73,6 +74,19 @@ func NewK8sExt() (*apiextension.Clientset, error) {
return clientset, nil
}

func NewDiscoveryClient() (*discovery.DiscoveryClient, error) {
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}

cli, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return nil, err
}
return cli, nil
}

func NewTopologyClient() (*topologyclientset.Clientset, error) {
cfg, err := config.GetConfig()
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions pkg/commands/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ type validationOutput struct {
}

func validateCluster(cmd *cobra.Command, commonOpts *CommonOptions, opts *validateOptions, args []string) error {
nodeList, err := nodes.GetWorkers()
vd, err := validator.NewValidator(commonOpts.DebugLog)
if err != nil {
return err
}

vd := validator.Validator{
Log: commonOpts.DebugLog,
}
items, err := vd.ValidateClusterConfig(nodeList)
nodeList, err := nodes.GetWorkers()
if err != nil {
return err
}

printValidationResults(items, opts.jsonOutput)
if _, err := vd.ValidateClusterConfig(nodeList); err != nil {
return err
}

printValidationResults(vd.Results(), opts.jsonOutput)
return nil
}

Expand Down
83 changes: 83 additions & 0 deletions pkg/validator/clusterversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2022 Red Hat, Inc.
*/

package validator

import (
"k8s.io/client-go/discovery"

goversion "github.com/aquasecurity/go-version/pkg/version"
)

const (
ExpectedMinKubeVersion = "1.21"
)

const (
ComponentAPIVersion = "API Version"
)

func (vd *Validator) ValidateClusterVersion(cli *discovery.DiscoveryClient) ([]ValidationResult, error) {
ver, err := cli.ServerVersion()
if err != nil {
return nil, err
}
vd.serverVersion = ver
vrs := ValidateClusterVersion(ver.GitVersion)
vd.results = append(vd.results, vrs...)
return vrs, nil
}

func ValidateClusterVersion(clusterVersion string) []ValidationResult {
ok, err := isAPIVersionAtLeast(clusterVersion, ExpectedMinKubeVersion)
if err != nil {
return []ValidationResult{
{
/* no specific nodes: all are affected! */
Area: AreaCluster,
Component: ComponentAPIVersion,
/* no specific Setting: implicit in the component! */
Expected: "valid version",
Detected: err.Error(),
},
}
}
if !ok {
return []ValidationResult{
{
/* no specific nodes: all are affected! */
Area: AreaCluster,
Component: ComponentAPIVersion,
/* no specific Setting: implicit in the component! */
Expected: ExpectedMinKubeVersion,
Detected: clusterVersion,
},
}
}
return nil
}

func isAPIVersionAtLeast(server, refver string) (bool, error) {
ref, err := goversion.Parse(refver)
if err != nil {
return false, err
}
ser, err := goversion.Parse(server)
if err != nil {
return false, err
}
return ser.Compare(ref) >= 0, nil
}
71 changes: 71 additions & 0 deletions pkg/validator/clusterversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2022 Red Hat, Inc.
*/

package validator

import (
"testing"
)

func TestClusterVersionValidations(t *testing.T) {
type testCase struct {
version string
expected []ValidationResult
}

testCases := []testCase{
{
version: "1.23",
expected: []ValidationResult{},
},
{
version: "",
expected: []ValidationResult{
{
Area: AreaCluster,
Component: ComponentAPIVersion,
},
},
},
{
version: "INVALID",
expected: []ValidationResult{
{
Area: AreaCluster,
Component: ComponentAPIVersion,
},
},
},
{
version: "1.10",
expected: []ValidationResult{
{
Area: AreaCluster,
Component: ComponentAPIVersion,
},
},
},
}

for _, tc := range testCases {
t.Run(tc.version, func(t *testing.T) {
got := ValidateClusterVersion(tc.version)
if !matchValidationResults(tc.expected, got) {
t.Fatalf("validation failed:\nexpected=%#v\ngot=%#v", tc.expected, got)
}
})
}
}
Loading

0 comments on commit ea4e77b

Please sign in to comment.