Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add owner reference to the resources made by rotator to delete them when read replica resources are deleted #2287

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Makefile.d/k8s.mk
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,45 @@ k8s/vald-helm-operator/delete:
kubectl delete -f $(TEMP_DIR)/vald-helm-operator/crds
rm -rf $(TEMP_DIR)

.PHONY: k8s/vald-readreplica/deploy
## deploy vald-readreplica to k8s
k8s/vald-readreplica/deploy:
helm template \
--values $(HELM_VALUES) \
--set defaults.image.tag=$(VERSION) \
--set agent.image.repository=$(CRORG)/$(AGENT_IMAGE) \
--set agent.sidecar.image.repository=$(CRORG)/$(AGENT_SIDECAR_IMAGE) \
--set discoverer.image.repository=$(CRORG)/$(DISCOVERER_IMAGE) \
--set gateway.filter.image.repository=$(CRORG)/$(FILTER_GATEWAY_IMAGE) \
--set gateway.lb.image.repository=$(CRORG)/$(LB_GATEWAY_IMAGE) \
--set manager.index.image.repository=$(CRORG)/$(MANAGER_INDEX_IMAGE) \
--set manager.index.creator.image.repository=$(CRORG)/$(INDEX_CREATION_IMAGE) \
--set manager.index.saver.image.repository=$(CRORG)/$(INDEX_SAVE_IMAGE) \
$(HELM_EXTRA_OPTIONS) \
--output-dir $(TEMP_DIR) \
charts/vald-readreplica
kubectl apply -f $(TEMP_DIR)/vald-readreplica/templates
sleep 2
kubectl wait --for=condition=ready pod -l app.kubernetes.io/name=vald-readreplica --timeout=600s

.PHONY: k8s/vald-readreplica/delete
## delete vald-helm-operator from k8s
k8s/vald-readreplica/delete:
helm template \
--values $(HELM_VALUES) \
--set defaults.image.tag=$(VERSION) \
--set agent.image.repository=$(CRORG)/$(AGENT_IMAGE) \
--set agent.sidecar.image.repository=$(CRORG)/$(AGENT_SIDECAR_IMAGE) \
--set discoverer.image.repository=$(CRORG)/$(DISCOVERER_IMAGE) \
--set gateway.filter.image.repository=$(CRORG)/$(FILTER_GATEWAY_IMAGE) \
--set gateway.lb.image.repository=$(CRORG)/$(LB_GATEWAY_IMAGE) \
--set manager.index.image.repository=$(CRORG)/$(MANAGER_INDEX_IMAGE) \
--output-dir $(TEMP_DIR) \
charts/vald-readreplica
kubectl delete -f $(TEMP_DIR)/vald-readreplica/templates
kubectl wait --for=delete pod -l app.kubernetes.io/name=vald-readreplica --timeout=600s
rm -rf $(TEMP_DIR)

.PHONY: k8s/vr/deploy
## deploy ValdRelease resource to k8s
k8s/vr/deploy: \
Expand Down
50 changes: 40 additions & 10 deletions pkg/index/job/readreplica/rotate/service/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

const (
Expand Down Expand Up @@ -109,12 +110,20 @@
}

func (r *rotator) rotate(ctx context.Context) error {
newSnap, oldSnap, err := r.createSnapshot(ctx)
// get deployment here to pass to create methods of snapshot and pvc
// and put it as owner reference of them so that they will be deleted when the deployment is deleted
deployment, err := r.getDeployment(ctx)

Check warning on line 115 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L113-L115

Added lines #L113 - L115 were not covered by tests
if err != nil {
log.Errorf("failed to get Deployment.")

Check warning on line 117 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L117

Added line #L117 was not covered by tests
return err
}

newPvc, oldPvc, err := r.createPVC(ctx, newSnap.GetName())
newSnap, oldSnap, err := r.createSnapshot(ctx, deployment)
if err != nil {
return err
}

Check warning on line 124 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L121-L124

Added lines #L121 - L124 were not covered by tests

newPvc, oldPvc, err := r.createPVC(ctx, newSnap.GetName(), deployment)

Check warning on line 126 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L126

Added line #L126 was not covered by tests
if err != nil {
log.Errorf("failed to create PVC. removing the new snapshot(%s)...", newSnap.GetName())
if dserr := r.deleteSnapshot(ctx, newSnap); dserr != nil {
Expand All @@ -123,7 +132,7 @@
return err
}

err = r.updateDeployment(ctx, newPvc.GetName())
err = r.updateDeployment(ctx, newPvc.GetName(), deployment)

Check warning on line 135 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L135

Added line #L135 was not covered by tests
if err != nil {
log.Errorf("failed to update Deployment. removing the new snapshot(%s) and pvc(%s)...", newSnap.GetName(), newPvc.GetName())
if dperr := r.deletePVC(ctx, newPvc); dperr != nil {
Expand All @@ -148,7 +157,7 @@
return nil
}

func (r *rotator) createSnapshot(ctx context.Context) (newSnap, oldSnap *client.VolumeSnapshot, err error) {
func (r *rotator) createSnapshot(ctx context.Context, deployment *appsv1.Deployment) (newSnap, oldSnap *client.VolumeSnapshot, err error) {

Check warning on line 160 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L160

Added line #L160 was not covered by tests
list := snapshotv1.VolumeSnapshotList{}
if err := r.client.List(ctx, &list, &r.listOpts); err != nil {
return nil, nil, fmt.Errorf("failed to get snapshot: %w", err)
Expand All @@ -168,6 +177,15 @@
Name: fmt.Sprintf("%s%d", newNameBase, time.Now().Unix()),
Namespace: cur.GetNamespace(),
Labels: cur.GetObjectMeta().GetLabels(),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment.GetName(),
UID: deployment.GetUID(),
Controller: pointer.Bool(true),
},
},

Check warning on line 188 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L180-L188

Added lines #L180 - L188 were not covered by tests
},
Spec: cur.Spec,
}
Expand All @@ -183,7 +201,7 @@
return newSnap, oldSnap, nil
}

func (r *rotator) createPVC(ctx context.Context, newSnapShot string) (newPvc, oldPvc *v1.PersistentVolumeClaim, err error) {
func (r *rotator) createPVC(ctx context.Context, newSnapShot string, deployment *appsv1.Deployment) (newPvc, oldPvc *v1.PersistentVolumeClaim, err error) {

Check warning on line 204 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L204

Added line #L204 was not covered by tests
list := v1.PersistentVolumeClaimList{}
if err := r.client.List(ctx, &list, &r.listOpts); err != nil {
return nil, nil, fmt.Errorf("failed to get PVC: %w", err)
Expand All @@ -205,6 +223,15 @@
Name: fmt.Sprintf("%s%d", newNameBase, time.Now().Unix()),
Namespace: cur.GetNamespace(),
Labels: cur.GetObjectMeta().GetLabels(),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment.GetName(),
UID: deployment.GetUID(),
Controller: pointer.Bool(true),
},
},

Check warning on line 234 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L226-L234

Added lines #L226 - L234 were not covered by tests
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: cur.Spec.AccessModes,
Expand All @@ -227,16 +254,19 @@
return newPvc, oldPvc, nil
}

func (r *rotator) updateDeployment(ctx context.Context, newPVC string) error {
func (r *rotator) getDeployment(ctx context.Context) (*appsv1.Deployment, error) {

Check warning on line 257 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L257

Added line #L257 was not covered by tests
list := appsv1.DeploymentList{}
if err := r.client.List(ctx, &list, &r.listOpts); err != nil {
return fmt.Errorf("failed to get deployment through client: %w", err)
return nil, fmt.Errorf("failed to get deployment through client: %w", err)

Check warning on line 260 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L260

Added line #L260 was not covered by tests
}
if len(list.Items) == 0 {
return fmt.Errorf("no deployment found")
return nil, fmt.Errorf("no deployment found")

Check warning on line 263 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L263

Added line #L263 was not covered by tests
}

deployment := list.Items[0]
return &list.Items[0], nil

Check warning on line 266 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L266

Added line #L266 was not covered by tests
}

func (r *rotator) updateDeployment(ctx context.Context, newPVC string, deployment *appsv1.Deployment) error {

Check warning on line 269 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L269

Added line #L269 was not covered by tests
if deployment.Spec.Template.ObjectMeta.Annotations == nil {
deployment.Spec.Template.ObjectMeta.Annotations = map[string]string{}
}
Expand All @@ -251,7 +281,7 @@
log.Infof("updating deployment(%s)...", deployment.GetName())
log.Debugf("deployment detail: %#v", deployment)

if err := r.client.Update(ctx, &deployment); err != nil {
if err := r.client.Update(ctx, deployment); err != nil {

Check warning on line 284 in pkg/index/job/readreplica/rotate/service/rotator.go

View check run for this annotation

Codecov / codecov/patch

pkg/index/job/readreplica/rotate/service/rotator.go#L284

Added line #L284 was not covered by tests
return fmt.Errorf("failed to update deployment: %w", err)
}

Expand Down
Loading