Skip to content

Commit

Permalink
Move Profile from ProviderConfig to resource inner level
Browse files Browse the repository at this point in the history
Signed-off-by: Zheng Xi Zhou <[email protected]>
  • Loading branch information
zzxwill committed Mar 20, 2021
1 parent c9d6a8a commit 96cc96c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 90 deletions.
11 changes: 5 additions & 6 deletions apis/oss/v1alpha1/oss_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ type Bucket struct {
// BucketSpec defines the desired state of Bucket
type BucketSpec struct {
runtimev1.ResourceSpec `json:",inline"`
ForProvider BucketParameters `json:"forProvider"`
BucketParameter `json:",inline"`

// Profile is used to extend store business information
// +kubebuilder:pruning:PreserveUnknownFields
Profile map[string]string `json:"profile,omitempty"`
}

// BucketStatus defines the observed state of Bucket
Expand All @@ -68,11 +72,6 @@ type BucketParameter struct {
DataRedundancyType string `json:"dataRedundancyType,omitempty"`
}

// BucketParameters define the desired state of an Bucket
type BucketParameters struct {
Bucket BucketParameter `json:"bucket,omitempty"`
}

// BucketObservation is the representation of the current state that is observed.
type BucketObservation struct {
BucketName string `json:"bucketName,omitempty"`
Expand Down
25 changes: 8 additions & 17 deletions apis/oss/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 0 additions & 4 deletions apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ type ProviderConfigSpec struct {
// Region for managed resources created using this Alibaba Cloud provider,
// e.g. "cn-hangzhou".
Region string `json:"region"`

// Profile is used to extend fields of ProviderConfigSpec.
// +kubebuilder:pruning:PreserveUnknownFields
Profile map[string]string `json:"profile,omitempty"`
}

// A ProviderConfigStatus represents the status of a ProviderConfig.
Expand Down
7 changes: 0 additions & 7 deletions apis/v1alpha1/zz_generated.deepcopy.go

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

14 changes: 6 additions & 8 deletions examples/oss/oss.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
apiVersion: oss.alibaba.crossplane.io/v1alpha1
kind: Bucket
metadata:
name: example-oss
name: example-oss-0320
spec:
forProvider:
bucket:
name: example-oss-bucket
acl: private
storageClass: Standard
dataRedundancyType: LRS
name: example-oss-bucket-0320
acl: private
storageClass: Standard
dataRedundancyType: LRS
writeConnectionSecretToRef:
name: example-oss
name: example-oss-0320
namespace: default
6 changes: 0 additions & 6 deletions package/crds/alibaba.crossplane.io_providerconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ spec:
required:
- source
type: object
profile:
additionalProperties:
type: string
description: Profile is used to extend fields of ProviderConfigSpec.
type: object
x-kubernetes-preserve-unknown-fields: true
region:
description: Region for managed resources created using this Alibaba Cloud provider, e.g. "cn-hangzhou".
type: string
Expand Down
32 changes: 14 additions & 18 deletions package/crds/oss.alibaba.crossplane.io_buckets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,24 @@ spec:
spec:
description: BucketSpec defines the desired state of Bucket
properties:
acl:
type: string
dataRedundancyType:
type: string
deletionPolicy:
description: DeletionPolicy specifies what will happen to the underlying external when this managed resource is deleted - either "Delete" or "Orphan" the external resource. The "Delete" policy is the default when no policy is specified.
enum:
- Orphan
- Delete
type: string
forProvider:
description: BucketParameters define the desired state of an Bucket
properties:
bucket:
description: BucketParameter is the isolated place to store files
properties:
acl:
type: string
dataRedundancyType:
type: string
name:
type: string
storageClass:
type: string
required:
- name
type: object
name:
type: string
profile:
additionalProperties:
type: string
description: Profile is used to extend store business information
type: object
x-kubernetes-preserve-unknown-fields: true
providerConfigRef:
description: ProviderConfigReference specifies how the provider that will be used to create, observe, update, and delete this managed resource should be configured.
properties:
Expand All @@ -86,6 +80,8 @@ spec:
required:
- name
type: object
storageClass:
type: string
writeConnectionSecretToRef:
description: WriteConnectionSecretToReference specifies the namespace and name of a Secret to which any connection details for this managed resource should be written. Connection details frequently include the endpoint, username, and password required to connect to the managed resource.
properties:
Expand All @@ -100,7 +96,7 @@ spec:
- namespace
type: object
required:
- forProvider
- name
type: object
status:
description: BucketStatus defines the observed state of Bucket
Expand Down
3 changes: 1 addition & 2 deletions pkg/clients/oss/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ func validateOSSDataRedundancyType(dataRedundancyTypeStr string) (sdk.DataRedund

// IsUpdateToDate checks whether cr is up to date
func IsUpdateToDate(cr *v1alpha1.Bucket, bucket *sdk.GetBucketInfoResult) bool {
bucketSpec := cr.Spec.ForProvider.Bucket
if (bucketSpec.ACL == bucket.BucketInfo.ACL) || (bucketSpec.ACL == "" && bucket.BucketInfo.ACL == "private") {
if (cr.Spec.ACL == bucket.BucketInfo.ACL) || (cr.Spec.ACL == "" && bucket.BucketInfo.ACL == "private") {
return true
}
return false
Expand Down
34 changes: 18 additions & 16 deletions pkg/controller/oss/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ func BaseObserve(mg resource.Managed, c ossclient.ClientInterface) (managed.Exte
if !ok {
return managed.ExternalObservation{}, errors.New(errNotOSS)
}
bucketSpec := cr.Spec.ForProvider.Bucket
klog.InfoS("observing Bucket resource", "Name", bucketSpec.Name)
klog.InfoS("observing Bucket resource", "Name", cr.Spec.Name)

bucket, err := c.Describe(bucketSpec.Name)
bucket, err := c.Describe(cr.Spec.Name)
if ossclient.IsNotFoundError(err) {
return managed.ExternalObservation{
ResourceExists: false,
Expand All @@ -54,10 +53,10 @@ func BaseObserve(mg resource.Managed, c ossclient.ClientInterface) (managed.Exte
}

cr.Status.AtProvider = ossclient.GenerateObservation(*bucket)
if bucketSpec.StorageClass != "" && bucketSpec.StorageClass != bucket.BucketInfo.StorageClass {
if cr.Spec.StorageClass != "" && cr.Spec.StorageClass != bucket.BucketInfo.StorageClass {
cr.Status.AtProvider.Message += "[Warning] StorageClass is not allowed to update after creation; "
}
if bucketSpec.DataRedundancyType != "" && bucketSpec.DataRedundancyType != bucket.BucketInfo.RedundancyType {
if cr.Spec.DataRedundancyType != "" && cr.Spec.DataRedundancyType != bucket.BucketInfo.RedundancyType {
cr.Status.AtProvider.Message += "[Warning] DataRedundancyType is not allowed to update after creation; "
}
var upToDate = ossclient.IsUpdateToDate(cr, bucket)
Expand All @@ -78,9 +77,15 @@ func BaseCreate(mg resource.Managed, c ossclient.ClientInterface) (managed.Exter
if !ok {
return managed.ExternalCreation{}, errors.New(errNotOSS)
}
klog.InfoS("creating Bucket resource", "Name", cr.Spec.ForProvider.Bucket.Name)
klog.InfoS("creating Bucket resource", "Name", cr.Spec.Name)
cr.SetConditions(xpv1.Creating())
if err := c.Create(cr.Spec.ForProvider.Bucket); err != nil {
bucketParameter := v1alpha1.BucketParameter{
Name: cr.Spec.Name,
ACL: cr.Spec.ACL,
StorageClass: cr.Spec.StorageClass,
DataRedundancyType: cr.Spec.DataRedundancyType,
}
if err := c.Create(bucketParameter); err != nil {
return managed.ExternalCreation{}, err
}
return managed.ExternalCreation{ConnectionDetails: GetConnectionDetails(cr)}, nil
Expand All @@ -92,17 +97,14 @@ func BaseUpdate(mg resource.Managed, client ossclient.ClientInterface) (managed.
if !ok {
return managed.ExternalUpdate{}, errors.New(errNotOSS)
}
klog.InfoS("updating Bucket resource", "Name", cr.Spec.ForProvider.Bucket.Name)
cr.Status.SetConditions(xpv1.Creating())
got, err := client.Describe(cr.Spec.ForProvider.Bucket.Name)
got, err := client.Describe(cr.Spec.Name)
if err != nil {
return managed.ExternalUpdate{}, err
}

target := cr.Spec.ForProvider.Bucket

if target.ACL != "" && target.ACL != got.BucketInfo.ACL {
if err := client.Update(target.Name, target.ACL); err != nil {
if cr.Spec.ACL != "" && cr.Spec.ACL != got.BucketInfo.ACL {
if err := client.Update(cr.Spec.Name, cr.Spec.ACL); err != nil {
return managed.ExternalUpdate{}, err
}
}
Expand All @@ -116,9 +118,9 @@ func BaseDelete(mg resource.Managed, client ossclient.ClientInterface) error {
if !ok {
return errors.New(errNotOSS)
}
klog.InfoS("deleting Bucket resource", "Name", cr.Spec.ForProvider.Bucket.Name)
klog.InfoS("deleting Bucket resource", "Name", cr.Spec.Name)
cr.SetConditions(xpv1.Deleting())
if err := client.Delete(cr.Spec.ForProvider.Bucket.Name); err != nil && !ossclient.IsNotFoundError(err) {
if err := client.Delete(cr.Spec.Name); err != nil && !ossclient.IsNotFoundError(err) {
return err
}
return nil
Expand All @@ -142,7 +144,7 @@ func BaseSetupOSS(mgr ctrl.Manager, l logging.Logger, o ...managed.ReconcilerOpt
// GetConnectionDetails generates connection details
func GetConnectionDetails(cr *v1alpha1.Bucket) managed.ConnectionDetails {
cd := managed.ConnectionDetails{
"Bucket": []byte(cr.Spec.ForProvider.Bucket.Name),
"Bucket": []byte(cr.Spec.Name),
}
if cr.Status.AtProvider.ExtranetEndpoint != "" {
cd["ExtranetEndpoint"] = []byte(cr.Status.AtProvider.ExtranetEndpoint)
Expand Down
20 changes: 14 additions & 6 deletions pkg/controller/oss/oss_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ func TestObserve(t *testing.T) {
var (
ctx = context.Background()
)
validCR := &ossv1alpha1.Bucket{}
validCR.Spec.Name = "def"

validCR := &ossv1alpha1.Bucket{Spec: ossv1alpha1.BucketSpec{ForProvider: ossv1alpha1.BucketParameters{Bucket: ossv1alpha1.BucketParameter{Name: "def"}}}}
invalidCR := &ossv1alpha1.Bucket{}
invalidCR.Spec.Name = "abc"

type want struct {
o managed.ExternalObservation
Expand Down Expand Up @@ -114,8 +117,7 @@ func TestObserve(t *testing.T) {
},
"OSSOtherError": {
reason: "We should report an unknown error",
mg: &ossv1alpha1.Bucket{
Spec: ossv1alpha1.BucketSpec{ForProvider: ossv1alpha1.BucketParameters{Bucket: ossv1alpha1.BucketParameter{Name: "abc"}}}},
mg: invalidCR,
want: want{
o: managed.ExternalObservation{},
err: errors.New("unknown error"),
Expand Down Expand Up @@ -153,7 +155,9 @@ func TestCreate(t *testing.T) {
ctx = context.Background()
)

validCR := &ossv1alpha1.Bucket{Spec: ossv1alpha1.BucketSpec{ForProvider: ossv1alpha1.BucketParameters{Bucket: ossv1alpha1.BucketParameter{Name: "def"}}}}
spec := ossv1alpha1.BucketSpec{}
spec.Name = "def"
validCR := &ossv1alpha1.Bucket{Spec: spec}

type want struct {
o managed.ExternalCreation
Expand Down Expand Up @@ -203,7 +207,9 @@ func TestUpdate(t *testing.T) {
ctx = context.Background()
)

validCR := &ossv1alpha1.Bucket{Spec: ossv1alpha1.BucketSpec{ForProvider: ossv1alpha1.BucketParameters{Bucket: ossv1alpha1.BucketParameter{Name: "def"}}}}
spec := ossv1alpha1.BucketSpec{}
spec.Name = "def"
validCR := &ossv1alpha1.Bucket{Spec: spec}

type want struct {
o managed.ExternalUpdate
Expand Down Expand Up @@ -252,7 +258,9 @@ func TestDelete(t *testing.T) {
ctx = context.Background()
)

validCR := &ossv1alpha1.Bucket{Spec: ossv1alpha1.BucketSpec{ForProvider: ossv1alpha1.BucketParameters{Bucket: ossv1alpha1.BucketParameter{Name: "def"}}}}
spec := ossv1alpha1.BucketSpec{}
spec.Name = "def"
validCR := &ossv1alpha1.Bucket{Spec: spec}

type want struct {
err error
Expand Down

0 comments on commit 96cc96c

Please sign in to comment.