From e6c71ee1c930deaca762adcc4c1f0ce8fdee6355 Mon Sep 17 00:00:00 2001 From: safronovD Date: Wed, 27 Apr 2022 11:22:50 +0300 Subject: [PATCH 1/3] deprecate specific node image version Signed-off-by: safronovD --- pkg/node/node.go | 123 +----------------- pkg/node/node_daemonset.go | 11 +- pkg/node/node_test.go | 210 +++---------------------------- pkg/node/parser.go | 68 ---------- pkg/node/parser_test.go | 94 -------------- pkg/node/platform_description.go | 81 ------------ 6 files changed, 24 insertions(+), 563 deletions(-) delete mode 100644 pkg/node/parser.go delete mode 100644 pkg/node/parser_test.go delete mode 100644 pkg/node/platform_description.go diff --git a/pkg/node/node.go b/pkg/node/node.go index cc4d0003..d2cdf81d 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -4,15 +4,12 @@ import ( "context" "errors" - nodeconst "github.com/dell/csi-baremetal/pkg/crcontrollers/operator/common" "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" csibaremetalv1 "github.com/dell/csi-baremetal-operator/api/v1" - "github.com/dell/csi-baremetal-operator/api/v1/components" "github.com/dell/csi-baremetal-operator/pkg/common" "github.com/dell/csi-baremetal-operator/pkg/constant" securityverifier "github.com/dell/csi-baremetal-operator/pkg/feature/security_verifier" @@ -48,12 +45,6 @@ func NewNode(clientset kubernetes.Interface, // Update updates csi-baremetal-node or creates if not found func (n *Node) Update(ctx context.Context, csi *csibaremetalv1.Deployment, scheme *runtime.Scheme) error { - var ( - // need to trying deploy each daemonset - // return err != nil to request reconcile again if one ore more daemonsets failed - resultErr error - ) - // in case of Openshift deployment and non default namespace - validate node service accounts security bindings if csi.Spec.Platform == constant.PlatformOpenShift && csi.Namespace != constant.DefaultNamespace { if err := n.securityContextConstraintsVerifier.Verify(ctx, csi, verifierModels.Node); err != nil { @@ -78,120 +69,16 @@ func (n *Node) Update(ctx context.Context, csi *csibaremetalv1.Deployment, schem } } - needToDeploy, err := n.updateNodeLabels(ctx, csi.Spec.NodeSelector) - if err != nil { + expected := createNodeDaemonSet(csi) + if err := controllerutil.SetControllerReference(csi, expected, scheme); err != nil { + n.log.Error(err, "Failed to set controller reference "+expected.Name) return err } - for platformName, isDeploying := range needToDeploy { - if isDeploying { - expected := createNodeDaemonSet(csi, platforms[platformName]) - if err := controllerutil.SetControllerReference(csi, expected, scheme); err != nil { - n.log.Error(err, "Failed to set controller reference "+expected.Name) - continue - } - - if err = common.UpdateDaemonSet(ctx, n.clientset, expected, n.log); err != nil { - n.log.Error(err, "Failed to update daemonset "+expected.Name) - resultErr = err - } - } - } - - return resultErr -} - -// Uninstall deletes platform-label on each node in cluster -func (n *Node) Uninstall(ctx context.Context, _ *csibaremetalv1.Deployment) error { - return n.cleanNodeLabels(ctx) -} - -// updateNodeLabels gets list of all nodes in cluster, -// selects fit platform for each one and add/update node platform-label -// returns a Set of platforms, which will be deployed -func (n *Node) updateNodeLabels(ctx context.Context, selector *components.NodeSelector) (Set, error) { - // need to trying getKernelVersion and update label on each node - // return err != nil to request reconcile again if one ore more nodes failed - var ( - resultErr error - ) - - needToDeploy := createPlatformsSet() - - nodes, err := common.GetSelectedNodes(ctx, n.clientset, selector) - if err != nil { - return needToDeploy, err - } - - for i, node := range nodes.Items { - kernelVersion, err := GetNodeKernelVersion(&nodes.Items[i]) - if err != nil { - n.log.Error(err, "Failed to get kernel version for "+node.Name) - resultErr = err - continue - } - - platformName := findPlatform(kernelVersion) - needToDeploy[platformName] = true - - // skip updating label if exists - if value, ok := node.Labels[platformLabel]; ok && (value == platforms[platformName].labeltag) { - continue - } - - node.Labels[platformLabel] = platforms[platformName].labeltag - if _, err := n.clientset.CoreV1().Nodes().Update(ctx, &nodes.Items[i], metav1.UpdateOptions{}); err != nil { - n.log.Error(err, "Failed to update label on "+node.Name) - resultErr = err - } - } - - return needToDeploy, resultErr -} - -func (n *Node) cleanNodeLabels(ctx context.Context) error { - nodes, err := n.clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) - if err != nil { + if err := common.UpdateDaemonSet(ctx, n.clientset, expected, n.log); err != nil { + n.log.Error(err, "Failed to update daemonset "+expected.Name) return err } - for _, node := range nodes.Items { - nodeIns := node - toUpdate := false - - // delete platform label - if _, ok := node.Labels[platformLabel]; ok { - delete(node.Labels, platformLabel) - toUpdate = true - } - - // delete label with NodeID - // workaround to work with csi-node-driver-registrar sidecar internal logic - // implemented in this method to decrease Kubernetes API calls - if _, ok := node.Labels[nodeconst.NodeIDTopologyLabelKey]; ok { - delete(node.Labels, nodeconst.NodeIDTopologyLabelKey) - toUpdate = true - } - - if toUpdate { - if _, err := n.clientset.CoreV1().Nodes().Update(ctx, &nodeIns, metav1.UpdateOptions{}); err != nil { - n.log.Error(err, "Failed to delete label on "+node.Name) - } - } - } - return nil } - -// Set is needed to check if one type of platform is exists in current cluster -type Set map[string]bool - -// createNeedToDeploySet returns set of platform-names -func createPlatformsSet() Set { - var result = Set{} - - for key := range platforms { - result[key] = false - } - return result -} diff --git a/pkg/node/node_daemonset.go b/pkg/node/node_daemonset.go index e4ba9f76..2f5cf153 100644 --- a/pkg/node/node_daemonset.go +++ b/pkg/node/node_daemonset.go @@ -44,13 +44,12 @@ func GetNodeDaemonsetPodsSelector() labels.Selector { return labels.SelectorFromSet(common.ConstructSelectorMap(nodeName)) } -func createNodeDaemonSet(csi *csibaremetalv1.Deployment, platform *PlatformDescription) *v1.DaemonSet { +func createNodeDaemonSet(csi *csibaremetalv1.Deployment) *v1.DaemonSet { var nodeSelectors = common.MakeNodeSelectorMap(csi.Spec.NodeSelector) - nodeSelectors[platformLabel] = platform.labeltag return &v1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ - Name: platform.DaemonsetName(nodeName), + Name: nodeName, Namespace: csi.GetNamespace(), Labels: common.ConstructLabelAppMap(), }, @@ -74,7 +73,7 @@ func createNodeDaemonSet(csi *csibaremetalv1.Deployment, platform *PlatformDescr }, Spec: corev1.PodSpec{ Volumes: createNodeVolumes(csi), - Containers: createNodeContainers(csi, platform), + Containers: createNodeContainers(csi), RestartPolicy: corev1.RestartPolicyAlways, DNSPolicy: corev1.DNSClusterFirst, TerminationGracePeriodSeconds: pointer.Int64Ptr(constant.TerminationGracePeriodSeconds), @@ -166,14 +165,14 @@ func isLoopbackMgr(imageName string) bool { } // todo split long methods - https://github.com/dell/csi-baremetal/issues/329 -func createNodeContainers(csi *csibaremetalv1.Deployment, platform *PlatformDescription) []corev1.Container { +func createNodeContainers(csi *csibaremetalv1.Deployment) []corev1.Container { var ( bidirectional = corev1.MountPropagationBidirectional driveMgr = csi.Spec.Driver.Node.DriveMgr node = csi.Spec.Driver.Node lp = node.Sidecars[constant.LivenessProbeName] dr = node.Sidecars[constant.DriverRegistrarName] - nodeImage = platform.NodeImage(node.Image) + nodeImage = node.Image ) args := []string{ "--loglevel=" + common.MatchLogLevel(node.Log.Level), diff --git a/pkg/node/node_test.go b/pkg/node/node_test.go index b212af9e..a9e7a1e5 100644 --- a/pkg/node/node_test.go +++ b/pkg/node/node_test.go @@ -9,7 +9,6 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - coreV1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -27,37 +26,11 @@ import ( "github.com/dell/csi-baremetal-operator/pkg/validator/rbac" ) -const ( - kernelVersion = "4.15" - newKernelVersion = "5.4" -) - var ( nodeSelector *components.NodeSelector logEntry = logrus.WithField("Test name", "NodeTest") - testNode1 = coreV1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node-1", - Labels: map[string]string{}}, - Status: coreV1.NodeStatus{ - NodeInfo: coreV1.NodeSystemInfo{ - KernelVersion: kernelVersion, - }, - }, - } - testNode2 = coreV1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node-2", - Labels: map[string]string{}}, - Status: coreV1.NodeStatus{ - NodeInfo: coreV1.NodeSystemInfo{ - KernelVersion: kernelVersion, - }, - }, - } - matchSecurityContextConstraintsPolicies = []rbacv1.PolicyRule{ { Verbs: []string{"use"}, @@ -119,6 +92,20 @@ var ( Enable: true, ResourceName: "privileged", }, + Image: &components.Image{}, + DriveMgr: &components.DriveMgr{ + Image: &components.Image{ + Name: "drivemgr", + }, + }, + Log: &components.Log{ + Level: components.InfoLevel, + Format: components.TextFormat, + }, + Sidecars: map[string]*components.Sidecar{ + constant.LivenessProbeName: {Image: &components.Image{}}, + constant.DriverRegistrarName: {Image: &components.Image{}}, + }, }, }, Platform: constant.PlatformOpenShift, @@ -251,175 +238,6 @@ func Test_ValidateRBACPodSecurityPolicy(t *testing.T) { }) } -func Test_updateNodeLabels(t *testing.T) { - t.Run("Should deploy default platform and label nodes", func(t *testing.T) { - var ( - ctx = context.Background() - node1 = testNode1.DeepCopy() - node2 = testNode2.DeepCopy() - ) - - eventRecorder := new(mocks.EventRecorder) - eventRecorder.On("Eventf", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - scheme, _ := common.PrepareScheme() - node := prepareNode(eventRecorder, prepareNodeClientSet(node1, node2), prepareValidatorClient(scheme)) - - needToDeploy, err := node.updateNodeLabels(ctx, nodeSelector) - assert.Nil(t, err) - assert.True(t, needToDeploy["default"]) - assert.False(t, needToDeploy["kernel-5.4"]) - - updatedNode, err := node.clientset.CoreV1().Nodes().Get(ctx, node1.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["default"].labeltag, updatedNode.Labels[platformLabel]) - - updatedNode, err = node.clientset.CoreV1().Nodes().Get(ctx, node2.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["default"].labeltag, updatedNode.Labels[platformLabel]) - - }) - - t.Run("Should deploy specific platform and label nodes", func(t *testing.T) { - var ( - ctx = context.Background() - node1 = testNode1.DeepCopy() - node2 = testNode2.DeepCopy() - ) - - node1.Status.NodeInfo = coreV1.NodeSystemInfo{KernelVersion: newKernelVersion} - node2.Status.NodeInfo = coreV1.NodeSystemInfo{KernelVersion: newKernelVersion} - - eventRecorder := new(mocks.EventRecorder) - eventRecorder.On("Eventf", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - scheme, _ := common.PrepareScheme() - node := prepareNode(eventRecorder, prepareNodeClientSet(node1, node2), prepareValidatorClient(scheme)) - - needToDeploy, err := node.updateNodeLabels(ctx, nodeSelector) - assert.Nil(t, err) - assert.True(t, needToDeploy["kernel-5.4"]) - assert.False(t, needToDeploy["default"]) - - updatedNode, err := node.clientset.CoreV1().Nodes().Get(ctx, node1.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["kernel-5.4"].labeltag, updatedNode.Labels[platformLabel]) - - updatedNode, err = node.clientset.CoreV1().Nodes().Get(ctx, node2.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["kernel-5.4"].labeltag, updatedNode.Labels[platformLabel]) - }) - - t.Run("Should deploy multi platform and label nodes", func(t *testing.T) { - var ( - ctx = context.Background() - node1 = testNode1.DeepCopy() - node2 = testNode2.DeepCopy() - ) - - node1.Status.NodeInfo = coreV1.NodeSystemInfo{KernelVersion: newKernelVersion} - - eventRecorder := new(mocks.EventRecorder) - eventRecorder.On("Eventf", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - scheme, _ := common.PrepareScheme() - node := prepareNode(eventRecorder, prepareNodeClientSet(node1, node2), prepareValidatorClient(scheme)) - - needToDeploy, err := node.updateNodeLabels(ctx, nodeSelector) - assert.Nil(t, err) - assert.True(t, needToDeploy["kernel-5.4"]) - assert.True(t, needToDeploy["default"]) - - updatedNode, err := node.clientset.CoreV1().Nodes().Get(ctx, node1.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["kernel-5.4"].labeltag, updatedNode.Labels[platformLabel]) - - updatedNode, err = node.clientset.CoreV1().Nodes().Get(ctx, node2.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["default"].labeltag, updatedNode.Labels[platformLabel]) - }) - - t.Run("Error when node kernel version not readable", func(t *testing.T) { - var ( - ctx = context.Background() - corruptedNode = testNode1.DeepCopy() - ) - - corruptedNode.Status.NodeInfo = coreV1.NodeSystemInfo{KernelVersion: "corrupted_version"} - - eventRecorder := new(mocks.EventRecorder) - eventRecorder.On("Eventf", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - scheme, _ := common.PrepareScheme() - node := prepareNode(eventRecorder, prepareNodeClientSet(corruptedNode), prepareValidatorClient(scheme)) - - needToDeploy, err := node.updateNodeLabels(ctx, nodeSelector) - assert.NotNil(t, err) - assert.False(t, needToDeploy["kernel-5.4"]) - assert.False(t, needToDeploy["default"]) - - updatedNode, err := node.clientset.CoreV1().Nodes().Get(ctx, corruptedNode.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, map[string]string{}, updatedNode.Labels) - }) - - t.Run("Should label nodes only with selector", func(t *testing.T) { - var ( - ctx = context.Background() - node1 = testNode1.DeepCopy() - node2 = testNode2.DeepCopy() - selectorLabel = "label" - selectorTag = "tag" - ) - - nodeSelector = &components.NodeSelector{Key: selectorLabel, Value: selectorTag} - node1.Labels[selectorLabel] = selectorTag - - eventRecorder := new(mocks.EventRecorder) - eventRecorder.On("Eventf", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - scheme, _ := common.PrepareScheme() - node := prepareNode(eventRecorder, prepareNodeClientSet(node1, node2), prepareValidatorClient(scheme)) - - needToDeploy, err := node.updateNodeLabels(ctx, nodeSelector) - assert.Nil(t, err) - assert.True(t, needToDeploy["default"]) - - updatedNode, err := node.clientset.CoreV1().Nodes().Get(ctx, node1.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, platforms["default"].labeltag, updatedNode.Labels[platformLabel]) - - updatedNode, err = node.clientset.CoreV1().Nodes().Get(ctx, node2.Name, metav1.GetOptions{}) - assert.Nil(t, err) - _, ok := updatedNode.Labels[selectorLabel] - assert.False(t, ok) - }) -} - -func Test_cleanNodeLabels(t *testing.T) { - t.Run("Should clean labels", func(t *testing.T) { - var ( - ctx = context.Background() - node1 = testNode1.DeepCopy() - node2 = testNode2.DeepCopy() - ) - - node1.Labels[platformLabel] = "default" - node2.Labels[platformLabel] = "default" - - eventRecorder := new(mocks.EventRecorder) - eventRecorder.On("Eventf", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - scheme, _ := common.PrepareScheme() - node := prepareNode(eventRecorder, prepareNodeClientSet(node1, node2), prepareValidatorClient(scheme)) - - err := node.cleanNodeLabels(ctx) - assert.Nil(t, err) - - updatedNode, err := node.clientset.CoreV1().Nodes().Get(ctx, node1.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, map[string]string{}, updatedNode.Labels) - - updatedNode, err = node.clientset.CoreV1().Nodes().Get(ctx, node2.Name, metav1.GetOptions{}) - assert.Nil(t, err) - assert.Equal(t, map[string]string{}, updatedNode.Labels) - }) -} - func prepareNodeClientSet(objects ...runtime.Object) kubernetes.Interface { return fake.NewSimpleClientset(objects...) } diff --git a/pkg/node/parser.go b/pkg/node/parser.go deleted file mode 100644 index 8dd508c3..00000000 --- a/pkg/node/parser.go +++ /dev/null @@ -1,68 +0,0 @@ -/* -Copyright © 2021 Dell Inc. or its subsidiaries. All Rights Reserved. - -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. -*/ - -package node - -import ( - "errors" - "regexp" - "strings" - - "github.com/masterminds/semver" - corev1 "k8s.io/api/core/v1" -) - -// GetOSNameAndVersion receives string with the OS information in th following format: -// " ". For example, "Ubuntu 18.04.4 LTS" -// returns os name with the lower case and major and minor version. For example, "ubuntu", "18.04" -func GetOSNameAndVersion(osInfo string) (name, version string, err error) { - // check input parameter - if len(osInfo) == 0 { - return "", "", errors.New("errorEmptyParameter") - } - - // extract OS name - name = regexp.MustCompile(`^[A-Za-z]+`).FindString(osInfo) - if len(name) == 0 { - return "", "", errors.New("errorEmptyParameter") - } - - // extract OS version - version = regexp.MustCompile(`[0-9]+\.[0-9]+`).FindString(osInfo) - if len(version) == 0 { - return "", "", errors.New("errorEmptyParameter") - } - - return strings.ToLower(name), version, nil -} - -// GetKernelVersion receives string with the kernel version information in the following format: -// "X.Y.Z--". For example, "5.4.0-66-generic" -// returns kernel version - major and minor. For example, "5.4" -func GetKernelVersion(kernelVersion string) (version *semver.Version, err error) { - if len(kernelVersion) == 0 { - return nil, errors.New("errorEmptyParameter") - } - - // extract kernel version - x.y.z - versionStr := regexp.MustCompile(`^[0-9]+\.[0-9]+`).FindString(kernelVersion) - return semver.NewVersion(versionStr) -} - -// GetNodeKernelVersion returns kernel version of Node -func GetNodeKernelVersion(node *corev1.Node) (version *semver.Version, err error) { - return GetKernelVersion(node.Status.NodeInfo.KernelVersion) -} diff --git a/pkg/node/parser_test.go b/pkg/node/parser_test.go deleted file mode 100644 index 91c9f545..00000000 --- a/pkg/node/parser_test.go +++ /dev/null @@ -1,94 +0,0 @@ -/* -Copyright © 2021 Dell Inc. or its subsidiaries. All Rights Reserved. - -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. -*/ - -package node - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGetOSNameAndVersion(t *testing.T) { - name, version, err := GetOSNameAndVersion("") - assert.NotNil(t, err) - assert.Equal(t, name, "") - assert.Equal(t, version, "") - - name, version, err = GetOSNameAndVersion("Wrong OS") - assert.NotNil(t, err) - assert.Equal(t, name, "") - assert.Equal(t, version, "") - - name, version, err = GetOSNameAndVersion("12.04") - assert.NotNil(t, err) - assert.Equal(t, name, "") - assert.Equal(t, version, "") - - name, version, err = GetOSNameAndVersion("Ubuntu 18.04.4 LTS") - assert.Equal(t, err, nil) - assert.Equal(t, name, "ubuntu") - assert.Equal(t, version, "18.04") - - name, version, err = GetOSNameAndVersion("Ubuntu 19.10") - assert.Equal(t, err, nil) - assert.Equal(t, name, "ubuntu") - assert.Equal(t, version, "19.10") - - // OpenShift has the following output for OS Image - name, version, err = GetOSNameAndVersion("Red Hat Enterprise Linux CoreOS 46.82.202101301821-0 (Ootpa)") - assert.Equal(t, err, nil) - assert.Equal(t, name, "red") - assert.Equal(t, version, "46.82") -} - -func TestGetKernelVersion(t *testing.T) { - _, err := GetKernelVersion("") - assert.NotNil(t, err) - - _, err = GetKernelVersion("bla-bla") - assert.NotNil(t, err) - - // ubuntu 19 - testSupportedKernel := "5.4" - testSupportedKernelVersion, err := GetKernelVersion(testSupportedKernel) - assert.Equal(t, err, nil) - - // ubuntu 19 - newKernel1 := "5.4.0-66-generic" - newKernel1Version, err := GetKernelVersion(newKernel1) - assert.Equal(t, err, nil) - // ubuntu 21 - newKernel2 := "5.12.13-051213-generic" - newKernel2Version, err := GetKernelVersion(newKernel2) - assert.Equal(t, err, nil) - - // ubuntu 18 - oldKernel1 := "4.15.0-76-generic" - oldKernel1Version, err := GetKernelVersion(oldKernel1) - assert.Equal(t, err, nil) - // rhel coreos 4.6 - oldKernel2 := "4.18.0-193.41.1.el8_2.x86_64" - oldKernel2Version, err := GetKernelVersion(oldKernel2) - assert.Equal(t, err, nil) - - assert.True(t, greaterOrEqual(testSupportedKernelVersion, testSupportedKernelVersion)) - assert.True(t, greaterOrEqual(newKernel1Version, testSupportedKernelVersion)) - assert.True(t, greaterOrEqual(newKernel2Version, testSupportedKernelVersion)) - - assert.False(t, greaterOrEqual(oldKernel1Version, testSupportedKernelVersion)) - assert.False(t, greaterOrEqual(oldKernel2Version, testSupportedKernelVersion)) -} diff --git a/pkg/node/platform_description.go b/pkg/node/platform_description.go deleted file mode 100644 index 84935a08..00000000 --- a/pkg/node/platform_description.go +++ /dev/null @@ -1,81 +0,0 @@ -package node - -import ( - "github.com/dell/csi-baremetal-operator/api/v1/components" - "github.com/masterminds/semver" -) - -const ( - supportedKernel = "5.4" - defaultPlatform = "default" -) - -var supportedKernelVersion = semver.MustParse(supportedKernel) - -// PlatformDescription contains info to deploy specific node daemonsets -// tag - the prefix for daemonset and image: csi-baremetal-node- -// labeltag - label for node selctor -// checkVersion - func to match related version -type PlatformDescription struct { - tag string - labeltag string - checkVersion -} - -type checkVersion func(version *semver.Version) bool - -var ( - platforms = map[string]*PlatformDescription{ - "default": { - tag: "", - labeltag: defaultPlatform, - // default checkVersion returns false everytime to detect only specific platforms - checkVersion: func(version *semver.Version) bool { return false }, - }, - "kernel-5.4": { - tag: "kernel-5.4", - labeltag: "kernel-5.4", - checkVersion: func(version *semver.Version) bool { return greaterOrEqual(version, supportedKernelVersion) }, - }, - } -) - -// DaemonsetName constructs name of daemonset based on tag -func (pd *PlatformDescription) DaemonsetName(baseName string) string { - return createNameWithTag(baseName, pd.tag) -} - -// NodeImage constructs name of image based on tag and updates Image -func (pd *PlatformDescription) NodeImage(baseImage *components.Image) *components.Image { - var taggedImage = components.Image{} - - taggedImage.Tag = baseImage.Tag - taggedImage.Name = createNameWithTag(baseImage.Name, pd.tag) - - return &taggedImage -} - -// findPlatform calls checkVersion for all platforms in list, -// returns first found platform-name or "default" if no one passed -func findPlatform(kernelVersion *semver.Version) string { - for key, value := range platforms { - if value.checkVersion(kernelVersion) { - return key - } - } - - return defaultPlatform -} - -func createNameWithTag(name, tag string) string { - if tag != "" { - return name + "-" + tag - } - - return name -} - -// greaterOrEqual returns true if version >= supported -func greaterOrEqual(version *semver.Version, supported *semver.Version) bool { - return !version.LessThan(supported) -} From 33d78508d6fbb873a5285a9393d4e8f0ab4f2634 Mon Sep 17 00:00:00 2001 From: safronovD Date: Thu, 28 Apr 2022 11:13:40 +0300 Subject: [PATCH 2/3] fix lint Signed-off-by: safronovD --- pkg/csi_deployment.go | 5 ----- pkg/node/node.go | 4 ---- 2 files changed, 9 deletions(-) diff --git a/pkg/csi_deployment.go b/pkg/csi_deployment.go index 52b85d28..8e05964b 100644 --- a/pkg/csi_deployment.go +++ b/pkg/csi_deployment.go @@ -160,11 +160,6 @@ func (c *CSIDeployment) Uninstall(ctx context.Context, csi *csibaremetalv1.Deplo errMsgs = append(errMsgs, err.Error()) } - err = c.node.Uninstall(ctx, csi) - if err != nil { - errMsgs = append(errMsgs, err.Error()) - } - if len(errMsgs) != 0 { return fmt.Errorf(strings.Join(errMsgs, "\n")) } diff --git a/pkg/node/node.go b/pkg/node/node.go index d2cdf81d..6c6ff02a 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -17,10 +17,6 @@ import ( verifierModels "github.com/dell/csi-baremetal-operator/pkg/feature/security_verifier/models" ) -const ( - platformLabel = "nodes.csi-baremetal.dell.com/platform" -) - // Node controls csi-baremetal-node type Node struct { clientset kubernetes.Interface From fffb7fda44ca502b0daaa6beea857e4199429e15 Mon Sep 17 00:00:00 2001 From: safronovD Date: Fri, 29 Apr 2022 16:10:23 +0300 Subject: [PATCH 3/3] fix lint Signed-off-by: safronovD --- pkg/csi_deployment.go | 5 +++++ pkg/node/node.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/pkg/csi_deployment.go b/pkg/csi_deployment.go index 8e05964b..52b85d28 100644 --- a/pkg/csi_deployment.go +++ b/pkg/csi_deployment.go @@ -160,6 +160,11 @@ func (c *CSIDeployment) Uninstall(ctx context.Context, csi *csibaremetalv1.Deplo errMsgs = append(errMsgs, err.Error()) } + err = c.node.Uninstall(ctx, csi) + if err != nil { + errMsgs = append(errMsgs, err.Error()) + } + if len(errMsgs) != 0 { return fmt.Errorf(strings.Join(errMsgs, "\n")) } diff --git a/pkg/node/node.go b/pkg/node/node.go index 6c6ff02a..96560f61 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -5,10 +5,13 @@ import ( "errors" "github.com/sirupsen/logrus" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + nodeconst "github.com/dell/csi-baremetal/pkg/crcontrollers/operator/common" + csibaremetalv1 "github.com/dell/csi-baremetal-operator/api/v1" "github.com/dell/csi-baremetal-operator/pkg/common" "github.com/dell/csi-baremetal-operator/pkg/constant" @@ -78,3 +81,30 @@ func (n *Node) Update(ctx context.Context, csi *csibaremetalv1.Deployment, schem return nil } + +// Uninstall deletes uuid-label on each node in cluster +func (n *Node) Uninstall(ctx context.Context, _ *csibaremetalv1.Deployment) error { + return n.cleanNodeLabels(ctx) +} + +func (n *Node) cleanNodeLabels(ctx context.Context) error { + nodes, err := n.clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + + for _, node := range nodes.Items { + nodeIns := node.DeepCopy() + // delete label with NodeID + // workaround to work with csi-node-driver-registrar sidecar internal logic + // implemented in this method to decrease Kubernetes API calls + if _, ok := nodeIns.Labels[nodeconst.NodeIDTopologyLabelKey]; ok { + delete(nodeIns.Labels, nodeconst.NodeIDTopologyLabelKey) + if _, err := n.clientset.CoreV1().Nodes().Update(ctx, nodeIns, metav1.UpdateOptions{}); err != nil { + n.log.Error(err, "Failed to delete label on "+nodeIns.Name) + } + } + } + + return nil +}