From 9f22fb805870b2b9a7d888767c326b0c524bd7c5 Mon Sep 17 00:00:00 2001 From: Nick Blaskey Date: Fri, 28 Aug 2026 23:32:58 +0000 Subject: [PATCH] feat: Add GlobalReplicationGroup resource support --- apis/v1alpha1/ack-generate-metadata.yaml | 12 +- apis/v1alpha1/generator.yaml | 95 ++- apis/v1alpha1/global_replication_group.go | 147 ++++ apis/v1alpha1/types.go | 44 +- apis/v1alpha1/zz_generated.deepcopy.go | 313 ++++++- cmd/controller/main.go | 1 + ...vices.k8s.aws_globalreplicationgroups.yaml | 283 +++++++ config/crd/kustomization.yaml | 1 + config/rbac/cluster-role-controller.yaml | 2 + config/rbac/role-reader.yaml | 1 + config/rbac/role-writer.yaml | 2 + generator.yaml | 95 ++- ...vices.k8s.aws_globalreplicationgroups.yaml | 286 +++++++ helm/templates/_helpers.tpl | 2 + helm/templates/role-reader.yaml | 1 + helm/templates/role-writer.yaml | 2 + helm/values.yaml | 1 + .../global_replication_group/delta.go | 113 +++ .../global_replication_group/descriptor.go | 155 ++++ .../global_replication_group/hooks.go | 676 ++++++++++++++++ .../global_replication_group/hooks_test.go | 61 ++ .../global_replication_group/identifiers.go | 64 ++ .../global_replication_group/manager.go | 401 +++++++++ .../manager_factory.go | 100 +++ .../global_replication_group/references.go | 175 ++++ .../global_replication_group/resource.go | 113 +++ pkg/resource/global_replication_group/sdk.go | 553 +++++++++++++ pkg/version/version.go | 4 +- .../sdk_create_post_set_output.go.tpl | 10 + .../global_replication_group_basic.yaml | 9 + .../global_replication_group_scale.yaml | 10 + .../replicationgroup_create_delete_grg.yaml | 22 + .../tests/test_global_replication_group.py | 766 ++++++++++++++++++ 33 files changed, 4447 insertions(+), 73 deletions(-) create mode 100644 apis/v1alpha1/global_replication_group.go create mode 100644 config/crd/bases/elasticache.services.k8s.aws_globalreplicationgroups.yaml create mode 100644 helm/crds/elasticache.services.k8s.aws_globalreplicationgroups.yaml create mode 100644 pkg/resource/global_replication_group/delta.go create mode 100644 pkg/resource/global_replication_group/descriptor.go create mode 100644 pkg/resource/global_replication_group/hooks.go create mode 100644 pkg/resource/global_replication_group/hooks_test.go create mode 100644 pkg/resource/global_replication_group/identifiers.go create mode 100644 pkg/resource/global_replication_group/manager.go create mode 100644 pkg/resource/global_replication_group/manager_factory.go create mode 100644 pkg/resource/global_replication_group/references.go create mode 100644 pkg/resource/global_replication_group/resource.go create mode 100644 pkg/resource/global_replication_group/sdk.go create mode 100644 templates/hooks/global_replication_group/sdk_create_post_set_output.go.tpl create mode 100644 test/e2e/resources/global_replication_group_basic.yaml create mode 100644 test/e2e/resources/global_replication_group_scale.yaml create mode 100644 test/e2e/resources/replicationgroup_create_delete_grg.yaml create mode 100644 test/e2e/tests/test_global_replication_group.py diff --git a/apis/v1alpha1/ack-generate-metadata.yaml b/apis/v1alpha1/ack-generate-metadata.yaml index 6d6141be..dd2b685d 100755 --- a/apis/v1alpha1/ack-generate-metadata.yaml +++ b/apis/v1alpha1/ack-generate-metadata.yaml @@ -1,13 +1,13 @@ ack_generate_info: - build_date: "2026-08-13T00:57:17Z" - build_hash: 65d45b2e6c9efd6aca20e0d36826d1e18e4ba2b7 - go_version: go1.26.5 - version: v0.62.1 -api_directory_checksum: d561b534bd88b72e078a237febc46f9b273f67e2 + build_date: "2026-08-27T20:01:00Z" + build_hash: d4aaca5e1dd6acfd81094936dc825ad30d2bbb39 + go_version: go1.26.4 + version: v0.62.1-4-gd4aaca5 +api_directory_checksum: 61363e520cb357e44d75180c8c22321864ee55ea api_version: v1alpha1 aws_sdk_go_version: v1.41.10 generator_config_info: - file_checksum: 3c29107cce971d3b60b6722c04f4771d5b346173 + file_checksum: 9ac55777c8dd17de0c41adcbff1d0aee5f0e34b7 original_file_name: generator.yaml last_modification: reason: API generation diff --git a/apis/v1alpha1/generator.yaml b/apis/v1alpha1/generator.yaml index 1b330476..45c4a8ca 100644 --- a/apis/v1alpha1/generator.yaml +++ b/apis/v1alpha1/generator.yaml @@ -476,6 +476,96 @@ resources: in: - available - create_failed + GlobalReplicationGroup: + fields: + # Create API only accepts a suffix; ElastiCache prepends a region-specific + # prefix (e.g. "sgaui-"). Full ID stored in status.globalReplicationGroupID. + GlobalReplicationGroupIdSuffix: + is_required: true + is_immutable: true + PrimaryReplicationGroupId: + is_required: true + is_immutable: true + references: + resource: ReplicationGroup + path: Spec.ReplicationGroupID + AutomaticFailoverEnabled: + from: + operation: ModifyGlobalReplicationGroup + path: AutomaticFailoverEnabled + CacheNodeType: + from: + operation: ModifyGlobalReplicationGroup + path: CacheNodeType + CacheParameterGroupName: + from: + operation: ModifyGlobalReplicationGroup + path: CacheParameterGroupName + EngineVersion: + from: + operation: ModifyGlobalReplicationGroup + path: EngineVersion + Engine: + from: + operation: ModifyGlobalReplicationGroup + path: Engine + # Custom field for declarative node group scaling. Not part of any single + # API input — our custom update logic compares this against the current + # GlobalNodeGroups count to call Increase or Decrease APIs. + NodeGroupCount: + type: "*int64" + print: + add_age_column: true + add_synced_column: true + order_by: index + additional_columns: + - name: STATUS + json_path: .status.status + type: string + index: 10 + - name: NODE-TYPE + json_path: .spec.cacheNodeType + type: string + index: 20 + exceptions: + errors: + 404: + code: GlobalReplicationGroupNotFoundFault + terminal_codes: + - GlobalReplicationGroupAlreadyExistsFault + - InvalidParameterValue + - InvalidParameterCombination + - ServiceLinkedRoleNotFoundFault + - ReplicationGroupNotFoundFault + # "primary-only" = no secondaries attached (steady state) + # "available" = secondaries ARE attached (steady state) + # Both are valid "ready" states for the controller. + synced: + when: + - path: Status.Status + in: + - available + - primary-only + hooks: + sdk_create_post_set_output: + template_path: hooks/global_replication_group/sdk_create_post_set_output.go.tpl + delete_operation: + custom_method_name: customDeleteGlobalReplicationGroup + update_operation: + custom_method_name: customUpdateGlobalReplicationGroup + renames: + operations: + CreateGlobalReplicationGroup: + input_fields: + GlobalReplicationGroupDescription: Description + DescribeGlobalReplicationGroups: + output_fields: + GlobalReplicationGroupDescription: Description + ModifyGlobalReplicationGroup: + input_fields: + GlobalReplicationGroupDescription: Description + tags: + ignore: true operations: DescribeServerlessCaches: operation_type: @@ -517,9 +607,12 @@ operations: set_output_custom_method_name: customModifyCacheClusterSetOutput override_values: ApplyImmediately: aws.Bool(true) + DescribeGlobalReplicationGroups: + override_values: + ShowMemberInfo: aws.Bool(true) + set_output_custom_method_name: CustomDescribeGlobalReplicationGroupsSetOutput ignore: resource_names: - - GlobalReplicationGroup - CacheSecurityGroup field_paths: - DescribeSnapshotsInput.CacheClusterId diff --git a/apis/v1alpha1/global_replication_group.go b/apis/v1alpha1/global_replication_group.go new file mode 100644 index 00000000..83377696 --- /dev/null +++ b/apis/v1alpha1/global_replication_group.go @@ -0,0 +1,147 @@ +// 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" +) + +// GlobalReplicationGroupSpec defines the desired state of GlobalReplicationGroup. +// +// Consists of a primary cluster that accepts writes and an associated secondary +// cluster that resides in a different Amazon region. The secondary cluster +// accepts only reads. The primary cluster automatically replicates updates +// to the secondary cluster. +// +// - The GlobalReplicationGroupIdSuffix represents the name of the Global +// datastore, which is what you use to associate a secondary cluster. +type GlobalReplicationGroupSpec struct { + + // Determines whether a read replica is automatically promoted to read/write + // primary if the existing primary encounters a failure. + AutomaticFailoverEnabled *bool `json:"automaticFailoverEnabled,omitempty"` + // A valid cache node type that you want to scale this Global datastore to. + CacheNodeType *string `json:"cacheNodeType,omitempty"` + // The name of the cache parameter group to use with the Global datastore. It + // must be compatible with the major engine version used by the Global datastore. + CacheParameterGroupName *string `json:"cacheParameterGroupName,omitempty"` + // Provides details of the Global datastore + Description *string `json:"description,omitempty"` + // Modifies the engine listed in a global replication group message. The options + // are valkey, memcached or redis. + Engine *string `json:"engine,omitempty"` + // The upgraded version of the cache engine to be run on the clusters in the + // Global datastore. + EngineVersion *string `json:"engineVersion,omitempty"` + // The suffix name of a Global datastore. Amazon ElastiCache automatically applies + // a prefix to the Global datastore ID when it is created. Each Amazon Region + // has its own prefix. For instance, a Global datastore ID created in the US-West-1 + // region will begin with "dsdfu" along with the suffix name you provide. The + // suffix, combined with the auto-generated prefix, guarantees uniqueness of + // the Global datastore name across multiple regions. + // + // For a full list of Amazon Regions and their respective Global datastore iD + // prefixes, see Using the Amazon CLI with Global datastores (http://docs.aws.amazon.com/AmazonElastiCache/latest/dg/Redis-Global-Datastores-CLI.html). + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + // +kubebuilder:validation:Required + GlobalReplicationGroupIDSuffix *string `json:"globalReplicationGroupIDSuffix"` + NodeGroupCount *int64 `json:"nodeGroupCount,omitempty"` + // The name of the primary cluster that accepts writes and will replicate updates + // to the secondary cluster. This value is stored as a lowercase string. + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + PrimaryReplicationGroupID *string `json:"primaryReplicationGroupID,omitempty"` + PrimaryReplicationGroupRef *ackv1alpha1.AWSResourceReferenceWrapper `json:"primaryReplicationGroupRef,omitempty"` +} + +// GlobalReplicationGroupStatus defines the observed state of GlobalReplicationGroup +type GlobalReplicationGroupStatus 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"` + // A flag that enables encryption at rest when set to true. + // + // You cannot modify the value of AtRestEncryptionEnabled after the replication + // group is created. To enable encryption at rest on a replication group you + // must set AtRestEncryptionEnabled to true when you create the replication + // group. + // + // Required: Only available when creating a replication group in an Amazon VPC + // using Redis OSS version 3.2.6, 4.x or later. + // +kubebuilder:validation:Optional + AtRestEncryptionEnabled *bool `json:"atRestEncryptionEnabled,omitempty"` + // A flag that enables using an AuthToken (password) when issuing Valkey or + // Redis OSS commands. + // + // Default: false + // +kubebuilder:validation:Optional + AuthTokenEnabled *bool `json:"authTokenEnabled,omitempty"` + // A flag that indicates whether the Global datastore is cluster enabled. + // +kubebuilder:validation:Optional + ClusterEnabled *bool `json:"clusterEnabled,omitempty"` + // Indicates the slot configuration and global identifier for each slice group. + // +kubebuilder:validation:Optional + GlobalNodeGroups []*GlobalNodeGroup `json:"globalNodeGroups,omitempty"` + // The name of the Global datastore + // +kubebuilder:validation:Optional + GlobalReplicationGroupID *string `json:"globalReplicationGroupID,omitempty"` + // The replication groups that comprise the Global datastore. + // +kubebuilder:validation:Optional + Members []*GlobalReplicationGroupMember `json:"members,omitempty"` + // The status of the Global datastore + // +kubebuilder:validation:Optional + Status *string `json:"status,omitempty"` + // A flag that enables in-transit encryption when set to true. + // + // Required: Only available when creating a replication group in an Amazon VPC + // using Redis OSS version 3.2.6, 4.x or later. + // +kubebuilder:validation:Optional + TransitEncryptionEnabled *bool `json:"transitEncryptionEnabled,omitempty"` +} + +// GlobalReplicationGroup is the Schema for the GlobalReplicationGroups API +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="STATUS",type=string,priority=0,JSONPath=`.status.status` +// +kubebuilder:printcolumn:name="NODE-TYPE",type=string,priority=0,JSONPath=`.spec.cacheNodeType` +// +kubebuilder:printcolumn:name="Synced",type="string",priority=0,JSONPath=".status.conditions[?(@.type==\"ACK.ResourceSynced\")].status" +// +kubebuilder:printcolumn:name="Age",type="date",priority=0,JSONPath=".metadata.creationTimestamp" +type GlobalReplicationGroup struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + Spec GlobalReplicationGroupSpec `json:"spec,omitempty"` + Status GlobalReplicationGroupStatus `json:"status,omitempty"` +} + +// GlobalReplicationGroupList contains a list of GlobalReplicationGroup +// +kubebuilder:object:root=true +type GlobalReplicationGroupList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []GlobalReplicationGroup `json:"items"` +} + +func init() { + SchemeBuilder.Register(&GlobalReplicationGroup{}, &GlobalReplicationGroupList{}) +} diff --git a/apis/v1alpha1/types.go b/apis/v1alpha1/types.go index 7654fc88..1b7bff78 100644 --- a/apis/v1alpha1/types.go +++ b/apis/v1alpha1/types.go @@ -344,27 +344,6 @@ type GlobalNodeGroup struct { Slots *string `json:"slots,omitempty"` } -// Consists of a primary cluster that accepts writes and an associated secondary -// cluster that resides in a different Amazon region. The secondary cluster -// accepts only reads. The primary cluster automatically replicates updates -// to the secondary cluster. -// -// - The GlobalReplicationGroupIdSuffix represents the name of the Global -// datastore, which is what you use to associate a secondary cluster. -type GlobalReplicationGroup struct { - ARN *string `json:"arn,omitempty"` - AtRestEncryptionEnabled *bool `json:"atRestEncryptionEnabled,omitempty"` - AuthTokenEnabled *bool `json:"authTokenEnabled,omitempty"` - CacheNodeType *string `json:"cacheNodeType,omitempty"` - ClusterEnabled *bool `json:"clusterEnabled,omitempty"` - Engine *string `json:"engine,omitempty"` - EngineVersion *string `json:"engineVersion,omitempty"` - GlobalReplicationGroupDescription *string `json:"globalReplicationGroupDescription,omitempty"` - GlobalReplicationGroupID *string `json:"globalReplicationGroupID,omitempty"` - Status *string `json:"status,omitempty"` - TransitEncryptionEnabled *bool `json:"transitEncryptionEnabled,omitempty"` -} - // The name of the Global datastore and role of this replication group in the // Global datastore. type GlobalReplicationGroupInfo struct { @@ -382,6 +361,29 @@ type GlobalReplicationGroupMember struct { Status *string `json:"status,omitempty"` } +// Consists of a primary cluster that accepts writes and an associated secondary +// cluster that resides in a different Amazon region. The secondary cluster +// accepts only reads. The primary cluster automatically replicates updates +// to the secondary cluster. +// +// - The GlobalReplicationGroupIdSuffix represents the name of the Global +// datastore, which is what you use to associate a secondary cluster. +type GlobalReplicationGroup_SDK struct { + ARN *string `json:"arn,omitempty"` + AtRestEncryptionEnabled *bool `json:"atRestEncryptionEnabled,omitempty"` + AuthTokenEnabled *bool `json:"authTokenEnabled,omitempty"` + CacheNodeType *string `json:"cacheNodeType,omitempty"` + ClusterEnabled *bool `json:"clusterEnabled,omitempty"` + Engine *string `json:"engine,omitempty"` + EngineVersion *string `json:"engineVersion,omitempty"` + GlobalNodeGroups []*GlobalNodeGroup `json:"globalNodeGroups,omitempty"` + GlobalReplicationGroupDescription *string `json:"globalReplicationGroupDescription,omitempty"` + GlobalReplicationGroupID *string `json:"globalReplicationGroupID,omitempty"` + Members []*GlobalReplicationGroupMember `json:"members,omitempty"` + Status *string `json:"status,omitempty"` + TransitEncryptionEnabled *bool `json:"transitEncryptionEnabled,omitempty"` +} + // The configuration details of the Kinesis Data Firehose destination. type KinesisFirehoseDestinationDetails struct { DeliveryStream *string `json:"deliveryStream,omitempty"` diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index da2ddb76..6e175af7 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -1836,18 +1836,132 @@ func (in *GlobalNodeGroup) DeepCopy() *GlobalNodeGroup { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GlobalReplicationGroup) DeepCopyInto(out *GlobalReplicationGroup) { *out = *in - if in.ARN != nil { - in, out := &in.ARN, &out.ARN + 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 GlobalReplicationGroup. +func (in *GlobalReplicationGroup) DeepCopy() *GlobalReplicationGroup { + if in == nil { + return nil + } + out := new(GlobalReplicationGroup) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *GlobalReplicationGroup) 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 *GlobalReplicationGroupInfo) DeepCopyInto(out *GlobalReplicationGroupInfo) { + *out = *in + if in.GlobalReplicationGroupID != nil { + in, out := &in.GlobalReplicationGroupID, &out.GlobalReplicationGroupID *out = new(string) **out = **in } - if in.AtRestEncryptionEnabled != nil { - in, out := &in.AtRestEncryptionEnabled, &out.AtRestEncryptionEnabled - *out = new(bool) + if in.GlobalReplicationGroupMemberRole != nil { + in, out := &in.GlobalReplicationGroupMemberRole, &out.GlobalReplicationGroupMemberRole + *out = new(string) **out = **in } - if in.AuthTokenEnabled != nil { - in, out := &in.AuthTokenEnabled, &out.AuthTokenEnabled +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupInfo. +func (in *GlobalReplicationGroupInfo) DeepCopy() *GlobalReplicationGroupInfo { + if in == nil { + return nil + } + out := new(GlobalReplicationGroupInfo) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GlobalReplicationGroupList) DeepCopyInto(out *GlobalReplicationGroupList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]GlobalReplicationGroup, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupList. +func (in *GlobalReplicationGroupList) DeepCopy() *GlobalReplicationGroupList { + if in == nil { + return nil + } + out := new(GlobalReplicationGroupList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *GlobalReplicationGroupList) 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 *GlobalReplicationGroupMember) DeepCopyInto(out *GlobalReplicationGroupMember) { + *out = *in + if in.AutomaticFailover != nil { + in, out := &in.AutomaticFailover, &out.AutomaticFailover + *out = new(string) + **out = **in + } + if in.ReplicationGroupID != nil { + in, out := &in.ReplicationGroupID, &out.ReplicationGroupID + *out = new(string) + **out = **in + } + if in.ReplicationGroupRegion != nil { + in, out := &in.ReplicationGroupRegion, &out.ReplicationGroupRegion + *out = new(string) + **out = **in + } + if in.Role != nil { + in, out := &in.Role, &out.Role + *out = new(string) + **out = **in + } + if in.Status != nil { + in, out := &in.Status, &out.Status + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupMember. +func (in *GlobalReplicationGroupMember) DeepCopy() *GlobalReplicationGroupMember { + if in == nil { + return nil + } + out := new(GlobalReplicationGroupMember) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GlobalReplicationGroupSpec) DeepCopyInto(out *GlobalReplicationGroupSpec) { + *out = *in + if in.AutomaticFailoverEnabled != nil { + in, out := &in.AutomaticFailoverEnabled, &out.AutomaticFailoverEnabled *out = new(bool) **out = **in } @@ -1856,9 +1970,14 @@ func (in *GlobalReplicationGroup) DeepCopyInto(out *GlobalReplicationGroup) { *out = new(string) **out = **in } - if in.ClusterEnabled != nil { - in, out := &in.ClusterEnabled, &out.ClusterEnabled - *out = new(bool) + if in.CacheParameterGroupName != nil { + in, out := &in.CacheParameterGroupName, &out.CacheParameterGroupName + *out = new(string) + **out = **in + } + if in.Description != nil { + in, out := &in.Description, &out.Description + *out = new(string) **out = **in } if in.Engine != nil { @@ -1871,99 +1990,209 @@ func (in *GlobalReplicationGroup) DeepCopyInto(out *GlobalReplicationGroup) { *out = new(string) **out = **in } - if in.GlobalReplicationGroupDescription != nil { - in, out := &in.GlobalReplicationGroupDescription, &out.GlobalReplicationGroupDescription + if in.GlobalReplicationGroupIDSuffix != nil { + in, out := &in.GlobalReplicationGroupIDSuffix, &out.GlobalReplicationGroupIDSuffix *out = new(string) **out = **in } - if in.GlobalReplicationGroupID != nil { - in, out := &in.GlobalReplicationGroupID, &out.GlobalReplicationGroupID - *out = new(string) + if in.NodeGroupCount != nil { + in, out := &in.NodeGroupCount, &out.NodeGroupCount + *out = new(int64) **out = **in } - if in.Status != nil { - in, out := &in.Status, &out.Status + if in.PrimaryReplicationGroupID != nil { + in, out := &in.PrimaryReplicationGroupID, &out.PrimaryReplicationGroupID *out = new(string) **out = **in } - if in.TransitEncryptionEnabled != nil { - in, out := &in.TransitEncryptionEnabled, &out.TransitEncryptionEnabled - *out = new(bool) - **out = **in + if in.PrimaryReplicationGroupRef != nil { + in, out := &in.PrimaryReplicationGroupRef, &out.PrimaryReplicationGroupRef + *out = new(corev1alpha1.AWSResourceReferenceWrapper) + (*in).DeepCopyInto(*out) } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroup. -func (in *GlobalReplicationGroup) DeepCopy() *GlobalReplicationGroup { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupSpec. +func (in *GlobalReplicationGroupSpec) DeepCopy() *GlobalReplicationGroupSpec { if in == nil { return nil } - out := new(GlobalReplicationGroup) + out := new(GlobalReplicationGroupSpec) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *GlobalReplicationGroupInfo) DeepCopyInto(out *GlobalReplicationGroupInfo) { +func (in *GlobalReplicationGroupStatus) DeepCopyInto(out *GlobalReplicationGroupStatus) { *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.AtRestEncryptionEnabled != nil { + in, out := &in.AtRestEncryptionEnabled, &out.AtRestEncryptionEnabled + *out = new(bool) + **out = **in + } + if in.AuthTokenEnabled != nil { + in, out := &in.AuthTokenEnabled, &out.AuthTokenEnabled + *out = new(bool) + **out = **in + } + if in.ClusterEnabled != nil { + in, out := &in.ClusterEnabled, &out.ClusterEnabled + *out = new(bool) + **out = **in + } + if in.GlobalNodeGroups != nil { + in, out := &in.GlobalNodeGroups, &out.GlobalNodeGroups + *out = make([]*GlobalNodeGroup, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(GlobalNodeGroup) + (*in).DeepCopyInto(*out) + } + } + } if in.GlobalReplicationGroupID != nil { in, out := &in.GlobalReplicationGroupID, &out.GlobalReplicationGroupID *out = new(string) **out = **in } - if in.GlobalReplicationGroupMemberRole != nil { - in, out := &in.GlobalReplicationGroupMemberRole, &out.GlobalReplicationGroupMemberRole + if in.Members != nil { + in, out := &in.Members, &out.Members + *out = make([]*GlobalReplicationGroupMember, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(GlobalReplicationGroupMember) + (*in).DeepCopyInto(*out) + } + } + } + if in.Status != nil { + in, out := &in.Status, &out.Status *out = new(string) **out = **in } + if in.TransitEncryptionEnabled != nil { + in, out := &in.TransitEncryptionEnabled, &out.TransitEncryptionEnabled + *out = new(bool) + **out = **in + } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupInfo. -func (in *GlobalReplicationGroupInfo) DeepCopy() *GlobalReplicationGroupInfo { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupStatus. +func (in *GlobalReplicationGroupStatus) DeepCopy() *GlobalReplicationGroupStatus { if in == nil { return nil } - out := new(GlobalReplicationGroupInfo) + out := new(GlobalReplicationGroupStatus) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *GlobalReplicationGroupMember) DeepCopyInto(out *GlobalReplicationGroupMember) { +func (in *GlobalReplicationGroup_SDK) DeepCopyInto(out *GlobalReplicationGroup_SDK) { *out = *in - if in.AutomaticFailover != nil { - in, out := &in.AutomaticFailover, &out.AutomaticFailover + if in.ARN != nil { + in, out := &in.ARN, &out.ARN *out = new(string) **out = **in } - if in.ReplicationGroupID != nil { - in, out := &in.ReplicationGroupID, &out.ReplicationGroupID + if in.AtRestEncryptionEnabled != nil { + in, out := &in.AtRestEncryptionEnabled, &out.AtRestEncryptionEnabled + *out = new(bool) + **out = **in + } + if in.AuthTokenEnabled != nil { + in, out := &in.AuthTokenEnabled, &out.AuthTokenEnabled + *out = new(bool) + **out = **in + } + if in.CacheNodeType != nil { + in, out := &in.CacheNodeType, &out.CacheNodeType *out = new(string) **out = **in } - if in.ReplicationGroupRegion != nil { - in, out := &in.ReplicationGroupRegion, &out.ReplicationGroupRegion + if in.ClusterEnabled != nil { + in, out := &in.ClusterEnabled, &out.ClusterEnabled + *out = new(bool) + **out = **in + } + if in.Engine != nil { + in, out := &in.Engine, &out.Engine *out = new(string) **out = **in } - if in.Role != nil { - in, out := &in.Role, &out.Role + if in.EngineVersion != nil { + in, out := &in.EngineVersion, &out.EngineVersion *out = new(string) **out = **in } + if in.GlobalNodeGroups != nil { + in, out := &in.GlobalNodeGroups, &out.GlobalNodeGroups + *out = make([]*GlobalNodeGroup, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(GlobalNodeGroup) + (*in).DeepCopyInto(*out) + } + } + } + if in.GlobalReplicationGroupDescription != nil { + in, out := &in.GlobalReplicationGroupDescription, &out.GlobalReplicationGroupDescription + *out = new(string) + **out = **in + } + if in.GlobalReplicationGroupID != nil { + in, out := &in.GlobalReplicationGroupID, &out.GlobalReplicationGroupID + *out = new(string) + **out = **in + } + if in.Members != nil { + in, out := &in.Members, &out.Members + *out = make([]*GlobalReplicationGroupMember, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(GlobalReplicationGroupMember) + (*in).DeepCopyInto(*out) + } + } + } if in.Status != nil { in, out := &in.Status, &out.Status *out = new(string) **out = **in } + if in.TransitEncryptionEnabled != nil { + in, out := &in.TransitEncryptionEnabled, &out.TransitEncryptionEnabled + *out = new(bool) + **out = **in + } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroupMember. -func (in *GlobalReplicationGroupMember) DeepCopy() *GlobalReplicationGroupMember { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalReplicationGroup_SDK. +func (in *GlobalReplicationGroup_SDK) DeepCopy() *GlobalReplicationGroup_SDK { if in == nil { return nil } - out := new(GlobalReplicationGroupMember) + out := new(GlobalReplicationGroup_SDK) in.DeepCopyInto(out) return out } diff --git a/cmd/controller/main.go b/cmd/controller/main.go index b7b8248d..9b4d8fc2 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -47,6 +47,7 @@ import ( _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/cache_cluster" _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/cache_parameter_group" _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/cache_subnet_group" + _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/global_replication_group" _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/replication_group" _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/serverless_cache" _ "github.com/aws-controllers-k8s/elasticache-controller/pkg/resource/serverless_cache_snapshot" diff --git a/config/crd/bases/elasticache.services.k8s.aws_globalreplicationgroups.yaml b/config/crd/bases/elasticache.services.k8s.aws_globalreplicationgroups.yaml new file mode 100644 index 00000000..abef8e04 --- /dev/null +++ b/config/crd/bases/elasticache.services.k8s.aws_globalreplicationgroups.yaml @@ -0,0 +1,283 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.19.0 + name: globalreplicationgroups.elasticache.services.k8s.aws +spec: + group: elasticache.services.k8s.aws + names: + kind: GlobalReplicationGroup + listKind: GlobalReplicationGroupList + plural: globalreplicationgroups + singular: globalreplicationgroup + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .status.status + name: STATUS + type: string + - jsonPath: .spec.cacheNodeType + name: NODE-TYPE + type: string + - jsonPath: .status.conditions[?(@.type=="ACK.ResourceSynced")].status + name: Synced + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 + schema: + openAPIV3Schema: + description: GlobalReplicationGroup is the Schema for the GlobalReplicationGroups + 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: |- + GlobalReplicationGroupSpec defines the desired state of GlobalReplicationGroup. + + Consists of a primary cluster that accepts writes and an associated secondary + cluster that resides in a different Amazon region. The secondary cluster + accepts only reads. The primary cluster automatically replicates updates + to the secondary cluster. + + * The GlobalReplicationGroupIdSuffix represents the name of the Global + datastore, which is what you use to associate a secondary cluster. + properties: + automaticFailoverEnabled: + description: |- + Determines whether a read replica is automatically promoted to read/write + primary if the existing primary encounters a failure. + type: boolean + cacheNodeType: + description: A valid cache node type that you want to scale this Global + datastore to. + type: string + cacheParameterGroupName: + description: |- + The name of the cache parameter group to use with the Global datastore. It + must be compatible with the major engine version used by the Global datastore. + type: string + description: + description: Provides details of the Global datastore + type: string + engine: + description: |- + Modifies the engine listed in a global replication group message. The options + are valkey, memcached or redis. + type: string + engineVersion: + description: |- + The upgraded version of the cache engine to be run on the clusters in the + Global datastore. + type: string + globalReplicationGroupIDSuffix: + description: |- + The suffix name of a Global datastore. Amazon ElastiCache automatically applies + a prefix to the Global datastore ID when it is created. Each Amazon Region + has its own prefix. For instance, a Global datastore ID created in the US-West-1 + region will begin with "dsdfu" along with the suffix name you provide. The + suffix, combined with the auto-generated prefix, guarantees uniqueness of + the Global datastore name across multiple regions. + + For a full list of Amazon Regions and their respective Global datastore iD + prefixes, see Using the Amazon CLI with Global datastores (http://docs.aws.amazon.com/AmazonElastiCache/latest/dg/Redis-Global-Datastores-CLI.html). + type: string + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf + nodeGroupCount: + format: int64 + type: integer + primaryReplicationGroupID: + description: |- + The name of the primary cluster that accepts writes and will replicate updates + to the secondary cluster. This value is stored as a lowercase string. + type: string + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf + primaryReplicationGroupRef: + description: "AWSResourceReferenceWrapper provides a wrapper around + *AWSResourceReference\ntype to provide more user friendly syntax + for references using 'from' field\nEx:\nAPIIDRef:\n\n\tfrom:\n\t + \ name: my-api" + properties: + from: + description: |- + AWSResourceReference provides all the values necessary to reference another + k8s resource for finding the identifier(Id/ARN/Name) + properties: + name: + type: string + namespace: + type: string + type: object + type: object + required: + - globalReplicationGroupIDSuffix + type: object + status: + description: GlobalReplicationGroupStatus defines the observed state of + GlobalReplicationGroup + 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 + partition: + description: Partition is the AWS partition in which the resource + exists or will exist + type: string + region: + description: Region is the AWS region in which the resource exists + or will exist. + type: string + required: + - ownerAccountID + - region + type: object + atRestEncryptionEnabled: + description: |- + A flag that enables encryption at rest when set to true. + + You cannot modify the value of AtRestEncryptionEnabled after the replication + group is created. To enable encryption at rest on a replication group you + must set AtRestEncryptionEnabled to true when you create the replication + group. + + Required: Only available when creating a replication group in an Amazon VPC + using Redis OSS version 3.2.6, 4.x or later. + type: boolean + authTokenEnabled: + description: |- + A flag that enables using an AuthToken (password) when issuing Valkey or + Redis OSS commands. + + Default: false + type: boolean + clusterEnabled: + description: A flag that indicates whether the Global datastore is + cluster enabled. + type: boolean + 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 + globalNodeGroups: + description: Indicates the slot configuration and global identifier + for each slice group. + items: + description: Indicates the slot configuration and global identifier + for a slice group. + properties: + globalNodeGroupID: + type: string + slots: + type: string + type: object + type: array + globalReplicationGroupID: + description: The name of the Global datastore + type: string + members: + description: The replication groups that comprise the Global datastore. + items: + description: |- + A member of a Global datastore. It contains the Replication Group Id, the + Amazon region and the role of the replication group. + properties: + automaticFailover: + type: string + replicationGroupID: + type: string + replicationGroupRegion: + type: string + role: + type: string + status: + type: string + type: object + type: array + status: + description: The status of the Global datastore + type: string + transitEncryptionEnabled: + description: |- + A flag that enables in-transit encryption when set to true. + + Required: Only available when creating a replication group in an Amazon VPC + using Redis OSS version 3.2.6, 4.x or later. + type: boolean + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 967b537c..312521f7 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -5,6 +5,7 @@ resources: - bases/elasticache.services.k8s.aws_cacheclusters.yaml - bases/elasticache.services.k8s.aws_cacheparametergroups.yaml - bases/elasticache.services.k8s.aws_cachesubnetgroups.yaml + - bases/elasticache.services.k8s.aws_globalreplicationgroups.yaml - bases/elasticache.services.k8s.aws_replicationgroups.yaml - bases/elasticache.services.k8s.aws_serverlesscaches.yaml - bases/elasticache.services.k8s.aws_serverlesscachesnapshots.yaml diff --git a/config/rbac/cluster-role-controller.yaml b/config/rbac/cluster-role-controller.yaml index e9defccc..2ba0f24f 100644 --- a/config/rbac/cluster-role-controller.yaml +++ b/config/rbac/cluster-role-controller.yaml @@ -38,6 +38,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots @@ -58,6 +59,7 @@ rules: - cacheclusters/status - cacheparametergroups/status - cachesubnetgroups/status + - globalreplicationgroups/status - replicationgroups/status - serverlesscaches/status - serverlesscachesnapshots/status diff --git a/config/rbac/role-reader.yaml b/config/rbac/role-reader.yaml index 801dbccf..edeabbff 100644 --- a/config/rbac/role-reader.yaml +++ b/config/rbac/role-reader.yaml @@ -12,6 +12,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots diff --git a/config/rbac/role-writer.yaml b/config/rbac/role-writer.yaml index b45826c5..55adc2ed 100644 --- a/config/rbac/role-writer.yaml +++ b/config/rbac/role-writer.yaml @@ -12,6 +12,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots @@ -32,6 +33,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots diff --git a/generator.yaml b/generator.yaml index 1b330476..45c4a8ca 100644 --- a/generator.yaml +++ b/generator.yaml @@ -476,6 +476,96 @@ resources: in: - available - create_failed + GlobalReplicationGroup: + fields: + # Create API only accepts a suffix; ElastiCache prepends a region-specific + # prefix (e.g. "sgaui-"). Full ID stored in status.globalReplicationGroupID. + GlobalReplicationGroupIdSuffix: + is_required: true + is_immutable: true + PrimaryReplicationGroupId: + is_required: true + is_immutable: true + references: + resource: ReplicationGroup + path: Spec.ReplicationGroupID + AutomaticFailoverEnabled: + from: + operation: ModifyGlobalReplicationGroup + path: AutomaticFailoverEnabled + CacheNodeType: + from: + operation: ModifyGlobalReplicationGroup + path: CacheNodeType + CacheParameterGroupName: + from: + operation: ModifyGlobalReplicationGroup + path: CacheParameterGroupName + EngineVersion: + from: + operation: ModifyGlobalReplicationGroup + path: EngineVersion + Engine: + from: + operation: ModifyGlobalReplicationGroup + path: Engine + # Custom field for declarative node group scaling. Not part of any single + # API input — our custom update logic compares this against the current + # GlobalNodeGroups count to call Increase or Decrease APIs. + NodeGroupCount: + type: "*int64" + print: + add_age_column: true + add_synced_column: true + order_by: index + additional_columns: + - name: STATUS + json_path: .status.status + type: string + index: 10 + - name: NODE-TYPE + json_path: .spec.cacheNodeType + type: string + index: 20 + exceptions: + errors: + 404: + code: GlobalReplicationGroupNotFoundFault + terminal_codes: + - GlobalReplicationGroupAlreadyExistsFault + - InvalidParameterValue + - InvalidParameterCombination + - ServiceLinkedRoleNotFoundFault + - ReplicationGroupNotFoundFault + # "primary-only" = no secondaries attached (steady state) + # "available" = secondaries ARE attached (steady state) + # Both are valid "ready" states for the controller. + synced: + when: + - path: Status.Status + in: + - available + - primary-only + hooks: + sdk_create_post_set_output: + template_path: hooks/global_replication_group/sdk_create_post_set_output.go.tpl + delete_operation: + custom_method_name: customDeleteGlobalReplicationGroup + update_operation: + custom_method_name: customUpdateGlobalReplicationGroup + renames: + operations: + CreateGlobalReplicationGroup: + input_fields: + GlobalReplicationGroupDescription: Description + DescribeGlobalReplicationGroups: + output_fields: + GlobalReplicationGroupDescription: Description + ModifyGlobalReplicationGroup: + input_fields: + GlobalReplicationGroupDescription: Description + tags: + ignore: true operations: DescribeServerlessCaches: operation_type: @@ -517,9 +607,12 @@ operations: set_output_custom_method_name: customModifyCacheClusterSetOutput override_values: ApplyImmediately: aws.Bool(true) + DescribeGlobalReplicationGroups: + override_values: + ShowMemberInfo: aws.Bool(true) + set_output_custom_method_name: CustomDescribeGlobalReplicationGroupsSetOutput ignore: resource_names: - - GlobalReplicationGroup - CacheSecurityGroup field_paths: - DescribeSnapshotsInput.CacheClusterId diff --git a/helm/crds/elasticache.services.k8s.aws_globalreplicationgroups.yaml b/helm/crds/elasticache.services.k8s.aws_globalreplicationgroups.yaml new file mode 100644 index 00000000..060b436a --- /dev/null +++ b/helm/crds/elasticache.services.k8s.aws_globalreplicationgroups.yaml @@ -0,0 +1,286 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.19.0 + name: globalreplicationgroups.elasticache.services.k8s.aws +spec: + group: elasticache.services.k8s.aws + names: + kind: GlobalReplicationGroup + listKind: GlobalReplicationGroupList + plural: globalreplicationgroups + singular: globalreplicationgroup + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .status.status + name: STATUS + type: string + - jsonPath: .status.status + name: STATUS + type: string + - jsonPath: .status.cacheNodeType + name: NODE-TYPE + type: string + - jsonPath: .status.conditions[?(@.type=="ACK.ResourceSynced")].status + name: Synced + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 + schema: + openAPIV3Schema: + description: GlobalReplicationGroup is the Schema for the GlobalReplicationGroups + 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: |- + GlobalReplicationGroupSpec defines the desired state of GlobalReplicationGroup. + + Consists of a primary cluster that accepts writes and an associated secondary + cluster that resides in a different Amazon region. The secondary cluster + accepts only reads. The primary cluster automatically replicates updates + to the secondary cluster. + + - The GlobalReplicationGroupIdSuffix represents the name of the Global + datastore, which is what you use to associate a secondary cluster. + properties: + automaticFailoverEnabled: + description: |- + Determines whether a read replica is automatically promoted to read/write + primary if the existing primary encounters a failure. + type: boolean + cacheNodeType: + description: A valid cache node type that you want to scale this Global + datastore to. + type: string + cacheParameterGroupName: + description: |- + The name of the cache parameter group to use with the Global datastore. It + must be compatible with the major engine version used by the Global datastore. + type: string + description: + description: Provides details of the Global datastore + type: string + engine: + description: |- + Modifies the engine listed in a global replication group message. The options + are valkey, memcached or redis. + type: string + engineVersion: + description: |- + The upgraded version of the cache engine to be run on the clusters in the + Global datastore. + type: string + globalReplicationGroupIDSuffix: + description: |- + The suffix name of a Global datastore. Amazon ElastiCache automatically applies + a prefix to the Global datastore ID when it is created. Each Amazon Region + has its own prefix. For instance, a Global datastore ID created in the US-West-1 + region will begin with "dsdfu" along with the suffix name you provide. The + suffix, combined with the auto-generated prefix, guarantees uniqueness of + the Global datastore name across multiple regions. + + For a full list of Amazon Regions and their respective Global datastore iD + prefixes, see Using the Amazon CLI with Global datastores (http://docs.aws.amazon.com/AmazonElastiCache/latest/dg/Redis-Global-Datastores-CLI.html). + type: string + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf + nodeGroupCount: + format: int64 + type: integer + primaryReplicationGroupID: + description: |- + The name of the primary cluster that accepts writes and will replicate updates + to the secondary cluster. This value is stored as a lowercase string. + type: string + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf + primaryReplicationGroupRef: + description: "AWSResourceReferenceWrapper provides a wrapper around + *AWSResourceReference\ntype to provide more user friendly syntax + for references using 'from' field\nEx:\nAPIIDRef:\n\n\tfrom:\n\t + \ name: my-api" + properties: + from: + description: |- + AWSResourceReference provides all the values necessary to reference another + k8s resource for finding the identifier(Id/ARN/Name) + properties: + name: + type: string + namespace: + type: string + type: object + type: object + required: + - globalReplicationGroupIDSuffix + type: object + status: + description: GlobalReplicationGroupStatus defines the observed state of + GlobalReplicationGroup + 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 + partition: + description: Partition is the AWS partition in which the resource + exists or will exist + type: string + region: + description: Region is the AWS region in which the resource exists + or will exist. + type: string + required: + - ownerAccountID + - region + type: object + atRestEncryptionEnabled: + description: |- + A flag that enables encryption at rest when set to true. + + You cannot modify the value of AtRestEncryptionEnabled after the replication + group is created. To enable encryption at rest on a replication group you + must set AtRestEncryptionEnabled to true when you create the replication + group. + + Required: Only available when creating a replication group in an Amazon VPC + using Redis OSS version 3.2.6, 4.x or later. + type: boolean + authTokenEnabled: + description: |- + A flag that enables using an AuthToken (password) when issuing Valkey or + Redis OSS commands. + + Default: false + type: boolean + clusterEnabled: + description: A flag that indicates whether the Global datastore is + cluster enabled. + type: boolean + 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 + globalNodeGroups: + description: Indicates the slot configuration and global identifier + for each slice group. + items: + description: Indicates the slot configuration and global identifier + for a slice group. + properties: + globalNodeGroupID: + type: string + slots: + type: string + type: object + type: array + globalReplicationGroupID: + description: The name of the Global datastore + type: string + members: + description: The replication groups that comprise the Global datastore. + items: + description: |- + A member of a Global datastore. It contains the Replication Group Id, the + Amazon region and the role of the replication group. + properties: + automaticFailover: + type: string + replicationGroupID: + type: string + replicationGroupRegion: + type: string + role: + type: string + status: + type: string + type: object + type: array + status: + description: The status of the Global datastore + type: string + transitEncryptionEnabled: + description: |- + A flag that enables in-transit encryption when set to true. + + Required: Only available when creating a replication group in an Amazon VPC + using Redis OSS version 3.2.6, 4.x or later. + type: boolean + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl index c0396b2f..6455793c 100644 --- a/helm/templates/_helpers.tpl +++ b/helm/templates/_helpers.tpl @@ -85,6 +85,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots @@ -105,6 +106,7 @@ rules: - cacheclusters/status - cacheparametergroups/status - cachesubnetgroups/status + - globalreplicationgroups/status - replicationgroups/status - serverlesscaches/status - serverlesscachesnapshots/status diff --git a/helm/templates/role-reader.yaml b/helm/templates/role-reader.yaml index acc08279..e09c77fb 100644 --- a/helm/templates/role-reader.yaml +++ b/helm/templates/role-reader.yaml @@ -19,6 +19,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots diff --git a/helm/templates/role-writer.yaml b/helm/templates/role-writer.yaml index 5e8f19b7..6ab50585 100644 --- a/helm/templates/role-writer.yaml +++ b/helm/templates/role-writer.yaml @@ -19,6 +19,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots @@ -39,6 +40,7 @@ rules: - cacheclusters - cacheparametergroups - cachesubnetgroups + - globalreplicationgroups - replicationgroups - serverlesscaches - serverlesscachesnapshots diff --git a/helm/values.yaml b/helm/values.yaml index 7e25f62d..2126b95b 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -153,6 +153,7 @@ reconcile: - CacheCluster - CacheParameterGroup - CacheSubnetGroup + - GlobalReplicationGroup - ReplicationGroup - ServerlessCache - ServerlessCacheSnapshot diff --git a/pkg/resource/global_replication_group/delta.go b/pkg/resource/global_replication_group/delta.go new file mode 100644 index 00000000..6e42a2ff --- /dev/null +++ b/pkg/resource/global_replication_group/delta.go @@ -0,0 +1,113 @@ +// 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 global_replication_group + +import ( + "bytes" + + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" + acktags "github.com/aws-controllers-k8s/runtime/pkg/tags" + "k8s.io/apimachinery/pkg/api/equality" +) + +// Hack to avoid import errors during build... +var ( + _ = &bytes.Buffer{} + _ = &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 ackcompare.HasNilDifference(a.ko.Spec.AutomaticFailoverEnabled, b.ko.Spec.AutomaticFailoverEnabled) { + delta.Add("Spec.AutomaticFailoverEnabled", a.ko.Spec.AutomaticFailoverEnabled, b.ko.Spec.AutomaticFailoverEnabled) + } else if a.ko.Spec.AutomaticFailoverEnabled != nil && b.ko.Spec.AutomaticFailoverEnabled != nil { + if *a.ko.Spec.AutomaticFailoverEnabled != *b.ko.Spec.AutomaticFailoverEnabled { + delta.Add("Spec.AutomaticFailoverEnabled", a.ko.Spec.AutomaticFailoverEnabled, b.ko.Spec.AutomaticFailoverEnabled) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.CacheNodeType, b.ko.Spec.CacheNodeType) { + delta.Add("Spec.CacheNodeType", a.ko.Spec.CacheNodeType, b.ko.Spec.CacheNodeType) + } else if a.ko.Spec.CacheNodeType != nil && b.ko.Spec.CacheNodeType != nil { + if *a.ko.Spec.CacheNodeType != *b.ko.Spec.CacheNodeType { + delta.Add("Spec.CacheNodeType", a.ko.Spec.CacheNodeType, b.ko.Spec.CacheNodeType) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.CacheParameterGroupName, b.ko.Spec.CacheParameterGroupName) { + delta.Add("Spec.CacheParameterGroupName", a.ko.Spec.CacheParameterGroupName, b.ko.Spec.CacheParameterGroupName) + } else if a.ko.Spec.CacheParameterGroupName != nil && b.ko.Spec.CacheParameterGroupName != nil { + if *a.ko.Spec.CacheParameterGroupName != *b.ko.Spec.CacheParameterGroupName { + delta.Add("Spec.CacheParameterGroupName", a.ko.Spec.CacheParameterGroupName, b.ko.Spec.CacheParameterGroupName) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.Description, b.ko.Spec.Description) { + delta.Add("Spec.Description", a.ko.Spec.Description, b.ko.Spec.Description) + } else if a.ko.Spec.Description != nil && b.ko.Spec.Description != nil { + if *a.ko.Spec.Description != *b.ko.Spec.Description { + delta.Add("Spec.Description", a.ko.Spec.Description, b.ko.Spec.Description) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.Engine, b.ko.Spec.Engine) { + delta.Add("Spec.Engine", a.ko.Spec.Engine, b.ko.Spec.Engine) + } else if a.ko.Spec.Engine != nil && b.ko.Spec.Engine != nil { + if *a.ko.Spec.Engine != *b.ko.Spec.Engine { + delta.Add("Spec.Engine", a.ko.Spec.Engine, b.ko.Spec.Engine) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.EngineVersion, b.ko.Spec.EngineVersion) { + delta.Add("Spec.EngineVersion", a.ko.Spec.EngineVersion, b.ko.Spec.EngineVersion) + } else if a.ko.Spec.EngineVersion != nil && b.ko.Spec.EngineVersion != nil { + if *a.ko.Spec.EngineVersion != *b.ko.Spec.EngineVersion { + delta.Add("Spec.EngineVersion", a.ko.Spec.EngineVersion, b.ko.Spec.EngineVersion) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.GlobalReplicationGroupIDSuffix, b.ko.Spec.GlobalReplicationGroupIDSuffix) { + delta.Add("Spec.GlobalReplicationGroupIDSuffix", a.ko.Spec.GlobalReplicationGroupIDSuffix, b.ko.Spec.GlobalReplicationGroupIDSuffix) + } else if a.ko.Spec.GlobalReplicationGroupIDSuffix != nil && b.ko.Spec.GlobalReplicationGroupIDSuffix != nil { + if *a.ko.Spec.GlobalReplicationGroupIDSuffix != *b.ko.Spec.GlobalReplicationGroupIDSuffix { + delta.Add("Spec.GlobalReplicationGroupIDSuffix", a.ko.Spec.GlobalReplicationGroupIDSuffix, b.ko.Spec.GlobalReplicationGroupIDSuffix) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.NodeGroupCount, b.ko.Spec.NodeGroupCount) { + delta.Add("Spec.NodeGroupCount", a.ko.Spec.NodeGroupCount, b.ko.Spec.NodeGroupCount) + } else if a.ko.Spec.NodeGroupCount != nil && b.ko.Spec.NodeGroupCount != nil { + if *a.ko.Spec.NodeGroupCount != *b.ko.Spec.NodeGroupCount { + delta.Add("Spec.NodeGroupCount", a.ko.Spec.NodeGroupCount, b.ko.Spec.NodeGroupCount) + } + } + if ackcompare.HasNilDifference(a.ko.Spec.PrimaryReplicationGroupID, b.ko.Spec.PrimaryReplicationGroupID) { + delta.Add("Spec.PrimaryReplicationGroupID", a.ko.Spec.PrimaryReplicationGroupID, b.ko.Spec.PrimaryReplicationGroupID) + } else if a.ko.Spec.PrimaryReplicationGroupID != nil && b.ko.Spec.PrimaryReplicationGroupID != nil { + if *a.ko.Spec.PrimaryReplicationGroupID != *b.ko.Spec.PrimaryReplicationGroupID { + delta.Add("Spec.PrimaryReplicationGroupID", a.ko.Spec.PrimaryReplicationGroupID, b.ko.Spec.PrimaryReplicationGroupID) + } + } + if !equality.Semantic.Equalities.DeepEqual(a.ko.Spec.PrimaryReplicationGroupRef, b.ko.Spec.PrimaryReplicationGroupRef) { + delta.Add("Spec.PrimaryReplicationGroupRef", a.ko.Spec.PrimaryReplicationGroupRef, b.ko.Spec.PrimaryReplicationGroupRef) + } + + return delta +} diff --git a/pkg/resource/global_replication_group/descriptor.go b/pkg/resource/global_replication_group/descriptor.go new file mode 100644 index 00000000..5d28e5da --- /dev/null +++ b/pkg/resource/global_replication_group/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 global_replication_group + +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/elasticache-controller/apis/v1alpha1" +) + +const ( + FinalizerString = "finalizers.elasticache.services.k8s.aws/GlobalReplicationGroup" +) + +var ( + GroupVersionResource = svcapitypes.GroupVersion.WithResource("globalreplicationgroups") + GroupKind = metav1.GroupKind{ + Group: "elasticache.services.k8s.aws", + Kind: "GlobalReplicationGroup", + } +) + +// 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.GlobalReplicationGroup{} +} + +// 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.GlobalReplicationGroup), + } +} + +// 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/global_replication_group/hooks.go b/pkg/resource/global_replication_group/hooks.go new file mode 100644 index 00000000..6942f51a --- /dev/null +++ b/pkg/resource/global_replication_group/hooks.go @@ -0,0 +1,676 @@ +// 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. + +package global_replication_group + +import ( + "context" + "errors" + "fmt" + "strconv" + "strings" + + "github.com/aws/aws-sdk-go-v2/aws" + svcsdk "github.com/aws/aws-sdk-go-v2/service/elasticache" + svcsdktypes "github.com/aws/aws-sdk-go-v2/service/elasticache/types" + smithy "github.com/aws/smithy-go" + + svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1" + "github.com/aws-controllers-k8s/elasticache-controller/pkg/util" + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" + ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors" + ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue" +) + +const ( + statusAvailable = "available" + statusPrimaryOnly = "primary-only" + statusDeleting = "deleting" + statusModifying = "modifying" + statusCreating = "creating" +) + +// The GlobalReplicationGroup Describe API does not return CacheParameterGroupName +// (ElastiCache copies it per member), so we cannot compare desired against +// observed state for it. We record the value passed to the most recent +// Create/Modify call in an annotation and use that as the "observed" value during +// delta comparison. AutomaticFailoverEnabled is also not returned as a top-level +// field, but it is derivable from live member state, so it does not need an +// annotation (see deriveAutomaticFailoverEnabled). +const ( + // AnnotationLastRequestedCPGN records the CacheParameterGroupName value + // sent in the most recent Create or Modify call. + AnnotationLastRequestedCPGN = svcapitypes.AnnotationPrefix + "last-requested-cache-parameter-group-name" +) + +var ( + requeueWaitWhileDeleting = ackrequeue.NeededAfter( + errors.New("GlobalReplicationGroup is being deleted."), + ackrequeue.DefaultRequeueAfterDuration, + ) + requeueWaitWhileDisassociating = ackrequeue.NeededAfter( + errors.New("Waiting for secondary members to be disassociated."), + ackrequeue.DefaultRequeueAfterDuration, + ) + requeueWaitWhileModifying = ackrequeue.NeededAfter( + errors.New("GlobalReplicationGroup is being modified."), + ackrequeue.DefaultRequeueAfterDuration, + ) +) + +// ============================================================================= +// CUSTOM DELETE +// ============================================================================= +// Multi-step delete: +// Phase 1: If already deleting → requeue +// Phase 2: If secondary members exist → disassociate each → requeue +// Phase 3: If primary-only → call DeleteGlobalReplicationGroup → requeue +// Phase 4: Eventually the Describe returns 404 → ACK runtime handles it +// ============================================================================= + +func (rm *resourceManager) customDeleteGlobalReplicationGroup( + ctx context.Context, + r *resource, +) (*resource, error) { + // Phase 1: Already deleting — just wait + if isGlobalDeleting(r) { + return r, requeueWaitWhileDeleting + } + + // Proceed only from a stable state. Anything else ("creating"/"modifying") + // means an operation is in flight and DeleteGlobalReplicationGroup would be + // rejected with InvalidGlobalReplicationGroupState, so requeue and wait. + // Once stable, exactly one state applies: "available" (secondaries attached + // -> disassociate first, Phase 2) or "primary-only" (none -> delete, Phase 3). + if !isGlobalPrimaryOnly(r) && !isGlobalAvailable(r) { + return r, requeueWaitWhileModifying + } + + globalId := r.ko.Status.GlobalReplicationGroupID + if globalId == nil { + return nil, fmt.Errorf("GlobalReplicationGroupID not found in status, cannot delete") + } + + // Describe with member info to see if secondaries still exist + descInput := &svcsdk.DescribeGlobalReplicationGroupsInput{ + GlobalReplicationGroupId: globalId, + ShowMemberInfo: aws.Bool(true), + } + descResp, err := rm.sdkapi.DescribeGlobalReplicationGroups(ctx, descInput) + rm.metrics.RecordAPICall("READ_ONE", "DescribeGlobalReplicationGroups", err) + if err != nil { + return nil, err + } + + if len(descResp.GlobalReplicationGroups) == 0 { + // Already gone + return nil, nil + } + + grg := descResp.GlobalReplicationGroups[0] + + // Find secondary members + secondaries := findSecondaryMembers(grg.Members) + + // Phase 2: Disassociate any remaining secondaries. + // Per integration testing, a member stays "associated" until it simply + // vanishes from the list when done -- there is no "disassociating" state. + // Disassociating one already in progress returns + // InvalidGlobalReplicationGroupState, which we requeue on. + if len(secondaries) > 0 { + for _, secondary := range secondaries { + if secondary.Status != nil && *secondary.Status != "associated" { + continue + } + + disInput := &svcsdk.DisassociateGlobalReplicationGroupInput{ + GlobalReplicationGroupId: globalId, + ReplicationGroupId: secondary.ReplicationGroupId, + ReplicationGroupRegion: secondary.ReplicationGroupRegion, + } + _, disErr := rm.sdkapi.DisassociateGlobalReplicationGroup(ctx, disInput) + rm.metrics.RecordAPICall("UPDATE", "DisassociateGlobalReplicationGroup", disErr) + if disErr != nil { + // InvalidGlobalReplicationGroupState is retryable — race between + // our guard and a state transition. Anything else is fatal. + if isInvalidGlobalReplicationGroupStateError(disErr) { + return r, requeueWaitWhileModifying + } + return nil, disErr + } + } + + return r, requeueWaitWhileDisassociating + } + + // Phase 3: No secondaries remain — delete the global group + delInput := &svcsdk.DeleteGlobalReplicationGroupInput{ + GlobalReplicationGroupId: globalId, + RetainPrimaryReplicationGroup: aws.Bool(true), + } + _, delErr := rm.sdkapi.DeleteGlobalReplicationGroup(ctx, delInput) + rm.metrics.RecordAPICall("DELETE", "DeleteGlobalReplicationGroup", delErr) + if delErr != nil { + return nil, delErr + } + + return r, requeueWaitWhileDeleting +} + +// ============================================================================= +// CUSTOM UPDATE +// ============================================================================= +// Node group (shard) scaling uses the separate Increase/DecreaseNodeGroups +// APIs; every other field goes through modifyGlobalReplicationGroup. A fully +// custom Update handler is required because ModifyGlobalReplicationGroup +// rejects arbitrary field combinations (see modifyGlobalReplicationGroup). +// ============================================================================= + +func (rm *resourceManager) customUpdateGlobalReplicationGroup( + ctx context.Context, + desired *resource, + latest *resource, + delta *ackcompare.Delta, +) (*resource, error) { + // Guard: must be in a modifiable state + if !isGlobalAvailable(latest) && !isGlobalPrimaryOnly(latest) { + return nil, requeueWaitWhileModifying + } + + globalId := latest.ko.Status.GlobalReplicationGroupID + if globalId == nil { + return nil, fmt.Errorf("GlobalReplicationGroupID not found in status") + } + + // --- Handle node group count changes --- + if desired.ko.Spec.NodeGroupCount != nil { + desiredCount := *desired.ko.Spec.NodeGroupCount + currentCount := int64(0) + if latest.ko.Status.GlobalNodeGroups != nil { + currentCount = int64(len(latest.ko.Status.GlobalNodeGroups)) + } else { + // GlobalNodeGroups not yet populated in status — fetch from AWS + descInput := &svcsdk.DescribeGlobalReplicationGroupsInput{ + GlobalReplicationGroupId: globalId, + } + descResp, err := rm.sdkapi.DescribeGlobalReplicationGroups(ctx, descInput) + rm.metrics.RecordAPICall("READ_ONE", "DescribeGlobalReplicationGroups", err) + if err == nil && len(descResp.GlobalReplicationGroups) > 0 { + currentCount = int64(len(descResp.GlobalReplicationGroups[0].GlobalNodeGroups)) + } + } + + if currentCount > 0 { + if desiredCount > currentCount { + return rm.increaseNodeGroups(ctx, desired, globalId, int32(desiredCount)) + } else if desiredCount < currentCount { + return rm.decreaseNodeGroups(ctx, desired, latest, globalId, int32(desiredCount)) + } + } + } + + // --- Standard Modify --- + return rm.modifyGlobalReplicationGroup(ctx, desired, latest, globalId) +} + +func (rm *resourceManager) increaseNodeGroups( + ctx context.Context, + desired *resource, + globalId *string, + nodeGroupCount int32, +) (*resource, error) { + input := &svcsdk.IncreaseNodeGroupsInGlobalReplicationGroupInput{ + GlobalReplicationGroupId: globalId, + ApplyImmediately: aws.Bool(true), + NodeGroupCount: aws.Int32(nodeGroupCount), + } + + resp, err := rm.sdkapi.IncreaseNodeGroupsInGlobalReplicationGroup(ctx, input) + rm.metrics.RecordAPICall("UPDATE", "IncreaseNodeGroupsInGlobalReplicationGroup", err) + if err != nil { + return nil, err + } + + return rm.setGlobalReplicationGroupOutput(desired, resp.GlobalReplicationGroup) +} + +func (rm *resourceManager) decreaseNodeGroups( + ctx context.Context, + desired *resource, + latest *resource, + globalId *string, + nodeGroupCount int32, +) (*resource, error) { + input := &svcsdk.DecreaseNodeGroupsInGlobalReplicationGroupInput{ + GlobalReplicationGroupId: globalId, + ApplyImmediately: aws.Bool(true), + NodeGroupCount: aws.Int32(nodeGroupCount), + } + + // Retain the first N node groups and drop the rest. Safe for data: ElastiCache + // migrates slots off dropped shards before removing them. Trade-off: users + // can't choose which specific shards are removed. + if latest.ko.Status.GlobalNodeGroups != nil { + retain := []string{} + for i, ng := range latest.ko.Status.GlobalNodeGroups { + if int32(i) >= nodeGroupCount { + break + } + if ng.GlobalNodeGroupID != nil { + retain = append(retain, *ng.GlobalNodeGroupID) + } + } + input.GlobalNodeGroupsToRetain = retain + } + + resp, err := rm.sdkapi.DecreaseNodeGroupsInGlobalReplicationGroup(ctx, input) + rm.metrics.RecordAPICall("UPDATE", "DecreaseNodeGroupsInGlobalReplicationGroup", err) + if err != nil { + return nil, err + } + + return rm.setGlobalReplicationGroupOutput(desired, resp.GlobalReplicationGroup) +} + +// modifyGlobalReplicationGroup sends a single logical change per call. AWS's +// ModifyGlobalReplicationGroup does not accept arbitrary combinations of +// fields in one request, so changes are grouped and prioritized: +// +// - Description, AutomaticFailoverEnabled, and CacheNodeType are each +// independent single-field changes, sent one at a time. +// - Engine / EngineVersion / CacheParameterGroupName form one logical +// "engine upgrade" change and must be sent together: a MAJOR upgrade (or +// any Engine change, e.g. redis -> valkey) requires all three +// (Engine + EngineVersion + CacheParameterGroupName); a MINOR upgrade +// requires Engine + EngineVersion (no parameter group). +// +// These groupings were verified empirically against the live API: +// - Engine alone -> "No modifications requested" +// - Engine + EngineVersion -> "Parameter group must be specified for major +// engine version upgrade" (for a major bump) +// - Engine + EngineVersion + CacheParameterGroupName -> accepted +// +// Only the first applicable change is sent per call. When more than one field +// changed, the function returns an explicit requeue so the remaining fields are +// applied on follow-up reconciles -- IsSynced() is status-based, so a fast +// change would otherwise return the group to a synced status and strand the +// rest of the delta. The delta is recomputed from the CR spec and live AWS +// state each reconcile, so this converges over multiple passes and is correct +// across controller restarts (no in-memory pending-change state). +// +// `latest` reflects observed AWS state: fields from Describe, AutomaticFailover +// derived from live member state, and CacheParameterGroupName restored from its +// last-requested annotation (Describe returns none of the latter two directly). +// Engine versions are compared with util.EngineVersionsMatch because AWS +// normalizes a requested "7.1" to "7.1.0" -- a plain string compare would treat +// them as different and re-issue the same upgrade on every reconcile. +func (rm *resourceManager) modifyGlobalReplicationGroup( + ctx context.Context, + desired *resource, + latest *resource, + globalId *string, +) (*resource, error) { + input := &svcsdk.ModifyGlobalReplicationGroupInput{ + GlobalReplicationGroupId: globalId, + ApplyImmediately: aws.Bool(true), + } + + // Detect each independent pending change once. Engine and EngineVersion + // form a single logical "engine upgrade" change (sent together), so they + // are combined into engineGroupChanged. + descChanged := desired.ko.Spec.Description != nil && + !strPtrEqual(desired.ko.Spec.Description, latest.ko.Spec.Description) + afeChanged := desired.ko.Spec.AutomaticFailoverEnabled != nil && + (latest.ko.Spec.AutomaticFailoverEnabled == nil || + *desired.ko.Spec.AutomaticFailoverEnabled != *latest.ko.Spec.AutomaticFailoverEnabled) + nodeTypeChanged := desired.ko.Spec.CacheNodeType != nil && + !strPtrEqual(desired.ko.Spec.CacheNodeType, latest.ko.Spec.CacheNodeType) + engineChanged := desired.ko.Spec.Engine != nil && + !strPtrEqual(desired.ko.Spec.Engine, latest.ko.Spec.Engine) + versionChanged := desired.ko.Spec.EngineVersion != nil && + (latest.ko.Spec.EngineVersion == nil || + !util.EngineVersionsMatch(*desired.ko.Spec.EngineVersion, *latest.ko.Spec.EngineVersion)) + engineGroupChanged := engineChanged || versionChanged + cpgnChanged := desired.ko.Spec.CacheParameterGroupName != nil && + !strPtrEqual(desired.ko.Spec.CacheParameterGroupName, latest.ko.Spec.CacheParameterGroupName) + + // CacheParameterGroupName is only valid as part of a major engine upgrade + // (it is sent together with Engine + EngineVersion in the engineGroupChanged + // branch). A CPGN change on its own -- whether it is the only pending change + // or it accompanies an unrelated change like Description -- is a permanent + // user misconfiguration AWS rejects, so surface it as terminal (recovers + // when the user corrects the spec) rather than swallowing it into another + // branch or requeuing forever. + if cpgnChanged && !engineGroupChanged { + return nil, ackerr.NewTerminalError(errors.New( + "spec.cacheParameterGroupName can only be changed as part of a major engine version upgrade")) + } + + // Count independent pending changes. AWS accepts only one per call, so if + // more than one is pending we apply the first and requeue explicitly for + // the rest (see below). CacheParameterGroupName is not counted separately: + // a valid CPGN change only ever travels with engineGroupChanged (already + // counted), and a standalone CPGN change is handled as terminal above. + pending := 0 + for _, c := range []bool{descChanged, afeChanged, nodeTypeChanged, engineGroupChanged} { + if c { + pending++ + } + } + + switch { + case descChanged: + input.GlobalReplicationGroupDescription = desired.ko.Spec.Description + case afeChanged: + input.AutomaticFailoverEnabled = desired.ko.Spec.AutomaticFailoverEnabled + case nodeTypeChanged: + input.CacheNodeType = desired.ko.Spec.CacheNodeType + case engineGroupChanged: + // Engine upgrade: send Engine + EngineVersion together. A major upgrade + // (or any Engine change) additionally requires CacheParameterGroupName. + if desired.ko.Spec.Engine != nil { + input.Engine = desired.ko.Spec.Engine + } + if desired.ko.Spec.EngineVersion != nil { + input.EngineVersion = desired.ko.Spec.EngineVersion + } + if engineChanged || isMajorVersionUpgrade(desired.ko.Spec.EngineVersion, latest.ko.Spec.EngineVersion) { + if desired.ko.Spec.CacheParameterGroupName == nil { + return nil, ackerr.NewTerminalError(errors.New( + "a major engine version upgrade requires spec.cacheParameterGroupName to be set")) + } + input.CacheParameterGroupName = desired.ko.Spec.CacheParameterGroupName + } + default: + // A Spec delta triggered Update but none of the fields we can modify + // changed (e.g. a change to an immutable field). Return the observed + // state unchanged -- returning nil here would nil-deref in the runtime's + // post-Update patch step. + return latest, nil + } + + resp, err := rm.sdkapi.ModifyGlobalReplicationGroup(ctx, input) + rm.metrics.RecordAPICall("UPDATE", "ModifyGlobalReplicationGroup", err) + if err != nil { + return nil, err + } + + // CacheParameterGroupName is not returned by Describe and is not derivable, + // so its "observed" value is tracked in an annotation. Record it only when + // it was actually sent to AWS (as part of the engine-upgrade branch above), + // so a value that was never sent is never falsely reported as applied. + out, err := rm.setGlobalReplicationGroupOutput(desired, resp.GlobalReplicationGroup) + if err != nil { + return nil, err + } + if input.CacheParameterGroupName != nil { + rm.setLastRequestedCPGN(desired, out.ko) + } + if pending > 1 { + return out, ackrequeue.NeededAfter( + errors.New("applied one change; additional field updates pending"), + ackrequeue.DefaultRequeueAfterDuration, + ) + } + return out, nil +} + +// isMajorVersionUpgrade reports whether moving from latest to desired engine +// version changes the major version component (e.g. 7.x -> 8.x). A nil desired +// version is not an upgrade; a nil latest version is treated conservatively as +// a major change so the parameter group is included. +func isMajorVersionUpgrade(desired, latest *string) bool { + if desired == nil { + return false + } + if latest == nil { + return true + } + dMaj := majorVersion(*desired) + lMaj := majorVersion(*latest) + return dMaj >= 0 && lMaj >= 0 && dMaj > lMaj +} + +// majorVersion extracts the leading major-version integer from strings like +// "7.1", "7.1.0", or "8.x". Returns -1 if it cannot be parsed. +func majorVersion(v string) int { + parts := strings.Split(v, ".") + if len(parts) == 0 { + return -1 + } + maj, err := strconv.Atoi(parts[0]) + if err != nil { + return -1 + } + return maj +} + +func strPtrEqual(a, b *string) bool { + if a == nil || b == nil { + return a == b + } + return *a == *b +} + +// deriveAutomaticFailoverEnabled infers the global datastore's automatic-failover +// setting from live member state, which the Describe API does return (the +// top-level AutomaticFailoverEnabled field is not returned). Returns nil if it +// cannot be determined -- e.g. members are not yet populated or a member omits +// the status -- so callers can avoid diffing against an unknown value. +func deriveAutomaticFailoverEnabled( + ko *svcapitypes.GlobalReplicationGroup, +) *bool { + if len(ko.Status.Members) == 0 { + return nil + } + allEnabled := true + for _, m := range ko.Status.Members { + if m == nil || m.AutomaticFailover == nil { + return nil + } + switch *m.AutomaticFailover { + case string(svcsdktypes.AutomaticFailoverStatusEnabled), + string(svcsdktypes.AutomaticFailoverStatusEnabling): + // failover on for this member + default: + allEnabled = false + } + } + return &allEnabled +} + +// ============================================================================= +// CUSTOM DESCRIBE SET OUTPUT +// ============================================================================= + +func (rm *resourceManager) CustomDescribeGlobalReplicationGroupsSetOutput( + ctx context.Context, + r *resource, + resp *svcsdk.DescribeGlobalReplicationGroupsOutput, + ko *svcapitypes.GlobalReplicationGroup, +) (*svcapitypes.GlobalReplicationGroup, error) { + if len(resp.GlobalReplicationGroups) == 0 { + return ko, nil + } + + // NodeGroupCount is a custom spec field for declarative scaling. + // Populate latest from the actual AWS state (count of GlobalNodeGroups) + // so the delta only fires when the user's desired count differs. + if len(resp.GlobalReplicationGroups[0].GlobalNodeGroups) > 0 { + count := int64(len(resp.GlobalReplicationGroups[0].GlobalNodeGroups)) + ko.Spec.NodeGroupCount = &count + } else { + ko.Spec.NodeGroupCount = nil + } + + // AutomaticFailoverEnabled is derivable from live member state (which + // Describe returns), so infer it rather than tracking an annotation. Only + // populate it when the user manages the field; fall back to the desired + // value when it can't be determined from members, so we never diff against + // an unknown state and issue a no-op Modify (which AWS rejects). + if r.ko.Spec.AutomaticFailoverEnabled != nil { + if derived := deriveAutomaticFailoverEnabled(ko); derived != nil { + ko.Spec.AutomaticFailoverEnabled = derived + } else { + ko.Spec.AutomaticFailoverEnabled = r.ko.Spec.AutomaticFailoverEnabled + } + } else { + ko.Spec.AutomaticFailoverEnabled = nil + } + + // CacheParameterGroupName is not returned by Describe and is not derivable + // (ElastiCache copies it per member), so restore it from the last-requested + // annotation. + restoreUnreadableFieldsFromAnnotations(r, ko) + + return ko, nil +} + +// ============================================================================= +// HELPERS +// ============================================================================= + +func isGlobalDeleting(r *resource) bool { + if r == nil || r.ko.Status.Status == nil { + return false + } + return *r.ko.Status.Status == statusDeleting +} + +func isGlobalAvailable(r *resource) bool { + if r == nil || r.ko.Status.Status == nil { + return false + } + return *r.ko.Status.Status == statusAvailable +} + +func isGlobalPrimaryOnly(r *resource) bool { + if r == nil || r.ko.Status.Status == nil { + return false + } + return *r.ko.Status.Status == statusPrimaryOnly +} + +// findSecondaryMembers returns all members whose role is NOT "PRIMARY". +func findSecondaryMembers(members []svcsdktypes.GlobalReplicationGroupMember) []svcsdktypes.GlobalReplicationGroupMember { + var secondaries []svcsdktypes.GlobalReplicationGroupMember + for _, m := range members { + if m.Role != nil && *m.Role != "PRIMARY" { + secondaries = append(secondaries, m) + } + } + return secondaries +} + +// setGlobalReplicationGroupOutput maps the SDK response back into the resource. +func (rm *resourceManager) setGlobalReplicationGroupOutput( + r *resource, + grg *svcsdktypes.GlobalReplicationGroup, +) (*resource, error) { + if r == nil || r.ko == nil { + return nil, nil + } + // A successful Modify/Increase/Decrease should always return the group; + // if it somehow didn't, return the input resource unchanged rather than a + // nil resource (which would nil-deref in the runtime's post-Update patch). + if grg == nil { + return r, nil + } + ko := r.ko.DeepCopy() + + if grg.GlobalReplicationGroupId != nil { + ko.Status.GlobalReplicationGroupID = grg.GlobalReplicationGroupId + } + if grg.Status != nil { + ko.Status.Status = grg.Status + } + + rm.setStatusDefaults(ko) + return &resource{ko}, nil +} + +// getAnnotationsFields returns the annotations map on ko, initializing it from +// the desired resource's annotations if not already present. +func getAnnotationsFields( + r *resource, + ko *svcapitypes.GlobalReplicationGroup, +) map[string]string { + if ko.ObjectMeta.Annotations != nil { + return ko.ObjectMeta.Annotations + } + + desiredAnnotations := r.ko.ObjectMeta.GetAnnotations() + annotations := make(map[string]string) + for k, v := range desiredAnnotations { + annotations[k] = v + } + + ko.ObjectMeta.Annotations = annotations + return annotations +} + +// setLastRequestedCPGN records the CacheParameterGroupName value that was just +// sent to AWS in an annotation. The Describe API does not return this field and +// it is not derivable, so this annotation is the only way a subsequent reconcile +// can tell what value is actually in effect. It is written only after a Modify +// that carried CacheParameterGroupName, so a value that was never sent is never +// recorded as applied. +func (rm *resourceManager) setLastRequestedCPGN( + r *resource, + ko *svcapitypes.GlobalReplicationGroup, +) { + annotations := getAnnotationsFields(r, ko) + if r.ko.Spec.CacheParameterGroupName != nil { + annotations[AnnotationLastRequestedCPGN] = *r.ko.Spec.CacheParameterGroupName + } +} + +// restoreUnreadableFieldsFromAnnotations populates CacheParameterGroupName, which +// Describe does not return and is not derivable from other state, using the value +// recorded in the annotation. This makes `latest` reflect what was actually last +// sent to AWS, so the delta only fires when the user changes their desired state. +func restoreUnreadableFieldsFromAnnotations( + r *resource, + ko *svcapitypes.GlobalReplicationGroup, +) { + annotations := r.ko.ObjectMeta.GetAnnotations() + if annotations == nil { + // No annotations yet (first reconcile). Clear the field so any + // user-specified value is treated as a change. + ko.Spec.CacheParameterGroupName = nil + return + } + + if val, ok := annotations[AnnotationLastRequestedCPGN]; ok { + if val == "null" { + ko.Spec.CacheParameterGroupName = nil + } else { + ko.Spec.CacheParameterGroupName = &val + } + } else { + ko.Spec.CacheParameterGroupName = nil + } +} + +// isInvalidGlobalReplicationGroupStateError checks if the error is the +// retryable InvalidGlobalReplicationGroupState error (GRG is modifying). +func isInvalidGlobalReplicationGroupStateError(err error) bool { + if err == nil { + return false + } + var oe *smithy.OperationError + if errors.As(err, &oe) { + return strings.Contains(oe.Error(), "InvalidGlobalReplicationGroupState") + } + return strings.Contains(err.Error(), "InvalidGlobalReplicationGroupState") +} diff --git a/pkg/resource/global_replication_group/hooks_test.go b/pkg/resource/global_replication_group/hooks_test.go new file mode 100644 index 00000000..5e74d37a --- /dev/null +++ b/pkg/resource/global_replication_group/hooks_test.go @@ -0,0 +1,61 @@ +// 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. + +package global_replication_group + +import ( + "testing" + + svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1" +) + +func strp(s string) *string { return &s } + +func member(af string) *svcapitypes.GlobalReplicationGroupMember { + return &svcapitypes.GlobalReplicationGroupMember{AutomaticFailover: strp(af)} +} + +func TestDeriveAutomaticFailoverEnabled(t *testing.T) { + cases := []struct { + name string + members []*svcapitypes.GlobalReplicationGroupMember + want *bool // nil means "undeterminable" + }{ + {"no members -> nil", nil, nil}, + {"empty members -> nil", []*svcapitypes.GlobalReplicationGroupMember{}, nil}, + {"all enabled -> true", []*svcapitypes.GlobalReplicationGroupMember{member("enabled"), member("enabled")}, boolp(true)}, + {"enabled + enabling -> true", []*svcapitypes.GlobalReplicationGroupMember{member("enabled"), member("enabling")}, boolp(true)}, + {"one disabled -> false", []*svcapitypes.GlobalReplicationGroupMember{member("enabled"), member("disabled")}, boolp(false)}, + {"all disabled -> false", []*svcapitypes.GlobalReplicationGroupMember{member("disabled")}, boolp(false)}, + {"disabling -> false", []*svcapitypes.GlobalReplicationGroupMember{member("disabling")}, boolp(false)}, + {"nil status on a member -> nil", []*svcapitypes.GlobalReplicationGroupMember{member("enabled"), {AutomaticFailover: nil}}, nil}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ko := &svcapitypes.GlobalReplicationGroup{} + ko.Status.Members = tc.members + got := deriveAutomaticFailoverEnabled(ko) + switch { + case tc.want == nil && got != nil: + t.Fatalf("want nil, got %v", *got) + case tc.want != nil && got == nil: + t.Fatalf("want %v, got nil", *tc.want) + case tc.want != nil && got != nil && *tc.want != *got: + t.Fatalf("want %v, got %v", *tc.want, *got) + } + }) + } +} + +func boolp(b bool) *bool { return &b } diff --git a/pkg/resource/global_replication_group/identifiers.go b/pkg/resource/global_replication_group/identifiers.go new file mode 100644 index 00000000..b0965c26 --- /dev/null +++ b/pkg/resource/global_replication_group/identifiers.go @@ -0,0 +1,64 @@ +// 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 global_replication_group + +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 +} + +// Partition returns the AWS partition in which the reosurce exists, or +// nil if this information is not known. +func (ri *resourceIdentifiers) Partition() *ackv1alpha1.AWSPartition { + if ri.meta != nil { + return ri.meta.Partition + } + return nil +} diff --git a/pkg/resource/global_replication_group/manager.go b/pkg/resource/global_replication_group/manager.go new file mode 100644 index 00000000..44e3ffe8 --- /dev/null +++ b/pkg/resource/global_replication_group/manager.go @@ -0,0 +1,401 @@ +// 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 global_replication_group + +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/elasticache" + "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" + + svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1" +) + +var ( + _ = ackutil.InStrings + _ = acktags.NewTags() + _ = ackrt.MissingImageTagValue + _ = svcapitypes.GlobalReplicationGroup{} +) + +// +kubebuilder:rbac:groups=elasticache.services.k8s.aws,resources=globalreplicationgroups,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=elasticache.services.k8s.aws,resources=globalreplicationgroups/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 + // The AWS Partition that this resource manager targets + awsPartition ackv1alpha1.AWSPartition + // 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:%s:elasticache:%s:%s:%s", + rm.awsPartition, + 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") + } + + if r.ko.Status.Status == nil { + return false, nil + } + statusCandidates := []string{"available", "primary-only"} + if !ackutil.InStrings(*r.ko.Status.Status, statusCandidates) { + return false, nil + } + + 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 +} + +// FilterSystemTags removes system-managed tags from the resource's tag collection +// to prevent the controller from attempting to manage them. This includes: +// - Tags with keys starting with "aws:" (AWS-managed system tags) +// - Tags specified via the --resource-tags startup flag (controller-level tags) +// - Tags injected by AWS services (e.g., CloudFormation, EKS, etc.) +// +// This filtering is essential because: +// 1. AWS services automatically add system tags that cannot be modified by users +// 2. Attempting to remove these tags would result in API errors +// 3. The controller should only manage user-defined tags, not system tags +// +// Must be called after each Read operation to ensure the resource state +// reflects only manageable tags. This prevents unnecessary update attempts +// and maintains consistency between desired and actual resource state. +// +// Example system tags that are filtered: +// - aws:cloudformation:stack-name (CloudFormation) +// - aws:eks:cluster-name (EKS) +// - services.k8s.aws/* (Kubernetes-managed) +func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource, systemTags []string) { + +} + +// 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, + awsPartition: ackv1alpha1.AWSPartition(cfg.Partition), + 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/global_replication_group/manager_factory.go b/pkg/resource/global_replication_group/manager_factory.go new file mode 100644 index 00000000..bcdf549c --- /dev/null +++ b/pkg/resource/global_replication_group/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 global_replication_group + +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/elasticache-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/global_replication_group/references.go b/pkg/resource/global_replication_group/references.go new file mode 100644 index 00000000..cc689ea8 --- /dev/null +++ b/pkg/resource/global_replication_group/references.go @@ -0,0 +1,175 @@ +// 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 global_replication_group + +import ( + "context" + "fmt" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors" + ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime" + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" + + svcapitypes "github.com/aws-controllers-k8s/elasticache-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() + + if ko.Spec.PrimaryReplicationGroupRef != nil { + ko.Spec.PrimaryReplicationGroupID = nil + } + + 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) { + ko := rm.concreteResource(res).ko + + resourceHasReferences := false + err := validateReferenceFields(ko) + if fieldHasReferences, err := rm.resolveReferenceForPrimaryReplicationGroupID(ctx, apiReader, ko); err != nil { + return &resource{ko}, (resourceHasReferences || fieldHasReferences), err + } else { + resourceHasReferences = resourceHasReferences || fieldHasReferences + } + + return &resource{ko}, resourceHasReferences, err +} + +// validateReferenceFields validates the reference field and corresponding +// identifier field. +func validateReferenceFields(ko *svcapitypes.GlobalReplicationGroup) error { + + if ko.Spec.PrimaryReplicationGroupRef != nil && ko.Spec.PrimaryReplicationGroupID != nil { + return ackerr.ResourceReferenceAndIDNotSupportedFor("PrimaryReplicationGroupID", "PrimaryReplicationGroupRef") + } + if ko.Spec.PrimaryReplicationGroupRef == nil && ko.Spec.PrimaryReplicationGroupID == nil { + return ackerr.ResourceReferenceOrIDRequiredFor("PrimaryReplicationGroupID", "PrimaryReplicationGroupRef") + } + return nil +} + +// resolveReferenceForPrimaryReplicationGroupID reads the resource referenced +// from PrimaryReplicationGroupRef field and sets the PrimaryReplicationGroupID +// from referenced resource. Returns a boolean indicating whether a reference +// contains references, or an error +func (rm *resourceManager) resolveReferenceForPrimaryReplicationGroupID( + ctx context.Context, + apiReader client.Reader, + ko *svcapitypes.GlobalReplicationGroup, +) (hasReferences bool, err error) { + if ko.Spec.PrimaryReplicationGroupRef != nil && ko.Spec.PrimaryReplicationGroupRef.From != nil { + hasReferences = true + arr := ko.Spec.PrimaryReplicationGroupRef.From + if arr.Name == nil || *arr.Name == "" { + return hasReferences, fmt.Errorf("provided resource reference is nil or empty: PrimaryReplicationGroupRef") + } + namespace, err := ackrt.ResolveCrossNamespaceReference( + ctx, + rm.cfg.EnableCrossNamespace, + &ko.Status.Conditions, + ackrt.CrossNamespaceRefKindResource, + ko.ObjectMeta.GetNamespace(), + arr.Namespace, + *arr.Name, + ) + if err != nil { + return hasReferences, err + } + obj := &svcapitypes.ReplicationGroup{} + if err := getReferencedResourceState_ReplicationGroup(ctx, apiReader, obj, *arr.Name, namespace); err != nil { + return hasReferences, err + } + ko.Spec.PrimaryReplicationGroupID = (*string)(obj.Spec.ReplicationGroupID) + } + + return hasReferences, nil +} + +// getReferencedResourceState_ReplicationGroup looks up whether a referenced resource +// exists and is in a ACK.ResourceSynced=True state. If the referenced resource does exist and is +// in a Synced state, returns nil, otherwise returns `ackerr.ResourceReferenceTerminalFor` or +// `ResourceReferenceNotSyncedFor` depending on if the resource is in a Terminal state. +func getReferencedResourceState_ReplicationGroup( + ctx context.Context, + apiReader client.Reader, + obj *svcapitypes.ReplicationGroup, + name string, // the Kubernetes name of the referenced resource + namespace string, // the Kubernetes namespace of the referenced resource +) error { + namespacedName := types.NamespacedName{ + Namespace: namespace, + Name: name, + } + err := apiReader.Get(ctx, namespacedName, obj) + if err != nil { + return err + } + var refResourceTerminal bool + for _, cond := range obj.Status.Conditions { + if cond.Type == ackv1alpha1.ConditionTypeTerminal && + cond.Status == corev1.ConditionTrue { + return ackerr.ResourceReferenceTerminalFor( + "ReplicationGroup", + namespace, name) + } + } + if refResourceTerminal { + return ackerr.ResourceReferenceTerminalFor( + "ReplicationGroup", + namespace, name) + } + var refResourceSynced bool + for _, cond := range obj.Status.Conditions { + if cond.Type == ackv1alpha1.ConditionTypeResourceSynced && + cond.Status == corev1.ConditionTrue { + refResourceSynced = true + } + } + if !refResourceSynced { + return ackerr.ResourceReferenceNotSyncedFor( + "ReplicationGroup", + namespace, name) + } + if obj.Spec.ReplicationGroupID == nil { + return ackerr.ResourceReferenceMissingTargetFieldFor( + "ReplicationGroup", + namespace, name, + "Spec.ReplicationGroupID") + } + return nil +} diff --git a/pkg/resource/global_replication_group/resource.go b/pkg/resource/global_replication_group/resource.go new file mode 100644 index 00000000..a62888b0 --- /dev/null +++ b/pkg/resource/global_replication_group/resource.go @@ -0,0 +1,113 @@ +// 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 global_replication_group + +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" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + rtclient "sigs.k8s.io/controller-runtime/pkg/client" + + svcapitypes "github.com/aws-controllers-k8s/elasticache-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.GlobalReplicationGroup +} + +// 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.Status.GlobalReplicationGroupID = &identifier.NameOrID + + return nil +} + +// PopulateResourceFromAnnotation populates the fields passed from adoption annotation +func (r *resource) PopulateResourceFromAnnotation(fields map[string]string) error { + f0, ok := fields["globalReplicationGroupID"] + if !ok { + return ackerrors.NewTerminalError(fmt.Errorf("required field missing: globalReplicationGroupID")) + } + r.ko.Status.GlobalReplicationGroupID = &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/global_replication_group/sdk.go b/pkg/resource/global_replication_group/sdk.go new file mode 100644 index 00000000..1fafd08c --- /dev/null +++ b/pkg/resource/global_replication_group/sdk.go @@ -0,0 +1,553 @@ +// 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 global_replication_group + +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/elasticache" + 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/elasticache-controller/apis/v1alpha1" +) + +// Hack to avoid import errors during build... +var ( + _ = &metav1.Time{} + _ = strings.ToLower("") + _ = &svcsdk.Client{} + _ = &svcapitypes.GlobalReplicationGroup{} + _ = 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.requiredFieldsMissingFromReadManyInput(r) { + return nil, ackerr.NotFound + } + + input, err := rm.newListRequestPayload(r) + if err != nil { + return nil, err + } + var resp *svcsdk.DescribeGlobalReplicationGroupsOutput + resp, err = rm.sdkapi.DescribeGlobalReplicationGroups(ctx, input) + rm.metrics.RecordAPICall("READ_MANY", "DescribeGlobalReplicationGroups", err) + if err != nil { + var awsErr smithy.APIError + if errors.As(err, &awsErr) && awsErr.ErrorCode() == "GlobalReplicationGroupNotFoundFault" { + 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() + + found := false + for _, elem := range resp.GlobalReplicationGroups { + if elem.ARN != nil { + if ko.Status.ACKResourceMetadata == nil { + ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{} + } + tmpARN := ackv1alpha1.AWSResourceName(*elem.ARN) + ko.Status.ACKResourceMetadata.ARN = &tmpARN + } + if elem.AtRestEncryptionEnabled != nil { + ko.Status.AtRestEncryptionEnabled = elem.AtRestEncryptionEnabled + } else { + ko.Status.AtRestEncryptionEnabled = nil + } + if elem.AuthTokenEnabled != nil { + ko.Status.AuthTokenEnabled = elem.AuthTokenEnabled + } else { + ko.Status.AuthTokenEnabled = nil + } + if elem.CacheNodeType != nil { + ko.Spec.CacheNodeType = elem.CacheNodeType + } else { + ko.Spec.CacheNodeType = nil + } + if elem.ClusterEnabled != nil { + ko.Status.ClusterEnabled = elem.ClusterEnabled + } else { + ko.Status.ClusterEnabled = nil + } + if elem.Engine != nil { + ko.Spec.Engine = elem.Engine + } else { + ko.Spec.Engine = nil + } + if elem.EngineVersion != nil { + ko.Spec.EngineVersion = elem.EngineVersion + } else { + ko.Spec.EngineVersion = nil + } + if elem.GlobalNodeGroups != nil { + f7 := []*svcapitypes.GlobalNodeGroup{} + for _, f7iter := range elem.GlobalNodeGroups { + f7elem := &svcapitypes.GlobalNodeGroup{} + if f7iter.GlobalNodeGroupId != nil { + f7elem.GlobalNodeGroupID = f7iter.GlobalNodeGroupId + } + if f7iter.Slots != nil { + f7elem.Slots = f7iter.Slots + } + f7 = append(f7, f7elem) + } + ko.Status.GlobalNodeGroups = f7 + } else { + ko.Status.GlobalNodeGroups = nil + } + if elem.GlobalReplicationGroupDescription != nil { + ko.Spec.Description = elem.GlobalReplicationGroupDescription + } else { + ko.Spec.Description = nil + } + if elem.GlobalReplicationGroupId != nil { + ko.Status.GlobalReplicationGroupID = elem.GlobalReplicationGroupId + } else { + ko.Status.GlobalReplicationGroupID = nil + } + if elem.Members != nil { + f10 := []*svcapitypes.GlobalReplicationGroupMember{} + for _, f10iter := range elem.Members { + f10elem := &svcapitypes.GlobalReplicationGroupMember{} + if f10iter.AutomaticFailover != "" { + f10elem.AutomaticFailover = aws.String(string(f10iter.AutomaticFailover)) + } + if f10iter.ReplicationGroupId != nil { + f10elem.ReplicationGroupID = f10iter.ReplicationGroupId + } + if f10iter.ReplicationGroupRegion != nil { + f10elem.ReplicationGroupRegion = f10iter.ReplicationGroupRegion + } + if f10iter.Role != nil { + f10elem.Role = f10iter.Role + } + if f10iter.Status != nil { + f10elem.Status = f10iter.Status + } + f10 = append(f10, f10elem) + } + ko.Status.Members = f10 + } else { + ko.Status.Members = nil + } + if elem.Status != nil { + ko.Status.Status = elem.Status + } else { + ko.Status.Status = nil + } + if elem.TransitEncryptionEnabled != nil { + ko.Status.TransitEncryptionEnabled = elem.TransitEncryptionEnabled + } else { + ko.Status.TransitEncryptionEnabled = nil + } + found = true + break + } + if !found { + return nil, ackerr.NotFound + } + + rm.setStatusDefaults(ko) + // custom set output from response + ko, err = rm.CustomDescribeGlobalReplicationGroupsSetOutput(ctx, r, resp, ko) + if err != nil { + return nil, err + } + return &resource{ko}, nil +} + +// requiredFieldsMissingFromReadManyInput returns true if there are any fields +// for the ReadMany Input shape that are required but not present in the +// resource's Spec or Status +func (rm *resourceManager) requiredFieldsMissingFromReadManyInput( + r *resource, +) bool { + return r.ko.Status.GlobalReplicationGroupID == nil + +} + +// newListRequestPayload returns SDK-specific struct for the HTTP request +// payload of the List API call for the resource +func (rm *resourceManager) newListRequestPayload( + r *resource, +) (*svcsdk.DescribeGlobalReplicationGroupsInput, error) { + res := &svcsdk.DescribeGlobalReplicationGroupsInput{} + + if r.ko.Status.GlobalReplicationGroupID != nil { + res.GlobalReplicationGroupId = r.ko.Status.GlobalReplicationGroupID + } + res.ShowMemberInfo = aws.Bool(true) + + 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.CreateGlobalReplicationGroupOutput + _ = resp + resp, err = rm.sdkapi.CreateGlobalReplicationGroup(ctx, input) + rm.metrics.RecordAPICall("CREATE", "CreateGlobalReplicationGroup", 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 ko.Status.ACKResourceMetadata == nil { + ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{} + } + if resp.GlobalReplicationGroup.ARN != nil { + arn := ackv1alpha1.AWSResourceName(*resp.GlobalReplicationGroup.ARN) + ko.Status.ACKResourceMetadata.ARN = &arn + } + if resp.GlobalReplicationGroup.AtRestEncryptionEnabled != nil { + ko.Status.AtRestEncryptionEnabled = resp.GlobalReplicationGroup.AtRestEncryptionEnabled + } else { + ko.Status.AtRestEncryptionEnabled = nil + } + if resp.GlobalReplicationGroup.AuthTokenEnabled != nil { + ko.Status.AuthTokenEnabled = resp.GlobalReplicationGroup.AuthTokenEnabled + } else { + ko.Status.AuthTokenEnabled = nil + } + if resp.GlobalReplicationGroup.CacheNodeType != nil { + ko.Spec.CacheNodeType = resp.GlobalReplicationGroup.CacheNodeType + } else { + ko.Spec.CacheNodeType = nil + } + if resp.GlobalReplicationGroup.ClusterEnabled != nil { + ko.Status.ClusterEnabled = resp.GlobalReplicationGroup.ClusterEnabled + } else { + ko.Status.ClusterEnabled = nil + } + if resp.GlobalReplicationGroup.Engine != nil { + ko.Spec.Engine = resp.GlobalReplicationGroup.Engine + } else { + ko.Spec.Engine = nil + } + if resp.GlobalReplicationGroup.EngineVersion != nil { + ko.Spec.EngineVersion = resp.GlobalReplicationGroup.EngineVersion + } else { + ko.Spec.EngineVersion = nil + } + if resp.GlobalReplicationGroup.GlobalNodeGroups != nil { + f7 := []*svcapitypes.GlobalNodeGroup{} + for _, f7iter := range resp.GlobalReplicationGroup.GlobalNodeGroups { + f7elem := &svcapitypes.GlobalNodeGroup{} + if f7iter.GlobalNodeGroupId != nil { + f7elem.GlobalNodeGroupID = f7iter.GlobalNodeGroupId + } + if f7iter.Slots != nil { + f7elem.Slots = f7iter.Slots + } + f7 = append(f7, f7elem) + } + ko.Status.GlobalNodeGroups = f7 + } else { + ko.Status.GlobalNodeGroups = nil + } + if resp.GlobalReplicationGroup.GlobalReplicationGroupDescription != nil { + ko.Spec.Description = resp.GlobalReplicationGroup.GlobalReplicationGroupDescription + } else { + ko.Spec.Description = nil + } + if resp.GlobalReplicationGroup.GlobalReplicationGroupId != nil { + ko.Status.GlobalReplicationGroupID = resp.GlobalReplicationGroup.GlobalReplicationGroupId + } else { + ko.Status.GlobalReplicationGroupID = nil + } + if resp.GlobalReplicationGroup.Members != nil { + f10 := []*svcapitypes.GlobalReplicationGroupMember{} + for _, f10iter := range resp.GlobalReplicationGroup.Members { + f10elem := &svcapitypes.GlobalReplicationGroupMember{} + if f10iter.AutomaticFailover != "" { + f10elem.AutomaticFailover = aws.String(string(f10iter.AutomaticFailover)) + } + if f10iter.ReplicationGroupId != nil { + f10elem.ReplicationGroupID = f10iter.ReplicationGroupId + } + if f10iter.ReplicationGroupRegion != nil { + f10elem.ReplicationGroupRegion = f10iter.ReplicationGroupRegion + } + if f10iter.Role != nil { + f10elem.Role = f10iter.Role + } + if f10iter.Status != nil { + f10elem.Status = f10iter.Status + } + f10 = append(f10, f10elem) + } + ko.Status.Members = f10 + } else { + ko.Status.Members = nil + } + if resp.GlobalReplicationGroup.Status != nil { + ko.Status.Status = resp.GlobalReplicationGroup.Status + } else { + ko.Status.Status = nil + } + if resp.GlobalReplicationGroup.TransitEncryptionEnabled != nil { + ko.Status.TransitEncryptionEnabled = resp.GlobalReplicationGroup.TransitEncryptionEnabled + } else { + ko.Status.TransitEncryptionEnabled = nil + } + + rm.setStatusDefaults(ko) + // After a successful Create call, capture the full GlobalReplicationGroupId + // (which includes the auto-generated region prefix) into status. + // The user only provides the suffix in spec; this is the canonical ID for all + // subsequent API calls. + if resp.GlobalReplicationGroup != nil && resp.GlobalReplicationGroup.GlobalReplicationGroupId != nil { + ko.Status.GlobalReplicationGroupID = resp.GlobalReplicationGroup.GlobalReplicationGroupId + } + if resp.GlobalReplicationGroup != nil && resp.GlobalReplicationGroup.Status != nil { + ko.Status.Status = resp.GlobalReplicationGroup.Status + } + + 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.CreateGlobalReplicationGroupInput, error) { + res := &svcsdk.CreateGlobalReplicationGroupInput{} + + if r.ko.Spec.Description != nil { + res.GlobalReplicationGroupDescription = r.ko.Spec.Description + } + if r.ko.Spec.GlobalReplicationGroupIDSuffix != nil { + res.GlobalReplicationGroupIdSuffix = r.ko.Spec.GlobalReplicationGroupIDSuffix + } + if r.ko.Spec.PrimaryReplicationGroupID != nil { + res.PrimaryReplicationGroupId = r.ko.Spec.PrimaryReplicationGroupID + } + + 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 rm.customUpdateGlobalReplicationGroup(ctx, desired, latest, delta) +} + +// 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) + }() + return rm.customDeleteGlobalReplicationGroup(ctx, r) +} + +// 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.DeleteGlobalReplicationGroupInput, error) { + res := &svcsdk.DeleteGlobalReplicationGroupInput{} + + if r.ko.Status.GlobalReplicationGroupID != nil { + res.GlobalReplicationGroupId = r.ko.Status.GlobalReplicationGroupID + } + + return res, nil +} + +// setStatusDefaults sets default properties into supplied custom resource +func (rm *resourceManager) setStatusDefaults( + ko *svcapitypes.GlobalReplicationGroup, +) { + 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.Partition == nil { + ko.Status.ACKResourceMetadata.Partition = &rm.awsPartition + } + 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 "GlobalReplicationGroupAlreadyExistsFault", + "InvalidParameterValue", + "InvalidParameterCombination", + "ServiceLinkedRoleNotFoundFault", + "ReplicationGroupNotFoundFault": + return true + default: + return false + } +} diff --git a/pkg/version/version.go b/pkg/version/version.go index de4ced57..587d76ab 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -27,6 +27,6 @@ var ( // that generated this controller's code. They are baked in at code-generation // time and are immutable for the life of the generated code. const ( - ACKGenerateVersion = "v0.62.1" - ACKGenerateBuildDate = "2026-08-13T00:57:17Z" + ACKGenerateVersion = "v0.62.1-4-gd4aaca5" + ACKGenerateBuildDate = "2026-08-27T20:01:00Z" ) diff --git a/templates/hooks/global_replication_group/sdk_create_post_set_output.go.tpl b/templates/hooks/global_replication_group/sdk_create_post_set_output.go.tpl new file mode 100644 index 00000000..0a5108c7 --- /dev/null +++ b/templates/hooks/global_replication_group/sdk_create_post_set_output.go.tpl @@ -0,0 +1,10 @@ +// After a successful Create call, capture the full GlobalReplicationGroupId +// (which includes the auto-generated region prefix) into status. +// The user only provides the suffix in spec; this is the canonical ID for all +// subsequent API calls. + if resp.GlobalReplicationGroup != nil && resp.GlobalReplicationGroup.GlobalReplicationGroupId != nil { + ko.Status.GlobalReplicationGroupID = resp.GlobalReplicationGroup.GlobalReplicationGroupId + } + if resp.GlobalReplicationGroup != nil && resp.GlobalReplicationGroup.Status != nil { + ko.Status.Status = resp.GlobalReplicationGroup.Status + } diff --git a/test/e2e/resources/global_replication_group_basic.yaml b/test/e2e/resources/global_replication_group_basic.yaml new file mode 100644 index 00000000..8eb87b63 --- /dev/null +++ b/test/e2e/resources/global_replication_group_basic.yaml @@ -0,0 +1,9 @@ +# Basic GlobalReplicationGroup for create/delete test +apiVersion: elasticache.services.k8s.aws/v1alpha1 +kind: GlobalReplicationGroup +metadata: + name: $GRG_NAME +spec: + globalReplicationGroupIDSuffix: $GRG_SUFFIX + primaryReplicationGroupID: $PRIMARY_RG_ID + description: $DESCRIPTION diff --git a/test/e2e/resources/global_replication_group_scale.yaml b/test/e2e/resources/global_replication_group_scale.yaml new file mode 100644 index 00000000..ac3dc60d --- /dev/null +++ b/test/e2e/resources/global_replication_group_scale.yaml @@ -0,0 +1,10 @@ +# GlobalReplicationGroup for modify/scale tests +apiVersion: elasticache.services.k8s.aws/v1alpha1 +kind: GlobalReplicationGroup +metadata: + name: $GRG_NAME +spec: + globalReplicationGroupIDSuffix: $GRG_SUFFIX + primaryReplicationGroupID: $PRIMARY_RG_ID + description: $DESCRIPTION + nodeGroupCount: $NODE_GROUP_COUNT diff --git a/test/e2e/resources/replicationgroup_create_delete_grg.yaml b/test/e2e/resources/replicationgroup_create_delete_grg.yaml new file mode 100644 index 00000000..e53a741c --- /dev/null +++ b/test/e2e/resources/replicationgroup_create_delete_grg.yaml @@ -0,0 +1,22 @@ +# Primary replication group for GlobalReplicationGroup E2E tests. +# +# Global Datastore requires large-and-above M/R/C-family node types (T-family +# and anything smaller than "large" is rejected by CreateGlobalReplicationGroup) +# and automatic failover enabled on the primary. See: +# https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/Redis-Global-Datastores-Getting-Started.html +# +# Forked from replicationgroup_create_delete.yaml so these GRG-specific +# requirements don't change what test_replicationgroup.py provisions. +apiVersion: elasticache.services.k8s.aws/v1alpha1 +kind: ReplicationGroup +metadata: + name: $RG_ID +spec: + cacheNodeType: cache.r6g.large + engine: redis + engineVersion: "$ENGINE_VERSION" + numNodeGroups: $NUM_NODE_GROUPS + replicasPerNodeGroup: $REPLICAS_PER_NODE_GROUP + automaticFailoverEnabled: true + description: cluster-mode disabled RG + replicationGroupID: $RG_ID diff --git a/test/e2e/tests/test_global_replication_group.py b/test/e2e/tests/test_global_replication_group.py new file mode 100644 index 00000000..b0a8c44a --- /dev/null +++ b/test/e2e/tests/test_global_replication_group.py @@ -0,0 +1,766 @@ +# 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 ElastiCache GlobalReplicationGroup resource +""" + +import pytest +import boto3 +import logging +from time import sleep + +from acktest.resources import random_suffix_name +from acktest.k8s import resource as k8s +from acktest.k8s import condition +from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_elasticache_resource + +RESOURCE_PLURAL_GRG = "globalreplicationgroups" +RESOURCE_PLURAL_RG = "replicationgroups" + +# Timeouts (in seconds) +CREATE_WAIT_PERIODS = 30 +CREATE_PERIOD_LENGTH = 30 # 30 * 30s = 15 min max +MODIFY_WAIT_PERIODS = 30 +MODIFY_PERIOD_LENGTH = 30 +DELETE_WAIT_PERIODS = 40 +DELETE_PERIOD_LENGTH = 30 # 40 * 30s = 20 min max +# The prerequisite primary ReplicationGroup can take considerably longer than a +# plain 15-min create budget to reach 'available' when the CI suite runs all +# resources in parallel (many ElastiCache resources provisioning at once, plus +# the controller pod restarting on its periodic credential rotation, both of +# which delay when the group settles). Observed ~13 min in one PR #228 CI run, +# landing right at the old 15-min ceiling and flaking the module fixture. This +# wait returns as soon as the group is available, so a generous ceiling costs +# nothing on the happy path and only absorbs that tail. +RG_AVAILABLE_WAIT_PERIODS = 40 +RG_AVAILABLE_PERIOD_LENGTH = 30 # 40 * 30s = 20 min max +# Node group scaling is by far the slowest GRG operation: ElastiCache migrates +# slots between shards and holds the GRG in 'modifying' the whole time. On a +# decrease the shard count only drops at the very end. Measured ~27 min for a +# 3->2 decrease against live AWS, so this budget is deliberately generous -- +# the wait returns as soon as the target state is reached, so a large ceiling +# costs nothing on the happy path and only prevents flakes. +SCALE_WAIT_PERIODS = 80 +SCALE_PERIOD_LENGTH = 30 # 80 * 30s = 40 min max + +ec = boto3.client("elasticache") + + +# ============================================================================= +# HELPERS +# ============================================================================= + +def wait_k8s_resource_global_id(reference, wait_periods=10, period_length=10) -> str: + """Poll until .status.globalReplicationGroupID is populated.""" + for _ in range(wait_periods): + global_id = get_k8s_resource_global_id(reference) + if global_id: + return global_id + sleep(period_length) + return None + + +def wait_global_replication_group_status( + global_rg_id: str, + target_status: str, + wait_periods: int = CREATE_WAIT_PERIODS, + period_length: int = CREATE_PERIOD_LENGTH, +) -> bool: + """Wait until the GlobalReplicationGroup reaches the target status.""" + for i in range(wait_periods): + logging.debug( + f"Waiting for GlobalReplicationGroup {global_rg_id} " + f"to reach '{target_status}' ({i}/{wait_periods})" + ) + try: + response = ec.describe_global_replication_groups( + GlobalReplicationGroupId=global_rg_id, + ShowMemberInfo=True, + ) + if len(response["GlobalReplicationGroups"]) == 0: + if target_status == "deleted": + return True + logging.warning(f"GlobalReplicationGroup {global_rg_id} not found") + return False + + grg = response["GlobalReplicationGroups"][0] + current_status = grg.get("Status", "unknown") + logging.debug(f" Current status: {current_status}") + + if current_status == target_status: + logging.info( + f"GlobalReplicationGroup {global_rg_id} reached " + f"'{target_status}', continuing..." + ) + return True + + except ec.exceptions.GlobalReplicationGroupNotFoundFault: + if target_status == "deleted": + return True + logging.warning(f"GlobalReplicationGroup {global_rg_id} not found (404)") + return False + + sleep(period_length) + + logging.error( + f"Timed out waiting for GlobalReplicationGroup {global_rg_id} " + f"to reach '{target_status}'" + ) + return False + + +def wait_replication_group_available(rg_id: str) -> bool: + """Wait until a ReplicationGroup is available (needed as GRG prerequisite).""" + for i in range(RG_AVAILABLE_WAIT_PERIODS): + logging.debug(f"Waiting for ReplicationGroup {rg_id} to be available ({i})") + try: + response = ec.describe_replication_groups(ReplicationGroupId=rg_id) + rg = response["ReplicationGroups"][0] + if rg["Status"] == "available": + logging.info(f"ReplicationGroup {rg_id} is available") + return True + except Exception as e: + logging.warning(f"Error checking RG status: {e}") + sleep(RG_AVAILABLE_PERIOD_LENGTH) + return False + + +def get_global_replication_group(global_rg_id: str): + """Describe a GlobalReplicationGroup from the AWS API directly.""" + try: + response = ec.describe_global_replication_groups( + GlobalReplicationGroupId=global_rg_id, + ShowMemberInfo=True, + ) + if len(response["GlobalReplicationGroups"]) > 0: + return response["GlobalReplicationGroups"][0] + except ec.exceptions.GlobalReplicationGroupNotFoundFault: + pass + return None + + +def wait_global_node_group_count( + global_rg_id: str, + expected_count: int, + wait_periods: int = SCALE_WAIT_PERIODS, + period_length: int = SCALE_PERIOD_LENGTH, +) -> bool: + """Wait until the GRG has `expected_count` node groups and has settled. + + Scaling is complete only when both the shard count matches AND the status + has returned to a steady state. Polling on status alone is racy: the GRG + stays in its pre-patch steady state for a few seconds after the spec is + patched, before the controller issues the scale call and AWS flips the + status to 'modifying' -- so a status-only wait can return immediately and + spuriously succeed. + """ + for i in range(wait_periods): + grg = get_global_replication_group(global_rg_id) + if grg is not None: + count = len(grg.get("GlobalNodeGroups") or []) + status = grg.get("Status") + logging.debug( + f"Waiting for GlobalReplicationGroup {global_rg_id} to have " + f"{expected_count} node groups and settle: " + f"count={count} status={status} ({i}/{wait_periods})" + ) + if count == expected_count and status in ("primary-only", "available"): + return True + sleep(period_length) + return False + + +def get_k8s_resource_status(reference) -> str: + """Get the .status.status field from the K8s resource.""" + resource = k8s.get_resource(reference) + if resource is None: + return None + return resource.get("status", {}).get("status") + + +def get_k8s_resource_global_id(reference) -> str: + """Get the .status.globalReplicationGroupID from the K8s resource.""" + resource = k8s.get_resource(reference) + if resource is None: + return None + return resource.get("status", {}).get("globalReplicationGroupID") + + +def create_grg( + suffix: str, + primary_rg_id: str, + description: str, + resource_template: str = "global_replication_group_basic", + target_status: str = "primary-only", + extra_replacements: dict = None, +): + """Create a GlobalReplicationGroup and wait until it reaches target_status. + + Returns (reference, global_id). + """ + grg_name = f"grg-{suffix}" + replacements = { + "GRG_NAME": grg_name, + "GRG_SUFFIX": suffix, + "PRIMARY_RG_ID": primary_rg_id, + "DESCRIPTION": description, + } + if extra_replacements: + replacements.update(extra_replacements) + + grg_resource = load_elasticache_resource( + resource_template, additional_replacements=replacements + ) + + reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL_GRG, grg_name, namespace="default" + ) + _ = k8s.create_custom_resource(reference, grg_resource) + resource = k8s.wait_resource_consumed_by_controller( + reference, wait_periods=15, period_length=20 + ) + assert resource is not None + + global_id = wait_k8s_resource_global_id(reference) + assert global_id is not None, "globalReplicationGroupID not populated in status" + + assert wait_global_replication_group_status( + global_id, target_status, + wait_periods=CREATE_WAIT_PERIODS, + period_length=CREATE_PERIOD_LENGTH, + ), f"GRG did not reach '{target_status}'" + + return reference, global_id + + +def delete_grg(reference, global_id: str): + """Delete a GlobalReplicationGroup and wait for it to be gone.""" + k8s.delete_custom_resource(reference) + wait_global_replication_group_status( + global_id, "deleted", + wait_periods=DELETE_WAIT_PERIODS, + period_length=DELETE_PERIOD_LENGTH, + ) + + +def wait_global_description( + global_rg_id: str, + expected_description: str, + wait_periods: int = MODIFY_WAIT_PERIODS, + period_length: int = MODIFY_PERIOD_LENGTH, +) -> bool: + """Wait until the GRG description matches and the group has settled. + + Polling on status alone is racy: the GRG stays in its pre-patch steady + state for a few seconds after the spec is patched, before the controller + issues the modify call and AWS flips the status to 'modifying'. + """ + for i in range(wait_periods): + grg = get_global_replication_group(global_rg_id) + if grg is not None: + desc = grg.get("GlobalReplicationGroupDescription") + status = grg.get("Status") + logging.debug( + f"Waiting for GlobalReplicationGroup {global_rg_id} description " + f"'{expected_description}': desc='{desc}' status={status} " + f"({i}/{wait_periods})" + ) + if desc == expected_description and status in ("primary-only", "available"): + return True + sleep(period_length) + return False + + +def wait_global_automatic_failover( + global_rg_id: str, + expected_enabled: bool, + wait_periods: int = MODIFY_WAIT_PERIODS, + period_length: int = MODIFY_PERIOD_LENGTH, +) -> bool: + """Wait until the primary member's automatic failover matches and the + group has settled. + + AutomaticFailoverEnabled isn't a top-level GRG attribute in the Describe + response -- it's read off the primary member's `AutomaticFailover` field + (enabled/disabled/enabling/disabling). Matches terraform-provider-aws's + flattenGlobalReplicationGroupAutomaticFailoverEnabled. + """ + target = {"enabled", "enabling"} if expected_enabled else {"disabled", "disabling"} + for i in range(wait_periods): + grg = get_global_replication_group(global_rg_id) + if grg is not None: + status = grg.get("Status") + primary_failover = None + for member in grg.get("Members") or []: + if member.get("Role") == "PRIMARY": + primary_failover = member.get("AutomaticFailover") + break + logging.debug( + f"Waiting for GlobalReplicationGroup {global_rg_id} automatic " + f"failover enabled={expected_enabled}: primary_failover=" + f"{primary_failover} status={status} ({i}/{wait_periods})" + ) + if primary_failover in target and status in ("primary-only", "available"): + return True + sleep(period_length) + return False + + +def wait_global_node_type( + global_rg_id: str, + expected_node_type: str, + wait_periods: int = SCALE_WAIT_PERIODS, + period_length: int = SCALE_PERIOD_LENGTH, +) -> bool: + """Wait until the GRG's node type matches and the group has settled. + + Changing the node type scales every member and holds the GRG in + 'modifying' for the duration, so this uses the generous scaling budget. + Requiring a steady status (not just the target node type) avoids the same + race as the other waiters, where the GRG briefly reports its pre-patch + state before the controller issues the modify. + """ + for i in range(wait_periods): + grg = get_global_replication_group(global_rg_id) + if grg is not None: + nt = grg.get("CacheNodeType") + status = grg.get("Status") + logging.debug( + f"Waiting for GlobalReplicationGroup {global_rg_id} node type " + f"'{expected_node_type}': nodeType='{nt}' status={status} " + f"({i}/{wait_periods})" + ) + if nt == expected_node_type and status in ("primary-only", "available"): + return True + sleep(period_length) + return False + + +# ============================================================================= +# FIXTURES +# ============================================================================= + +@pytest.fixture(scope="module") +def primary_replication_group(): + """Create a ReplicationGroup to serve as the primary for GlobalReplicationGroup tests. + + This is created once per test module and cleaned up at the end. + """ + rg_id = random_suffix_name("ack-e2e-grg-primary", 32) + + rg_resource = load_elasticache_resource( + "replicationgroup_create_delete_grg", + additional_replacements={ + "RG_ID": rg_id, + "ENGINE_VERSION": "7.0", + "NUM_NODE_GROUPS": "1", + "REPLICAS_PER_NODE_GROUP": "1", + }, + ) + + reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL_RG, rg_id, namespace="default" + ) + _ = k8s.create_custom_resource(reference, rg_resource) + resource = k8s.wait_resource_consumed_by_controller( + reference, wait_periods=15, period_length=20 + ) + assert resource is not None + + # Wait for the RG to be available on the AWS side + assert wait_replication_group_available(rg_id), ( + f"Primary ReplicationGroup {rg_id} did not become available" + ) + + yield rg_id + + # Cleanup: delete the primary RG + k8s.delete_custom_resource(reference) + # Wait for it to be deleted + try: + waiter = ec.get_waiter("replication_group_deleted") + waiter.wait( + ReplicationGroupId=rg_id, + WaiterConfig={"Delay": 30, "MaxAttempts": 40}, + ) + except Exception: + pass + + +# ============================================================================= +# TESTS +# ============================================================================= + +@service_marker +class TestGlobalReplicationGroupBasicLifecycle: + """Test basic create → describe → delete lifecycle.""" + + def test_create_and_verify(self, primary_replication_group): + """Create a GlobalReplicationGroup and verify it reaches 'primary-only'.""" + rg_id = primary_replication_group + grg_suffix = random_suffix_name("ack-e2e-grg", 20) + grg_name = f"grg-{grg_suffix}" + + grg_resource = load_elasticache_resource( + "global_replication_group_basic", + additional_replacements={ + "GRG_NAME": grg_name, + "GRG_SUFFIX": grg_suffix, + "PRIMARY_RG_ID": rg_id, + "DESCRIPTION": "ACK E2E test - basic lifecycle", + }, + ) + + reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL_GRG, grg_name, namespace="default" + ) + + # Create the resource + _ = k8s.create_custom_resource(reference, grg_resource) + resource = k8s.wait_resource_consumed_by_controller( + reference, wait_periods=15, period_length=20 + ) + assert resource is not None + + # Get the full global ID from status (has region prefix) + global_id = wait_k8s_resource_global_id(reference) + assert global_id is not None, "globalReplicationGroupID not populated in status" + assert grg_suffix in global_id, ( + f"Expected suffix '{grg_suffix}' in full ID '{global_id}'" + ) + assert len(global_id) > len(grg_suffix), ( + "Full ID should be longer than suffix (has region prefix)" + ) + + # Wait for primary-only on the AWS side (no secondaries = primary-only, not available) + assert wait_global_replication_group_status(global_id, "primary-only"), ( + f"GlobalReplicationGroup {global_id} did not become primary-only" + ) + + # Wait for the controller's own reconcile loop to catch up and mark the + # resource Synced. IsSynced() only returns true once status.status is + # 'available'/'primary-only', so this guarantees the status field below + # already reflects the target state -- checking status.status directly + # without this wait is racy, since the AWS-side wait above can observe + # the transition slightly ahead of the controller's next poll cycle. + assert k8s.wait_on_condition( + reference, condition.CONDITION_TYPE_RESOURCE_SYNCED, "True", + wait_periods=10, period_length=15, + ), "resource did not reach ACK.ResourceSynced=True after create" + + # Verify K8s resource status + assert get_k8s_resource_status(reference) == "primary-only" + + # Verify AWS-side resource exists and matches + grg = get_global_replication_group(global_id) + assert grg is not None + assert grg["Status"] == "primary-only" + assert len(grg.get("Members", [])) >= 1 # At least the primary + + # --- DELETE --- + k8s.delete_custom_resource(reference) + + # The delete is multi-step: disassociate secondaries (none here), then delete + assert wait_global_replication_group_status( + global_id, "deleted", + wait_periods=DELETE_WAIT_PERIODS, + period_length=DELETE_PERIOD_LENGTH, + ), f"GlobalReplicationGroup {global_id} was not deleted" + + # Verify the primary RG still exists (was retained) + assert wait_replication_group_available(rg_id), ( + f"Primary ReplicationGroup {rg_id} should still exist after GRG deletion" + ) + + +@service_marker +class TestGlobalReplicationGroupInvalidPrimary: + """Test that creating with a nonexistent primary produces a terminal condition.""" + + def test_invalid_primary_terminal(self): + grg_suffix = random_suffix_name("ack-e2e-bad", 20) + grg_name = f"grg-bad-{grg_suffix}" + + grg_resource = load_elasticache_resource( + "global_replication_group_basic", + additional_replacements={ + "GRG_NAME": grg_name, + "GRG_SUFFIX": grg_suffix, + "PRIMARY_RG_ID": "nonexistent-rg-that-does-not-exist-12345", + "DESCRIPTION": "ACK E2E test - should fail", + }, + ) + + reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL_GRG, grg_name, namespace="default" + ) + + _ = k8s.create_custom_resource(reference, grg_resource) + resource = k8s.wait_resource_consumed_by_controller( + reference, wait_periods=10, period_length=15 + ) + assert resource is not None + + # An invalid primary must drive the resource terminal + assert k8s.wait_on_condition( + reference, condition.CONDITION_TYPE_TERMINAL, "True", + wait_periods=12, period_length=10, + ), "resource did not reach ACK.Terminal=True for an invalid primary" + + # Cleanup + k8s.delete_custom_resource(reference) + + +@service_marker +class TestGlobalReplicationGroupModify: + """Test modifying a GlobalReplicationGroup (description change).""" + + def test_modify_description(self, primary_replication_group): + rg_id = primary_replication_group + grg_suffix = random_suffix_name("ack-e2e-mod", 20) + + reference, global_id = create_grg( + grg_suffix, rg_id, "Original description", target_status="primary-only" + ) + + # Capture the Synced condition's lastTransitionTime before patching. + # ACK rewrites it on every reconcile, so requiring a newer value proves + # the controller actually reconciled this change rather than us reading + # a stale 'True' left over from the previous reconcile. + synced_before = condition.get_synced_last_transition_time(reference) + + # Modify the description + k8s.patch_custom_resource(reference, { + "spec": {"description": "Updated description via ACK E2E"} + }) + + # Wait for the modify to land in AWS and the group to settle + assert wait_global_description( + global_id, "Updated description via ACK E2E" + ), f"GlobalReplicationGroup {global_id} description was not updated" + + # Verify on AWS side + grg = get_global_replication_group(global_id) + assert grg is not None + assert grg.get("GlobalReplicationGroupDescription") == "Updated description via ACK E2E" + + # The controller must drive the CR back to Synced via a fresh reconcile + assert k8s.wait_on_condition_after( + reference, condition.CONDITION_TYPE_RESOURCE_SYNCED, "True", + last_transition_after=synced_before, + wait_periods=MODIFY_WAIT_PERIODS, + period_length=MODIFY_PERIOD_LENGTH, + ), "resource did not return to ACK.ResourceSynced=True after modify" + + # Cleanup + delete_grg(reference, global_id) + + def test_modify_automatic_failover_is_noop(self, primary_replication_group): + """Setting automaticFailoverEnabled to a value AWS already reports must + converge, not loop. + + A global datastore's primary always has automatic failover enabled, and + the GRG inherits it, so Modify(AutomaticFailoverEnabled=true) is a no-op + that AWS rejects ("Requested product type is the same current product + type"). The controller derives the current failover state from live + member info (not an annotation), so it must recognize the value already + matches and reach Synced without ever issuing that Modify or landing in + ACK.Terminal. Regression test for the standalone-AFE reconcile loop. + """ + rg_id = primary_replication_group + grg_suffix = random_suffix_name("ack-e2e-afe", 20) + + # Create without automaticFailoverEnabled so the patch below is the + # first time the field is set -- the exact trigger for the old loop. + reference, global_id = create_grg( + grg_suffix, rg_id, "Original description", target_status="primary-only" + ) + + k8s.patch_custom_resource(reference, { + "spec": {"automaticFailoverEnabled": True} + }) + + # AWS already reports failover enabled on the primary (inherited). + assert wait_global_automatic_failover( + global_id, True + ), f"GlobalReplicationGroup {global_id} primary failover not enabled" + + # The no-op must never drive the resource terminal, and -- crucially -- + # must never surface a recoverable error. The old annotation-based code + # issued Modify(AutomaticFailoverEnabled=true), which AWS rejects as a + # no-op, flipping ACK.Recoverable=True and looping. Deriving the value + # from live member state means no Modify is issued at all, so neither + # condition should ever appear across several reconcile cycles. + assert not k8s.wait_on_condition( + reference, condition.CONDITION_TYPE_TERMINAL, "True", + wait_periods=8, period_length=15, + ), "resource unexpectedly reached ACK.Terminal=True on a no-op AFE change" + assert not k8s.wait_on_condition( + reference, condition.CONDITION_TYPE_RECOVERABLE, "True", + wait_periods=1, period_length=1, + ), "resource surfaced ACK.Recoverable=True -- controller issued the no-op Modify" + + # And it must be Synced (it never left the synced state, since no + # Modify was needed). + condition.assert_synced(reference) + + delete_grg(reference, global_id) + + def test_modify_multiple_fields_converges(self, primary_replication_group): + """Changing two independent modify-eligible fields in one patch must + converge over multiple reconciles, not fail. + + AWS's ModifyGlobalReplicationGroup rejects a request that sets more + than one field at once. Patching description and cacheNodeType in the + same apply creates a genuine two-field delta in a single reconcile: + description is a fast metadata change and cacheNodeType is a real + scaling change (cache.r6g.large -> cache.r6g.xlarge). The controller + must send one field per Modify call and let the other field's delta + persist into the next reconcile, converging on both values without + ever landing in ACK.Terminal. + """ + rg_id = primary_replication_group + grg_suffix = random_suffix_name("ack-e2e-multi", 20) + + reference, global_id = create_grg( + grg_suffix, rg_id, "Original description", target_status="primary-only" + ) + + synced_before = condition.get_synced_last_transition_time(reference) + + # Two independent fields changed in the same patch. + k8s.patch_custom_resource(reference, { + "spec": { + "description": "Updated via multi-field E2E", + "cacheNodeType": "cache.r6g.xlarge", + } + }) + + # Both must eventually land on the AWS side, across however many + # reconciles it takes -- never simultaneously in one Modify call. + assert wait_global_description( + global_id, "Updated via multi-field E2E" + ), f"GlobalReplicationGroup {global_id} description was not updated" + assert wait_global_node_type( + global_id, "cache.r6g.xlarge" + ), f"GlobalReplicationGroup {global_id} node type was not updated" + + # Must never have landed in a terminal state along the way. + assert not k8s.wait_on_condition( + reference, condition.CONDITION_TYPE_TERMINAL, "True", + wait_periods=1, period_length=1, + ), "resource unexpectedly reached ACK.Terminal=True during multi-field modify" + + assert k8s.wait_on_condition_after( + reference, condition.CONDITION_TYPE_RESOURCE_SYNCED, "True", + last_transition_after=synced_before, + wait_periods=SCALE_WAIT_PERIODS, + period_length=SCALE_PERIOD_LENGTH, + ), "resource did not return to ACK.ResourceSynced=True after multi-field modify" + + # Cleanup + delete_grg(reference, global_id) + + +@service_marker +class TestGlobalReplicationGroupNodeGroupScaling: + """Test increasing and decreasing node groups. + + Uses a separate cluster-mode fixture with 2 shards. + Verified against live AWS (account 585008087740, us-west-2): + - IncreaseNodeGroups 2→3 + - DecreaseNodeGroups 3→2 (retains first N node groups) + """ + + @pytest.fixture(scope="class") + def cluster_mode_primary(self): + """Create a cluster-mode ReplicationGroup with 2 node groups.""" + rg_id = random_suffix_name("ack-e2e-grg-cm", 32) + + rg_resource = load_elasticache_resource( + "replicationgroup_create_delete_grg", + additional_replacements={ + "RG_ID": rg_id, + "ENGINE_VERSION": "7.0", + "NUM_NODE_GROUPS": "2", + "REPLICAS_PER_NODE_GROUP": "1", + }, + ) + + reference = k8s.CustomResourceReference( + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL_RG, rg_id, namespace="default" + ) + _ = k8s.create_custom_resource(reference, rg_resource) + resource = k8s.wait_resource_consumed_by_controller( + reference, wait_periods=15, period_length=20 + ) + assert resource is not None + assert wait_replication_group_available(rg_id), ( + f"Cluster-mode ReplicationGroup {rg_id} did not become available" + ) + + yield rg_id + + # Cleanup + k8s.delete_custom_resource(reference) + try: + waiter = ec.get_waiter("replication_group_deleted") + waiter.wait( + ReplicationGroupId=rg_id, + WaiterConfig={"Delay": 30, "MaxAttempts": 40}, + ) + except Exception: + pass + + def test_scale_up_and_down(self, cluster_mode_primary): + """Scale node groups 2→3→2 via nodeGroupCount spec field.""" + rg_id = cluster_mode_primary + grg_suffix = random_suffix_name("ack-e2e-scale", 20) + + reference, global_id = create_grg( + grg_suffix, rg_id, "ACK E2E test - scaling", + resource_template="global_replication_group_scale", + target_status="primary-only", + extra_replacements={"NODE_GROUP_COUNT": "2"}, + ) + + # Scale UP: 2→3 + synced_before = condition.get_synced_last_transition_time(reference) + k8s.patch_custom_resource(reference, {"spec": {"nodeGroupCount": 3}}) + + assert wait_global_node_group_count(global_id, 3), ( + f"GlobalReplicationGroup {global_id} did not scale up to 3 node groups" + ) + assert k8s.wait_on_condition_after( + reference, condition.CONDITION_TYPE_RESOURCE_SYNCED, "True", + last_transition_after=synced_before, + wait_periods=SCALE_WAIT_PERIODS, + period_length=SCALE_PERIOD_LENGTH, + ), "resource did not return to ACK.ResourceSynced=True after scale up" + + # Scale DOWN: 3→2 + synced_before = condition.get_synced_last_transition_time(reference) + k8s.patch_custom_resource(reference, {"spec": {"nodeGroupCount": 2}}) + + assert wait_global_node_group_count(global_id, 2), ( + f"GlobalReplicationGroup {global_id} did not scale down to 2 node groups" + ) + assert k8s.wait_on_condition_after( + reference, condition.CONDITION_TYPE_RESOURCE_SYNCED, "True", + last_transition_after=synced_before, + wait_periods=SCALE_WAIT_PERIODS, + period_length=SCALE_PERIOD_LENGTH, + ), "resource did not return to ACK.ResourceSynced=True after scale down" + + # Cleanup + delete_grg(reference, global_id)