Skip to content

Commit 6368981

Browse files
authored
ensure Synced condition on Domain resources (#4)
Adds custom code to generic hook templates that places a ConditionTypeResourceSynced on Domain resources with a status of False when the Status.Processing field of the Domain is True and a status of True otherwise. Refactors the e2e tests to match the style of the RDS controller and adds assertions for the ResourceSynced condition. Signed-off-by: Jay Pipes <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 6b4a9e6 commit 6368981

15 files changed

+522
-710
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2021-10-15T18:16:23Z"
3-
build_hash: f4166ae9942034b0552f244685515eef5a92dc25
4-
go_version: go1.17.1
2+
build_date: "2021-11-04T22:37:28Z"
3+
build_hash: 0a8fc32cdf2d33693e919e4a59da05599b9f916e
4+
go_version: go1.17
55
version: v0.15.1
66
api_directory_checksum: 550c87ef2158e5b3d58bc5a8ff5d3367c192b04a
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.40.51
99
generator_config_info:
10-
file_checksum: 829c0e98be7f1960e999ed9df55b722a2b697921
10+
file_checksum: 1aa3cbb5df5c91de25b9935978e30138af9b696d
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ resources:
1515
code: ResourceNotFoundException
1616
terminal_codes:
1717
- ValidationException
18+
hooks:
19+
sdk_create_post_set_output:
20+
template_path: hooks/domain/sdk_create_post_set_output.go.tpl
21+
sdk_read_one_post_set_output:
22+
template_path: hooks/domain/sdk_read_one_post_set_output.go.tpl
23+
sdk_update_pre_build_request:
24+
template_path: hooks/domain/sdk_update_pre_build_request.go.tpl
25+
sdk_delete_pre_build_request:
26+
template_path: hooks/domain/sdk_delete_pre_build_request.go.tpl

config/crd/bases/opensearch.services.k8s.aws_domains.yaml

Lines changed: 0 additions & 441 deletions
This file was deleted.

config/crd/bases/opensearch.services.k8s.aws_outboundconnections.yaml

Lines changed: 0 additions & 166 deletions
This file was deleted.

generator.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ resources:
1515
code: ResourceNotFoundException
1616
terminal_codes:
1717
- ValidationException
18+
hooks:
19+
sdk_create_post_set_output:
20+
template_path: hooks/domain/sdk_create_post_set_output.go.tpl
21+
sdk_read_one_post_set_output:
22+
template_path: hooks/domain/sdk_read_one_post_set_output.go.tpl
23+
sdk_update_pre_build_request:
24+
template_path: hooks/domain/sdk_update_pre_build_request.go.tpl
25+
sdk_delete_pre_build_request:
26+
template_path: hooks/domain/sdk_delete_pre_build_request.go.tpl

pkg/resource/domain/conditions.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package domain
15+
16+
import (
17+
corev1 "k8s.io/api/core/v1"
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
20+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
21+
)
22+
23+
// getSyncedCondition returns the Condition in the resource's Conditions
24+
// collection that is of type ConditionTypeResourceSynced. If no such condition
25+
// is found, returns nil.
26+
//
27+
// TODO(jaypipes): Move to ACK code-gen templates.
28+
func getSyncedCondition(r *resource) *ackv1alpha1.Condition {
29+
return getConditionOfType(r, ackv1alpha1.ConditionTypeResourceSynced)
30+
}
31+
32+
// getTerminalCondition returns the Condition in the resource's Conditions
33+
// collection that is of type ConditionTypeTerminal. If no such condition is
34+
// found, returns nil.
35+
//
36+
// TODO(jaypipes): Move to ACK code-gen templates.
37+
func getTerminalCondition(r *resource) *ackv1alpha1.Condition {
38+
return getConditionOfType(r, ackv1alpha1.ConditionTypeTerminal)
39+
}
40+
41+
// getConditionOfType returns the Condition in the resource's Conditions
42+
// collection of the supplied type. If no such condition is found, returns nil.
43+
//
44+
// TODO(jaypipes): Move to ACK code-gen templates.
45+
func getConditionOfType(
46+
r *resource,
47+
condType ackv1alpha1.ConditionType,
48+
) *ackv1alpha1.Condition {
49+
for _, condition := range r.ko.Status.Conditions {
50+
if condition.Type == condType {
51+
return condition
52+
}
53+
}
54+
return nil
55+
}
56+
57+
// setSyncedCondition sets the resource's Condition of type
58+
// ConditionTypeResourceSynced to the supplied status, optional message and
59+
// reason.
60+
//
61+
// TODO(jaypipes): Move to ACK code-gen templates.
62+
func setSyncedCondition(
63+
r *resource,
64+
status corev1.ConditionStatus,
65+
message *string,
66+
reason *string,
67+
) {
68+
c := getSyncedCondition(r)
69+
if c == nil {
70+
c = &ackv1alpha1.Condition{
71+
Type: ackv1alpha1.ConditionTypeResourceSynced,
72+
}
73+
r.ko.Status.Conditions = append(r.ko.Status.Conditions, c)
74+
}
75+
now := metav1.Now()
76+
c.LastTransitionTime = &now
77+
c.Status = status
78+
}
79+
80+
// setTerminalCondition sets the resource's Condition of type
81+
// ConditionTypeTerminal to the supplied status, optional message and reason.
82+
//
83+
// TODO(jaypipes): Move to ACK code-gen templates.
84+
func setTerminalCondition(
85+
r *resource,
86+
status corev1.ConditionStatus,
87+
message *string,
88+
reason *string,
89+
) {
90+
c := getTerminalCondition(r)
91+
if c == nil {
92+
c = &ackv1alpha1.Condition{
93+
Type: ackv1alpha1.ConditionTypeTerminal,
94+
}
95+
r.ko.Status.Conditions = append(r.ko.Status.Conditions, c)
96+
}
97+
now := metav1.Now()
98+
c.LastTransitionTime = &now
99+
c.Status = status
100+
c.Message = message
101+
c.Reason = reason
102+
}

pkg/resource/domain/hooks.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package domain
15+
16+
import (
17+
"errors"
18+
19+
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
20+
)
21+
22+
var (
23+
requeueWaitWhileProcessing = ackrequeue.NeededAfter(
24+
errors.New("domain is currently processing changes, cannot be modified or deleted."),
25+
ackrequeue.DefaultRequeueAfterDuration,
26+
)
27+
)
28+
29+
// domainProcessing returns true if the supplied domain is in a state of
30+
// processing
31+
func domainProcessing(r *resource) bool {
32+
if r.ko.Status.Processing == nil {
33+
return false
34+
}
35+
return *r.ko.Status.Processing
36+
}

pkg/resource/domain/sdk.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if domainProcessing(&resource{ko}) {
2+
// Setting resource synced condition to false will trigger a requeue of
3+
// the resource. No need to return a requeue error here.
4+
setSyncedCondition(&resource{ko}, corev1.ConditionFalse, nil, nil)
5+
return &resource{ko}, nil
6+
}

0 commit comments

Comments
 (0)