Skip to content

Commit

Permalink
Allow Bucket.PublicAccessPrevention to be fully optional and managed …
Browse files Browse the repository at this point in the history
…independently

Signed-off-by: Njal Karevoll <[email protected]>
  • Loading branch information
nkvoll committed Nov 21, 2022
1 parent a82be3d commit c3f0076
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
27 changes: 21 additions & 6 deletions apis/storage/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"cloud.google.com/go/storage"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

gcp "github.com/crossplane-contrib/provider-gcp/pkg/clients"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
)

Expand Down Expand Up @@ -605,8 +606,7 @@ type BucketUpdatableAttrs struct {
//
// +optional
// +kubebuilder:validation:Enum="";unspecified;inherited;enforced
// +kubebuilder:default:=""
PublicAccessPrevention string `json:"publicAccessPrevention,omitempty"`
PublicAccessPrevention *string `json:"publicAccessPrevention,omitempty"`

// RequesterPays reports whether the bucket is a Requester Pays bucket.
// Clients performing operations on Requester Pays buckets must provide
Expand Down Expand Up @@ -646,7 +646,7 @@ func NewBucketUpdatableAttrs(ba *storage.BucketAttrs) *BucketUpdatableAttrs {
Logging: NewBucketLogging(ba.Logging),
PredefinedACL: ba.PredefinedACL,
PredefinedDefaultObjectACL: ba.PredefinedDefaultObjectACL,
PublicAccessPrevention: ba.PublicAccessPrevention.String(),
PublicAccessPrevention: convertPublicAccessPreventionEnumToStringPtr(ba.PublicAccessPrevention),
RequesterPays: ba.RequesterPays,
RetentionPolicy: NewRetentionPolicy(ba.RetentionPolicy),
VersioningEnabled: ba.VersioningEnabled,
Expand All @@ -656,8 +656,13 @@ func NewBucketUpdatableAttrs(ba *storage.BucketAttrs) *BucketUpdatableAttrs {

// convertPublicAccessPreventionStringToEnum converts a string representation of storage.PublicAccessPrevention to its
// enum value.
func convertPublicAccessPreventionStringToEnum(pap string) storage.PublicAccessPrevention {
switch pap {
func convertPublicAccessPreventionStringToEnum(pap *string) storage.PublicAccessPrevention {
// if the field is not set, treat it as unknown
if pap == nil {
return storage.PublicAccessPreventionUnknown
}

switch *pap {
case "unspecified", "inherited":
return storage.PublicAccessPreventionInherited
case "enforced":
Expand All @@ -667,6 +672,16 @@ func convertPublicAccessPreventionStringToEnum(pap string) storage.PublicAccessP
}
}

// convertPublicAccessPreventionEnumToStringPtr converts an enum value of storage.PublicAccessPrevention to its
// string pointer value used in BucketUpdatableAttrs.
func convertPublicAccessPreventionEnumToStringPtr(pap storage.PublicAccessPrevention) *string {
if pap == storage.PublicAccessPreventionUnknown {
return nil
}

return gcp.StringPtr(pap.String())
}

// CopyToBucketAttrs create a copy in storage format
func CopyToBucketAttrs(ba *BucketUpdatableAttrs) *storage.BucketAttrs {
if ba == nil {
Expand Down Expand Up @@ -750,7 +765,7 @@ type BucketSpecAttrs struct {
StorageClass string `json:"storageClass,omitempty"`
}

// NewBucketSpecAttrs create new instance from storage BuckateAttrs
// NewBucketSpecAttrs create new instance from storage.BucketAttrs
func NewBucketSpecAttrs(ba *storage.BucketAttrs) BucketSpecAttrs {
if ba == nil {
return BucketSpecAttrs{}
Expand Down
5 changes: 5 additions & 0 deletions apis/storage/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package/crds/storage.gcp.crossplane.io_buckets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ spec:
- name
type: object
publicAccessPrevention:
default: ""
description: PublicAccessPrevention is the setting for the bucket's
PublicAccessPrevention policy, which can be used to prevent public
access of data in the bucket. See https://cloud.google.com/storage/docs/public-access-prevention
Expand Down
11 changes: 10 additions & 1 deletion pkg/controller/storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,16 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
}

proposed := cr.Spec.BucketSpecAttrs.DeepCopy()
if err := mergo.Merge(proposed, v1alpha3.NewBucketSpecAttrs(a)); err != nil {
bsa := v1alpha3.NewBucketSpecAttrs(a)

// If the spec has no value set for the PublicAccessPrevention field, ignore the one stored in GCP API for the
// purposes of comparison. This allows public access prevention to be managed in the GCP console independently of
// the Bucket CR if the field is not set.
if proposed.PublicAccessPrevention == nil {
bsa.PublicAccessPrevention = nil
}

if err := mergo.Merge(proposed, bsa); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errLateInit)
}
if !cmp.Equal(*proposed, cr.Spec.BucketSpecAttrs) {
Expand Down

0 comments on commit c3f0076

Please sign in to comment.