Skip to content

Commit

Permalink
Add Table resource custom hooks, terminalCodes, printcolumns and e2…
Browse files Browse the repository at this point in the history
…e tests

- Add custom hooks, terminalCodes, printcomns to `generator.yaml`
- Regenerate `Table` resource
- Add e2e tests for create and delete operations
  • Loading branch information
a-hilaly committed Jun 2, 2021
1 parent 9730082 commit c571d3e
Show file tree
Hide file tree
Showing 19 changed files with 662 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apis/v1alpha1/table.go

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

9 changes: 8 additions & 1 deletion config/crd/bases/dynamodb.services.k8s.aws_tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ spec:
singular: table
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .status.tableStatus
name: Status
type: string
- jsonPath: .spec.tableName
name: TableName
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: Table is the Schema for the Tables API
Expand Down
17 changes: 17 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
resources:
Table:
fields:
TableStatus:
is_printable: true
print_name: Status
TableName:
is_printable: true
exceptions:
errors:
404:
code: ResourceNotFoundException
terminal_codes:
- InternalServerError
- LimitExceededException
- ResourceInUseException
hooks:
sdk_read_one_post_set_output:
template_path: hooks/table/sdk_read_one_post_set_output.go.tpl
sdk_update_pre_build_request:
template_path: hooks/table/sdk_update_pre_build_request.go.tpl
sdk_delete_pre_build_request:
template_path: hooks/table/sdk_delete_pre_build_request.go.tpl
GlobalTable:
exceptions:
errors:
Expand Down
93 changes: 93 additions & 0 deletions pkg/resource/table/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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 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.
//
// TODO(jaypipes): Move to ACK code-gen templates.
func getSyncedCondition(r *resource) *ackv1alpha1.Condition {
return getConditionOfType(r, ackv1alpha1.ConditionTypeResourceSynced)
}

// getConditionOfType returns the Condition in the resource's Conditions
// collection of the supplied type. If no such condition is found, returns nil.
//
// TODO(jaypipes): Move to ACK code-gen templates.
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.
//
// TODO(jaypipes): Move to ACK code-gen templates.
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
}
97 changes: 97 additions & 0 deletions pkg/resource/table/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 table

import (
"errors"
"time"

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

var (
ErrTableDeleting = errors.New("table in 'DELETING' state, cannot be modified or deleted")
ErrTableCreating = errors.New("table in 'CREATING' state, cannot be modified or deleted")
ErrTableUpdating = errors.New("table in 'UPDATING' state, cannot be modified or deleted")
)

var (
// TerminalStatuses are the status strings that are terminal states for a
// DynamoDB table
TerminalStatuses = []v1alpha1.TableStatus_SDK{
v1alpha1.TableStatus_SDK_ARCHIVING,
v1alpha1.TableStatus_SDK_DELETING,
}
)

var (
requeueWaitWhileDeleting = ackrequeue.NeededAfter(
ErrTableDeleting,
5*time.Second,
)
requeueWaitWhileCreating = ackrequeue.NeededAfter(
ErrTableDeleting,
5*time.Second,
)
requeueWaitWhileUpdating = ackrequeue.NeededAfter(
ErrTableUpdating,
5*time.Second,
)
)

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

// tableCreating returns true if the supplied DynamodbDB table is in the process
// of being created
func tableCreating(r *resource) bool {
if r.ko.Status.TableStatus == nil {
return false
}
dbis := *r.ko.Status.TableStatus
return dbis == string(v1alpha1.TableStatus_SDK_CREATING)
}

// tableDeleting returns true if the supplied DynamodbDB table is in the process
// of being deleted
func tableDeleting(r *resource) bool {
if r.ko.Status.TableStatus == nil {
return false
}
dbis := *r.ko.Status.TableStatus
return dbis == string(v1alpha1.TableStatus_SDK_DELETING)
}

// tableUpdating returns true if the supplied DynamodbDB table is in the process
// of being deleted
func tableUpdating(r *resource) bool {
if r.ko.Status.TableStatus == nil {
return false
}
dbis := *r.ko.Status.TableStatus
return dbis == string(v1alpha1.TableStatus_SDK_UPDATING)
}
50 changes: 48 additions & 2 deletions pkg/resource/table/sdk.go

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

6 changes: 6 additions & 0 deletions templates/hooks/table/sdk_delete_pre_build_request.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if tableDeleting(r) {
return requeueWaitWhileDeleting
}
if tableUpdating(r) {
return requeueWaitWhileUpdating
}
6 changes: 6 additions & 0 deletions templates/hooks/table/sdk_read_one_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if tableCreating(&resource{ko}) {
return &resource{ko}, requeueWaitWhileCreating
}
if tableUpdating(&resource{ko}) {
return &resource{ko}, requeueWaitWhileUpdating
}
21 changes: 21 additions & 0 deletions templates/hooks/table/sdk_update_pre_build_request.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
if tableDeleting(latest) {
msg := "table is currently being deleted"
setSyncedCondition(desired, corev1.ConditionFalse, &msg, nil)
return desired, requeueWaitWhileDeleting
}
if tableCreating(latest) {
msg := "table is currently being created"
setSyncedCondition(desired, corev1.ConditionFalse, &msg, nil)
return desired, requeueWaitWhileCreating
}
if tableUpdating(latest) {
msg := "table is currently being created"
setSyncedCondition(desired, corev1.ConditionFalse, &msg, nil)
return desired, requeueWaitWhileUpdating
}
if tableHasTerminalStatus(latest) {
msg := "table is in '"+*latest.ko.Status.TableStatus+"' status"
setTerminalCondition(desired, corev1.ConditionTrue, &msg, nil)
setSyncedCondition(desired, corev1.ConditionTrue, nil, nil)
return desired, nil
}
3 changes: 3 additions & 0 deletions test/e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.py[cod]
**/bootstrap.yaml
Loading

0 comments on commit c571d3e

Please sign in to comment.