Skip to content

Commit

Permalink
Fix managed resource issue
Browse files Browse the repository at this point in the history
Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>
  • Loading branch information
zzxwill committed Jun 24, 2021
1 parent 6750d7d commit 59184e2
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apis/nas/v1alpha1/nas_fs_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type NASFileSystemList struct {
// +kubebuilder:object:root=true

// NASFileSystem is a managed resource that represents an NASFileSystem instance
// +kubebuilder:printcolumn:name="FILE-SYSTEM-ID",type="string",JSONPath=".status.atProvider.fileSystemID"
// +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].status"
// +kubebuilder:printcolumn:name="WARNING",type="string",JSONPath=".status.atProvider.message"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster,categories={crossplane,managed,alibaba}
Expand Down
32 changes: 31 additions & 1 deletion apis/nas/v1alpha1/zz_generated.deepcopy.go

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

11 changes: 11 additions & 0 deletions examples/nas/nas-filesystem.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: nas.alibaba.crossplane.io/v1alpha1
kind: NASFileSystem
metadata:
name: na-filesystem-test
namespace: default
spec:
storageType: Performance
protocolType: NFS
writeConnectionSecretToRef:
name: nas-endpoint
namespace: default
17 changes: 13 additions & 4 deletions package/crds/nas.alibaba.crossplane.io_nasfilesystems.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ spec:
scope: Cluster
versions:
- additionalPrinterColumns:
- jsonPath: .status.atProvider.fileSystemID
name: FILE-SYSTEM-ID
type: string
- jsonPath: .status.conditions[?(@.type=='Ready')].status
name: READY
type: string
- jsonPath: .status.conditions[?(@.type=='Synced')].status
name: SYNCED
type: string
- jsonPath: .status.atProvider.message
name: WARNING
type: string
- jsonPath: .metadata.creationTimestamp
name: AGE
type: date
Expand Down Expand Up @@ -57,6 +57,8 @@ spec:
type: string
fileSystemType:
type: string
protocolType:
type: string
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 @@ -75,9 +77,11 @@ spec:
required:
- name
type: object
storageType:
type: string
vSwitchId:
type: string
vpcDd:
vpcId:
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.
Expand All @@ -92,13 +96,18 @@ spec:
- name
- namespace
type: object
required:
- protocolType
- storageType
type: object
status:
description: NASFileSystemStatus defines the observed state of NASFileSystem
properties:
atProvider:
description: NASFileSystemObservation is the representation of the current state that is observed.
properties:
fileSystemID:
type: string
mountTargetDomain:
type: string
type: object
Expand Down
25 changes: 17 additions & 8 deletions pkg/clients/nas/nas.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package nas

import (
"context"
"fmt"

openapi "github.com/alibabacloud-go/darabonba-openapi/client"
sdk "github.com/alibabacloud-go/nas-20170626/v2/client"
Expand All @@ -30,8 +29,8 @@ import (

// ErrCodeNoSuchNASFileSystem is the error code "NoSuchNASFileSystem" returned by SDK
const (
ErrCodeNoSuchNASFileSystem = "NoSuchNASFileSystem"
errFailedToCreateNASClient = "failed to crate NAS client"
errCodeFileSystemNotExist = "InvalidFileSystem.NotFound"
)

// ClientInterface will help fakeOSSClient in unit tests
Expand All @@ -47,13 +46,13 @@ type SDKClient struct {
}

// NewClient will create OSS client
func NewClient(ctx context.Context, region string, accessKeyID string, accessKeySecret string, securityToken string) (*SDKClient, error) {
func NewClient(ctx context.Context, endpoint string, accessKeyID string, accessKeySecret string, securityToken string) (*SDKClient, error) {
config := &openapi.Config{
AccessKeyId: &accessKeyID,
AccessKeySecret: &accessKeySecret,
SecurityToken: &securityToken,
Endpoint: &endpoint,
}
config.Endpoint = tea.String(fmt.Sprintf("nas.%s.aliyuncs.com", region))
client, err := sdk.NewClient(config)
if err != nil {
return nil, errors.Wrap(err, errFailedToCreateNASClient)
Expand Down Expand Up @@ -104,7 +103,7 @@ func (c *SDKClient) DeleteFileSystem(fileSystemID string) error {
}

// GenerateObservation generates NASFileSystemObservation from fileSystem information
func GenerateObservation(r sdk.DescribeFileSystemsResponse) v1alpha1.NASFileSystemObservation {
func GenerateObservation(r *sdk.DescribeFileSystemsResponse) v1alpha1.NASFileSystemObservation {
var domain string
if len(r.Body.FileSystems.FileSystem) == 0 {
return v1alpha1.NASFileSystemObservation{}
Expand All @@ -122,13 +121,23 @@ func GenerateObservation(r sdk.DescribeFileSystemsResponse) v1alpha1.NASFileSyst

// IsUpdateToDate checks whether cr is up to date
func IsUpdateToDate(cr *v1alpha1.NASFileSystem, fsResponse *sdk.DescribeFileSystemsResponse) bool {
if len(fsResponse.Body.FileSystems.FileSystem) == 0 {
if *fsResponse.Body.TotalCount == 0 {
return false
}
fs := fsResponse.Body.FileSystems.FileSystem[0]

if cr.Spec.StorageType == fs.StorageType && cr.Spec.ProtocolType == fs.ProtocolType &&
cr.Spec.ChargeType == fs.ChargeType && cr.Spec.FileSystemType == fs.FileSystemType {
if *cr.Spec.StorageType == *fs.StorageType && *cr.Spec.ProtocolType == *fs.ProtocolType {
return true
}
return false
}

// IsNotFoundError helper function to test for SLS project not found error
func IsNotFoundError(err error) bool {
if err == nil {
return false
}
if e, ok := errors.Cause(err).(*tea.SDKError); ok && (*e.Code == errCodeFileSystemNotExist) {
return true
}
return false
Expand Down
22 changes: 14 additions & 8 deletions pkg/controller/nas/nas_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/pkg/errors"
Expand Down Expand Up @@ -133,19 +132,23 @@ func (e *External) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotNASFileSystem)
}

if cr.Status.AtProvider.FileSystemID == "" {
fsID := cr.Status.AtProvider.FileSystemID
if fsID == "" {
return managed.ExternalObservation{
ResourceExists: false,
}, nil
}

fsID := meta.GetExternalName(cr)
filesystem, err := e.ExternalClient.DescribeFileSystems(&fsID, cr.Spec.FileSystemType, cr.Spec.VpcID)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errFailedToDescribeNASFileSystem)
// Managed resource `NASFileSystem` is special, the identifier of if `name` is different to the cloud resource identifier `FileSystemID`
if nasclient.IsNotFoundError(err) {
return managed.ExternalObservation{ResourceExists: false, ResourceUpToDate: true}, nil
}
return managed.ExternalObservation{ResourceExists: false}, nil
}

cr.Status.AtProvider = nasclient.GenerateObservation(*filesystem)
cr.Status.AtProvider = nasclient.GenerateObservation(filesystem)
var upToDate = nasclient.IsUpdateToDate(cr, filesystem)
if upToDate {
cr.SetConditions(xpv1.Available())
Expand Down Expand Up @@ -177,8 +180,11 @@ func (e *External) Create(ctx context.Context, mg resource.Managed) (managed.Ext
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errFailedToCreateNASFileSystem)
}
// The name of this managed resource could not be regarded as its external name, so set it as FileSystem ID
meta.SetExternalName(cr, *res.Body.FileSystemId)
fsRes, err := e.ExternalClient.DescribeFileSystems(res.Body.FileSystemId, cr.Spec.FileSystemType, cr.Spec.VpcID)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errFailedToDescribeNASFileSystem)
}
cr.Status.AtProvider = nasclient.GenerateObservation(fsRes)
return managed.ExternalCreation{ConnectionDetails: GetConnectionDetails(cr)}, nil
}

Expand All @@ -194,7 +200,7 @@ func (e *External) Delete(ctx context.Context, mg resource.Managed) error {
return errors.New(errNotNASFileSystem)
}
cr.SetConditions(xpv1.Deleting())
if err := e.ExternalClient.DeleteFileSystem(meta.GetExternalName(cr)); err != nil {
if err := e.ExternalClient.DeleteFileSystem(cr.Status.AtProvider.FileSystemID); err != nil {
return errors.Wrap(err, errFailedToDeleteNASFileSystem)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/nas/nas_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestCreate(t *testing.T) {

validCR := &v1alpha1.NASFileSystem{Spec: v1alpha1.NASFileSystemSpec{}}
validCR.Spec.StorageType = pointer.StringPtr("standard")
validCR.Spec.ProtocolType = pointer.StringPtr("standard")
validCR.Spec.ProtocolType = pointer.StringPtr("nfs")
validCR.ObjectMeta.Annotations = map[string]string{meta.AnnotationKeyExternalName: "def"}

type want struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"

nasapi "github.com/crossplane/provider-alibaba/apis/nas/v1alpha1"
ossapi "github.com/crossplane/provider-alibaba/apis/oss/v1alpha1"
)

Expand All @@ -47,6 +48,8 @@ func GetEndpoint(res runtime.Object, region string) (string, error) {
switch res.GetObjectKind().GroupVersionKind().Kind {
case ossapi.BucketKind:
endpoint = fmt.Sprintf("http://oss-%s.%s", region, Domain)
case nasapi.NASFileSystemKind:
endpoint = fmt.Sprintf("nas.%s.aliyuncs.com", region)
default:
return "", errors.New(errCloudResourceNotSupported)
}
Expand Down

0 comments on commit 59184e2

Please # to comment.