support to set etcd pvc
Signed-off-by: calvin <wen.chen@daocloud.io>
This commit is contained in:
parent
64d88459bb
commit
c7741aa20b
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -45,9 +45,9 @@ 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
|
||||
StatefulSetName, Namespace, Image, EtcdClientService string
|
||||
CertsSecretName, EtcdPeerServiceName string
|
||||
InitialCluster, EtcdDataVolumeName string
|
||||
Replicas, EtcdListenClientPort, EtcdListenPeerPort int32
|
||||
}{
|
||||
StatefulSetName: util.KarmadaEtcdName(name),
|
||||
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue