|
| 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 table |
| 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 | +} |
0 commit comments