diff --git a/apis/v1alpha1/ack-generate-metadata.yaml b/apis/v1alpha1/ack-generate-metadata.yaml index 9de530a..4bb6a15 100755 --- a/apis/v1alpha1/ack-generate-metadata.yaml +++ b/apis/v1alpha1/ack-generate-metadata.yaml @@ -1,13 +1,13 @@ ack_generate_info: - build_date: "2025-05-13T05:07:08Z" + build_date: "2025-05-14T19:59:03Z" build_hash: 55bf57b2806c33a7fcd074be403f26ce3f8e58db - go_version: go1.24.2 + go_version: go1.24.0 version: v0.46.2 -api_directory_checksum: c40438b41545af54669ac6a75a4f0b80cbade887 +api_directory_checksum: 249e70c68adf331dfdba2528f760cdbed329a9d7 api_version: v1alpha1 aws_sdk_go_version: v1.32.6 generator_config_info: - file_checksum: b207c87922237e3a1ca38bd92f173b6e5046d851 + file_checksum: ecd1cf4017fb208de55893b919ec0c326738f7f7 original_file_name: generator.yaml last_modification: reason: API generation diff --git a/apis/v1alpha1/enums.go b/apis/v1alpha1/enums.go index 82bf23a..c6df4a5 100644 --- a/apis/v1alpha1/enums.go +++ b/apis/v1alpha1/enums.go @@ -83,11 +83,11 @@ const ( TimeToLiveStatus_ENABLED TimeToLiveStatus = "ENABLED" ) -type TypeStatus string +type TypeStatus_SDK string const ( - TypeStatus_ACTIVE TypeStatus = "ACTIVE" - TypeStatus_CREATING TypeStatus = "CREATING" - TypeStatus_DELETING TypeStatus = "DELETING" - TypeStatus_RESTORING TypeStatus = "RESTORING" + TypeStatus_SDK_ACTIVE TypeStatus_SDK = "ACTIVE" + TypeStatus_SDK_CREATING TypeStatus_SDK = "CREATING" + TypeStatus_SDK_DELETING TypeStatus_SDK = "DELETING" + TypeStatus_SDK_RESTORING TypeStatus_SDK = "RESTORING" ) diff --git a/apis/v1alpha1/generator.yaml b/apis/v1alpha1/generator.yaml index 50d0c17..085f8f4 100644 --- a/apis/v1alpha1/generator.yaml +++ b/apis/v1alpha1/generator.yaml @@ -3,7 +3,7 @@ ignore: # - Keyspace # - Table - Types - - Type + # - Type field_paths: - CreateTableInput.AutoScalingSpecification - CreateTableInput.ReplicaSpecifications @@ -30,4 +30,16 @@ resources: 404: code: ResourceNotFoundException terminal_codes: - - InvalidParameterException \ No newline at end of file + - InvalidParameterException + Type: + fields: + FieldDefinitions: + is_immutable: true + tags: + ignore: true + exceptions: + errors: + 404: + code: ResourceNotFoundException + terminal_codes: + - ValidationException diff --git a/apis/v1alpha1/type.go b/apis/v1alpha1/type.go new file mode 100644 index 0000000..9cfb15e --- /dev/null +++ b/apis/v1alpha1/type.go @@ -0,0 +1,90 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package v1alpha1 + +import ( + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// TypeSpec defines the desired state of Type. +type TypeSpec struct { + + // The field definitions, consisting of names and types, that define this type. + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + // +kubebuilder:validation:Required + FieldDefinitions []*FieldDefinition `json:"fieldDefinitions"` + // The name of the keyspace. + // +kubebuilder:validation:Required + KeyspaceName *string `json:"keyspaceName"` + // The name of the user-defined type. + // + // UDT names must contain 48 characters or less, must begin with an alphabetic + // character, and can only contain alpha-numeric characters and underscores. + // Amazon Keyspaces converts upper case characters automatically into lower + // case characters. + // + // Alternatively, you can declare a UDT name in double quotes. When declaring + // a UDT name inside double quotes, Amazon Keyspaces preserves upper casing + // and allows special characters. + // + // You can also use double quotes as part of the name when you create the UDT, + // but you must escape each double quote character with an additional double + // quote character. + // +kubebuilder:validation:Required + TypeName *string `json:"typeName"` +} + +// TypeStatus defines the observed state of Type +type TypeStatus struct { + // All CRs managed by ACK have a common `Status.ACKResourceMetadata` member + // that is used to contain resource sync state, account ownership, + // constructed ARN for the resource + // +kubebuilder:validation:Optional + ACKResourceMetadata *ackv1alpha1.ResourceMetadata `json:"ackResourceMetadata"` + // All CRs managed by ACK have a common `Status.Conditions` member that + // contains a collection of `ackv1alpha1.Condition` objects that describe + // the various terminal states of the CR and its backend AWS service API + // resource + // +kubebuilder:validation:Optional + Conditions []*ackv1alpha1.Condition `json:"conditions"` + // The unique identifier of the keyspace that contains the new type in the format + // of an Amazon Resource Name (ARN). + // +kubebuilder:validation:Optional + KeyspaceARN *string `json:"keyspaceARN,omitempty"` +} + +// Type is the Schema for the Types API +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +type Type struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + Spec TypeSpec `json:"spec,omitempty"` + Status TypeStatus `json:"status,omitempty"` +} + +// TypeList contains a list of Type +// +kubebuilder:object:root=true +type TypeList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []Type `json:"items"` +} + +func init() { + SchemeBuilder.Register(&Type{}, &TypeList{}) +} diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index d42e990..bdba7d8 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -1049,3 +1049,134 @@ func (in *TimeToLive) DeepCopy() *TimeToLive { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Type) DeepCopyInto(out *Type) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Type. +func (in *Type) DeepCopy() *Type { + if in == nil { + return nil + } + out := new(Type) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *Type) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TypeList) DeepCopyInto(out *TypeList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]Type, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TypeList. +func (in *TypeList) DeepCopy() *TypeList { + if in == nil { + return nil + } + out := new(TypeList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *TypeList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TypeSpec) DeepCopyInto(out *TypeSpec) { + *out = *in + if in.FieldDefinitions != nil { + in, out := &in.FieldDefinitions, &out.FieldDefinitions + *out = make([]*FieldDefinition, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(FieldDefinition) + (*in).DeepCopyInto(*out) + } + } + } + if in.KeyspaceName != nil { + in, out := &in.KeyspaceName, &out.KeyspaceName + *out = new(string) + **out = **in + } + if in.TypeName != nil { + in, out := &in.TypeName, &out.TypeName + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TypeSpec. +func (in *TypeSpec) DeepCopy() *TypeSpec { + if in == nil { + return nil + } + out := new(TypeSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TypeStatus) DeepCopyInto(out *TypeStatus) { + *out = *in + if in.ACKResourceMetadata != nil { + in, out := &in.ACKResourceMetadata, &out.ACKResourceMetadata + *out = new(corev1alpha1.ResourceMetadata) + (*in).DeepCopyInto(*out) + } + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]*corev1alpha1.Condition, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(corev1alpha1.Condition) + (*in).DeepCopyInto(*out) + } + } + } + if in.KeyspaceARN != nil { + in, out := &in.KeyspaceARN, &out.KeyspaceARN + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TypeStatus. +func (in *TypeStatus) DeepCopy() *TypeStatus { + if in == nil { + return nil + } + out := new(TypeStatus) + in.DeepCopyInto(out) + return out +} diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 1ebd11e..cf4d5f6 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -41,6 +41,7 @@ import ( _ "github.com/aws-controllers-k8s/keyspaces-controller/pkg/resource/keyspace" _ "github.com/aws-controllers-k8s/keyspaces-controller/pkg/resource/table" + _ "github.com/aws-controllers-k8s/keyspaces-controller/pkg/resource/type_" "github.com/aws-controllers-k8s/keyspaces-controller/pkg/version" ) diff --git a/config/controller/kustomization.yaml b/config/controller/kustomization.yaml index a189b0d..3db0254 100644 --- a/config/controller/kustomization.yaml +++ b/config/controller/kustomization.yaml @@ -6,4 +6,4 @@ kind: Kustomization images: - name: controller newName: public.ecr.aws/aws-controllers-k8s/keyspaces-controller - newTag: 1.0.9 + newTag: 1.0.10 diff --git a/config/crd/bases/keyspaces.services.k8s.aws_types.yaml b/config/crd/bases/keyspaces.services.k8s.aws_types.yaml new file mode 100644 index 0000000..b735b34 --- /dev/null +++ b/config/crd/bases/keyspaces.services.k8s.aws_types.yaml @@ -0,0 +1,159 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.16.2 + name: types.keyspaces.services.k8s.aws +spec: + group: keyspaces.services.k8s.aws + names: + kind: Type + listKind: TypeList + plural: types + singular: type + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: Type is the Schema for the Types API + properties: + apiVersion: + description: |- + APIVersion defines the versioned schema of this representation of an object. + Servers should convert recognized schemas to the latest internal value, and + may reject unrecognized values. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + type: string + kind: + description: |- + Kind is a string value representing the REST resource this object represents. + Servers may infer this from the endpoint the client submits requests to. + Cannot be updated. + In CamelCase. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + metadata: + type: object + spec: + description: TypeSpec defines the desired state of Type. + properties: + fieldDefinitions: + description: The field definitions, consisting of names and types, + that define this type. + items: + description: A field definition consists out of a name and a type. + properties: + name: + type: string + type_: + type: string + type: object + type: array + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf + keyspaceName: + description: The name of the keyspace. + type: string + typeName: + description: |- + The name of the user-defined type. + + UDT names must contain 48 characters or less, must begin with an alphabetic + character, and can only contain alpha-numeric characters and underscores. + Amazon Keyspaces converts upper case characters automatically into lower + case characters. + + Alternatively, you can declare a UDT name in double quotes. When declaring + a UDT name inside double quotes, Amazon Keyspaces preserves upper casing + and allows special characters. + + You can also use double quotes as part of the name when you create the UDT, + but you must escape each double quote character with an additional double + quote character. + type: string + required: + - fieldDefinitions + - keyspaceName + - typeName + type: object + status: + description: TypeStatus defines the observed state of Type + properties: + ackResourceMetadata: + description: |- + All CRs managed by ACK have a common `Status.ACKResourceMetadata` member + that is used to contain resource sync state, account ownership, + constructed ARN for the resource + properties: + arn: + description: |- + ARN is the Amazon Resource Name for the resource. This is a + globally-unique identifier and is set only by the ACK service controller + once the controller has orchestrated the creation of the resource OR + when it has verified that an "adopted" resource (a resource where the + ARN annotation was set by the Kubernetes user on the CR) exists and + matches the supplied CR's Spec field values. + https://github.com/aws/aws-controllers-k8s/issues/270 + type: string + ownerAccountID: + description: |- + OwnerAccountID is the AWS Account ID of the account that owns the + backend AWS service API resource. + type: string + region: + description: Region is the AWS region in which the resource exists + or will exist. + type: string + required: + - ownerAccountID + - region + type: object + conditions: + description: |- + All CRs managed by ACK have a common `Status.Conditions` member that + contains a collection of `ackv1alpha1.Condition` objects that describe + the various terminal states of the CR and its backend AWS service API + resource + items: + description: |- + Condition is the common struct used by all CRDs managed by ACK service + controllers to indicate terminal states of the CR and its backend AWS + service API resource + properties: + lastTransitionTime: + description: Last time the condition transitioned from one status + to another. + format: date-time + type: string + message: + description: A human readable message indicating details about + the transition. + type: string + reason: + description: The reason for the condition's last transition. + type: string + status: + description: Status of the condition, one of True, False, Unknown. + type: string + type: + description: Type is the type of the Condition + type: string + required: + - status + - type + type: object + type: array + keyspaceARN: + description: |- + The unique identifier of the keyspace that contains the new type in the format + of an Amazon Resource Name (ARN). + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 1a70e9f..57be678 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -4,3 +4,4 @@ resources: - common - bases/keyspaces.services.k8s.aws_keyspaces.yaml - bases/keyspaces.services.k8s.aws_tables.yaml + - bases/keyspaces.services.k8s.aws_types.yaml diff --git a/config/rbac/cluster-role-controller.yaml b/config/rbac/cluster-role-controller.yaml index be56693..d0a2c7f 100644 --- a/config/rbac/cluster-role-controller.yaml +++ b/config/rbac/cluster-role-controller.yaml @@ -27,6 +27,7 @@ rules: resources: - keyspaces - tables + - types verbs: - create - delete @@ -40,6 +41,7 @@ rules: resources: - keyspaces/status - tables/status + - types/status verbs: - get - patch diff --git a/config/rbac/role-reader.yaml b/config/rbac/role-reader.yaml index af4f448..3d629c3 100644 --- a/config/rbac/role-reader.yaml +++ b/config/rbac/role-reader.yaml @@ -11,6 +11,7 @@ rules: resources: - keyspaces - tables + - types verbs: - get - list diff --git a/config/rbac/role-writer.yaml b/config/rbac/role-writer.yaml index 3583e13..e3aa640 100644 --- a/config/rbac/role-writer.yaml +++ b/config/rbac/role-writer.yaml @@ -11,6 +11,7 @@ rules: resources: - keyspaces - tables + - types verbs: - create - delete @@ -24,6 +25,7 @@ rules: resources: - keyspaces - tables + - types verbs: - get - patch diff --git a/generator.yaml b/generator.yaml index 50d0c17..085f8f4 100644 --- a/generator.yaml +++ b/generator.yaml @@ -3,7 +3,7 @@ ignore: # - Keyspace # - Table - Types - - Type + # - Type field_paths: - CreateTableInput.AutoScalingSpecification - CreateTableInput.ReplicaSpecifications @@ -30,4 +30,16 @@ resources: 404: code: ResourceNotFoundException terminal_codes: - - InvalidParameterException \ No newline at end of file + - InvalidParameterException + Type: + fields: + FieldDefinitions: + is_immutable: true + tags: + ignore: true + exceptions: + errors: + 404: + code: ResourceNotFoundException + terminal_codes: + - ValidationException diff --git a/helm/Chart.yaml b/helm/Chart.yaml index dfc6b78..ad347be 100644 --- a/helm/Chart.yaml +++ b/helm/Chart.yaml @@ -1,8 +1,8 @@ apiVersion: v1 name: keyspaces-chart description: A Helm chart for the ACK service controller for Amazon Keyspaces (Keyspaces) -version: 1.0.9 -appVersion: 1.0.9 +version: 1.0.10 +appVersion: 1.0.10 home: https://github.com/aws-controllers-k8s/keyspaces-controller icon: https://raw.githubusercontent.com/aws/eks-charts/master/docs/logo/aws.png sources: diff --git a/helm/crds/keyspaces.services.k8s.aws_types.yaml b/helm/crds/keyspaces.services.k8s.aws_types.yaml new file mode 100644 index 0000000..b735b34 --- /dev/null +++ b/helm/crds/keyspaces.services.k8s.aws_types.yaml @@ -0,0 +1,159 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.16.2 + name: types.keyspaces.services.k8s.aws +spec: + group: keyspaces.services.k8s.aws + names: + kind: Type + listKind: TypeList + plural: types + singular: type + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: Type is the Schema for the Types API + properties: + apiVersion: + description: |- + APIVersion defines the versioned schema of this representation of an object. + Servers should convert recognized schemas to the latest internal value, and + may reject unrecognized values. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + type: string + kind: + description: |- + Kind is a string value representing the REST resource this object represents. + Servers may infer this from the endpoint the client submits requests to. + Cannot be updated. + In CamelCase. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + metadata: + type: object + spec: + description: TypeSpec defines the desired state of Type. + properties: + fieldDefinitions: + description: The field definitions, consisting of names and types, + that define this type. + items: + description: A field definition consists out of a name and a type. + properties: + name: + type: string + type_: + type: string + type: object + type: array + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf + keyspaceName: + description: The name of the keyspace. + type: string + typeName: + description: |- + The name of the user-defined type. + + UDT names must contain 48 characters or less, must begin with an alphabetic + character, and can only contain alpha-numeric characters and underscores. + Amazon Keyspaces converts upper case characters automatically into lower + case characters. + + Alternatively, you can declare a UDT name in double quotes. When declaring + a UDT name inside double quotes, Amazon Keyspaces preserves upper casing + and allows special characters. + + You can also use double quotes as part of the name when you create the UDT, + but you must escape each double quote character with an additional double + quote character. + type: string + required: + - fieldDefinitions + - keyspaceName + - typeName + type: object + status: + description: TypeStatus defines the observed state of Type + properties: + ackResourceMetadata: + description: |- + All CRs managed by ACK have a common `Status.ACKResourceMetadata` member + that is used to contain resource sync state, account ownership, + constructed ARN for the resource + properties: + arn: + description: |- + ARN is the Amazon Resource Name for the resource. This is a + globally-unique identifier and is set only by the ACK service controller + once the controller has orchestrated the creation of the resource OR + when it has verified that an "adopted" resource (a resource where the + ARN annotation was set by the Kubernetes user on the CR) exists and + matches the supplied CR's Spec field values. + https://github.com/aws/aws-controllers-k8s/issues/270 + type: string + ownerAccountID: + description: |- + OwnerAccountID is the AWS Account ID of the account that owns the + backend AWS service API resource. + type: string + region: + description: Region is the AWS region in which the resource exists + or will exist. + type: string + required: + - ownerAccountID + - region + type: object + conditions: + description: |- + All CRs managed by ACK have a common `Status.Conditions` member that + contains a collection of `ackv1alpha1.Condition` objects that describe + the various terminal states of the CR and its backend AWS service API + resource + items: + description: |- + Condition is the common struct used by all CRDs managed by ACK service + controllers to indicate terminal states of the CR and its backend AWS + service API resource + properties: + lastTransitionTime: + description: Last time the condition transitioned from one status + to another. + format: date-time + type: string + message: + description: A human readable message indicating details about + the transition. + type: string + reason: + description: The reason for the condition's last transition. + type: string + status: + description: Status of the condition, one of True, False, Unknown. + type: string + type: + description: Type is the type of the Condition + type: string + required: + - status + - type + type: object + type: array + keyspaceARN: + description: |- + The unique identifier of the keyspace that contains the new type in the format + of an Amazon Resource Name (ARN). + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/helm/templates/NOTES.txt b/helm/templates/NOTES.txt index 9eb9015..afe820b 100644 --- a/helm/templates/NOTES.txt +++ b/helm/templates/NOTES.txt @@ -1,5 +1,5 @@ {{ .Chart.Name }} has been installed. -This chart deploys "public.ecr.aws/aws-controllers-k8s/keyspaces-controller:1.0.9". +This chart deploys "public.ecr.aws/aws-controllers-k8s/keyspaces-controller:1.0.10". Check its status by running: kubectl --namespace {{ .Release.Namespace }} get pods -l "app.kubernetes.io/instance={{ .Release.Name }}" diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl index fac5cb9..c7b172e 100644 --- a/helm/templates/_helpers.tpl +++ b/helm/templates/_helpers.tpl @@ -74,6 +74,7 @@ rules: resources: - keyspaces - tables + - types verbs: - create - delete @@ -87,6 +88,7 @@ rules: resources: - keyspaces/status - tables/status + - types/status verbs: - get - patch diff --git a/helm/templates/role-reader.yaml b/helm/templates/role-reader.yaml index f5e5963..bcec6d9 100644 --- a/helm/templates/role-reader.yaml +++ b/helm/templates/role-reader.yaml @@ -11,6 +11,7 @@ rules: resources: - keyspaces - tables + - types verbs: - get - list diff --git a/helm/templates/role-writer.yaml b/helm/templates/role-writer.yaml index 9dcf4ff..b56b001 100644 --- a/helm/templates/role-writer.yaml +++ b/helm/templates/role-writer.yaml @@ -11,6 +11,7 @@ rules: resources: - keyspaces - tables + - types verbs: - create - delete @@ -24,6 +25,7 @@ rules: resources: - keyspaces - tables + - types verbs: - get - patch diff --git a/helm/values.yaml b/helm/values.yaml index d10a8f0..fe7f8b2 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -4,7 +4,7 @@ image: repository: public.ecr.aws/aws-controllers-k8s/keyspaces-controller - tag: 1.0.9 + tag: 1.0.10 pullPolicy: IfNotPresent pullSecrets: [] @@ -144,6 +144,7 @@ reconcile: resources: - Keyspace - Table + - Type serviceAccount: # Specifies whether a service account should be created diff --git a/pkg/resource/type_/delta.go b/pkg/resource/type_/delta.go new file mode 100644 index 0000000..5dae06d --- /dev/null +++ b/pkg/resource/type_/delta.go @@ -0,0 +1,69 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + "bytes" + "reflect" + + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" + acktags "github.com/aws-controllers-k8s/runtime/pkg/tags" +) + +// Hack to avoid import errors during build... +var ( + _ = &bytes.Buffer{} + _ = &reflect.Method{} + _ = &acktags.Tags{} +) + +// newResourceDelta returns a new `ackcompare.Delta` used to compare two +// resources +func newResourceDelta( + a *resource, + b *resource, +) *ackcompare.Delta { + delta := ackcompare.NewDelta() + if (a == nil && b != nil) || + (a != nil && b == nil) { + delta.Add("", a, b) + return delta + } + + if len(a.ko.Spec.FieldDefinitions) != len(b.ko.Spec.FieldDefinitions) { + delta.Add("Spec.FieldDefinitions", a.ko.Spec.FieldDefinitions, b.ko.Spec.FieldDefinitions) + } else if len(a.ko.Spec.FieldDefinitions) > 0 { + if !reflect.DeepEqual(a.ko.Spec.FieldDefinitions, b.ko.Spec.FieldDefinitions) { + delta.Add("Spec.FieldDefinitions", a.ko.Spec.FieldDefinitions, b.ko.Spec.FieldDefinitions) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.KeyspaceName, b.ko.Spec.KeyspaceName) { + delta.Add("Spec.KeyspaceName", a.ko.Spec.KeyspaceName, b.ko.Spec.KeyspaceName) + } else if a.ko.Spec.KeyspaceName != nil && b.ko.Spec.KeyspaceName != nil { + if *a.ko.Spec.KeyspaceName != *b.ko.Spec.KeyspaceName { + delta.Add("Spec.KeyspaceName", a.ko.Spec.KeyspaceName, b.ko.Spec.KeyspaceName) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.TypeName, b.ko.Spec.TypeName) { + delta.Add("Spec.TypeName", a.ko.Spec.TypeName, b.ko.Spec.TypeName) + } else if a.ko.Spec.TypeName != nil && b.ko.Spec.TypeName != nil { + if *a.ko.Spec.TypeName != *b.ko.Spec.TypeName { + delta.Add("Spec.TypeName", a.ko.Spec.TypeName, b.ko.Spec.TypeName) + } + } + + return delta +} diff --git a/pkg/resource/type_/descriptor.go b/pkg/resource/type_/descriptor.go new file mode 100644 index 0000000..d04e6c3 --- /dev/null +++ b/pkg/resource/type_/descriptor.go @@ -0,0 +1,155 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + rtclient "sigs.k8s.io/controller-runtime/pkg/client" + k8sctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + svcapitypes "github.com/aws-controllers-k8s/keyspaces-controller/apis/v1alpha1" +) + +const ( + FinalizerString = "finalizers.keyspaces.services.k8s.aws/Type" +) + +var ( + GroupVersionResource = svcapitypes.GroupVersion.WithResource("types") + GroupKind = metav1.GroupKind{ + Group: "keyspaces.services.k8s.aws", + Kind: "Type", + } +) + +// resourceDescriptor implements the +// `aws-service-operator-k8s/pkg/types.AWSResourceDescriptor` interface +type resourceDescriptor struct { +} + +// GroupVersionKind returns a Kubernetes schema.GroupVersionKind struct that +// describes the API Group, Version and Kind of CRs described by the descriptor +func (d *resourceDescriptor) GroupVersionKind() schema.GroupVersionKind { + return svcapitypes.GroupVersion.WithKind(GroupKind.Kind) +} + +// EmptyRuntimeObject returns an empty object prototype that may be used in +// apimachinery and k8s client operations +func (d *resourceDescriptor) EmptyRuntimeObject() rtclient.Object { + return &svcapitypes.Type{} +} + +// ResourceFromRuntimeObject returns an AWSResource that has been initialized +// with the supplied runtime.Object +func (d *resourceDescriptor) ResourceFromRuntimeObject( + obj rtclient.Object, +) acktypes.AWSResource { + return &resource{ + ko: obj.(*svcapitypes.Type), + } +} + +// Delta returns an `ackcompare.Delta` object containing the difference between +// one `AWSResource` and another. +func (d *resourceDescriptor) Delta(a, b acktypes.AWSResource) *ackcompare.Delta { + return newResourceDelta(a.(*resource), b.(*resource)) +} + +// IsManaged returns true if the supplied AWSResource is under the management +// of an ACK service controller. What this means in practice is that the +// underlying custom resource (CR) in the AWSResource has had a +// resource-specific finalizer associated with it. +func (d *resourceDescriptor) IsManaged( + res acktypes.AWSResource, +) bool { + obj := res.RuntimeObject() + if obj == nil { + // Should not happen. If it does, there is a bug in the code + panic("nil RuntimeMetaObject in AWSResource") + } + // Remove use of custom code once + // https://github.com/kubernetes-sigs/controller-runtime/issues/994 is + // fixed. This should be able to be: + // + // return k8sctrlutil.ContainsFinalizer(obj, FinalizerString) + return containsFinalizer(obj, FinalizerString) +} + +// Remove once https://github.com/kubernetes-sigs/controller-runtime/issues/994 +// is fixed. +func containsFinalizer(obj rtclient.Object, finalizer string) bool { + f := obj.GetFinalizers() + for _, e := range f { + if e == finalizer { + return true + } + } + return false +} + +// MarkManaged places the supplied resource under the management of ACK. What +// this typically means is that the resource manager will decorate the +// underlying custom resource (CR) with a finalizer that indicates ACK is +// managing the resource and the underlying CR may not be deleted until ACK is +// finished cleaning up any backend AWS service resources associated with the +// CR. +func (d *resourceDescriptor) MarkManaged( + res acktypes.AWSResource, +) { + obj := res.RuntimeObject() + if obj == nil { + // Should not happen. If it does, there is a bug in the code + panic("nil RuntimeMetaObject in AWSResource") + } + k8sctrlutil.AddFinalizer(obj, FinalizerString) +} + +// MarkUnmanaged removes the supplied resource from management by ACK. What +// this typically means is that the resource manager will remove a finalizer +// underlying custom resource (CR) that indicates ACK is managing the resource. +// This will allow the Kubernetes API server to delete the underlying CR. +func (d *resourceDescriptor) MarkUnmanaged( + res acktypes.AWSResource, +) { + obj := res.RuntimeObject() + if obj == nil { + // Should not happen. If it does, there is a bug in the code + panic("nil RuntimeMetaObject in AWSResource") + } + k8sctrlutil.RemoveFinalizer(obj, FinalizerString) +} + +// MarkAdopted places descriptors on the custom resource that indicate the +// resource was not created from within ACK. +func (d *resourceDescriptor) MarkAdopted( + res acktypes.AWSResource, +) { + obj := res.RuntimeObject() + if obj == nil { + // Should not happen. If it does, there is a bug in the code + panic("nil RuntimeObject in AWSResource") + } + curr := obj.GetAnnotations() + if curr == nil { + curr = make(map[string]string) + } + curr[ackv1alpha1.AnnotationAdopted] = "true" + obj.SetAnnotations(curr) +} diff --git a/pkg/resource/type_/identifiers.go b/pkg/resource/type_/identifiers.go new file mode 100644 index 0000000..4ae1af6 --- /dev/null +++ b/pkg/resource/type_/identifiers.go @@ -0,0 +1,55 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" +) + +// resourceIdentifiers implements the +// `aws-service-operator-k8s/pkg/types.AWSResourceIdentifiers` interface +type resourceIdentifiers struct { + meta *ackv1alpha1.ResourceMetadata +} + +// ARN returns the AWS Resource Name for the backend AWS resource. If nil, +// this means the resource has not yet been created in the backend AWS +// service. +func (ri *resourceIdentifiers) ARN() *ackv1alpha1.AWSResourceName { + if ri.meta != nil { + return ri.meta.ARN + } + return nil +} + +// OwnerAccountID returns the AWS account identifier in which the +// backend AWS resource resides, or nil if this information is not known +// for the resource +func (ri *resourceIdentifiers) OwnerAccountID() *ackv1alpha1.AWSAccountID { + if ri.meta != nil { + return ri.meta.OwnerAccountID + } + return nil +} + +// Region returns the AWS region in which the resource exists, or +// nil if this information is not known. +func (ri *resourceIdentifiers) Region() *ackv1alpha1.AWSRegion { + if ri.meta != nil { + return ri.meta.Region + } + return nil +} diff --git a/pkg/resource/type_/manager.go b/pkg/resource/type_/manager.go new file mode 100644 index 0000000..a119254 --- /dev/null +++ b/pkg/resource/type_/manager.go @@ -0,0 +1,376 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + "context" + "fmt" + "time" + + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" + ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition" + ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config" + ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors" + ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics" + ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue" + ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime" + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" + acktags "github.com/aws-controllers-k8s/runtime/pkg/tags" + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" + ackutil "github.com/aws-controllers-k8s/runtime/pkg/util" + "github.com/aws/aws-sdk-go-v2/aws" + svcsdk "github.com/aws/aws-sdk-go-v2/service/keyspaces" + "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" + + svcapitypes "github.com/aws-controllers-k8s/keyspaces-controller/apis/v1alpha1" +) + +var ( + _ = ackutil.InStrings + _ = acktags.NewTags() + _ = ackrt.MissingImageTagValue + _ = svcapitypes.Type{} +) + +// +kubebuilder:rbac:groups=keyspaces.services.k8s.aws,resources=types,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=keyspaces.services.k8s.aws,resources=types/status,verbs=get;update;patch + +var lateInitializeFieldNames = []string{} + +// resourceManager is responsible for providing a consistent way to perform +// CRUD operations in a backend AWS service API for Book custom resources. +type resourceManager struct { + // cfg is a copy of the ackcfg.Config object passed on start of the service + // controller + cfg ackcfg.Config + // clientcfg is a copy of the client configuration passed on start of the + // service controller + clientcfg aws.Config + // log refers to the logr.Logger object handling logging for the service + // controller + log logr.Logger + // metrics contains a collection of Prometheus metric objects that the + // service controller and its reconcilers track + metrics *ackmetrics.Metrics + // rr is the Reconciler which can be used for various utility + // functions such as querying for Secret values given a SecretReference + rr acktypes.Reconciler + // awsAccountID is the AWS account identifier that contains the resources + // managed by this resource manager + awsAccountID ackv1alpha1.AWSAccountID + // The AWS Region that this resource manager targets + awsRegion ackv1alpha1.AWSRegion + // sdk is a pointer to the AWS service API client exposed by the + // aws-sdk-go-v2/services/{alias} package. + sdkapi *svcsdk.Client +} + +// concreteResource returns a pointer to a resource from the supplied +// generic AWSResource interface +func (rm *resourceManager) concreteResource( + res acktypes.AWSResource, +) *resource { + // cast the generic interface into a pointer type specific to the concrete + // implementing resource type managed by this resource manager + return res.(*resource) +} + +// ReadOne returns the currently-observed state of the supplied AWSResource in +// the backend AWS service API. +func (rm *resourceManager) ReadOne( + ctx context.Context, + res acktypes.AWSResource, +) (acktypes.AWSResource, error) { + r := rm.concreteResource(res) + if r.ko == nil { + // Should never happen... if it does, it's buggy code. + panic("resource manager's ReadOne() method received resource with nil CR object") + } + observed, err := rm.sdkFind(ctx, r) + mirrorAWSTags(r, observed) + if err != nil { + if observed != nil { + return rm.onError(observed, err) + } + return rm.onError(r, err) + } + return rm.onSuccess(observed) +} + +// Create attempts to create the supplied AWSResource in the backend AWS +// service API, returning an AWSResource representing the newly-created +// resource +func (rm *resourceManager) Create( + ctx context.Context, + res acktypes.AWSResource, +) (acktypes.AWSResource, error) { + r := rm.concreteResource(res) + if r.ko == nil { + // Should never happen... if it does, it's buggy code. + panic("resource manager's Create() method received resource with nil CR object") + } + created, err := rm.sdkCreate(ctx, r) + if err != nil { + if created != nil { + return rm.onError(created, err) + } + return rm.onError(r, err) + } + return rm.onSuccess(created) +} + +// Update attempts to mutate the supplied desired AWSResource in the backend AWS +// service API, returning an AWSResource representing the newly-mutated +// resource. +// Note for specialized logic implementers can check to see how the latest +// observed resource differs from the supplied desired state. The +// higher-level reonciler determines whether or not the desired differs +// from the latest observed and decides whether to call the resource +// manager's Update method +func (rm *resourceManager) Update( + ctx context.Context, + resDesired acktypes.AWSResource, + resLatest acktypes.AWSResource, + delta *ackcompare.Delta, +) (acktypes.AWSResource, error) { + desired := rm.concreteResource(resDesired) + latest := rm.concreteResource(resLatest) + if desired.ko == nil || latest.ko == nil { + // Should never happen... if it does, it's buggy code. + panic("resource manager's Update() method received resource with nil CR object") + } + updated, err := rm.sdkUpdate(ctx, desired, latest, delta) + if err != nil { + if updated != nil { + return rm.onError(updated, err) + } + return rm.onError(latest, err) + } + return rm.onSuccess(updated) +} + +// Delete attempts to destroy the supplied AWSResource in the backend AWS +// service API, returning an AWSResource representing the +// resource being deleted (if delete is asynchronous and takes time) +func (rm *resourceManager) Delete( + ctx context.Context, + res acktypes.AWSResource, +) (acktypes.AWSResource, error) { + r := rm.concreteResource(res) + if r.ko == nil { + // Should never happen... if it does, it's buggy code. + panic("resource manager's Update() method received resource with nil CR object") + } + observed, err := rm.sdkDelete(ctx, r) + if err != nil { + if observed != nil { + return rm.onError(observed, err) + } + return rm.onError(r, err) + } + + return rm.onSuccess(observed) +} + +// ARNFromName returns an AWS Resource Name from a given string name. This +// is useful for constructing ARNs for APIs that require ARNs in their +// GetAttributes operations but all we have (for new CRs at least) is a +// name for the resource +func (rm *resourceManager) ARNFromName(name string) string { + return fmt.Sprintf( + "arn:aws:keyspaces:%s:%s:%s", + rm.awsRegion, + rm.awsAccountID, + name, + ) +} + +// LateInitialize returns an acktypes.AWSResource after setting the late initialized +// fields from the readOne call. This method will initialize the optional fields +// which were not provided by the k8s user but were defaulted by the AWS service. +// If there are no such fields to be initialized, the returned object is similar to +// object passed in the parameter. +func (rm *resourceManager) LateInitialize( + ctx context.Context, + latest acktypes.AWSResource, +) (acktypes.AWSResource, error) { + rlog := ackrtlog.FromContext(ctx) + // If there are no fields to late initialize, do nothing + if len(lateInitializeFieldNames) == 0 { + rlog.Debug("no late initialization required.") + return latest, nil + } + latestCopy := latest.DeepCopy() + lateInitConditionReason := "" + lateInitConditionMessage := "" + observed, err := rm.ReadOne(ctx, latestCopy) + if err != nil { + lateInitConditionMessage = "Unable to complete Read operation required for late initialization" + lateInitConditionReason = "Late Initialization Failure" + ackcondition.SetLateInitialized(latestCopy, corev1.ConditionFalse, &lateInitConditionMessage, &lateInitConditionReason) + ackcondition.SetSynced(latestCopy, corev1.ConditionFalse, nil, nil) + return latestCopy, err + } + lateInitializedRes := rm.lateInitializeFromReadOneOutput(observed, latestCopy) + incompleteInitialization := rm.incompleteLateInitialization(lateInitializedRes) + if incompleteInitialization { + // Add the condition with LateInitialized=False + lateInitConditionMessage = "Late initialization did not complete, requeuing with delay of 5 seconds" + lateInitConditionReason = "Delayed Late Initialization" + ackcondition.SetLateInitialized(lateInitializedRes, corev1.ConditionFalse, &lateInitConditionMessage, &lateInitConditionReason) + ackcondition.SetSynced(lateInitializedRes, corev1.ConditionFalse, nil, nil) + return lateInitializedRes, ackrequeue.NeededAfter(nil, time.Duration(5)*time.Second) + } + // Set LateInitialized condition to True + lateInitConditionMessage = "Late initialization successful" + lateInitConditionReason = "Late initialization successful" + ackcondition.SetLateInitialized(lateInitializedRes, corev1.ConditionTrue, &lateInitConditionMessage, &lateInitConditionReason) + return lateInitializedRes, nil +} + +// incompleteLateInitialization return true if there are fields which were supposed to be +// late initialized but are not. If all the fields are late initialized, false is returned +func (rm *resourceManager) incompleteLateInitialization( + res acktypes.AWSResource, +) bool { + return false +} + +// lateInitializeFromReadOneOutput late initializes the 'latest' resource from the 'observed' +// resource and returns 'latest' resource +func (rm *resourceManager) lateInitializeFromReadOneOutput( + observed acktypes.AWSResource, + latest acktypes.AWSResource, +) acktypes.AWSResource { + return latest +} + +// IsSynced returns true if the resource is synced. +func (rm *resourceManager) IsSynced(ctx context.Context, res acktypes.AWSResource) (bool, error) { + r := rm.concreteResource(res) + if r.ko == nil { + // Should never happen... if it does, it's buggy code. + panic("resource manager's IsSynced() method received resource with nil CR object") + } + + return true, nil +} + +// EnsureTags ensures that tags are present inside the AWSResource. +// If the AWSResource does not have any existing resource tags, the 'tags' +// field is initialized and the controller tags are added. +// If the AWSResource has existing resource tags, then controller tags are +// added to the existing resource tags without overriding them. +// If the AWSResource does not support tags, only then the controller tags +// will not be added to the AWSResource. +func (rm *resourceManager) EnsureTags( + ctx context.Context, + res acktypes.AWSResource, + md acktypes.ServiceControllerMetadata, +) error { + + return nil +} + +// FilterAWSTags ignores tags that have keys that start with "aws:" +// is needed to ensure the controller does not attempt to remove +// tags set by AWS. This function needs to be called after each Read +// operation. +// Eg. resources created with cloudformation have tags that cannot be +// removed by an ACK controller +func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { + +} + +// mirrorAWSTags ensures that AWS tags are included in the desired resource +// if they are present in the latest resource. This will ensure that the +// aws tags are not present in a diff. The logic of the controller will +// ensure these tags aren't patched to the resource in the cluster, and +// will only be present to make sure we don't try to remove these tags. +// +// Although there are a lot of similarities between this function and +// EnsureTags, they are very much different. +// While EnsureTags tries to make sure the resource contains the controller +// tags, mirrowAWSTags tries to make sure tags injected by AWS are mirrored +// from the latest resoruce to the desired resource. +func mirrorAWSTags(a *resource, b *resource) { + +} + +// newResourceManager returns a new struct implementing +// acktypes.AWSResourceManager +// This is for AWS-SDK-GO-V2 - Created newResourceManager With AWS sdk-Go-ClientV2 +func newResourceManager( + cfg ackcfg.Config, + clientcfg aws.Config, + log logr.Logger, + metrics *ackmetrics.Metrics, + rr acktypes.Reconciler, + id ackv1alpha1.AWSAccountID, + region ackv1alpha1.AWSRegion, +) (*resourceManager, error) { + return &resourceManager{ + cfg: cfg, + clientcfg: clientcfg, + log: log, + metrics: metrics, + rr: rr, + awsAccountID: id, + awsRegion: region, + sdkapi: svcsdk.NewFromConfig(clientcfg), + }, nil +} + +// onError updates resource conditions and returns updated resource +// it returns nil if no condition is updated. +func (rm *resourceManager) onError( + r *resource, + err error, +) (acktypes.AWSResource, error) { + if r == nil { + return nil, err + } + r1, updated := rm.updateConditions(r, false, err) + if !updated { + return r, err + } + for _, condition := range r1.Conditions() { + if condition.Type == ackv1alpha1.ConditionTypeTerminal && + condition.Status == corev1.ConditionTrue { + // resource is in Terminal condition + // return Terminal error + return r1, ackerr.Terminal + } + } + return r1, err +} + +// onSuccess updates resource conditions and returns updated resource +// it returns the supplied resource if no condition is updated. +func (rm *resourceManager) onSuccess( + r *resource, +) (acktypes.AWSResource, error) { + if r == nil { + return nil, nil + } + r1, updated := rm.updateConditions(r, true, nil) + if !updated { + return r, nil + } + return r1, nil +} diff --git a/pkg/resource/type_/manager_factory.go b/pkg/resource/type_/manager_factory.go new file mode 100644 index 0000000..af7a7a9 --- /dev/null +++ b/pkg/resource/type_/manager_factory.go @@ -0,0 +1,100 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + "fmt" + "sync" + + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config" + ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics" + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/go-logr/logr" + + svcresource "github.com/aws-controllers-k8s/keyspaces-controller/pkg/resource" +) + +// resourceManagerFactory produces resourceManager objects. It implements the +// `types.AWSResourceManagerFactory` interface. +type resourceManagerFactory struct { + sync.RWMutex + // rmCache contains resource managers for a particular AWS account ID + rmCache map[string]*resourceManager +} + +// ResourcePrototype returns an AWSResource that resource managers produced by +// this factory will handle +func (f *resourceManagerFactory) ResourceDescriptor() acktypes.AWSResourceDescriptor { + return &resourceDescriptor{} +} + +// ManagerFor returns a resource manager object that can manage resources for a +// supplied AWS account +func (f *resourceManagerFactory) ManagerFor( + cfg ackcfg.Config, + clientcfg aws.Config, + log logr.Logger, + metrics *ackmetrics.Metrics, + rr acktypes.Reconciler, + id ackv1alpha1.AWSAccountID, + region ackv1alpha1.AWSRegion, + roleARN ackv1alpha1.AWSResourceName, +) (acktypes.AWSResourceManager, error) { + // We use the account ID, region, and role ARN to uniquely identify a + // resource manager. This helps us to avoid creating multiple resource + // managers for the same account/region/roleARN combination. + rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN) + f.RLock() + rm, found := f.rmCache[rmId] + f.RUnlock() + + if found { + return rm, nil + } + + f.Lock() + defer f.Unlock() + + rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region) + if err != nil { + return nil, err + } + f.rmCache[rmId] = rm + return rm, nil +} + +// IsAdoptable returns true if the resource is able to be adopted +func (f *resourceManagerFactory) IsAdoptable() bool { + return true +} + +// RequeueOnSuccessSeconds returns true if the resource should be requeued after specified seconds +// Default is false which means resource will not be requeued after success. +func (f *resourceManagerFactory) RequeueOnSuccessSeconds() int { + return 0 +} + +func newResourceManagerFactory() *resourceManagerFactory { + return &resourceManagerFactory{ + rmCache: map[string]*resourceManager{}, + } +} + +func init() { + svcresource.RegisterManagerFactory(newResourceManagerFactory()) +} diff --git a/pkg/resource/type_/references.go b/pkg/resource/type_/references.go new file mode 100644 index 0000000..556bce6 --- /dev/null +++ b/pkg/resource/type_/references.go @@ -0,0 +1,57 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + "context" + + "sigs.k8s.io/controller-runtime/pkg/client" + + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" + + svcapitypes "github.com/aws-controllers-k8s/keyspaces-controller/apis/v1alpha1" +) + +// ClearResolvedReferences removes any reference values that were made +// concrete in the spec. It returns a copy of the input AWSResource which +// contains the original *Ref values, but none of their respective concrete +// values. +func (rm *resourceManager) ClearResolvedReferences(res acktypes.AWSResource) acktypes.AWSResource { + ko := rm.concreteResource(res).ko.DeepCopy() + + return &resource{ko} +} + +// ResolveReferences finds if there are any Reference field(s) present +// inside AWSResource passed in the parameter and attempts to resolve those +// reference field(s) into their respective target field(s). It returns a +// copy of the input AWSResource with resolved reference(s), a boolean which +// is set to true if the resource contains any references (regardless of if +// they are resolved successfully) and an error if the passed AWSResource's +// reference field(s) could not be resolved. +func (rm *resourceManager) ResolveReferences( + ctx context.Context, + apiReader client.Reader, + res acktypes.AWSResource, +) (acktypes.AWSResource, bool, error) { + return res, false, nil +} + +// validateReferenceFields validates the reference field and corresponding +// identifier field. +func validateReferenceFields(ko *svcapitypes.Type) error { + return nil +} diff --git a/pkg/resource/type_/resource.go b/pkg/resource/type_/resource.go new file mode 100644 index 0000000..8a68514 --- /dev/null +++ b/pkg/resource/type_/resource.go @@ -0,0 +1,124 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + "fmt" + + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + ackerrors "github.com/aws-controllers-k8s/runtime/pkg/errors" + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" + "github.com/aws/aws-sdk-go-v2/aws" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + rtclient "sigs.k8s.io/controller-runtime/pkg/client" + + svcapitypes "github.com/aws-controllers-k8s/keyspaces-controller/apis/v1alpha1" +) + +// Hack to avoid import errors during build... +var ( + _ = &ackerrors.MissingNameIdentifier +) + +// resource implements the `aws-controller-k8s/runtime/pkg/types.AWSResource` +// interface +type resource struct { + // The Kubernetes-native CR representing the resource + ko *svcapitypes.Type +} + +// Identifiers returns an AWSResourceIdentifiers object containing various +// identifying information, including the AWS account ID that owns the +// resource, the resource's AWS Resource Name (ARN) +func (r *resource) Identifiers() acktypes.AWSResourceIdentifiers { + return &resourceIdentifiers{r.ko.Status.ACKResourceMetadata} +} + +// IsBeingDeleted returns true if the Kubernetes resource has a non-zero +// deletion timestamp +func (r *resource) IsBeingDeleted() bool { + return !r.ko.DeletionTimestamp.IsZero() +} + +// RuntimeObject returns the Kubernetes apimachinery/runtime representation of +// the AWSResource +func (r *resource) RuntimeObject() rtclient.Object { + return r.ko +} + +// MetaObject returns the Kubernetes apimachinery/apis/meta/v1.Object +// representation of the AWSResource +func (r *resource) MetaObject() metav1.Object { + return r.ko.GetObjectMeta() +} + +// Conditions returns the ACK Conditions collection for the AWSResource +func (r *resource) Conditions() []*ackv1alpha1.Condition { + return r.ko.Status.Conditions +} + +// ReplaceConditions sets the Conditions status field for the resource +func (r *resource) ReplaceConditions(conditions []*ackv1alpha1.Condition) { + r.ko.Status.Conditions = conditions +} + +// SetObjectMeta sets the ObjectMeta field for the resource +func (r *resource) SetObjectMeta(meta metav1.ObjectMeta) { + r.ko.ObjectMeta = meta +} + +// SetStatus will set the Status field for the resource +func (r *resource) SetStatus(desired acktypes.AWSResource) { + r.ko.Status = desired.(*resource).ko.Status +} + +// SetIdentifiers sets the Spec or Status field that is referenced as the unique +// resource identifier +func (r *resource) SetIdentifiers(identifier *ackv1alpha1.AWSIdentifiers) error { + if identifier.NameOrID == "" { + return ackerrors.MissingNameIdentifier + } + r.ko.Spec.TypeName = &identifier.NameOrID + + f0, f0ok := identifier.AdditionalKeys["keyspaceName"] + if f0ok { + r.ko.Spec.KeyspaceName = aws.String(f0) + } + + return nil +} + +// PopulateResourceFromAnnotation populates the fields passed from adoption annotation +func (r *resource) PopulateResourceFromAnnotation(fields map[string]string) error { + tmp, ok := fields["typeName"] + if !ok { + return ackerrors.NewTerminalError(fmt.Errorf("required field missing: typeName")) + } + r.ko.Spec.TypeName = &tmp + + f0, f0ok := fields["keyspaceName"] + if f0ok { + r.ko.Spec.KeyspaceName = aws.String(f0) + } + + return nil +} + +// DeepCopy will return a copy of the resource +func (r *resource) DeepCopy() acktypes.AWSResource { + koCopy := r.ko.DeepCopy() + return &resource{koCopy} +} diff --git a/pkg/resource/type_/sdk.go b/pkg/resource/type_/sdk.go new file mode 100644 index 0000000..c13c316 --- /dev/null +++ b/pkg/resource/type_/sdk.go @@ -0,0 +1,393 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +// Code generated by ack-generate. DO NOT EDIT. + +package type_ + +import ( + "context" + "errors" + "fmt" + "reflect" + "strings" + + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" + ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition" + ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors" + ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue" + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" + "github.com/aws/aws-sdk-go-v2/aws" + svcsdk "github.com/aws/aws-sdk-go-v2/service/keyspaces" + svcsdktypes "github.com/aws/aws-sdk-go-v2/service/keyspaces/types" + smithy "github.com/aws/smithy-go" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + svcapitypes "github.com/aws-controllers-k8s/keyspaces-controller/apis/v1alpha1" +) + +// Hack to avoid import errors during build... +var ( + _ = &metav1.Time{} + _ = strings.ToLower("") + _ = &svcsdk.Client{} + _ = &svcapitypes.Type{} + _ = ackv1alpha1.AWSAccountID("") + _ = &ackerr.NotFound + _ = &ackcondition.NotManagedMessage + _ = &reflect.Value{} + _ = fmt.Sprintf("") + _ = &ackrequeue.NoRequeue{} + _ = &aws.Config{} +) + +// sdkFind returns SDK-specific information about a supplied resource +func (rm *resourceManager) sdkFind( + ctx context.Context, + r *resource, +) (latest *resource, err error) { + rlog := ackrtlog.FromContext(ctx) + exit := rlog.Trace("rm.sdkFind") + defer func() { + exit(err) + }() + // If any required fields in the input shape are missing, AWS resource is + // not created yet. Return NotFound here to indicate to callers that the + // resource isn't yet created. + if rm.requiredFieldsMissingFromReadOneInput(r) { + return nil, ackerr.NotFound + } + + input, err := rm.newDescribeRequestPayload(r) + if err != nil { + return nil, err + } + + var resp *svcsdk.GetTypeOutput + resp, err = rm.sdkapi.GetType(ctx, input) + rm.metrics.RecordAPICall("READ_ONE", "GetType", err) + if err != nil { + var awsErr smithy.APIError + if errors.As(err, &awsErr) && awsErr.ErrorCode() == "ResourceNotFoundException" { + return nil, ackerr.NotFound + } + return nil, err + } + + // Merge in the information we read from the API call above to the copy of + // the original Kubernetes object we passed to the function + ko := r.ko.DeepCopy() + + if resp.FieldDefinitions != nil { + f2 := []*svcapitypes.FieldDefinition{} + for _, f2iter := range resp.FieldDefinitions { + f2elem := &svcapitypes.FieldDefinition{} + if f2iter.Name != nil { + f2elem.Name = f2iter.Name + } + if f2iter.Type != nil { + f2elem.Type = f2iter.Type + } + f2 = append(f2, f2elem) + } + ko.Spec.FieldDefinitions = f2 + } else { + ko.Spec.FieldDefinitions = nil + } + if resp.KeyspaceArn != nil { + ko.Status.KeyspaceARN = resp.KeyspaceArn + } else { + ko.Status.KeyspaceARN = nil + } + if resp.KeyspaceName != nil { + ko.Spec.KeyspaceName = resp.KeyspaceName + } else { + ko.Spec.KeyspaceName = nil + } + if resp.TypeName != nil { + ko.Spec.TypeName = resp.TypeName + } else { + ko.Spec.TypeName = nil + } + + rm.setStatusDefaults(ko) + return &resource{ko}, nil +} + +// requiredFieldsMissingFromReadOneInput returns true if there are any fields +// for the ReadOne Input shape that are required but not present in the +// resource's Spec or Status +func (rm *resourceManager) requiredFieldsMissingFromReadOneInput( + r *resource, +) bool { + return r.ko.Spec.KeyspaceName == nil || r.ko.Spec.TypeName == nil + +} + +// newDescribeRequestPayload returns SDK-specific struct for the HTTP request +// payload of the Describe API call for the resource +func (rm *resourceManager) newDescribeRequestPayload( + r *resource, +) (*svcsdk.GetTypeInput, error) { + res := &svcsdk.GetTypeInput{} + + if r.ko.Spec.KeyspaceName != nil { + res.KeyspaceName = r.ko.Spec.KeyspaceName + } + if r.ko.Spec.TypeName != nil { + res.TypeName = r.ko.Spec.TypeName + } + + return res, nil +} + +// sdkCreate creates the supplied resource in the backend AWS service API and +// returns a copy of the resource with resource fields (in both Spec and +// Status) filled in with values from the CREATE API operation's Output shape. +func (rm *resourceManager) sdkCreate( + ctx context.Context, + desired *resource, +) (created *resource, err error) { + rlog := ackrtlog.FromContext(ctx) + exit := rlog.Trace("rm.sdkCreate") + defer func() { + exit(err) + }() + input, err := rm.newCreateRequestPayload(ctx, desired) + if err != nil { + return nil, err + } + + var resp *svcsdk.CreateTypeOutput + _ = resp + resp, err = rm.sdkapi.CreateType(ctx, input) + rm.metrics.RecordAPICall("CREATE", "CreateType", err) + if err != nil { + return nil, err + } + // Merge in the information we read from the API call above to the copy of + // the original Kubernetes object we passed to the function + ko := desired.ko.DeepCopy() + + if resp.KeyspaceArn != nil { + ko.Status.KeyspaceARN = resp.KeyspaceArn + } else { + ko.Status.KeyspaceARN = nil + } + if resp.TypeName != nil { + ko.Spec.TypeName = resp.TypeName + } else { + ko.Spec.TypeName = nil + } + + rm.setStatusDefaults(ko) + return &resource{ko}, nil +} + +// newCreateRequestPayload returns an SDK-specific struct for the HTTP request +// payload of the Create API call for the resource +func (rm *resourceManager) newCreateRequestPayload( + ctx context.Context, + r *resource, +) (*svcsdk.CreateTypeInput, error) { + res := &svcsdk.CreateTypeInput{} + + if r.ko.Spec.FieldDefinitions != nil { + f0 := []svcsdktypes.FieldDefinition{} + for _, f0iter := range r.ko.Spec.FieldDefinitions { + f0elem := &svcsdktypes.FieldDefinition{} + if f0iter.Name != nil { + f0elem.Name = f0iter.Name + } + if f0iter.Type != nil { + f0elem.Type = f0iter.Type + } + f0 = append(f0, *f0elem) + } + res.FieldDefinitions = f0 + } + if r.ko.Spec.KeyspaceName != nil { + res.KeyspaceName = r.ko.Spec.KeyspaceName + } + if r.ko.Spec.TypeName != nil { + res.TypeName = r.ko.Spec.TypeName + } + + return res, nil +} + +// sdkUpdate patches the supplied resource in the backend AWS service API and +// returns a new resource with updated fields. +func (rm *resourceManager) sdkUpdate( + ctx context.Context, + desired *resource, + latest *resource, + delta *ackcompare.Delta, +) (*resource, error) { + return nil, ackerr.NewTerminalError(ackerr.NotImplemented) +} + +// sdkDelete deletes the supplied resource in the backend AWS service API +func (rm *resourceManager) sdkDelete( + ctx context.Context, + r *resource, +) (latest *resource, err error) { + rlog := ackrtlog.FromContext(ctx) + exit := rlog.Trace("rm.sdkDelete") + defer func() { + exit(err) + }() + input, err := rm.newDeleteRequestPayload(r) + if err != nil { + return nil, err + } + var resp *svcsdk.DeleteTypeOutput + _ = resp + resp, err = rm.sdkapi.DeleteType(ctx, input) + rm.metrics.RecordAPICall("DELETE", "DeleteType", err) + return nil, err +} + +// newDeleteRequestPayload returns an SDK-specific struct for the HTTP request +// payload of the Delete API call for the resource +func (rm *resourceManager) newDeleteRequestPayload( + r *resource, +) (*svcsdk.DeleteTypeInput, error) { + res := &svcsdk.DeleteTypeInput{} + + if r.ko.Spec.KeyspaceName != nil { + res.KeyspaceName = r.ko.Spec.KeyspaceName + } + if r.ko.Spec.TypeName != nil { + res.TypeName = r.ko.Spec.TypeName + } + + return res, nil +} + +// setStatusDefaults sets default properties into supplied custom resource +func (rm *resourceManager) setStatusDefaults( + ko *svcapitypes.Type, +) { + if ko.Status.ACKResourceMetadata == nil { + ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{} + } + if ko.Status.ACKResourceMetadata.Region == nil { + ko.Status.ACKResourceMetadata.Region = &rm.awsRegion + } + if ko.Status.ACKResourceMetadata.OwnerAccountID == nil { + ko.Status.ACKResourceMetadata.OwnerAccountID = &rm.awsAccountID + } + if ko.Status.Conditions == nil { + ko.Status.Conditions = []*ackv1alpha1.Condition{} + } +} + +// updateConditions returns updated resource, true; if conditions were updated +// else it returns nil, false +func (rm *resourceManager) updateConditions( + r *resource, + onSuccess bool, + err error, +) (*resource, bool) { + ko := r.ko.DeepCopy() + rm.setStatusDefaults(ko) + + // Terminal condition + var terminalCondition *ackv1alpha1.Condition = nil + var recoverableCondition *ackv1alpha1.Condition = nil + var syncCondition *ackv1alpha1.Condition = nil + for _, condition := range ko.Status.Conditions { + if condition.Type == ackv1alpha1.ConditionTypeTerminal { + terminalCondition = condition + } + if condition.Type == ackv1alpha1.ConditionTypeRecoverable { + recoverableCondition = condition + } + if condition.Type == ackv1alpha1.ConditionTypeResourceSynced { + syncCondition = condition + } + } + var termError *ackerr.TerminalError + if rm.terminalAWSError(err) || err == ackerr.SecretTypeNotSupported || err == ackerr.SecretNotFound || errors.As(err, &termError) { + if terminalCondition == nil { + terminalCondition = &ackv1alpha1.Condition{ + Type: ackv1alpha1.ConditionTypeTerminal, + } + ko.Status.Conditions = append(ko.Status.Conditions, terminalCondition) + } + var errorMessage = "" + if err == ackerr.SecretTypeNotSupported || err == ackerr.SecretNotFound || errors.As(err, &termError) { + errorMessage = err.Error() + } else { + awsErr, _ := ackerr.AWSError(err) + errorMessage = awsErr.Error() + } + terminalCondition.Status = corev1.ConditionTrue + terminalCondition.Message = &errorMessage + } else { + // Clear the terminal condition if no longer present + if terminalCondition != nil { + terminalCondition.Status = corev1.ConditionFalse + terminalCondition.Message = nil + } + // Handling Recoverable Conditions + if err != nil { + if recoverableCondition == nil { + // Add a new Condition containing a non-terminal error + recoverableCondition = &ackv1alpha1.Condition{ + Type: ackv1alpha1.ConditionTypeRecoverable, + } + ko.Status.Conditions = append(ko.Status.Conditions, recoverableCondition) + } + recoverableCondition.Status = corev1.ConditionTrue + awsErr, _ := ackerr.AWSError(err) + errorMessage := err.Error() + if awsErr != nil { + errorMessage = awsErr.Error() + } + recoverableCondition.Message = &errorMessage + } else if recoverableCondition != nil { + recoverableCondition.Status = corev1.ConditionFalse + recoverableCondition.Message = nil + } + } + // Required to avoid the "declared but not used" error in the default case + _ = syncCondition + if terminalCondition != nil || recoverableCondition != nil || syncCondition != nil { + return &resource{ko}, true // updated + } + return nil, false // not updated +} + +// terminalAWSError returns awserr, true; if the supplied error is an aws Error type +// and if the exception indicates that it is a Terminal exception +// 'Terminal' exception are specified in generator configuration +func (rm *resourceManager) terminalAWSError(err error) bool { + if err == nil { + return false + } + + var terminalErr smithy.APIError + if !errors.As(err, &terminalErr) { + return false + } + switch terminalErr.ErrorCode() { + case "ValidationException": + return true + default: + return false + } +} diff --git a/test/e2e/condition.py b/test/e2e/condition.py index cc306fa..265a214 100644 --- a/test/e2e/condition.py +++ b/test/e2e/condition.py @@ -40,7 +40,7 @@ def assert_type_status( ref = resource.CustomResourceReference( CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, - db_cluster_id, namespace="default", + keyspace_name, namespace="default", ) resource.create_custom_resource(ref, resource_data) resource.wait_resource_consumed_by_controller(ref) @@ -102,7 +102,7 @@ def assert_synced(ref: resource.CustomResourceReference): ref = resource.CustomResourceReference( CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, - db_cluster_id, namespace="default", + keyspace_name, namespace="default", ) resource.create_custom_resource(ref, resource_data) resource.wait_resource_consumed_by_controller(ref) @@ -125,7 +125,7 @@ def assert_not_synced(ref: resource.CustomResourceReference): ref = resource.CustomResourceReference( CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, - db_cluster_id, namespace="default", + keyspace_name, namespace="default", ) resource.create_custom_resource(ref, resource_data) resource.wait_resource_consumed_by_controller(ref) diff --git a/test/e2e/requirements.txt b/test/e2e/requirements.txt index aff9aa4..93df854 100644 --- a/test/e2e/requirements.txt +++ b/test/e2e/requirements.txt @@ -1 +1 @@ -acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@c3ce6b7404c5bf71593a1f23d8b0f13039f4b910 +acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@a11aa82e147253507b2a6f75d45ca26c4c0002f2 diff --git a/test/e2e/resources/table_with_udt.yaml b/test/e2e/resources/table_with_udt.yaml new file mode 100644 index 0000000..41cb016 --- /dev/null +++ b/test/e2e/resources/table_with_udt.yaml @@ -0,0 +1,28 @@ +kind: Table +apiVersion: keyspaces.services.k8s.aws/v1alpha1 +metadata: + name: $TABLE_NAME +spec: + keyspaceName: $KEYSPACE_NAME + tableName: $TABLE_NAME + pointInTimeRecovery: + status: "ENABLED" + capacitySpecification: + throughputMode: "PAY_PER_REQUEST" + schemaDefinition: + partitionKeys: + - name: "id" + clusteringKeys: + - name: "name" + orderBy: "ASC" + staticColumns: + - name: "description" + allColumns: + - name: "id" + type_: "uuid" + - name: "name" + type_: "text" + - name: "description" + type_: "text" + - name: "test_udt" + type_: $TYPE_NAME \ No newline at end of file diff --git a/test/e2e/resources/type_basic.yaml b/test/e2e/resources/type_basic.yaml new file mode 100644 index 0000000..32e55e3 --- /dev/null +++ b/test/e2e/resources/type_basic.yaml @@ -0,0 +1,12 @@ +kind: Type +apiVersion: keyspaces.services.k8s.aws/v1alpha1 +metadata: + name: $TYPE_NAME +spec: + keyspaceName: $KEYSPACE_NAME + typeName: $TYPE_NAME + fieldDefinitions: + - name: "test_field1" + type_: "text" + - name: "test_field2" + type_: "int" diff --git a/test/e2e/tests/test_type.py b/test/e2e/tests/test_type.py new file mode 100644 index 0000000..84ca0ee --- /dev/null +++ b/test/e2e/tests/test_type.py @@ -0,0 +1,166 @@ +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You may +# not use this file except in compliance with the License. A copy of the +# License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "license" file accompanying this file. This file is distributed +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +# express or implied. See the License for the specific language governing +# permissions and limitations under the License. + +"""Integration tests for the Keyspaces Table API. +""" + +import time +import logging +import pytest +from acktest.resources import random_suffix_name +from acktest.k8s import resource as k8s +from e2e import ( + service_marker, CRD_GROUP, CRD_VERSION, + load_keyspaces_resource, type_, condition +) +from e2e.replacement_values import REPLACEMENT_VALUES + +RESOURCE_PLURAL = "types" + +CREATE_WAIT_AFTER_SECONDS = 45 +DELETE_WAIT_AFTER_SECONDS = 15 + +def create_keyspace_for_type(name: str): + replacements = REPLACEMENT_VALUES.copy() + replacements["KEYSPACE_NAME"] = name + + # load resource + resource_data = load_keyspaces_resource( + "keyspace_basic", + additional_replacements=replacements, + ) + logging.debug(resource_data) + + keyspace_reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, "keyspaces", + name, namespace="default", + ) + + # Create keyspace + k8s.create_custom_resource(keyspace_reference, resource_data) + time.sleep(CREATE_WAIT_AFTER_SECONDS) + keyspace_resource = k8s.wait_resource_consumed_by_controller(keyspace_reference) + + assert keyspace_reference is not None + assert k8s.get_resource_exists(keyspace_reference) + return keyspace_reference, keyspace_resource + +def create_type(name: str, keyspace_name: str, resource_template): + replacements = REPLACEMENT_VALUES.copy() + replacements["TYPE_NAME"] = name + replacements["KEYSPACE_NAME"] = keyspace_name + + # load resource + resource_data = load_keyspaces_resource( + resource_template, + additional_replacements=replacements, + ) + + type_reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, + name, namespace="default", + ) + + # Create type + k8s.create_custom_resource(type_reference, resource_data) + time.sleep(CREATE_WAIT_AFTER_SECONDS) + type_resource = k8s.wait_resource_consumed_by_controller(type_reference) + + assert type_resource is not None + assert k8s.get_resource_exists(type_reference) + return type_reference, type_resource + +def create_table_with_udt(name: str, keyspace_name: str, type_name: str, resource_template): + replacements = REPLACEMENT_VALUES.copy() + replacements["TABLE_NAME"] = name + replacements["KEYSPACE_NAME"] = keyspace_name + replacements["TYPE_NAME"] = type_name + + # load resource + resource_data = load_keyspaces_resource( + resource_template, + additional_replacements=replacements, + ) + + table_reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, "tables", + name, namespace="default", + ) + + # Create type + k8s.create_custom_resource(table_reference, resource_data) + time.sleep(CREATE_WAIT_AFTER_SECONDS) + table_resource = k8s.wait_resource_consumed_by_controller(table_reference) + + assert table_resource is not None + assert k8s.get_resource_exists(table_reference) + return table_reference, table_resource + +@pytest.fixture(scope="module") +def keyspace_basic(): + keyspace_name = random_suffix_name("udt", 32, "test") + (ref, cr) = create_keyspace_for_type(keyspace_name) + yield ref, cr + deleted_keyspace = k8s.delete_custom_resource(ref, wait_periods=3, period_length=10) + assert deleted_keyspace + +@pytest.fixture(scope="module") +def type_basic(keyspace_basic): + keyspace_ref, _ = keyspace_basic + type_name = random_suffix_name("udt", 32, "test") + (ref, cr) = create_type(type_name,keyspace_ref.name, "type_basic") + yield ref, cr + deleted_type = k8s.delete_custom_resource(ref, wait_periods=3, period_length=10) + assert deleted_type + +@pytest.fixture(scope="module") +def table_with_udt(keyspace_basic,type_basic): + table_name = random_suffix_name("udttable", 32, "test") + keyspace_ref, _ = keyspace_basic + type_ref, _ = type_basic + (ref, cr) = create_table_with_udt(table_name, keyspace_ref.name, type_ref.name, "table_with_udt") + yield ref, cr + deleted_table = k8s.delete_custom_resource(ref, wait_periods=3, period_length=10) + assert deleted_table + + +@service_marker +@pytest.mark.canary +class TestType: + def type_exists(self, type_name: str, keyspace_name: str) -> bool: + return type_.get(type_name, keyspace_name) is not None + + def test_create_delete_type(self, keyspace_basic, type_basic): + (keyspace_basic_ref, _) = keyspace_basic + (ref, res) = type_basic + type_name = res["spec"]["typeName"] + keyspace_name = res["spec"]["keyspaceName"] + condition.assert_synced(ref) + condition.assert_synced(keyspace_basic_ref) + + # Check Type exists + assert self.type_exists(type_name, keyspace_name) + + def test_create_delete_table_with_udt(self, keyspace_basic, type_basic, table_with_udt): + (keyspace_basic_ref, _) = keyspace_basic + (ref, res) = type_basic + (ref_table, res_table) = table_with_udt + type_name = res["spec"]["typeName"] + keyspace_name = res["spec"]["keyspaceName"] + table_name = res_table["spec"]["tableName"] + condition.assert_synced(keyspace_basic_ref) + condition.assert_synced(ref) + condition.assert_synced(ref_table) + + # Check Type exists + assert self.type_exists(type_name, keyspace_name) diff --git a/test/e2e/type_.py b/test/e2e/type_.py new file mode 100644 index 0000000..47a333c --- /dev/null +++ b/test/e2e/type_.py @@ -0,0 +1,138 @@ +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You may +# not use this file except in compliance with the License. A copy of the +# License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "license" file accompanying this file. This file is distributed +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +# express or implied. See the License for the specific language governing +# permissions and limitations under the License. + +"""Utilities for working with Type resources""" + +import datetime +import time +import typing +import logging + +import boto3 +import pytest + +from acktest.aws.identity import get_region + +DEFAULT_WAIT_UNTIL_TIMEOUT_SECONDS = 60*2 +DEFAULT_WAIT_UNTIL_INTERVAL_SECONDS = 15 +DEFAULT_WAIT_UNTIL_DELETED_TIMEOUT_SECONDS = 60*2 +DEFAULT_WAIT_UNTIL_DELETED_INTERVAL_SECONDS = 15 + +TypeMatchFunc = typing.NewType( + 'TypeMatchFunc', + typing.Callable[[dict], bool], +) + +class StatusMatcher: + def __init__(self, status): + self.match_on = status + + def __call__(self, record: dict) -> bool: + return ('TypeStatus' in record + and record['TypeStatus'] == self.match_on) + + +def status_matches(status: str) -> TypeMatchFunc: + return StatusMatcher(status) + +def wait_until( + type_name: str, + keyspace_name: str, + match_fn: TypeMatchFunc, + timeout_seconds: int = DEFAULT_WAIT_UNTIL_TIMEOUT_SECONDS, + interval_seconds: int = DEFAULT_WAIT_UNTIL_INTERVAL_SECONDS, + ) -> None: + """Waits until a Type with a supplied name is returned from the Keyspaces + API and the matching functor returns True. + + Usage: + from e2e.type_ import wait_until, status_matches + + wait_until( + type_name, + keyspace_name, + status_matches("ACTIVE"), + ) + + Raises: + pytest.fail upon timeout + """ + now = datetime.datetime.now() + timeout = now + datetime.timedelta(seconds=timeout_seconds) + + while not match_fn(get(type_name, keyspace_name)): + if datetime.datetime.now() >= timeout: + pytest.fail("failed to match type before timeout") + time.sleep(interval_seconds) + +def wait_until_deleted( + type_name: str, + keyspace_name: str, + timeout_seconds: int = DEFAULT_WAIT_UNTIL_DELETED_TIMEOUT_SECONDS, + interval_seconds: int = DEFAULT_WAIT_UNTIL_DELETED_INTERVAL_SECONDS, + ) -> None: + """Waits until Type with a supplied type_name is no longer returned from + the Keyspaces API. + + Usage: + from e2e.type_ import wait_until_deleted + + wait_until_deleted( + type_name, + keyspace_name + ) + + Raises: + pytest.fail upon timeout or if the Type goes to any other status + other than 'deleting' + """ + now = datetime.datetime.now() + timeout = now + datetime.timedelta(seconds=timeout_seconds) + + while True: + if datetime.datetime.now() >= timeout: + pytest.fail( + "Timed out waiting for Type to be " + "deleted in Keyspaces API" + ) + time.sleep(interval_seconds) + + latest = get(type_name,keyspace_name) + if latest is None: + break + + if latest['status'] != "DELETING": + pytest.fail( + "Status is not 'deleting' for Type that was " + "deleted. Status is " + latest['status'] + ) + +def get(type_name,keyspace_name): + """Returns a dict containing the Type record from the keyspaces API. + + If no such Type exists, returns None. + """ + c = boto3.client('keyspaces', region_name=get_region()) + try: + resp = c.get_type(keyspaceName=keyspace_name, typeName=type_name) + return resp + except c.exceptions.ResourceNotFoundException: + logging.info("Type %s not found", type_name) + return None + except c.exceptions.ValidationException: + logging.info( + "Couldn't verify %s exists. Here's why: %s", + type_name, + c.exceptions + ) + return None \ No newline at end of file