Skip to content

Commit

Permalink
Add Backup resource custom hooks, terminalCodes, printcolumns and e2e
Browse files Browse the repository at this point in the history
tests

- Add custom hooks, terminalCodes, printcomns to `generator.yaml`
- Regenerate `Backup` resource
- Add e2e tests for create and delete operations
  • Loading branch information
a-hilaly committed Jun 3, 2021
1 parent a8d3363 commit 107889e
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apis/v1alpha1/backup.go

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

15 changes: 14 additions & 1 deletion config/crd/bases/dynamodb.services.k8s.aws_backups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ spec:
singular: backup
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .status.backupCreationDateTime
name: CreationTime
type: date
- jsonPath: .status.backupExpiryDateTime
name: ExpiryTime
type: date
- jsonPath: .status.backupStatus
name: Status
type: string
- jsonPath: .status.backupType
name: Type
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: Backup is the Schema for the Backups API
Expand Down
18 changes: 18 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ operations:
UpdateGlobalTable:
operation_type: Delete
resource_name: GlobalTable
DescribeBackup:
set_output_wrapper_field_path: BackupDescription.BackupDetails
resources:
Table:
fields:
Expand Down Expand Up @@ -46,3 +48,19 @@ resources:
errors:
404:
code: BackupNotFoundException
fields:
BackupStatus:
is_printable: true
print_name: Status
BackupCreationDateTime:
is_printable: true
print_name: CreationTime
BackupExpiryDateTime:
is_printable: true
print_name: ExpiryTime
BackupType:
is_printable: true
print_name: Type
hooks:
sdk_read_one_post_set_output:
template_path: hooks/backup/sdk_read_one_post_set_output.go.tpl
94 changes: 94 additions & 0 deletions pkg/resource/backup/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 backup

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
}
83 changes: 83 additions & 0 deletions pkg/resource/backup/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 backup

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.BackupStatus_SDK{}
)

var (
requeueWaitWhileDeleting = ackrequeue.NeededAfter(
errors.New("Backup in 'DELETING' state, cannot be modified or deleted."),
ackrequeue.DefaultRequeueAfterDuration/6,
)
requeueWaitWhileCreating = ackrequeue.NeededAfter(
errors.New("Backup 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.BackupStatus == nil {
return nil
}
status := *r.ko.Status.BackupStatus
msg := fmt.Sprintf(
"Backup in '%s' state, cannot be modified until '%s'.",
status, v1alpha1.BackupStatus_SDK_AVAILABLE,
)

return ackrequeue.NeededAfter(
errors.New(msg),
ackrequeue.DefaultRequeueAfterDuration/6,
)
}

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

// backupCreating returns true if the supplied DynamodbDB backup is in the process
// of being created
func backupCreating(r *resource) bool {
if r.ko.Status.BackupStatus == nil {
return false
}
dbis := *r.ko.Status.BackupStatus
return dbis == string(v1alpha1.BackupStatus_SDK_CREATING)
}
43 changes: 42 additions & 1 deletion pkg/resource/backup/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 templates/hooks/backup/sdk_read_one_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if backupCreating(&resource{ko}) {
return &resource{ko}, requeueWaitWhileCreating
}
7 changes: 7 additions & 0 deletions test/e2e/resources/backup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: dynamodb.services.k8s.aws/v1alpha1
kind: Backup
metadata:
name: $BACKUP_NAME
spec:
backupName: $BACKUP_NAME
tableName: $TABLE_NAME
Loading

0 comments on commit 107889e

Please sign in to comment.