Refactor bootstrap data management (#869)

* Refactor bootstrap data management

* Change errors from errors.New to fmt.Errorf

Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com>
This commit is contained in:
Hussein Galal
2026-06-03 19:40:31 +03:00
committed by GitHub
parent 0a16522e39
commit 5bd779cdaa
11 changed files with 390 additions and 363 deletions
+48 -41
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"path/filepath"
"sort"
"strings"
"text/template"
@@ -26,12 +27,22 @@ import (
)
const (
serverName = "server"
configName = "server-config"
initConfigName = "init-server-config"
initConfigName = "init-server-config"
configName = "server-config"
serverName = "server"
k3sInitConfigDir = "/opt/rancher/k3s/init"
k3sConfigDir = "/opt/rancher/k3s/server"
k3sRunDir = "/run"
k3sCNIDir = "/var/lib/cni"
k3sKubeletDir = "/var/lib/kubelet"
k3sDataDir = "/var/lib/rancher/k3s"
k3sETCDDataDir = "/var/lib/rancher/k3s/server/db/etcd"
k3sManifestDir = "/var/lib/rancher/k3s/server/manifests"
k3sTLSDir = "/var/lib/rancher/k3s/server/tls"
k3sLogDir = "/var/log"
k3sVarRunDir = "/var/run"
)
// Server
type Server struct {
cluster *v1beta1.Cluster
client client.Client
@@ -70,7 +81,7 @@ func (s *Server) podSpec(ctx context.Context, image, name string, persistent boo
PriorityClassName: s.cluster.Spec.PriorityClass,
Volumes: []corev1.Volume{
{
Name: "initconfig",
Name: "init-config",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: configSecretName(s.cluster.Name, true),
@@ -104,25 +115,25 @@ func (s *Server) podSpec(ctx context.Context, image, name string, persistent boo
},
},
{
Name: "varrun",
Name: "var-run",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "varlibcni",
Name: "var-lib-cni",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "varlog",
Name: "var-log",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "varlibkubelet",
Name: "var-lib-kubelet",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
@@ -154,42 +165,42 @@ func (s *Server) podSpec(ctx context.Context, image, name string, persistent boo
VolumeMounts: []corev1.VolumeMount{
{
Name: "config",
MountPath: "/opt/rancher/k3s/server",
MountPath: k3sConfigDir,
ReadOnly: false,
},
{
Name: "initconfig",
MountPath: "/opt/rancher/k3s/init",
Name: "init-config",
MountPath: k3sInitConfigDir,
ReadOnly: false,
},
{
Name: "run",
MountPath: "/run",
MountPath: k3sRunDir,
ReadOnly: false,
},
{
Name: "varrun",
MountPath: "/var/run",
Name: "var-run",
MountPath: k3sVarRunDir,
ReadOnly: false,
},
{
Name: "varlibcni",
MountPath: "/var/lib/cni",
Name: "var-lib-cni",
MountPath: k3sCNIDir,
ReadOnly: false,
},
{
Name: "varlibkubelet",
MountPath: "/var/lib/kubelet",
Name: "var-lib-kubelet",
MountPath: k3sKubeletDir,
ReadOnly: false,
},
{
Name: "varlibrancherk3s",
MountPath: "/var/lib/rancher/k3s",
Name: "var-lib-rancher-k3s",
MountPath: k3sDataDir,
ReadOnly: false,
},
{
Name: "varlog",
MountPath: "/var/log",
Name: "var-log",
MountPath: k3sLogDir,
ReadOnly: false,
},
},
@@ -206,7 +217,7 @@ func (s *Server) podSpec(ctx context.Context, image, name string, persistent boo
podSpec.Containers[0].Command = cmd
if !persistent {
podSpec.Volumes = append(podSpec.Volumes, corev1.Volume{
Name: "varlibrancherk3s",
Name: "var-lib-rancher-k3s",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
@@ -405,7 +416,7 @@ func (s *Server) setupDynamicPersistence() corev1.PersistentVolumeClaim {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "varlibrancherk3s",
Name: "var-lib-rancher-k3s",
Namespace: s.cluster.Namespace,
},
Spec: corev1.PersistentVolumeClaimSpec{
@@ -441,9 +452,9 @@ func (s *Server) setupStartCommand() (string, error) {
}
if err := tmplCmd.Execute(&output, map[string]string{
"ETCD_DIR": "/var/lib/rancher/k3s/server/db/etcd",
"INIT_CONFIG": "/opt/rancher/k3s/init/config.yaml",
"SERVER_CONFIG": "/opt/rancher/k3s/server/config.yaml",
"ETCD_DIR": k3sETCDDataDir,
"INIT_CONFIG": filepath.Join(k3sInitConfigDir, "config.yaml"),
"SERVER_CONFIG": filepath.Join(k3sConfigDir, "config.yaml"),
"CLUSTER_MODE": mode,
"K3K_MODE": string(s.cluster.Spec.Mode),
"EXTRA_ARGS": strings.Join(s.cluster.Spec.ServerArgs, " "),
@@ -511,7 +522,6 @@ func (s *Server) mountCACert(volumeName, certName, secretName string, subPathMou
)
// avoid re-adding secretName in case of combined secret
volume = &corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
@@ -528,22 +538,19 @@ func (s *Server) mountCACert(volumeName, certName, secretName string, subPathMou
mountFile = strings.TrimPrefix(certName, "etcd-")
}
// add the mount for the cert except for the service account token
if certName != "service" {
for _, crtOrKey := range []string{"crt", "key"} {
// skip adding cert mount for service account token
if certName == "service" && crtOrKey == "crt" {
continue
}
mounts = append(mounts, corev1.VolumeMount{
Name: volumeName,
MountPath: fmt.Sprintf("/var/lib/rancher/k3s/server/tls%s/%s.crt", etcdPrefix, mountFile),
SubPath: subPathMount + ".crt",
MountPath: filepath.Join(k3sTLSDir, etcdPrefix, mountFile) + "." + crtOrKey,
SubPath: subPathMount + "." + crtOrKey,
})
}
// add the mount for the key
mounts = append(mounts, corev1.VolumeMount{
Name: volumeName,
MountPath: fmt.Sprintf("/var/lib/rancher/k3s/server/tls%s/%s.key", etcdPrefix, mountFile),
SubPath: subPathMount + ".key",
})
return volume, mounts
}
@@ -603,7 +610,7 @@ func (s *Server) buildAddonsVolumes(ctx context.Context) ([]corev1.Volume, []cor
volumeMount := corev1.VolumeMount{
Name: name,
MountPath: "/var/lib/rancher/k3s/server/manifests/" + addon.SecretRef,
MountPath: filepath.Join(k3sManifestDir, addon.SecretRef),
ReadOnly: true,
}
mounts = append(mounts, volumeMount)