From efa868168965fd09add0764aece6941be5e9396d Mon Sep 17 00:00:00 2001 From: Abhishek Dasgupta Date: Thu, 11 Nov 2021 19:15:40 +0530 Subject: [PATCH] Addressed Stoyan and Tim's comment on 11 Nov. --- ...ller.go => compaction_lease_controller.go} | 32 +-- ...go => compaction_lease_controller_test.go} | 0 controllers/config/compaction.go | 4 +- controllers/controller_ref_manager.go | 245 ------------------ controllers/controllers_suite_test.go | 2 +- controllers/etcd_controller.go | 243 +++++++++++++++++ controllers/etcdcopybackupstask_controller.go | 6 +- main.go | 9 +- pkg/predicate/predicate_test.go | 10 - 9 files changed, 267 insertions(+), 284 deletions(-) rename controllers/{lease_controller.go => compaction_lease_controller.go} (92%) rename controllers/{lease_controller_test.go => compaction_lease_controller_test.go} (100%) diff --git a/controllers/lease_controller.go b/controllers/compaction_lease_controller.go similarity index 92% rename from controllers/lease_controller.go rename to controllers/compaction_lease_controller.go index 5fd38648d..b06a96c5b 100644 --- a/controllers/lease_controller.go +++ b/controllers/compaction_lease_controller.go @@ -45,32 +45,32 @@ import ( ) const DefaultETCDQuota = 8 * 1024 * 1024 * 1024 // 8Gi -// LeaseController reconciles compaction job -type LeaseController struct { +// CompactionLeaseController reconciles compaction job +type CompactionLeaseController struct { client.Client logger logr.Logger ImageVector imagevector.ImageVector - config controllersconfig.CompactionConfig + config controllersconfig.CompactionLeaseConfig } -// NewLeaseController creates a new LeaseController object -func NewLeaseController(mgr manager.Manager, config controllersconfig.CompactionConfig) *LeaseController { - return &LeaseController{ +// NewCompactionLeaseController creates a new LeaseController object +func NewCompactionLeaseController(mgr manager.Manager, config controllersconfig.CompactionLeaseConfig) *CompactionLeaseController { + return &CompactionLeaseController{ Client: mgr.GetClient(), - logger: log.Log.WithName("lease-controller"), + logger: log.Log.WithName("compaction-lease-controller"), config: config, } } -// NewLeaseControllerWithImageVector creates a new LeaseController object -func NewLeaseControllerWithImageVector(mgr manager.Manager, config controllersconfig.CompactionConfig) (*LeaseController, error) { - lc := NewLeaseController(mgr, config) +// NewCompactionLeaseControllerWithImageVector creates a new LeaseController object +func NewCompactionLeaseControllerWithImageVector(mgr manager.Manager, config controllersconfig.CompactionLeaseConfig) (*CompactionLeaseController, error) { + lc := NewCompactionLeaseController(mgr, config) return lc.InitializeControllerWithImageVector() } // InitializeControllerWithImageVector will use LeaseController client to initialize image vector for etcd // and backup restore images. -func (lc *LeaseController) InitializeControllerWithImageVector() (*LeaseController, error) { +func (lc *CompactionLeaseController) InitializeControllerWithImageVector() (*CompactionLeaseController, error) { imageVector, err := imagevector.ReadGlobalImageVectorWithEnvOverride(getImageYAMLPath()) if err != nil { return nil, err @@ -84,7 +84,7 @@ func (lc *LeaseController) InitializeControllerWithImageVector() (*LeaseControll // +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;create;list;watch;update;patch;delete // Reconcile reconciles the compaction job. -func (lc *LeaseController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (lc *CompactionLeaseController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { lc.logger.Info("Lease controller reconciliation started") etcd := &druidv1alpha1.Etcd{} if err := lc.Get(ctx, req.NamespacedName, etcd); err != nil { @@ -168,7 +168,7 @@ func (lc *LeaseController) Reconcile(ctx context.Context, req ctrl.Request) (ctr }, nil } -func (lc *LeaseController) reconcileJob(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (ctrl.Result, error) { +func (lc *CompactionLeaseController) reconcileJob(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (ctrl.Result, error) { logger.Info("Reconcile etcd compaction job") // First check if a job is already running @@ -214,7 +214,7 @@ func (lc *LeaseController) reconcileJob(ctx context.Context, logger logr.Logger, return ctrl.Result{Requeue: false}, nil } -func (lc *LeaseController) delete(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (ctrl.Result, error) { +func (lc *CompactionLeaseController) delete(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (ctrl.Result, error) { job := &batchv1.Job{} err := lc.Get(ctx, types.NamespacedName{Name: getJobName(etcd), Namespace: etcd.Namespace}, job) if err != nil { @@ -237,7 +237,7 @@ func (lc *LeaseController) delete(ctx context.Context, logger logr.Logger, etcd return ctrl.Result{Requeue: false}, nil } -func (lc *LeaseController) createCompactJob(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (*batchv1.Job, error) { +func (lc *CompactionLeaseController) createCompactJob(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (*batchv1.Job, error) { activeDeadlineSeconds := lc.config.ActiveDeadlineDuration.Seconds() _, etcdBackupImage, err := getEtcdImages(lc.ImageVector, etcd) @@ -531,7 +531,7 @@ func getCompactJobCommands(etcd *druidv1alpha1.Etcd) []string { } // SetupWithManager sets up manager with a new controller and ec as the reconcile.Reconciler -func (lc *LeaseController) SetupWithManager(mgr ctrl.Manager, workers int) error { +func (lc *CompactionLeaseController) SetupWithManager(mgr ctrl.Manager, workers int) error { builder := ctrl.NewControllerManagedBy(mgr).WithOptions(controller.Options{ MaxConcurrentReconciles: workers, }) diff --git a/controllers/lease_controller_test.go b/controllers/compaction_lease_controller_test.go similarity index 100% rename from controllers/lease_controller_test.go rename to controllers/compaction_lease_controller_test.go diff --git a/controllers/config/compaction.go b/controllers/config/compaction.go index 091593e8e..9c89a7c6b 100644 --- a/controllers/config/compaction.go +++ b/controllers/config/compaction.go @@ -16,8 +16,8 @@ package config import "time" -// CompactionConfig contains configuration for the compaction controller. -type CompactionConfig struct { +// CompactionLeaseConfig contains configuration for the compaction controller. +type CompactionLeaseConfig struct { // ActiveDeadlineDuration is the duration after which a running compaction job will be killed (Ex: "300ms", "20s", "-1.5h" or "2h45m") ActiveDeadlineDuration time.Duration // EventsThreshold is total number of etcd events that can be allowed before a backup compaction job is triggered diff --git a/controllers/controller_ref_manager.go b/controllers/controller_ref_manager.go index f6b44be58..626192c5f 100644 --- a/controllers/controller_ref_manager.go +++ b/controllers/controller_ref_manager.go @@ -37,8 +37,6 @@ import ( druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" "github.com/gardener/etcd-druid/pkg/common" - "github.com/gardener/etcd-druid/pkg/utils" - "github.com/gardener/gardener/pkg/utils/imagevector" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -523,246 +521,3 @@ func CheckStatefulSet(etcd *druidv1alpha1.Etcd, statefulSet *appsv1.StatefulSet) return nil } - -func getMapFromEtcd(im imagevector.ImageVector, etcd *druidv1alpha1.Etcd) (map[string]interface{}, error) { - var statefulsetReplicas int - if etcd.Spec.Replicas != 0 { - statefulsetReplicas = 1 - } - - etcdValues := map[string]interface{}{ - "defragmentationSchedule": etcd.Spec.Etcd.DefragmentationSchedule, - "enableTLS": (etcd.Spec.Etcd.TLS != nil), - "pullPolicy": corev1.PullIfNotPresent, - // "username": etcd.Spec.Etcd.Username, - // "password": etcd.Spec.Etcd.Password, - } - - if etcd.Spec.Etcd.Resources != nil { - etcdValues["resources"] = etcd.Spec.Etcd.Resources - } - - if etcd.Spec.Etcd.Metrics != nil { - etcdValues["metrics"] = etcd.Spec.Etcd.Metrics - } - - if etcd.Spec.Etcd.ServerPort != nil { - etcdValues["serverPort"] = etcd.Spec.Etcd.ServerPort - } - - if etcd.Spec.Etcd.ClientPort != nil { - etcdValues["clientPort"] = etcd.Spec.Etcd.ClientPort - } - - if etcd.Spec.Etcd.EtcdDefragTimeout != nil { - etcdValues["etcdDefragTimeout"] = etcd.Spec.Etcd.EtcdDefragTimeout - } - - etcdImage, etcdBackupImage, err := getEtcdImages(im, etcd) - if err != nil { - return map[string]interface{}{}, err - } - - if etcd.Spec.Etcd.Image == nil { - if etcdImage == "" { - return map[string]interface{}{}, fmt.Errorf("either etcd resource or image vector should have %s image", common.Etcd) - } - etcdValues["image"] = etcdImage - } else { - etcdValues["image"] = etcd.Spec.Etcd.Image - } - var quota int64 = 8 * 1024 * 1024 * 1024 // 8Gi - if etcd.Spec.Etcd.Quota != nil { - quota = etcd.Spec.Etcd.Quota.Value() - } - - var deltaSnapshotMemoryLimit int64 = 100 * 1024 * 1024 // 100Mi - if etcd.Spec.Backup.DeltaSnapshotMemoryLimit != nil { - deltaSnapshotMemoryLimit = etcd.Spec.Backup.DeltaSnapshotMemoryLimit.Value() - } - - var enableProfiling = false - if etcd.Spec.Backup.EnableProfiling != nil { - enableProfiling = *etcd.Spec.Backup.EnableProfiling - } - - backupValues := map[string]interface{}{ - "pullPolicy": corev1.PullIfNotPresent, - "etcdQuotaBytes": quota, - "etcdConnectionTimeout": "5m", - "snapstoreTempDir": "/var/etcd/data/temp", - "deltaSnapshotMemoryLimit": deltaSnapshotMemoryLimit, - "enableProfiling": enableProfiling, - } - - if etcd.Spec.Backup.Resources != nil { - backupValues["resources"] = etcd.Spec.Backup.Resources - } - - if etcd.Spec.Backup.FullSnapshotSchedule != nil { - backupValues["fullSnapshotSchedule"] = etcd.Spec.Backup.FullSnapshotSchedule - } - - if etcd.Spec.Backup.GarbageCollectionPolicy != nil { - backupValues["garbageCollectionPolicy"] = etcd.Spec.Backup.GarbageCollectionPolicy - } - - if etcd.Spec.Backup.GarbageCollectionPeriod != nil { - backupValues["garbageCollectionPeriod"] = etcd.Spec.Backup.GarbageCollectionPeriod - } - - if etcd.Spec.Backup.DeltaSnapshotPeriod != nil { - backupValues["deltaSnapshotPeriod"] = etcd.Spec.Backup.DeltaSnapshotPeriod - } - - if etcd.Spec.Backup.EtcdSnapshotTimeout != nil { - backupValues["etcdSnapshotTimeout"] = etcd.Spec.Backup.EtcdSnapshotTimeout - } - - if etcd.Spec.Backup.Port != nil { - backupValues["port"] = etcd.Spec.Backup.Port - } - - if etcd.Spec.Backup.SnapshotCompression != nil { - compressionValues := make(map[string]interface{}) - if etcd.Spec.Backup.SnapshotCompression.Enabled { - compressionValues["enabled"] = etcd.Spec.Backup.SnapshotCompression.Enabled - } - if etcd.Spec.Backup.SnapshotCompression.Policy != nil { - compressionValues["policy"] = etcd.Spec.Backup.SnapshotCompression.Policy - } - backupValues["compression"] = compressionValues - } - - if etcd.Spec.Backup.Image == nil { - if etcdBackupImage == "" { - return map[string]interface{}{}, fmt.Errorf("either etcd resource or image vector should have %s image", common.BackupRestore) - } - backupValues["image"] = etcdBackupImage - } else { - backupValues["image"] = etcd.Spec.Backup.Image - } - - if etcd.Spec.Backup.OwnerCheck != nil { - ownerCheckValues := map[string]interface{}{ - "name": etcd.Spec.Backup.OwnerCheck.Name, - "id": etcd.Spec.Backup.OwnerCheck.ID, - } - if etcd.Spec.Backup.OwnerCheck.Interval != nil { - ownerCheckValues["interval"] = etcd.Spec.Backup.OwnerCheck.Interval - } - if etcd.Spec.Backup.OwnerCheck.Timeout != nil { - ownerCheckValues["timeout"] = etcd.Spec.Backup.OwnerCheck.Timeout - } - if etcd.Spec.Backup.OwnerCheck.DNSCacheTTL != nil { - ownerCheckValues["dnsCacheTTL"] = etcd.Spec.Backup.OwnerCheck.DNSCacheTTL - } - backupValues["ownerCheck"] = ownerCheckValues - } - - volumeClaimTemplateName := etcd.Name - if etcd.Spec.VolumeClaimTemplate != nil && len(*etcd.Spec.VolumeClaimTemplate) != 0 { - volumeClaimTemplateName = *etcd.Spec.VolumeClaimTemplate - } - - sharedConfigValues := map[string]interface{}{ - "autoCompactionMode": druidv1alpha1.Periodic, - "autoCompactionRetention": DefaultAutoCompactionRetention, - } - - if etcd.Spec.Common.AutoCompactionMode != nil { - sharedConfigValues["autoCompactionMode"] = etcd.Spec.Common.AutoCompactionMode - } - - if etcd.Spec.Common.AutoCompactionRetention != nil { - sharedConfigValues["autoCompactionRetention"] = etcd.Spec.Common.AutoCompactionRetention - } - - values := map[string]interface{}{ - "name": etcd.Name, - "uid": etcd.UID, - "selector": etcd.Spec.Selector, - "labels": etcd.Spec.Labels, - "annotations": etcd.Spec.Annotations, - "etcd": etcdValues, - "backup": backupValues, - "sharedConfig": sharedConfigValues, - "replicas": etcd.Spec.Replicas, - "statefulsetReplicas": statefulsetReplicas, - "serviceName": fmt.Sprintf("%s-client", etcd.Name), - "configMapName": fmt.Sprintf("etcd-bootstrap-%s", string(etcd.UID[:6])), - "fullSnapLeaseName": getFullSnapshotLeaseName(etcd), - "deltaSnapLeaseName": getDeltaSnapshotLeaseName(etcd), - "jobName": getJobName(etcd), - "volumeClaimTemplateName": volumeClaimTemplateName, - "serviceAccountName": getServiceAccountName(etcd), - "roleName": fmt.Sprintf("%s-br-role", etcd.Name), - "roleBindingName": fmt.Sprintf("%s-br-rolebinding", etcd.Name), - } - - if etcd.Spec.StorageCapacity != nil { - values["storageCapacity"] = etcd.Spec.StorageCapacity - } - - if etcd.Spec.StorageClass != nil { - values["storageClass"] = etcd.Spec.StorageClass - } - - if etcd.Spec.PriorityClassName != nil { - values["priorityClassName"] = *etcd.Spec.PriorityClassName - } - - if etcd.Spec.Etcd.TLS != nil { - values["tlsServerSecret"] = etcd.Spec.Etcd.TLS.ServerTLSSecretRef.Name - values["tlsClientSecret"] = etcd.Spec.Etcd.TLS.ClientTLSSecretRef.Name - values["tlsCASecret"] = etcd.Spec.Etcd.TLS.TLSCASecretRef.Name - } - - if etcd.Spec.Backup.Store != nil { - if values["store"], err = utils.GetStoreValues(etcd.Spec.Backup.Store); err != nil { - return nil, err - } - } - - return values, nil -} - -func getServiceAccountName(etcd *druidv1alpha1.Etcd) string { - return fmt.Sprintf("%s-br-serviceaccount", etcd.Name) -} - -func getEtcdImages(im imagevector.ImageVector, etcd *druidv1alpha1.Etcd) (string, string, error) { - var ( - err error - images map[string]*imagevector.Image - etcdImage, etcdBackupImage string - ) - - imageNames := []string{ - common.Etcd, - common.BackupRestore, - } - - if etcd.Spec.Etcd.Image == nil || etcd.Spec.Backup.Image == nil { - - images, err = imagevector.FindImages(im, imageNames) - if err != nil { - return "", "", err - } - } - - val, ok := images[common.Etcd] - if !ok { - etcdImage = "" - } else { - etcdImage = val.String() - } - - val, ok = images[common.BackupRestore] - if !ok { - etcdBackupImage = "" - } else { - etcdBackupImage = val.String() - } - return etcdImage, etcdBackupImage, nil -} diff --git a/controllers/controllers_suite_test.go b/controllers/controllers_suite_test.go index c8a666d79..106cd3b0e 100644 --- a/controllers/controllers_suite_test.go +++ b/controllers/controllers_suite_test.go @@ -126,7 +126,7 @@ var _ = BeforeSuite(func(done Done) { activeDeadlineDuration, err = time.ParseDuration("2m") Expect(err).NotTo(HaveOccurred()) - lc, err := NewLeaseControllerWithImageVector(mgr, controllersconfig.CompactionConfig{ + lc, err := NewCompactionLeaseControllerWithImageVector(mgr, controllersconfig.CompactionLeaseConfig{ EventsThreshold: 1000000, ActiveDeadlineDuration: activeDeadlineDuration, }) diff --git a/controllers/etcd_controller.go b/controllers/etcd_controller.go index 1f1b3ca0b..0c019ede4 100644 --- a/controllers/etcd_controller.go +++ b/controllers/etcd_controller.go @@ -1180,6 +1180,249 @@ func checkEtcdAnnotations(annotations map[string]string, etcd metav1.Object) boo } +func getMapFromEtcd(im imagevector.ImageVector, etcd *druidv1alpha1.Etcd) (map[string]interface{}, error) { + var statefulsetReplicas int + if etcd.Spec.Replicas != 0 { + statefulsetReplicas = 1 + } + + etcdValues := map[string]interface{}{ + "defragmentationSchedule": etcd.Spec.Etcd.DefragmentationSchedule, + "enableTLS": (etcd.Spec.Etcd.TLS != nil), + "pullPolicy": corev1.PullIfNotPresent, + // "username": etcd.Spec.Etcd.Username, + // "password": etcd.Spec.Etcd.Password, + } + + if etcd.Spec.Etcd.Resources != nil { + etcdValues["resources"] = etcd.Spec.Etcd.Resources + } + + if etcd.Spec.Etcd.Metrics != nil { + etcdValues["metrics"] = etcd.Spec.Etcd.Metrics + } + + if etcd.Spec.Etcd.ServerPort != nil { + etcdValues["serverPort"] = etcd.Spec.Etcd.ServerPort + } + + if etcd.Spec.Etcd.ClientPort != nil { + etcdValues["clientPort"] = etcd.Spec.Etcd.ClientPort + } + + if etcd.Spec.Etcd.EtcdDefragTimeout != nil { + etcdValues["etcdDefragTimeout"] = etcd.Spec.Etcd.EtcdDefragTimeout + } + + etcdImage, etcdBackupImage, err := getEtcdImages(im, etcd) + if err != nil { + return map[string]interface{}{}, err + } + + if etcd.Spec.Etcd.Image == nil { + if etcdImage == "" { + return map[string]interface{}{}, fmt.Errorf("either etcd resource or image vector should have %s image", common.Etcd) + } + etcdValues["image"] = etcdImage + } else { + etcdValues["image"] = etcd.Spec.Etcd.Image + } + var quota int64 = 8 * 1024 * 1024 * 1024 // 8Gi + if etcd.Spec.Etcd.Quota != nil { + quota = etcd.Spec.Etcd.Quota.Value() + } + + var deltaSnapshotMemoryLimit int64 = 100 * 1024 * 1024 // 100Mi + if etcd.Spec.Backup.DeltaSnapshotMemoryLimit != nil { + deltaSnapshotMemoryLimit = etcd.Spec.Backup.DeltaSnapshotMemoryLimit.Value() + } + + var enableProfiling = false + if etcd.Spec.Backup.EnableProfiling != nil { + enableProfiling = *etcd.Spec.Backup.EnableProfiling + } + + backupValues := map[string]interface{}{ + "pullPolicy": corev1.PullIfNotPresent, + "etcdQuotaBytes": quota, + "etcdConnectionTimeout": "5m", + "snapstoreTempDir": "/var/etcd/data/temp", + "deltaSnapshotMemoryLimit": deltaSnapshotMemoryLimit, + "enableProfiling": enableProfiling, + } + + if etcd.Spec.Backup.Resources != nil { + backupValues["resources"] = etcd.Spec.Backup.Resources + } + + if etcd.Spec.Backup.FullSnapshotSchedule != nil { + backupValues["fullSnapshotSchedule"] = etcd.Spec.Backup.FullSnapshotSchedule + } + + if etcd.Spec.Backup.GarbageCollectionPolicy != nil { + backupValues["garbageCollectionPolicy"] = etcd.Spec.Backup.GarbageCollectionPolicy + } + + if etcd.Spec.Backup.GarbageCollectionPeriod != nil { + backupValues["garbageCollectionPeriod"] = etcd.Spec.Backup.GarbageCollectionPeriod + } + + if etcd.Spec.Backup.DeltaSnapshotPeriod != nil { + backupValues["deltaSnapshotPeriod"] = etcd.Spec.Backup.DeltaSnapshotPeriod + } + + if etcd.Spec.Backup.EtcdSnapshotTimeout != nil { + backupValues["etcdSnapshotTimeout"] = etcd.Spec.Backup.EtcdSnapshotTimeout + } + + if etcd.Spec.Backup.Port != nil { + backupValues["port"] = etcd.Spec.Backup.Port + } + + if etcd.Spec.Backup.SnapshotCompression != nil { + compressionValues := make(map[string]interface{}) + if etcd.Spec.Backup.SnapshotCompression.Enabled { + compressionValues["enabled"] = etcd.Spec.Backup.SnapshotCompression.Enabled + } + if etcd.Spec.Backup.SnapshotCompression.Policy != nil { + compressionValues["policy"] = etcd.Spec.Backup.SnapshotCompression.Policy + } + backupValues["compression"] = compressionValues + } + + if etcd.Spec.Backup.Image == nil { + if etcdBackupImage == "" { + return map[string]interface{}{}, fmt.Errorf("either etcd resource or image vector should have %s image", common.BackupRestore) + } + backupValues["image"] = etcdBackupImage + } else { + backupValues["image"] = etcd.Spec.Backup.Image + } + + if etcd.Spec.Backup.OwnerCheck != nil { + ownerCheckValues := map[string]interface{}{ + "name": etcd.Spec.Backup.OwnerCheck.Name, + "id": etcd.Spec.Backup.OwnerCheck.ID, + } + if etcd.Spec.Backup.OwnerCheck.Interval != nil { + ownerCheckValues["interval"] = etcd.Spec.Backup.OwnerCheck.Interval + } + if etcd.Spec.Backup.OwnerCheck.Timeout != nil { + ownerCheckValues["timeout"] = etcd.Spec.Backup.OwnerCheck.Timeout + } + if etcd.Spec.Backup.OwnerCheck.DNSCacheTTL != nil { + ownerCheckValues["dnsCacheTTL"] = etcd.Spec.Backup.OwnerCheck.DNSCacheTTL + } + backupValues["ownerCheck"] = ownerCheckValues + } + + volumeClaimTemplateName := etcd.Name + if etcd.Spec.VolumeClaimTemplate != nil && len(*etcd.Spec.VolumeClaimTemplate) != 0 { + volumeClaimTemplateName = *etcd.Spec.VolumeClaimTemplate + } + + sharedConfigValues := map[string]interface{}{ + "autoCompactionMode": druidv1alpha1.Periodic, + "autoCompactionRetention": DefaultAutoCompactionRetention, + } + + if etcd.Spec.Common.AutoCompactionMode != nil { + sharedConfigValues["autoCompactionMode"] = etcd.Spec.Common.AutoCompactionMode + } + + if etcd.Spec.Common.AutoCompactionRetention != nil { + sharedConfigValues["autoCompactionRetention"] = etcd.Spec.Common.AutoCompactionRetention + } + + values := map[string]interface{}{ + "name": etcd.Name, + "uid": etcd.UID, + "selector": etcd.Spec.Selector, + "labels": etcd.Spec.Labels, + "annotations": etcd.Spec.Annotations, + "etcd": etcdValues, + "backup": backupValues, + "sharedConfig": sharedConfigValues, + "replicas": etcd.Spec.Replicas, + "statefulsetReplicas": statefulsetReplicas, + "serviceName": fmt.Sprintf("%s-client", etcd.Name), + "configMapName": fmt.Sprintf("etcd-bootstrap-%s", string(etcd.UID[:6])), + "fullSnapLeaseName": getFullSnapshotLeaseName(etcd), + "deltaSnapLeaseName": getDeltaSnapshotLeaseName(etcd), + "jobName": getJobName(etcd), + "volumeClaimTemplateName": volumeClaimTemplateName, + "serviceAccountName": getServiceAccountName(etcd), + "roleName": fmt.Sprintf("%s-br-role", etcd.Name), + "roleBindingName": fmt.Sprintf("%s-br-rolebinding", etcd.Name), + } + + if etcd.Spec.StorageCapacity != nil { + values["storageCapacity"] = etcd.Spec.StorageCapacity + } + + if etcd.Spec.StorageClass != nil { + values["storageClass"] = etcd.Spec.StorageClass + } + + if etcd.Spec.PriorityClassName != nil { + values["priorityClassName"] = *etcd.Spec.PriorityClassName + } + + if etcd.Spec.Etcd.TLS != nil { + values["tlsServerSecret"] = etcd.Spec.Etcd.TLS.ServerTLSSecretRef.Name + values["tlsClientSecret"] = etcd.Spec.Etcd.TLS.ClientTLSSecretRef.Name + values["tlsCASecret"] = etcd.Spec.Etcd.TLS.TLSCASecretRef.Name + } + + if etcd.Spec.Backup.Store != nil { + if values["store"], err = utils.GetStoreValues(etcd.Spec.Backup.Store); err != nil { + return nil, err + } + } + + return values, nil +} + +func getServiceAccountName(etcd *druidv1alpha1.Etcd) string { + return fmt.Sprintf("%s-br-serviceaccount", etcd.Name) +} + +func getEtcdImages(im imagevector.ImageVector, etcd *druidv1alpha1.Etcd) (string, string, error) { + var ( + err error + images map[string]*imagevector.Image + etcdImage, etcdBackupImage string + ) + + imageNames := []string{ + common.Etcd, + common.BackupRestore, + } + + if etcd.Spec.Etcd.Image == nil || etcd.Spec.Backup.Image == nil { + + images, err = imagevector.FindImages(im, imageNames) + if err != nil { + return "", "", err + } + } + + val, ok := images[common.Etcd] + if !ok { + etcdImage = "" + } else { + etcdImage = val.String() + } + + val, ok = images[common.BackupRestore] + if !ok { + etcdBackupImage = "" + } else { + etcdBackupImage = val.String() + } + return etcdImage, etcdBackupImage, nil +} + func (r *EtcdReconciler) addFinalizersToDependantSecrets(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) error { secrets := []*corev1.SecretReference{} if etcd.Spec.Etcd.TLS != nil { diff --git a/controllers/etcdcopybackupstask_controller.go b/controllers/etcdcopybackupstask_controller.go index df1f72055..133e43642 100644 --- a/controllers/etcdcopybackupstask_controller.go +++ b/controllers/etcdcopybackupstask_controller.go @@ -265,7 +265,7 @@ func (r *EtcdCopyBackupsTaskReconciler) doDelete(ctx context.Context, task *drui func (r *EtcdCopyBackupsTaskReconciler) getJob(ctx context.Context, task *druidv1alpha1.EtcdCopyBackupsTask) (*batchv1.Job, error) { job := &batchv1.Job{} - if err := r.Get(ctx, kutil.Key(task.Namespace, getCopyJobName(task)), job); err != nil { + if err := r.Get(ctx, kutil.Key(task.Namespace, getCopyBackupsJobName(task)), job); err != nil { if apierrors.IsNotFound(err) { return nil, nil } @@ -285,7 +285,7 @@ func (r *EtcdCopyBackupsTaskReconciler) updateStatus(ctx context.Context, task * func (r *EtcdCopyBackupsTaskReconciler) getChartValues(task *druidv1alpha1.EtcdCopyBackupsTask) (map[string]interface{}, error) { values := map[string]interface{}{ - "name": getCopyJobName(task), + "name": getCopyBackupsJobName(task), "ownerName": task.Name, "ownerUID": task.UID, } @@ -369,7 +369,7 @@ func getConditionType(jobConditionType batchv1.JobConditionType) druidv1alpha1.C return "" } -func getCopyJobName(task *druidv1alpha1.EtcdCopyBackupsTask) string { +func getCopyBackupsJobName(task *druidv1alpha1.EtcdCopyBackupsTask) string { return task.Name + workerSuffix } diff --git a/main.go b/main.go index 72570b1d7..d2a7e2de9 100644 --- a/main.go +++ b/main.go @@ -75,7 +75,7 @@ func main() { flag.IntVar(&custodianWorkers, "custodian-workers", 3, "Number of worker threads of the custodian controller.") flag.IntVar(&etcdCopyBackupsTaskWorkers, "etcd-copy-backups-task-workers", 3, "Number of worker threads of the EtcdCopyBackupsTask controller.") flag.DurationVar(&custodianSyncPeriod, "custodian-sync-period", 30*time.Second, "Sync period of the custodian controller.") - flag.IntVar(&compactionWorkers, "compaction-Workers", 3, "Number of worker threads of the CompactionJob controller. CompactionJob controller runs backup compaction job in parallel. CompactionJob controller can be disabled if this flag is set to 0.") + flag.IntVar(&compactionWorkers, "compaction-workers", 3, "Number of worker threads of the CompactionJob controller. The controller creates a backup compaction job if a certain etcd event threshold is reached. Setting this flag to 0 disabled the controller.") flag.Int64Var(&eventsThreshold, "etcd-events-threshold", 1000000, "Total number of etcd events that can be allowed before a backup compaction job is triggered.") flag.DurationVar(&activeDeadlineDuration, "active-deadline-duration", 3*time.Hour, "Duration after which a running backup compaction job will be killed (Ex: \"300ms\", \"20s\", \"-1.5h\" or \"2h45m\").") flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") @@ -132,11 +132,6 @@ func main() { SyncPeriod: custodianSyncPeriod, }) - if err != nil { - setupLog.Error(err, "Unable to initialize etcd custodian controller with image vector") - os.Exit(1) - } - if err := custodian.SetupWithManager(ctx, mgr, custodianWorkers); err != nil { setupLog.Error(err, "Unable to create controller", "Controller", "Etcd Custodian") os.Exit(1) @@ -152,7 +147,7 @@ func main() { setupLog.Error(err, "Unable to create controller", "Controller", "EtcdCopyBackupsTask") } - lc, err := controllers.NewLeaseControllerWithImageVector(mgr, controllersconfig.CompactionConfig{ + lc, err := controllers.NewCompactionLeaseControllerWithImageVector(mgr, controllersconfig.CompactionLeaseConfig{ EventsThreshold: eventsThreshold, ActiveDeadlineDuration: activeDeadlineDuration, }) diff --git a/pkg/predicate/predicate_test.go b/pkg/predicate/predicate_test.go index 5a6925def..924a823d1 100644 --- a/pkg/predicate/predicate_test.go +++ b/pkg/predicate/predicate_test.go @@ -15,8 +15,6 @@ package predicate_test import ( - "time" - druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" . "github.com/onsi/ginkgo" @@ -120,18 +118,14 @@ var _ = Describe("Druid Predicate", func() { Context("when holder identity matches", func() { BeforeEach(func() { - renewTime := metav1.NewMicroTime(time.Now()) obj = &coordinationv1.Lease{ Spec: coordinationv1.LeaseSpec{ HolderIdentity: pointer.StringPtr("0"), - RenewTime: &renewTime, }, } - renewTime = metav1.NewMicroTime(time.Now()) oldObj = &coordinationv1.Lease{ Spec: coordinationv1.LeaseSpec{ HolderIdentity: pointer.StringPtr("0"), - RenewTime: &renewTime, }, } }) @@ -146,18 +140,14 @@ var _ = Describe("Druid Predicate", func() { Context("when holder identity differs", func() { BeforeEach(func() { - renewTime := metav1.NewMicroTime(time.Now()) obj = &coordinationv1.Lease{ Spec: coordinationv1.LeaseSpec{ HolderIdentity: pointer.StringPtr("5"), - RenewTime: &renewTime, }, } - renewTime = metav1.NewMicroTime(time.Now()) oldObj = &coordinationv1.Lease{ Spec: coordinationv1.LeaseSpec{ HolderIdentity: pointer.StringPtr("0"), - RenewTime: &renewTime, }, } })