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

feat: properly resolve reference fields in GKEBackupBackupPlan controller #4257

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
1 change: 1 addition & 0 deletions pkg/controller/direct/backupdr/backupplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (m *modelBackupPlan) AdapterForObject(ctx context.Context, reader client.Re
return nil, err
}

// resolve required reference fields
if obj.Spec.BackupVaultRef != nil {
if _, err := obj.Spec.BackupVaultRef.NormalizedExternal(ctx, reader, obj.GetNamespace()); err != nil {
return nil, err
Expand Down
57 changes: 35 additions & 22 deletions pkg/controller/direct/gkebackup/backupplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides"
)

func init() {
Expand Down Expand Up @@ -69,20 +68,6 @@ func (m *backupPlanModel) AdapterForObject(ctx context.Context, reader client.Re
return nil, err
}

// normalize reference fields
if obj.Spec.ClusterRef != nil {
if _, err := obj.Spec.ClusterRef.NormalizedExternal(ctx, reader, obj.GetNamespace()); err != nil {
return nil, err
}
}
if obj.Spec.BackupConfig != nil &&
obj.Spec.BackupConfig.EncryptionKey != nil &&
obj.Spec.BackupConfig.EncryptionKey.KMSKeyRef != nil {
if _, err := refs.ResolveKMSCryptoKeyRef(ctx, reader, obj, obj.Spec.BackupConfig.EncryptionKey.KMSKeyRef); err != nil {
return nil, err
}
}

// Get gkebackup GCP client
gcpClient, err := newGCPClient(ctx, &m.config)
if err != nil {
Expand All @@ -96,6 +81,7 @@ func (m *backupPlanModel) AdapterForObject(ctx context.Context, reader client.Re
gcpClient: client,
id: id,
desired: obj,
reader: reader,
}, nil
}

Expand All @@ -105,11 +91,11 @@ func (m *backupPlanModel) AdapterForURL(ctx context.Context, url string) (direct
}

type backupPlanAdapter struct {
gcpClient *gcp.BackupForGKEClient
id *krm.BackupPlanIdentity
desired *krm.GKEBackupBackupPlan
actual *pb.BackupPlan
resourceOverrides resourceoverrides.ResourceOverrides
gcpClient *gcp.BackupForGKEClient
id *krm.BackupPlanIdentity
desired *krm.GKEBackupBackupPlan
actual *pb.BackupPlan
reader client.Reader
}

var _ directbase.Adapter = &backupPlanAdapter{}
Expand All @@ -134,8 +120,12 @@ func (a *backupPlanAdapter) Find(ctx context.Context) (bool, error) {
func (a *backupPlanAdapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error {
log := klog.FromContext(ctx)
log.V(2).Info("creating gkebackup backupplan", "name", a.id)
mapCtx := &direct.MapContext{}

if err := a.normalizeReferenceFields(ctx); err != nil {
return err
}

mapCtx := &direct.MapContext{}
desired := a.desired.DeepCopy()
resource := GKEBackupBackupPlanSpec_ToProto(mapCtx, &desired.Spec)
if mapCtx.Err() != nil {
Expand Down Expand Up @@ -169,8 +159,12 @@ func (a *backupPlanAdapter) Create(ctx context.Context, createOp *directbase.Cre
func (a *backupPlanAdapter) Update(ctx context.Context, updateOp *directbase.UpdateOperation) error {
log := klog.FromContext(ctx)
log.V(2).Info("updating gkebackup backupplan", "name", a.id)
mapCtx := &direct.MapContext{}

if err := a.normalizeReferenceFields(ctx); err != nil {
return err
}

mapCtx := &direct.MapContext{}
desired := a.desired.DeepCopy()
resource := GKEBackupBackupPlanSpec_ToProto(mapCtx, &desired.Spec)
if mapCtx.Err() != nil {
Expand Down Expand Up @@ -330,3 +324,22 @@ func backupConfigsEqual(a, b *gkebackuppb.BackupPlan_BackupConfig) bool {

return true
}

func (a *backupPlanAdapter) normalizeReferenceFields(ctx context.Context) error {
obj := a.desired

if obj.Spec.ClusterRef != nil {
if _, err := obj.Spec.ClusterRef.NormalizedExternal(ctx, a.reader, obj.GetNamespace()); err != nil {
return err
}
}
if obj.Spec.BackupConfig != nil &&
obj.Spec.BackupConfig.EncryptionKey != nil &&
obj.Spec.BackupConfig.EncryptionKey.KMSKeyRef != nil {
if _, err := refs.ResolveKMSCryptoKeyRef(ctx, a.reader, obj, obj.Spec.BackupConfig.EncryptionKey.KMSKeyRef); err != nil {
return err
}
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/controller/direct/gkebackup/restoreplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (m *restorePlanModel) AdapterForObject(ctx context.Context, reader client.R
return nil, err
}

// normalize reference fields
// normalize required reference fields
if obj.Spec.BackupPlanRef != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original suggestion is to use the CRD + required to guardrail this? From the controller code, it seems that the field is optional (comment does not validate the code).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a required field. But I usually still check for nil before using a pointer, as an extra layer of safety at runtime.

// Required. Immutable. A reference to the
// [BackupPlan][google.cloud.gkebackup.v1.BackupPlan] from which Backups may
// be used as the source for Restores created via this RestorePlan.
// +kcc:proto:field=google.cloud.gkebackup.v1.RestorePlan.backup_plan
// +required
BackupPlanRef *BackupPlanRef `json:"backupPlanRef,omitempty"`

if _, err := obj.Spec.BackupPlanRef.NormalizedExternal(ctx, reader, obj.GetNamespace()); err != nil {
return nil, err
Expand Down
Loading