Skip to content

Commit

Permalink
Add GlobalTable resource custom hooks, terminalCodes and e2e tests
Browse files Browse the repository at this point in the history
Part of aws-controllers-k8s/community#803

Description of changes
- Add custom hooks and terminalCodes to `GlobalTable` resource in `generator.yaml`
- Add e2e tests for `GlobalTable` create and delete operations

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
a-hilaly committed Jun 14, 2021
1 parent e786654 commit 3ff9a67
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 47 deletions.
7 changes: 7 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
operations:
UpdateGlobalTable:
operation_type: Delete
resource_name: GlobalTable
resources:
Table:
exceptions:
Expand All @@ -20,6 +24,9 @@ resources:
errors:
404:
code: GlobalTableNotFoundException
hooks:
sdk_delete_post_build_request:
code: customSetDeleteInput(r, input)
Backup:
exceptions:
errors:
Expand Down
3 changes: 3 additions & 0 deletions pkg/resource/backup/manager.go

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

94 changes: 94 additions & 0 deletions pkg/resource/global_table/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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_table

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
)

// getSyncedCondition returns the Condition in the resource's Conditions
// collection that is of type ConditionTypeResourceSynced. If no such condition
// is found, returns nil.
func getSyncedCondition(r *resource) *ackv1alpha1.Condition {
return getConditionOfType(r, ackv1alpha1.ConditionTypeResourceSynced)
}

// getTerminalCondition returns the Condition in the resource's Conditions
// collection that is of type ConditionTypeTerminal. If no such condition is
// found, returns nil.
func getTerminalCondition(r *resource) *ackv1alpha1.Condition {
return getConditionOfType(r, ackv1alpha1.ConditionTypeTerminal)
}

// getConditionOfType returns the Condition in the resource's Conditions
// collection of the supplied type. If no such condition is found, returns nil.
func getConditionOfType(
r *resource,
condType ackv1alpha1.ConditionType,
) *ackv1alpha1.Condition {
for _, condition := range r.ko.Status.Conditions {
if condition.Type == condType {
return condition
}
}
return nil
}

// setSyncedCondition sets the resource's Condition of type
// ConditionTypeResourceSynced to the supplied status, optional message and
// reason.
func setSyncedCondition(
r *resource,
status corev1.ConditionStatus,
message *string,
reason *string,
) {
c := getSyncedCondition(r)
if c == nil {
c = &ackv1alpha1.Condition{
Type: ackv1alpha1.ConditionTypeResourceSynced,
}
r.ko.Status.Conditions = append(r.ko.Status.Conditions, c)
}
now := metav1.Now()
c.LastTransitionTime = &now
c.Status = status
}

// setTerminalCondition sets the resource's Condition of type
// ConditionTypeTerminal to the supplied status, optional message and reason.
//
// TODO(jaypipes): Move to ACK code-gen templates.
func setTerminalCondition(
r *resource,
status corev1.ConditionStatus,
message *string,
reason *string,
) {
c := getSyncedCondition(r)
if c == nil {
c = &ackv1alpha1.Condition{
Type: ackv1alpha1.ConditionTypeTerminal,
}
r.ko.Status.Conditions = append(r.ko.Status.Conditions, c)
}
now := metav1.Now()
c.LastTransitionTime = &now
c.Status = status
c.Message = message
c.Reason = reason
}
27 changes: 27 additions & 0 deletions pkg/resource/global_table/custom_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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_table

import svcsdk "github.com/aws/aws-sdk-go/service/dynamodb"

func customSetDeleteInput(r *resource, input *svcsdk.UpdateGlobalTableInput) {
for _, replica := range r.ko.Spec.ReplicationGroup {
replicaUpdate := &svcsdk.ReplicaUpdate{
Delete: &svcsdk.DeleteReplicaAction{
RegionName: replica.RegionName,
},
}
input.ReplicaUpdates = append(input.ReplicaUpdates, replicaUpdate)
}
}
100 changes: 100 additions & 0 deletions pkg/resource/global_table/hooks.go
Original file line number Diff line number Diff line change
@@ -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.

package global_table

import (
"errors"
"fmt"

"github.com/aws-controllers-k8s/dynamodb-controller/apis/v1alpha1"
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
)

var (
// TerminalStatuses are the status strings that are terminal states for a
// DB instance.
TerminalStatuses = []v1alpha1.GlobalTableStatus_SDK{
v1alpha1.GlobalTableStatus_SDK_DELETING,
v1alpha1.GlobalTableStatus_SDK_UPDATING,
}
)

var (
requeueWaitWhileDeleting = ackrequeue.NeededAfter(
errors.New("GlobalTable in 'DELETING' state, cannot be modified or deleted."),
ackrequeue.DefaultRequeueAfterDuration/6,
)
requeueWaitWhileCreating = ackrequeue.NeededAfter(
errors.New("GlobalTable in 'CREATING' state, cannot be modified or deleted."),
ackrequeue.DefaultRequeueAfterDuration/15,
)
)

// requeueWaitUntilCanModify returns a `ackrequeue.RequeueNeededAfter` struct
// explaining the DB instance cannot be modified until it reaches an available
// status.
func requeueWaitUntilCanModify(r *resource) *ackrequeue.RequeueNeededAfter {
if r.ko.Status.GlobalTableStatus == nil {
return nil
}
status := *r.ko.Status.GlobalTableStatus
msg := fmt.Sprintf(
"GlobalTable in '%s' state, cannot be modified until '%s'.",
status, v1alpha1.GlobalTableStatus_SDK_ACTIVE,
)

fmt.Println("REQUE: WAITING UNTIL NEXT", errors.New(msg))
return ackrequeue.NeededAfter(
errors.New(msg),
ackrequeue.DefaultRequeueAfterDuration/6,
)
}

// instanceHasTerminalStatus returns whether the supplied DB Instance is in a
// terminal state
func globalTableHasTerminalStatus(r *resource) bool {
if r.ko.Status.GlobalTableStatus == nil {
return false
}
ts := *r.ko.Status.GlobalTableStatus
for _, s := range TerminalStatuses {
if ts == string(s) {
return true
}
}
return false
}

// globalTableCreating returns true if the supplied DynamodbDB globalTable is in the process
// of being created
func globalTableCreating(r *resource) bool {
fmt.Println("IS GBCREATING?")
if r.ko.Status.GlobalTableStatus == nil {
fmt.Println("IS GBCREATING? NO")
return false
}
fmt.Println("IS GBCREATING? YES")
dbis := *r.ko.Status.GlobalTableStatus
return dbis == string(v1alpha1.GlobalTableStatus_SDK_CREATING)
}

// globalTableDeleting returns true if the supplied DynamodbDB globalTable is in the process
// of being deleted
func globalTableDeleting(r *resource) bool {
if r.ko.Status.GlobalTableStatus == nil {
return false
}
dbis := *r.ko.Status.GlobalTableStatus
return dbis == string(v1alpha1.GlobalTableStatus_SDK_DELETING)
}
3 changes: 3 additions & 0 deletions pkg/resource/global_table/manager.go

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

65 changes: 18 additions & 47 deletions pkg/resource/global_table/sdk.go

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

3 changes: 3 additions & 0 deletions pkg/resource/table/manager.go

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

Empty file.
8 changes: 8 additions & 0 deletions test/e2e/resources/global_table.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: dynamodb.services.k8s.aws/v1alpha1
kind: GlobalTable
metadata:
name: $GLOBAL_TABLE_NAME
spec:
globalTableName: $GLOBAL_TABLE_NAME
replicationGroup:
- regionName: $REGION_NAME
Loading

0 comments on commit 3ff9a67

Please sign in to comment.