Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.

Commit c3f0076

Browse files
committed
Allow Bucket.PublicAccessPrevention to be fully optional and managed independently
Signed-off-by: Njal Karevoll <njal@karevoll.no>
1 parent a82be3d commit c3f0076

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

apis/storage/v1alpha3/types.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"cloud.google.com/go/storage"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424

25+
gcp "github.com/crossplane-contrib/provider-gcp/pkg/clients"
2526
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
2627
)
2728

@@ -605,8 +606,7 @@ type BucketUpdatableAttrs struct {
605606
//
606607
// +optional
607608
// +kubebuilder:validation:Enum="";unspecified;inherited;enforced
608-
// +kubebuilder:default:=""
609-
PublicAccessPrevention string `json:"publicAccessPrevention,omitempty"`
609+
PublicAccessPrevention *string `json:"publicAccessPrevention,omitempty"`
610610

611611
// RequesterPays reports whether the bucket is a Requester Pays bucket.
612612
// Clients performing operations on Requester Pays buckets must provide
@@ -646,7 +646,7 @@ func NewBucketUpdatableAttrs(ba *storage.BucketAttrs) *BucketUpdatableAttrs {
646646
Logging: NewBucketLogging(ba.Logging),
647647
PredefinedACL: ba.PredefinedACL,
648648
PredefinedDefaultObjectACL: ba.PredefinedDefaultObjectACL,
649-
PublicAccessPrevention: ba.PublicAccessPrevention.String(),
649+
PublicAccessPrevention: convertPublicAccessPreventionEnumToStringPtr(ba.PublicAccessPrevention),
650650
RequesterPays: ba.RequesterPays,
651651
RetentionPolicy: NewRetentionPolicy(ba.RetentionPolicy),
652652
VersioningEnabled: ba.VersioningEnabled,
@@ -656,8 +656,13 @@ func NewBucketUpdatableAttrs(ba *storage.BucketAttrs) *BucketUpdatableAttrs {
656656

657657
// convertPublicAccessPreventionStringToEnum converts a string representation of storage.PublicAccessPrevention to its
658658
// enum value.
659-
func convertPublicAccessPreventionStringToEnum(pap string) storage.PublicAccessPrevention {
660-
switch pap {
659+
func convertPublicAccessPreventionStringToEnum(pap *string) storage.PublicAccessPrevention {
660+
// if the field is not set, treat it as unknown
661+
if pap == nil {
662+
return storage.PublicAccessPreventionUnknown
663+
}
664+
665+
switch *pap {
661666
case "unspecified", "inherited":
662667
return storage.PublicAccessPreventionInherited
663668
case "enforced":
@@ -667,6 +672,16 @@ func convertPublicAccessPreventionStringToEnum(pap string) storage.PublicAccessP
667672
}
668673
}
669674

675+
// convertPublicAccessPreventionEnumToStringPtr converts an enum value of storage.PublicAccessPrevention to its
676+
// string pointer value used in BucketUpdatableAttrs.
677+
func convertPublicAccessPreventionEnumToStringPtr(pap storage.PublicAccessPrevention) *string {
678+
if pap == storage.PublicAccessPreventionUnknown {
679+
return nil
680+
}
681+
682+
return gcp.StringPtr(pap.String())
683+
}
684+
670685
// CopyToBucketAttrs create a copy in storage format
671686
func CopyToBucketAttrs(ba *BucketUpdatableAttrs) *storage.BucketAttrs {
672687
if ba == nil {
@@ -750,7 +765,7 @@ type BucketSpecAttrs struct {
750765
StorageClass string `json:"storageClass,omitempty"`
751766
}
752767

753-
// NewBucketSpecAttrs create new instance from storage BuckateAttrs
768+
// NewBucketSpecAttrs create new instance from storage.BucketAttrs
754769
func NewBucketSpecAttrs(ba *storage.BucketAttrs) BucketSpecAttrs {
755770
if ba == nil {
756771
return BucketSpecAttrs{}

apis/storage/v1alpha3/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/crds/storage.gcp.crossplane.io_buckets.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ spec:
400400
- name
401401
type: object
402402
publicAccessPrevention:
403-
default: ""
404403
description: PublicAccessPrevention is the setting for the bucket's
405404
PublicAccessPrevention policy, which can be used to prevent public
406405
access of data in the bucket. See https://cloud.google.com/storage/docs/public-access-prevention

pkg/controller/storage/bucket.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,16 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
142142
}
143143

144144
proposed := cr.Spec.BucketSpecAttrs.DeepCopy()
145-
if err := mergo.Merge(proposed, v1alpha3.NewBucketSpecAttrs(a)); err != nil {
145+
bsa := v1alpha3.NewBucketSpecAttrs(a)
146+
147+
// If the spec has no value set for the PublicAccessPrevention field, ignore the one stored in GCP API for the
148+
// purposes of comparison. This allows public access prevention to be managed in the GCP console independently of
149+
// the Bucket CR if the field is not set.
150+
if proposed.PublicAccessPrevention == nil {
151+
bsa.PublicAccessPrevention = nil
152+
}
153+
154+
if err := mergo.Merge(proposed, bsa); err != nil {
146155
return managed.ExternalObservation{}, errors.Wrap(err, errLateInit)
147156
}
148157
if !cmp.Equal(*proposed, cr.Spec.BucketSpecAttrs) {

0 commit comments

Comments
 (0)