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

Exclude node from loadbalancer on servicing #661

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use-octavia = {{ default "no" .Values.openstack.useOctavia }}
subnet-id= {{ required "missing openstack.lbSubnetID" .Values.openstack.lbSubnetID }}
floating-network-id= {{ required "missing openstack.lbFloatingNetworkID" .Values.openstack.lbFloatingNetworkID }}
create-monitor = yes
monitor-delay = 1m
monitor-timeout = 30s
monitor-max-retries = 3
monitor-delay = 10s
monitor-timeout = 5s
monitor-max-retries = 1
[BlockStorage]
trust-device-path = no
[Route]
Expand Down
23 changes: 22 additions & 1 deletion pkg/controller/servicing/lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (

const (
// EvictionTimeout defines when to abort the draining of a node
EvictionTimeout = 5 * time.Minute
EvictionTimeout = 5 * time.Minute
LabelExcludeFromLoadbalancers = "node.kubernetes.io/exclude-from-external-load-balancers"
)

type (
Expand Down Expand Up @@ -141,6 +142,9 @@ func (lc *NodeLifeCycler) Drain(node *core_v1.Node) error {
if err := lc.setUpdatingAnnotation(node); err != nil {
return errors.Wrap(err, "Failed to drain node")
}
if err := lc.setExcludeLoadbalancersLabel(node); err != nil {
return errors.Wrap(err, "Failed to add exclude loadbalancer label, drain aborted.")
}
logger := log.With(lc.Logger, "node", node.GetName())

drainer := &drain.Helper{
Expand Down Expand Up @@ -201,6 +205,9 @@ func (lc *NodeLifeCycler) Uncordon(node *core_v1.Node) error {
if err := lc.removeUpdatingAnnotation(node); err != nil {
return errors.Wrap(err, "failed to uncordon node")
}
if err := lc.removeExcludeLoadbalancersLabel(node); err != nil {
return errors.Wrap(err, "Failed to remove exclude loadbalancer label, uncordon aborted.")
}
logger := log.With(lc.Logger, "node", node.GetName())

drainer := &drain.Helper{
Expand Down Expand Up @@ -239,6 +246,20 @@ func (lc *NodeLifeCycler) removeUpdatingAnnotation(node *core_v1.Node) error {
return nil
}

func (lc *NodeLifeCycler) setExcludeLoadbalancersLabel(node *core_v1.Node) error {
if err := util.AddNodeLabel(node.Name, LabelExcludeFromLoadbalancers, "true", lc.Kubernetes); err != nil {
return errors.Wrap(err, "failed to set updating annotation")
}
return nil
}

func (lc *NodeLifeCycler) removeExcludeLoadbalancersLabel(node *core_v1.Node) error {
if err := util.RemoveNodeAnnotation(node.Name, LabelExcludeFromLoadbalancers, lc.Kubernetes); err != nil {
return errors.Wrap(err, "failed to remove updating annotation")
}
return nil
}

// Drain logs the action
func (lc *LoggingLifeCycler) Drain(node *core_v1.Node) (err error) {
defer func(begin time.Time) {
Expand Down
28 changes: 28 additions & 0 deletions pkg/util/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ type annotations struct {
} `json:"metadata"`
}

type nodeLabels struct {
Metadata struct {
Labels map[string]interface{} `json:"labels"`
} `json:"metadata"`
}

func AddNodeAnnotation(nodeName, key, val string, client kubernetes.Interface) error {
var a annotations
a.Metadata.Annotations = map[string]interface{}{key: val}
Expand All @@ -140,6 +146,28 @@ func RemoveNodeAnnotation(nodeName, key string, client kubernetes.Interface) err
return err
}

func AddNodeLabel(nodeName, key, val string, client kubernetes.Interface) error {
var l nodeLabels
l.Metadata.Labels = map[string]interface{}{key: val}
data, err := json.Marshal(l)
if err != nil {
return fmt.Errorf("Failed to marshal label %v = %v: %s", key, val, err)
}
_, err = client.CoreV1().Nodes().Patch(nodeName, types.MergePatchType, data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be super careful regarding the patch type. I thinks this shreds every lable, but the one given. (https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment) I think we need a strategic merge patch. ObjectMeta.Labels is not tagged with patchStrategy: merge, so that does not cover it.

So, either we fetch all the labels beforehand or generate a proper (non-merge) JSON Patch

Copy link
Collaborator Author

@jknipper jknipper Jun 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and it worked that way, eg.

  labels:
    beta.kubernetes.io/arch: amd64
    beta.kubernetes.io/instance-type: c_c2_m2
    beta.kubernetes.io/os: linux
    ccloud.sap.com/nodepool: payload
    failure-domain.beta.kubernetes.io/region: eu-nl-1
    failure-domain.beta.kubernetes.io/zone: eu-nl-1b
    kubernetes.io/arch: amd64
    kubernetes.io/hostname: kks-jan-payload-s7xbf
    kubernetes.io/os: linux
    kubernikus.cloud.sap/template-version: "6"
    node.kubernetes.io/exclude-from-external-load-balancers: "true"
    node.kubernetes.io/instance-type: c_c2_m2
    topology.cinder.csi.openstack.org/zone: eu-nl-1b
    topology.kubernetes.io/region: eu-nl-1
    topology.kubernetes.io/zone: eu-nl-1b

It seems only the label itself gets replaced. Otherwise we should have the same problem here I guess: https://github.com/sapcc/kubernikus/blob/master/pkg/util/node.go#L121-L141

return err
}

func RemoveNodeLabel(nodeName, key string, client kubernetes.Interface) error {
var l nodeLabels
l.Metadata.Labels = map[string]interface{}{key: nil}
data, err := json.Marshal(l)
if err != nil {
return fmt.Errorf("Failed to marshal label %v = %v: %s", key, nil, err)
}
_, err = client.CoreV1().Nodes().Patch(nodeName, types.MergePatchType, data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same note regarding patch strategy.

return err
}

func IsCoreOSNode(node *v1.Node) bool {
return strings.HasPrefix(node.Status.NodeInfo.OSImage, NODE_COREOS_PREFIX)
}
Expand Down