Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-660] Remove specific node image support #127

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 15 additions & 102 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ 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"

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/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"
"github.com/dell/csi-baremetal-operator/pkg/feature/security_verifier/models"
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
Expand All @@ -48,12 +44,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 {
Expand All @@ -78,120 +68,43 @@ 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
}
}
if err := common.UpdateDaemonSet(ctx, n.clientset, expected, n.log); err != nil {
n.log.Error(err, "Failed to update daemonset "+expected.Name)
return err
}

return resultErr
return nil
}

// Uninstall deletes platform-label on each node in cluster
// Uninstall deletes uuid-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 {
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
}

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 := 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)
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
}

// 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
}
11 changes: 5 additions & 6 deletions pkg/node/node_daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
Loading