package resources import ( "context" "fmt" "strings" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" calico "edge-infra.dev/pkg/k8s/net/calico" nodemeta "edge-infra.dev/pkg/sds/ien/node" kubeclienttypes "edge-infra.dev/pkg/sds/lib/k8s/retryclient/types" ) var ( Worker = "worker" ControlPlane = "controlplane" IENVersionLabel = "feature.node.kubernetes.io/ien-version" ) type NodeHandler struct { *corev1.Node Client kubeclienttypes.Retrier Key types.NamespacedName Found bool } // Validate will validate the Node using a DryRun create func (h *NodeHandler) Validate(ctx context.Context) error { opts := &client.CreateOptions{} client.DryRunAll.ApplyToCreate(opts) return client.IgnoreAlreadyExists(h.Client.SafeCreate(ctx, h.Node, opts)) } // ReconcileLocal updates the local copy of the Node with the latest // version from the API server func (h *NodeHandler) ReconcileLocal(ctx context.Context) error { return h.Client.SafeGet(ctx, h.Key, h.Node) } // DeepCopyFrom copies the contents of the provided Node into the local // copy of the Node func (h *NodeHandler) DeepCopyFrom(from *corev1.Node) { from.DeepCopyInto(h.Node) } // IsWorker returns true if the Node is a worker node func (h *NodeHandler) IsWorker() bool { return strings.Contains(h.GetLabels()[nodemeta.RoleLabel], Worker) } // IENVersion returns the IEN version of the Node func (h *NodeHandler) IENVersion() string { return h.GetLabels()[IENVersionLabel] } // IP returns the IP address of the Node func (h *NodeHandler) IP() (string, error) { ip, err := calico.ParseNodeIPs(*h.Node) if err != nil { return "", err } if len(ip) == 0 { return "", fmt.Errorf("no ip found for node %s", h.Name) } return ip[0].String(), nil }