Generate CRDs with controller-gen instead

This commit is contained in:
2026-04-03 03:10:20 +08:00
parent 1ce15e9ac5
commit 53f9f9376a
13 changed files with 137 additions and 145 deletions

View File

@@ -15,7 +15,6 @@ import (
"k8s.io/klog/v2"
monov1alpha1 "example.com/monok8s/pkg/apis/monok8s/v1alpha1"
"example.com/monok8s/pkg/crds"
"example.com/monok8s/pkg/kube"
templates "example.com/monok8s/pkg/templates"
)
@@ -27,48 +26,6 @@ const (
kubeconfig = "/etc/kubernetes/admin.conf"
)
func ApplyCRDs(ctx context.Context, n *NodeContext) error {
if n.Config.Spec.ClusterRole != "control-plane" {
return nil
}
clients, err := kube.NewClientsFromKubeconfig(kubeconfig)
if err != nil {
return fmt.Errorf("build kube clients from %s: %w", kubeconfig, err)
}
crdClient := clients.APIExtensions.ApiextensionsV1().CustomResourceDefinitions()
for _, wanted := range crds.Definitions() {
_, err := crdClient.Create(ctx, wanted, metav1.CreateOptions{})
if err == nil {
klog.InfoS("crd created", "name", wanted.Name)
continue
}
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("create CRD %s: %w", wanted.Name, err)
}
current, getErr := crdClient.Get(ctx, wanted.Name, metav1.GetOptions{})
if getErr != nil {
return fmt.Errorf("get existing CRD %s: %w", wanted.Name, getErr)
}
updated := wanted.DeepCopy()
updated.ResourceVersion = current.ResourceVersion
_, err = crdClient.Update(ctx, updated, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("update CRD %s: %w", wanted.Name, err)
}
klog.InfoS("crd updated", "name", wanted.Name)
}
return nil
}
func ApplyControlAgentDaemonSetResources(ctx context.Context, n *NodeContext) error {
// Only the control-plane should bootstrap this DaemonSet definition.
// And only when the feature is enabled.

View File

@@ -206,11 +206,6 @@ func InitControlPlane(ctx context.Context, nctx *NodeContext) error {
return fmt.Errorf("init control-plane requires fresh local state, got %q", nctx.LocalClusterState.MembershipKind)
}
// Example:
// if err := RunKubeadmInit(ctx, nctx); err != nil {
// return fmt.Errorf("kubeadm init: %w", err)
// }
return nil
}

View File

@@ -8,9 +8,15 @@ import (
"strings"
"time"
"k8s.io/klog/v2"
system "example.com/monok8s/pkg/system"
)
const (
crdsPath = "/usr/lib/monok8s/crds/"
)
func StartKubelet(ctx context.Context, n *NodeContext) error {
return system.EnsureServiceRunning(ctx, n.SystemRunner, "kubelet")
}
@@ -47,3 +53,22 @@ func waitForKubeletHealthy(ctx context.Context, timeout time.Duration) error {
}
}
}
func ApplyCRDs(ctx context.Context, nctx *NodeContext) error {
if nctx.Config.Spec.ClusterRole != "control-plane" {
return nil
}
_, err := nctx.SystemRunner.RunWithOptions(
ctx,
"kubectl",
[]string{"apply", "-f", crdsPath},
system.RunOptions{
Timeout: 10 * time.Minute,
OnStdoutLine: func(line string) { klog.Infof("[kubectl] %s", line) },
OnStderrLine: func(line string) { klog.Infof("[kubectl] %s", line) },
},
)
return err
}