Skip to content

Commit

Permalink
Addressed Tim and Shreyas's reviews of 10 Nov.
Browse files Browse the repository at this point in the history
  • Loading branch information
abdasgupta committed Nov 11, 2021
1 parent 8f3d21b commit 56cf3a1
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 286 deletions.
2 changes: 1 addition & 1 deletion controllers/config/compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import "time"
type CompactionConfig 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 events that can be allowed before a compaction job is triggered
// EventsThreshold is total number of etcd events that can be allowed before a backup compaction job is triggered
EventsThreshold int64
}
5 changes: 2 additions & 3 deletions controllers/controller_ref_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,6 @@ func getMapFromEtcd(im imagevector.ImageVector, etcd *druidv1alpha1.Etcd) (map[s
var enableProfiling = false
if etcd.Spec.Backup.EnableProfiling != nil {
enableProfiling = *etcd.Spec.Backup.EnableProfiling

}

backupValues := map[string]interface{}{
Expand Down Expand Up @@ -692,8 +691,8 @@ func getMapFromEtcd(im imagevector.ImageVector, etcd *druidv1alpha1.Etcd) (map[s
"statefulsetReplicas": statefulsetReplicas,
"serviceName": fmt.Sprintf("%s-client", etcd.Name),
"configMapName": fmt.Sprintf("etcd-bootstrap-%s", string(etcd.UID[:6])),
"fullSnapLeaseName": getFullLease(etcd),
"deltaSnapLeaseName": getDeltaLease(etcd),
"fullSnapLeaseName": getFullSnapshotLeaseName(etcd),
"deltaSnapLeaseName": getDeltaSnapshotLeaseName(etcd),
"jobName": getJobName(etcd),
"volumeClaimTemplateName": volumeClaimTemplateName,
"serviceAccountName": getServiceAccountName(etcd),
Expand Down
2 changes: 0 additions & 2 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ var _ = BeforeSuite(func(done Done) {
},
})

Expect(err).NotTo(HaveOccurred())

err = custodian.SetupWithManager(mgrCtx, mgr, 1)
Expect(err).NotTo(HaveOccurred())

Expand Down
107 changes: 26 additions & 81 deletions controllers/etcd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ func (r *EtcdReconciler) getStatefulSetFromEtcd(etcd *druidv1alpha1.Etcd, values

func (r *EtcdReconciler) reconcileFullLease(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (*coordinationv1.Lease, error) {
// Get or Create fullSnapshotRevisions lease object that will help to set BackupReady condition
fullSnapshotRevisions := getFullLease(etcd)
fullSnapshotRevisions := getFullSnapshotLeaseName(etcd)
nsName := types.NamespacedName{
Name: fullSnapshotRevisions,
Namespace: etcd.Namespace,
Expand All @@ -892,27 +892,7 @@ func (r *EtcdReconciler) reconcileFullLease(ctx context.Context, logger logr.Log
if apierrors.IsNotFound(err1) {
logger.Info("Creating the full snap lease " + fullSnapshotRevisions)

renewTime := metav1.NewMicroTime(time.Now())
fullLease = &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: fullSnapshotRevisions,
Namespace: etcd.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "druid.gardener.cloud/v1alpha1",
BlockOwnerDeletion: pointer.BoolPtr(true),
Controller: pointer.BoolPtr(true),
Kind: "Etcd",
Name: etcd.Name,
UID: etcd.UID,
},
},
},
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr("0"),
RenewTime: &renewTime,
},
}
fullLease = createSnapshotLease(etcd, fullSnapshotRevisions)
err2 := r.Create(ctx, fullLease)
if err2 != nil {
logger.Error(err2, "Full snap lease "+fullSnapshotRevisions+" couldn't be created")
Expand All @@ -922,36 +902,18 @@ func (r *EtcdReconciler) reconcileFullLease(ctx context.Context, logger logr.Log
return nil, err1
}
}
if !checkEtcdOwnerReference(fullLease.GetOwnerReferences(), etcd) {
err := kutil.TryPatch(ctx, retry.DefaultBackoff, r.Client, fullLease, func() error {
fullLease.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "druid.gardener.cloud/v1alpha1",
BlockOwnerDeletion: pointer.BoolPtr(true),
Controller: pointer.BoolPtr(true),
Kind: "Etcd",
Name: etcd.Name,
UID: etcd.UID,
},
}
return nil
})
if err != nil {
logger.Error(err, "Full snap lease found but error occured during claiming it")
return nil, err
}
}

return fullLease, nil
}

func getFullLease(etcd *druidv1alpha1.Etcd) string {
func getFullSnapshotLeaseName(etcd *druidv1alpha1.Etcd) string {
return fmt.Sprintf("%s-full-snap", string(etcd.Name))
}

func (r *EtcdReconciler) reconcileDeltaLease(ctx context.Context, logger logr.Logger, etcd *druidv1alpha1.Etcd) (*coordinationv1.Lease, error) {
// Get or Create delta_snapshot_revisions lease object that will keep track of delta snapshot revisions based on which
// compaction job will be scheduled
deltaSnapshotRevisions := getDeltaLease(etcd)
deltaSnapshotRevisions := getDeltaSnapshotLeaseName(etcd)
nsName := types.NamespacedName{
Name: deltaSnapshotRevisions,
Namespace: etcd.Namespace,
Expand All @@ -965,27 +927,7 @@ func (r *EtcdReconciler) reconcileDeltaLease(ctx context.Context, logger logr.Lo
if apierrors.IsNotFound(err1) {
logger.Info("Creating the delta snap lease " + deltaSnapshotRevisions)

renewTime := metav1.NewMicroTime(time.Now())
deltaLease = &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: getDeltaLease(etcd),
Namespace: etcd.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "druid.gardener.cloud/v1alpha1",
BlockOwnerDeletion: pointer.BoolPtr(true),
Controller: pointer.BoolPtr(true),
Kind: "Etcd",
Name: etcd.Name,
UID: etcd.UID,
},
},
},
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr("0"),
RenewTime: &renewTime,
},
}
deltaLease = createSnapshotLease(etcd, deltaSnapshotRevisions)
err2 := r.Create(ctx, deltaLease)
if err2 != nil {
logger.Error(err2, "Delta snap lease "+deltaSnapshotRevisions+" couldn't be created")
Expand All @@ -996,9 +938,20 @@ func (r *EtcdReconciler) reconcileDeltaLease(ctx context.Context, logger logr.Lo
}
}

if !checkEtcdOwnerReference(deltaLease.GetOwnerReferences(), etcd) {
err := kutil.TryPatch(ctx, retry.DefaultBackoff, r.Client, deltaLease, func() error {
deltaLease.OwnerReferences = []metav1.OwnerReference{
return deltaLease, nil
}

func getDeltaSnapshotLeaseName(etcd *druidv1alpha1.Etcd) string {
return fmt.Sprintf("%s-delta-snap", string(etcd.Name))
}

func createSnapshotLease(etcd *druidv1alpha1.Etcd, snapshotLeaseName string) *coordinationv1.Lease {
renewTime := metav1.NewMicroTime(time.Now())
return &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: snapshotLeaseName,
Namespace: etcd.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "druid.gardener.cloud/v1alpha1",
BlockOwnerDeletion: pointer.BoolPtr(true),
Expand All @@ -1007,22 +960,14 @@ func (r *EtcdReconciler) reconcileDeltaLease(ctx context.Context, logger logr.Lo
Name: etcd.Name,
UID: etcd.UID,
},
}
return nil
})
if err != nil {
logger.Error(err, "Delta snap lease found but error occured during claiming it")
return nil, err
}
},
},
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr("0"),
RenewTime: &renewTime,
},
}

return deltaLease, nil
}

func getDeltaLease(etcd *druidv1alpha1.Etcd) string {
return fmt.Sprintf("%s-delta-snap", string(etcd.Name))
}

func decodeObject(renderedChart *chartrenderer.RenderedChart, path string, object interface{}) error {
if content, ok := renderedChart.Files()[path]; ok {
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader([]byte(content)), 1024)
Expand Down
Loading

0 comments on commit 56cf3a1

Please sign in to comment.