Skip to content

Commit

Permalink
feat: preventing zombie filestore backups and patching status instead…
Browse files Browse the repository at this point in the history
… of update
  • Loading branch information
abalaie committed Sep 3, 2024
1 parent f49cbf3 commit 7bac086
Show file tree
Hide file tree
Showing 55 changed files with 277 additions and 147 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
strategy:
matrix:
ipRangeAutomaticCidrAllocation: [ "false", "true" ]
gcpNfsVolumeAutomaticLocationAllocation: [ "false", "true" ]
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand All @@ -22,6 +23,7 @@ jobs:
- name: Build and test
env:
FF_IP_RANGE_AUTOMATIC_CIDR_ALLOCATION: ${{ matrix.ipRangeAutomaticCidrAllocation }}
FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION: ${{ matrix.gcpNfsVolumeAutomaticLocationAllocation }}
run: |
./config/sync.sh
go mod tidy
Expand Down
15 changes: 15 additions & 0 deletions api/cloud-resources/v1beta1/gcpnfsbackupschedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
featuretypes "github.com/kyma-project/cloud-manager/pkg/feature/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -280,3 +281,17 @@ type GcpNfsBackupScheduleList struct {
func init() {
SchemeBuilder.Register(&GcpNfsBackupSchedule{}, &GcpNfsBackupScheduleList{})
}

func (sc *GcpNfsBackupSchedule) CloneForPatchStatus() client.Object {
return &GcpNfsBackupSchedule{
TypeMeta: metav1.TypeMeta{
Kind: "GcpNfsBackupSchedule",
APIVersion: GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: sc.Namespace,
Name: sc.Name,
},
Status: sc.Status,
}
}
20 changes: 16 additions & 4 deletions api/cloud-resources/v1beta1/gcpnfsvolumebackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
featuretypes "github.com/kyma-project/cloud-manager/pkg/feature/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type GcpNfsBackupState string
Expand All @@ -32,9 +33,6 @@ const (
// GcpNfsBackupError signifies the backup operation resulted in error.
GcpNfsBackupError GcpNfsBackupState = "Error"

// GcpNfsBackupProcessing signifies backup operation is in-progress.
GcpNfsBackupProcessing GcpNfsBackupState = "Processing"

// GcpNfsBackupCreating signifies backup create operation is in-progress.
GcpNfsBackupCreating GcpNfsBackupState = "Creating"

Expand Down Expand Up @@ -62,7 +60,7 @@ type GcpNfsVolumeRef struct {
Namespace string `json:"namespace,omitempty"`
}

func (v GcpNfsVolumeRef) ToNamespacedName(fallbackNamespace string) types.NamespacedName {
func (v *GcpNfsVolumeRef) ToNamespacedName(fallbackNamespace string) types.NamespacedName {
ns := v.Namespace
if len(ns) == 0 {
ns = fallbackNamespace
Expand Down Expand Up @@ -158,3 +156,17 @@ type GcpNfsVolumeBackupList struct {
func init() {
SchemeBuilder.Register(&GcpNfsVolumeBackup{}, &GcpNfsVolumeBackupList{})
}

func (in *GcpNfsVolumeBackup) CloneForPatchStatus() client.Object {
return &GcpNfsVolumeBackup{
TypeMeta: metav1.TypeMeta{
Kind: "GcpNfsVolumeBackup",
APIVersion: GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: in.Namespace,
Name: in.Name,
},
Status: in.Status,
}
}
17 changes: 16 additions & 1 deletion api/cloud-resources/v1beta1/gcpnfsvolumerestore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
featuretypes "github.com/kyma-project/cloud-manager/pkg/feature/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type GcpNfsVolumeBackupRef struct {
Expand All @@ -31,7 +32,7 @@ type GcpNfsVolumeBackupRef struct {
Namespace string `json:"namespace"`
}

func (v GcpNfsVolumeBackupRef) ToNamespacedName(fallbackNamespace string) types.NamespacedName {
func (v *GcpNfsVolumeBackupRef) ToNamespacedName(fallbackNamespace string) types.NamespacedName {
ns := v.Namespace
if len(ns) == 0 {
ns = fallbackNamespace
Expand Down Expand Up @@ -125,3 +126,17 @@ type GcpNfsVolumeRestoreList struct {
func init() {
SchemeBuilder.Register(&GcpNfsVolumeRestore{}, &GcpNfsVolumeRestoreList{})
}

func (in *GcpNfsVolumeRestore) CloneForPatchStatus() client.Object {
return &GcpNfsVolumeRestore{
TypeMeta: metav1.TypeMeta{
Kind: "GcpNfsVolumeRestore",
APIVersion: GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: in.Namespace,
Name: in.Name,
},
Status: in.Status,
}
}
30 changes: 21 additions & 9 deletions internal/controller/cloud-resources/gcpnfsvolume_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cloudresources

import (
"context"
"fmt"
"os"
"github.com/kyma-project/cloud-manager/pkg/feature"
"time"

cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
Expand Down Expand Up @@ -31,9 +32,14 @@ var _ = Describe("Feature: SKR GcpNfsVolume", func() {
kcpIpRangeName := "513f20b4-7b73-4246-9397-f8dd55344479"
kcpIpRange := &cloudcontrolv1beta1.IpRange{}

shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled := func() (bool, string) {
if feature.GcpNfsVolumeAutomaticLocationAllocation.Value(context.Background()) {
return false, ""
}
return true, "cpNfsVolumeAutomaticLocationAllocation is disabled"
}

BeforeEach(func() {
err := os.Unsetenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION")
Expect(err).ToNot(HaveOccurred())
By("And Given SKR namespace exists", func() {
//Create namespace if it doesn't exist.
Eventually(CreateNamespace).
Expand Down Expand Up @@ -678,8 +684,10 @@ var _ = Describe("Feature: SKR GcpNfsVolume", func() {
kcpNfsInstance := &cloudcontrolv1beta1.NfsInstance{}
pv := &corev1.PersistentVolume{}
scope := &cloudcontrolv1beta1.Scope{}
err := os.Setenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION", "true")
Expect(err).ToNot(HaveOccurred())
shouldSkip, msg := shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled()
if shouldSkip {
Skip(msg)
}
gcpNfsVolumeName := "gcp-nfs-volume-4"
nfsIpAddress := "10.11.12.14"
pvSpec := &cloudresourcesv1beta1.GcpNfsVolumePvSpec{
Expand Down Expand Up @@ -898,8 +906,10 @@ var _ = Describe("Feature: SKR GcpNfsVolume", func() {
kcpNfsInstance := &cloudcontrolv1beta1.NfsInstance{}
pv := &corev1.PersistentVolume{}
scope := &cloudcontrolv1beta1.Scope{}
err := os.Setenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION", "true")
Expect(err).ToNot(HaveOccurred())
shouldSkip, msg := shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled()
if shouldSkip {
Skip(msg)
}

gcpNfsVolumeName := "gcp-nfs-volume-5"
nfsIpAddress := "10.11.12.16"
Expand Down Expand Up @@ -1165,8 +1175,10 @@ var _ = Describe("Feature: SKR GcpNfsVolume", func() {
pv := &corev1.PersistentVolume{}
pvc := &corev1.PersistentVolumeClaim{}
scope := &cloudcontrolv1beta1.Scope{}
err := os.Setenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION", "true")
Expect(err).ToNot(HaveOccurred())
shouldSkip, msg := shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled()
if shouldSkip {
Skip(msg)
}

gcpNfsVolumeName := "gcp-nfs-volume-6"
nfsIpAddress := "10.11.12.16"
Expand Down
24 changes: 17 additions & 7 deletions internal/controller/cloud-resources/gcpnfsvolumebackup_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cloudresources

import (
"context"
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
"os"
"github.com/kyma-project/cloud-manager/pkg/feature"
"time"

cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
Expand All @@ -28,9 +29,14 @@ var _ = Describe("Feature: SKR GcpNfsVolumeBackup", func() {
skrIpRangeName := "gcp-iprange-1"
scope := &cloudcontrolv1beta1.Scope{}

shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled := func() (bool, string) {
if feature.GcpNfsVolumeAutomaticLocationAllocation.Value(context.Background()) {
return false, ""
}
return true, "cpNfsVolumeAutomaticLocationAllocation is disabled"
}

BeforeEach(func() {
err := os.Unsetenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION")
Expect(err).ToNot(HaveOccurred())
By("Given KCP Scope exists", func() {

// Given Scope exists
Expand Down Expand Up @@ -173,8 +179,10 @@ var _ = Describe("Feature: SKR GcpNfsVolumeBackup", func() {
//Define variables.
gcpNfsVolumeBackup := &cloudresourcesv1beta1.GcpNfsVolumeBackup{}
gcpNfsVolumeBackupName := "gcp-nfs-volume-backup-1"
err := os.Setenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION", "true")
Expect(err).ToNot(HaveOccurred())
shouldSkip, msg := shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled()
if shouldSkip {
Skip(msg)
}
It("When GcpNfsVolumeBackup Create is called", func() {
Eventually(CreateGcpNfsVolumeBackup).
WithArguments(
Expand Down Expand Up @@ -211,8 +219,10 @@ var _ = Describe("Feature: SKR GcpNfsVolumeBackup", func() {
//Define variables.
gcpNfsVolumeBackup := &cloudresourcesv1beta1.GcpNfsVolumeBackup{}
gcpNfsVolumeBackupName := "gcp-nfs-volume-backup-2"
err := os.Setenv("FF_GCP_NFS_VOLUME_AUTOMATIC_LOCATION_ALLOCATION", "true")
Expect(err).ToNot(HaveOccurred())
shouldSkip, msg := shouldSkipIfGcpNfsVolumeAutomaticLocationAllocationDisabled()
if shouldSkip {
Skip(msg)
}
BeforeEach(func() {
By("And Given SKR GcpNfsVolumeBackup has Ready condition", func() {

Expand Down
12 changes: 4 additions & 8 deletions pkg/skr/backupschedule/addFinalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import (
"context"
cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
"github.com/kyma-project/cloud-manager/pkg/composed"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func addFinalizer(ctx context.Context, st composed.State) (error, context.Context) {
if composed.MarkedForDeletionPredicate(ctx, st) {
return nil, nil
}

added := controllerutil.AddFinalizer(st.Obj(), cloudresourcesv1beta1.Finalizer)
if !added {
// finalizer already added
return nil, nil
}

err := st.UpdateObj(ctx)
modified, err := st.PatchObjAddFinalizer(ctx, cloudresourcesv1beta1.Finalizer)
if err != nil {
return composed.LogErrorAndReturn(err, "Error saving object after finalizer added", composed.StopWithRequeue, ctx)
}
if modified {
composed.LoggerFromCtx(ctx).Info("Finalizer added")
}

return nil, nil
}
2 changes: 1 addition & 1 deletion pkg/skr/backupschedule/backupImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func loadBackupImpl(ctx context.Context, st composed.State) (error, context.Cont
if _, ok := schedule.(*cloudresourcesv1beta1.GcpNfsBackupSchedule); ok {
state.backupImpl = &backupImplGcpNfs{}
} else {
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SetExclusiveConditions(metav1.Condition{
Type: cloudresourcesv1beta1.ConditionTypeError,
Status: metav1.ConditionTrue,
Expand Down
2 changes: 1 addition & 1 deletion pkg/skr/backupschedule/calculateOnetimeSchedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func calculateOnetimeSchedule(ctx context.Context, st composed.State) (error, co
schedule.SetState(cloudresourcesv1beta1.JobStateActive)
schedule.SetNextRunTimes([]string{nextRunTime.UTC().Format(time.RFC3339)})

return composed.UpdateStatus(schedule.(composed.ObjWithConditions)).
return composed.PatchStatus(schedule.(composed.ObjWithConditions)).
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
}
2 changes: 1 addition & 1 deletion pkg/skr/backupschedule/calculateRecurringSchedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func calculateRecurringSchedule(ctx context.Context, st composed.State) (error,
}
schedule.SetNextRunTimes(runtimes)

return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
}
6 changes: 3 additions & 3 deletions pkg/skr/backupschedule/checkCompleted.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func checkCompleted(ctx context.Context, st composed.State) (error, context.Cont
//If the schedule is in Done state, stop reconciliation
if schedule.State() == cloudresourcesv1beta1.JobStateDone {
logger.WithValues("GcpNfsBackupSchedule :", schedule.GetName()).Info("Schedule already completed, stopping reconciliation.")
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SuccessError(composed.StopAndForget).
Run(ctx, state)
}
Expand All @@ -34,7 +34,7 @@ func checkCompleted(ctx context.Context, st composed.State) (error, context.Cont
logger.WithValues("GcpNfsBackupSchedule :", schedule.GetName()).Info("Current Time is after the EndTime. Stopping reconciliation.")
schedule.SetState(cloudresourcesv1beta1.JobStateDone)
schedule.SetNextRunTimes(nil)
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SuccessError(composed.StopAndForget).
Run(ctx, state)
}
Expand All @@ -50,7 +50,7 @@ func checkCompleted(ctx context.Context, st composed.State) (error, context.Cont
logger.WithValues("GcpNfsBackupSchedule :", schedule.GetName()).Info("One-time schedule already ran. Stopping reconciliation.")
schedule.SetState(cloudresourcesv1beta1.JobStateDone)
schedule.SetNextRunTimes(nil)
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SuccessError(composed.StopAndForget).
Run(ctx, state)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skr/backupschedule/checkSuspension.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func checkSuspension(ctx context.Context, st composed.State) (error, context.Con
schedule.SetState(cloudresourcesv1beta1.JobStateSuspended)
schedule.SetNextRunTimes(nil)
schedule.SetNextDeleteTimes(nil)
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SuccessError(composed.StopAndForget).
Run(ctx, state)
}
4 changes: 2 additions & 2 deletions pkg/skr/backupschedule/createBackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func createBackup(ctx context.Context, st composed.State) (error, context.Contex

if err != nil {
schedule.SetState(cloudresourcesv1beta1.JobStateError)
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SetExclusiveConditions(metav1.Condition{
Type: cloudresourcesv1beta1.ConditionTypeError,
Status: metav1.ConditionTrue,
Expand All @@ -85,7 +85,7 @@ func createBackup(ctx context.Context, st composed.State) (error, context.Contex
Namespace: backup.GetNamespace(),
Name: backup.GetName(),
})
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SetExclusiveConditions().
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
Expand Down
4 changes: 2 additions & 2 deletions pkg/skr/backupschedule/deleteBackups.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func deleteBackups(ctx context.Context, st composed.State) (error, context.Conte
schedule.SetNextDeleteTimes(nil)
schedule.SetLastDeletedBackups(nil)
schedule.SetBackupCount(len(state.Backups))
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SetExclusiveConditions().
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
Expand Down Expand Up @@ -78,7 +78,7 @@ func deleteBackups(ctx context.Context, st composed.State) (error, context.Conte
schedule.SetLastDeletedBackups(lastDeleted)
schedule.SetNextDeleteTimes(nextDeleteTimes)
schedule.SetBackupCount(len(state.Backups) - len(lastDeleted))
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SetExclusiveConditions().
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
Expand Down
2 changes: 1 addition & 1 deletion pkg/skr/backupschedule/deleteCascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func deleteCascade(ctx context.Context, st composed.State) (error, context.Conte
}

schedule.SetState(cloudresourcesv1beta1.StateDeleting)
return composed.UpdateStatus(schedule).
return composed.PatchStatus(schedule).
SetExclusiveConditions().
SuccessError(composed.StopWithRequeueDelay(util.Timing.T1000ms())).
Run(ctx, state)
Expand Down
Loading

0 comments on commit 7bac086

Please sign in to comment.