penguin/monok8s

k8s image for Mono Gateway Dev Kit

clitools/pkg/node/agent.go

raw ยท 1133 bytes

package node

import (
	"context"
	"fmt"
	"strings"

	"k8s.io/klog/v2"

	"example.com/monok8s/pkg/kube"
	"example.com/monok8s/pkg/render"
	templates "example.com/monok8s/pkg/templates"
)

const kubeconfig = "/etc/kubernetes/admin.conf"

func ApplyNodeControlDaemonSetResources(ctx context.Context, n *NodeContext) error {
	if strings.TrimSpace(n.Config.Spec.ClusterRole) != "control-plane" || !n.Config.Spec.EnableNodeControl {
		klog.InfoS("skipped for",
			"clusterRole", n.Config.Spec.ClusterRole,
			"enableNodeAgent", n.Config.Spec.EnableNodeControl,
		)
		return nil
	}

	if err := ApplyCRDs(ctx, n); err != nil {
		return err
	}

	namespace := strings.TrimSpace(n.Config.Namespace)
	if namespace == "" {
		namespace = templates.DefaultNamespace
	}

	clients, err := kube.NewClientsFromKubeconfig(kubeconfig)
	if err != nil {
		return fmt.Errorf("build kube clients from %s: %w", kubeconfig, err)
	}

	conf := render.AgentConf{
		Namespace: namespace,
	}

	if err := render.ApplyAgentDaemonSets(ctx, clients.Kubernetes, conf); err != nil {
		return fmt.Errorf("apply node agent daemonset resources: %w", err)
	}

	return nil
}