2022-11-23 04:29:36 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
// Package kubernetes interacts with the Kubernetes API to update an fetch objects related to joining nodes.
|
2022-11-23 04:29:36 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-23 12:59:15 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-12-09 12:30:20 -05:00
|
|
|
"time"
|
2022-11-23 04:29:36 -05:00
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-01-06 06:04:36 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
2022-11-23 04:29:36 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-12-06 12:48:01 -05:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/dynamic"
|
2022-11-23 04:29:36 -05:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a kubernetes client.
|
|
|
|
type Client struct {
|
2022-12-06 12:48:01 -05:00
|
|
|
client *kubernetes.Clientset
|
|
|
|
dynClient dynamic.Interface
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new kubernetes client.
|
|
|
|
func New() (*Client, error) {
|
|
|
|
// creates the in-cluster config
|
|
|
|
config, err := rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create in-cluster config: %w", err)
|
|
|
|
}
|
|
|
|
// creates the clientset
|
|
|
|
clientset, err := kubernetes.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create clientset: %w", err)
|
|
|
|
}
|
2022-12-06 12:48:01 -05:00
|
|
|
|
|
|
|
dynClient, err := dynamic.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create dynamic client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{client: clientset, dynClient: dynClient}, nil
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetComponents returns the components of the cluster.
|
2023-01-06 06:04:36 -05:00
|
|
|
func (c *Client) GetComponents(ctx context.Context, configMapName string) (components.Components, error) {
|
2022-11-23 04:29:36 -05:00
|
|
|
componentsRaw, err := c.getConfigMapData(ctx, configMapName, constants.ComponentsListKey)
|
|
|
|
if err != nil {
|
2023-01-06 06:04:36 -05:00
|
|
|
return components.Components{}, fmt.Errorf("failed to get components: %w", err)
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|
2023-01-06 06:04:36 -05:00
|
|
|
var clusterComponents components.Components
|
|
|
|
if err := json.Unmarshal([]byte(componentsRaw), &clusterComponents); err != nil {
|
|
|
|
return components.Components{}, fmt.Errorf("failed to unmarshal components %s: %w", componentsRaw, err)
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|
2023-01-06 06:04:36 -05:00
|
|
|
return clusterComponents, nil
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getConfigMapData(ctx context.Context, name, key string) (string, error) {
|
|
|
|
cm, err := c.client.CoreV1().ConfigMaps("kube-system").Get(ctx, name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get configmap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm.Data[key], nil
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
// GetK8sComponentsRefFromNodeVersionCRD returns the K8sComponentsRef from the node version CRD.
|
|
|
|
func (c *Client) GetK8sComponentsRefFromNodeVersionCRD(ctx context.Context, nodeName string) (string, error) {
|
|
|
|
nodeVersionResource := schema.GroupVersionResource{Group: "update.edgeless.systems", Version: "v1alpha1", Resource: "nodeversions"}
|
|
|
|
nodeVersion, err := c.dynClient.Resource(nodeVersionResource).Get(ctx, nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get node version: %w", err)
|
|
|
|
}
|
|
|
|
// Extract K8sComponentsRef from nodeVersion.
|
|
|
|
k8sComponentsRef, found, err := unstructured.NestedString(nodeVersion.Object, "spec", "kubernetesComponentsReference")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get K8sComponentsRef from node version: %w", err)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return "", fmt.Errorf("kubernetesComponentsReference not found in node version")
|
|
|
|
}
|
|
|
|
return k8sComponentsRef, nil
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:48:01 -05:00
|
|
|
// AddNodeToJoiningNodes adds the provided node as a joining node CRD.
|
2023-01-03 06:09:53 -05:00
|
|
|
func (c *Client) AddNodeToJoiningNodes(ctx context.Context, nodeName string, componentsReference string, isControlPlane bool) error {
|
2022-12-06 12:48:01 -05:00
|
|
|
joiningNode := &unstructured.Unstructured{}
|
2022-12-09 12:30:20 -05:00
|
|
|
|
2022-12-23 12:59:15 -05:00
|
|
|
compliantNodeName, err := k8sCompliantHostname(nodeName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get k8s compliant hostname: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoiningNodes referencing a worker node are named after the worker node.
|
|
|
|
// JoiningNodes referencing the control-plane node are named "control-plane".
|
|
|
|
objectMetadataName := compliantNodeName
|
2022-12-09 12:30:20 -05:00
|
|
|
deadline := metav1.NewTime(time.Now().Add(48 * time.Hour))
|
|
|
|
if isControlPlane {
|
|
|
|
objectMetadataName = "control-plane"
|
|
|
|
deadline = metav1.NewTime(time.Now().Add(10 * time.Minute))
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:48:01 -05:00
|
|
|
joiningNode.SetUnstructuredContent(map[string]any{
|
|
|
|
"apiVersion": "update.edgeless.systems/v1alpha1",
|
|
|
|
"kind": "JoiningNode",
|
|
|
|
"metadata": map[string]any{
|
2022-12-09 12:30:20 -05:00
|
|
|
"name": objectMetadataName,
|
2022-12-06 12:48:01 -05:00
|
|
|
},
|
|
|
|
"spec": map[string]any{
|
2023-01-03 06:09:53 -05:00
|
|
|
"name": compliantNodeName,
|
|
|
|
"componentsreference": componentsReference,
|
|
|
|
"iscontrolplane": isControlPlane,
|
|
|
|
"deadline": deadline,
|
2022-12-06 12:48:01 -05:00
|
|
|
},
|
|
|
|
})
|
2022-12-09 12:30:20 -05:00
|
|
|
if isControlPlane {
|
|
|
|
return c.addControlPlaneToJoiningNodes(ctx, joiningNode)
|
|
|
|
}
|
|
|
|
return c.addWorkerToJoiningNodes(ctx, joiningNode)
|
|
|
|
}
|
2022-12-06 12:48:01 -05:00
|
|
|
|
2022-12-09 12:30:20 -05:00
|
|
|
func (c *Client) addControlPlaneToJoiningNodes(ctx context.Context, joiningNode *unstructured.Unstructured) error {
|
|
|
|
joiningNodeResource := schema.GroupVersionResource{Group: "update.edgeless.systems", Version: "v1alpha1", Resource: "joiningnodes"}
|
|
|
|
_, err := c.dynClient.Resource(joiningNodeResource).Create(ctx, joiningNode, metav1.CreateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create joining control-plane node, maybe another node is already joining: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) addWorkerToJoiningNodes(ctx context.Context, joiningNode *unstructured.Unstructured) error {
|
|
|
|
joiningNodeResource := schema.GroupVersionResource{Group: "update.edgeless.systems", Version: "v1alpha1", Resource: "joiningnodes"}
|
2022-12-06 12:48:01 -05:00
|
|
|
_, err := c.dynClient.Resource(joiningNodeResource).Apply(ctx, joiningNode.GetName(), joiningNode, metav1.ApplyOptions{FieldManager: "join-service"})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create joining node: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-23 12:59:15 -05:00
|
|
|
var validHostnameRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)
|
|
|
|
|
|
|
|
// k8sCompliantHostname transforms a hostname to an RFC 1123 compliant, lowercase subdomain as required by Kubernetes node names.
|
|
|
|
// Only a simple heuristic is used for now (to lowercase, replace underscores).
|
|
|
|
func k8sCompliantHostname(in string) (string, error) {
|
|
|
|
hostname := strings.ToLower(in)
|
|
|
|
hostname = strings.ReplaceAll(hostname, "_", "-")
|
|
|
|
if !validHostnameRegex.MatchString(hostname) {
|
|
|
|
return "", fmt.Errorf("failed to generate a Kubernetes compliant hostname for %s", in)
|
|
|
|
}
|
|
|
|
return hostname, nil
|
|
|
|
}
|