diff --git a/operator/config/samples/karmada.yaml b/operator/config/samples/karmada.yaml index 2f8c462d2..ad13d2448 100644 --- a/operator/config/samples/karmada.yaml +++ b/operator/config/samples/karmada.yaml @@ -11,7 +11,18 @@ spec: imageTag: 3.5.3-0 replicas: 1 volumeData: - emptyDir: {} + # hostPath: + # type: DirectoryOrCreate + # path: /var/lib/karmada/etcd/karmada-demo + volumeClaim: + metadata: + name: etcd-data + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 3Gi karmadaAPIServer: imageRepository: registry.k8s.io/kube-apiserver imageTag: v1.25.4 diff --git a/operator/pkg/constants/constants.go b/operator/pkg/constants/constants.go index 95dfa407d..2eecdc8e1 100644 --- a/operator/pkg/constants/constants.go +++ b/operator/pkg/constants/constants.go @@ -56,6 +56,8 @@ const ( EtcdListenPeerPort = 2380 // KarmadaAPIserverListenClientPort defines the port karmada apiserver listen on for client traffic KarmadaAPIserverListenClientPort = 5443 + // EtcdDataVolumeName defines the name to etcd data volume + EtcdDataVolumeName = "etcd-data" // CertificateValidity Certificate validity period CertificateValidity = time.Hour * 24 * 365 diff --git a/operator/pkg/controlplane/etcd/etcd.go b/operator/pkg/controlplane/etcd/etcd.go index e9f7b0532..0f3d37e38 100644 --- a/operator/pkg/controlplane/etcd/etcd.go +++ b/operator/pkg/controlplane/etcd/etcd.go @@ -45,10 +45,10 @@ func installKarmadaEtcd(client clientset.Interface, name, namespace string, cfg } etcdStatefuleSetBytes, err := util.ParseTemplate(KarmadaEtcdStatefulSet, struct { - StatefulSetName, Namespace, Image string - EtcdClientService, CertsSecretName string - EtcdPeerServiceName, InitialCluster string - Replicas, EtcdListenClientPort, EtcdListenPeerPort int32 + StatefulSetName, Namespace, Image, EtcdClientService string + CertsSecretName, EtcdPeerServiceName string + InitialCluster, EtcdDataVolumeName string + Replicas, EtcdListenClientPort, EtcdListenPeerPort int32 }{ StatefulSetName: util.KarmadaEtcdName(name), Namespace: namespace, @@ -56,6 +56,7 @@ func installKarmadaEtcd(client clientset.Interface, name, namespace string, cfg EtcdClientService: util.KarmadaEtcdClientName(name), CertsSecretName: util.EtcdCertSecretName(name), EtcdPeerServiceName: util.KarmadaEtcdName(name), + EtcdDataVolumeName: constants.EtcdDataVolumeName, InitialCluster: strings.Join(initialClusters, ","), Replicas: *cfg.Replicas, EtcdListenClientPort: constants.EtcdListenClientPort, @@ -70,7 +71,8 @@ func installKarmadaEtcd(client clientset.Interface, name, namespace string, cfg return fmt.Errorf("error when decoding Etcd StatefulSet: %w", err) } - patcher.NewPatcher().WithAnnotations(cfg.Annotations).WithLabels(cfg.Labels).ForStatefulSet(etcdStatefulSet) + patcher.NewPatcher().WithAnnotations(cfg.Annotations).WithLabels(cfg.Labels). + WithVolumeData(cfg.VolumeData).ForStatefulSet(etcdStatefulSet) if err := apiclient.CreateOrUpdateStatefulSet(client, etcdStatefulSet); err != nil { return fmt.Errorf("error when creating Etcd statefulset, err: %w", err) diff --git a/operator/pkg/controlplane/etcd/mainfests.go b/operator/pkg/controlplane/etcd/mainfests.go index 7eea3dbde..a0e8d8b8a 100644 --- a/operator/pkg/controlplane/etcd/mainfests.go +++ b/operator/pkg/controlplane/etcd/mainfests.go @@ -71,15 +71,13 @@ spec: protocol: TCP volumeMounts: - mountPath: /var/lib/etcd - name: etcd-data + name: {{ .EtcdDataVolumeName }} - mountPath: /etc/karmada/pki/etcd name: etcd-cert volumes: - name: etcd-cert secret: secretName: {{ .CertsSecretName }} - - name: etcd-data - emptyDir: {} ` // KarmadaEtcdClientService is karmada etcd client service manifest diff --git a/operator/pkg/util/patcher/pather.go b/operator/pkg/util/patcher/pather.go index 091c787a3..5ed1d5eef 100644 --- a/operator/pkg/util/patcher/pather.go +++ b/operator/pkg/util/patcher/pather.go @@ -2,13 +2,19 @@ package patcher import ( appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" + + operatorv1alpha1 "github.com/karmada-io/karmada/operator/pkg/apis/operator/v1alpha1" + "github.com/karmada-io/karmada/operator/pkg/constants" ) // Patcher defines multiple variables that need to be patched. type Patcher struct { labels map[string]string annotations map[string]string + volume *operatorv1alpha1.VolumeData } // NewPatcher returns a patcher. @@ -28,6 +34,12 @@ func (p *Patcher) WithAnnotations(annotations labels.Set) *Patcher { return p } +// WithVolumeData sets VolumeData to the patcher. +func (p *Patcher) WithVolumeData(volume *operatorv1alpha1.VolumeData) *Patcher { + p.volume = volume + return p +} + // ForDeployment patches the deployment manifest. func (p *Patcher) ForDeployment(deployment *appsv1.Deployment) { deployment.Labels = labels.Merge(deployment.Labels, p.labels) @@ -44,4 +56,46 @@ func (p *Patcher) ForStatefulSet(sts *appsv1.StatefulSet) { sts.Annotations = labels.Merge(sts.Annotations, p.annotations) sts.Spec.Template.Annotations = labels.Merge(sts.Spec.Template.Annotations, p.annotations) + + if p.volume != nil { + patchVolumeForStatefulSet(sts, p.volume) + } +} + +func patchVolumeForStatefulSet(sts *appsv1.StatefulSet, volume *operatorv1alpha1.VolumeData) { + if volume.EmptyDir != nil { + volumes := sts.Spec.Template.Spec.Volumes + volumes = append(volumes, corev1.Volume{ + Name: constants.EtcdDataVolumeName, + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }) + sts.Spec.Template.Spec.Volumes = volumes + } + + if volume.HostPath != nil { + volumes := sts.Spec.Template.Spec.Volumes + volumes = append(volumes, corev1.Volume{ + Name: constants.EtcdDataVolumeName, + VolumeSource: corev1.VolumeSource{ + HostPath: &corev1.HostPathVolumeSource{ + Path: volume.HostPath.Path, + Type: volume.HostPath.Type, + }, + }, + }) + sts.Spec.Template.Spec.Volumes = volumes + } + + if volume.VolumeClaim != nil { + sts.Spec.VolumeClaimTemplates = []corev1.PersistentVolumeClaim{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: constants.EtcdDataVolumeName, + }, + Spec: volume.VolumeClaim.Spec, + }, + } + } }