...

Source file src/edge-infra.dev/pkg/sds/etcd/operator/internal/resources/node.go

Documentation: edge-infra.dev/pkg/sds/etcd/operator/internal/resources

     1  package resources
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	corev1 "k8s.io/api/core/v1"
     9  	"k8s.io/apimachinery/pkg/types"
    10  	"sigs.k8s.io/controller-runtime/pkg/client"
    11  
    12  	calico "edge-infra.dev/pkg/k8s/net/calico"
    13  	nodemeta "edge-infra.dev/pkg/sds/ien/node"
    14  	kubeclienttypes "edge-infra.dev/pkg/sds/lib/k8s/retryclient/types"
    15  )
    16  
    17  var (
    18  	Worker       = "worker"
    19  	ControlPlane = "controlplane"
    20  
    21  	IENVersionLabel = "feature.node.kubernetes.io/ien-version"
    22  )
    23  
    24  type NodeHandler struct {
    25  	*corev1.Node
    26  
    27  	Client kubeclienttypes.Retrier
    28  	Key    types.NamespacedName
    29  	Found  bool
    30  }
    31  
    32  // Validate will validate the Node using a DryRun create
    33  func (h *NodeHandler) Validate(ctx context.Context) error {
    34  	opts := &client.CreateOptions{}
    35  	client.DryRunAll.ApplyToCreate(opts)
    36  	return client.IgnoreAlreadyExists(h.Client.SafeCreate(ctx, h.Node, opts))
    37  }
    38  
    39  // ReconcileLocal updates the local copy of the Node with the latest
    40  // version from the API server
    41  func (h *NodeHandler) ReconcileLocal(ctx context.Context) error {
    42  	return h.Client.SafeGet(ctx, h.Key, h.Node)
    43  }
    44  
    45  // DeepCopyFrom copies the contents of the provided Node into the local
    46  // copy of the Node
    47  func (h *NodeHandler) DeepCopyFrom(from *corev1.Node) {
    48  	from.DeepCopyInto(h.Node)
    49  }
    50  
    51  // IsWorker returns true if the Node is a worker node
    52  func (h *NodeHandler) IsWorker() bool {
    53  	return strings.Contains(h.GetLabels()[nodemeta.RoleLabel], Worker)
    54  }
    55  
    56  // IENVersion returns the IEN version of the Node
    57  func (h *NodeHandler) IENVersion() string {
    58  	return h.GetLabels()[IENVersionLabel]
    59  }
    60  
    61  // IP returns the IP address of the Node
    62  func (h *NodeHandler) IP() (string, error) {
    63  	ip, err := calico.ParseNodeIPs(*h.Node)
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  	if len(ip) == 0 {
    68  		return "", fmt.Errorf("no ip found for node %s", h.Name)
    69  	}
    70  	return ip[0].String(), nil
    71  }
    72  

View as plain text