|
| 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 | +// Use this file if conditions need to be updated based on the latest status |
| 15 | +// of endpoint which is not evident from API response |
| 16 | + |
| 17 | +package common |
| 18 | + |
| 19 | +import ( |
| 20 | + ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition" |
| 21 | + acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | +) |
| 24 | + |
| 25 | +// SetSyncedCondition sets the ACK Synced Condition |
| 26 | +// status to true if the resource status exists but |
| 27 | +// is not in one of the given modifying statuses, |
| 28 | +// or false if the resource status is one of the |
| 29 | +// given modifying statuses. |
| 30 | +func SetSyncedCondition( |
| 31 | + r acktypes.AWSResource, |
| 32 | + latestStatus *string, |
| 33 | + resourceName *string, |
| 34 | + modifyingStatuses *[]string, |
| 35 | +) { |
| 36 | + if latestStatus == nil { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + msg := *resourceName + " is in " + *latestStatus + " status." |
| 41 | + conditionStatus := corev1.ConditionTrue |
| 42 | + if IsModifyingStatus(latestStatus, modifyingStatuses) { |
| 43 | + conditionStatus = corev1.ConditionFalse |
| 44 | + } |
| 45 | + |
| 46 | + ackcondition.SetSynced(r, conditionStatus, &msg, nil) |
| 47 | +} |
| 48 | + |
| 49 | +// SetTerminalState sets conditions (terminal) on |
| 50 | +// a resource's supplied status conditions if the |
| 51 | +// latest status matches the terminal status. |
| 52 | +// It returns true if conditions are updated. |
| 53 | +func SetTerminalState( |
| 54 | + r acktypes.AWSResource, |
| 55 | + latestStatus *string, |
| 56 | + resourceName *string, |
| 57 | + terminalStatus string, |
| 58 | +) bool { |
| 59 | + if latestStatus == nil || *latestStatus != terminalStatus { |
| 60 | + return false |
| 61 | + } |
| 62 | + |
| 63 | + terminalCondition := ackcondition.Terminal(r) |
| 64 | + if terminalCondition != nil && terminalCondition.Status == corev1.ConditionTrue { |
| 65 | + // some other exception already put the resource in terminal condition |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + // setting terminal condition since controller can no longer recover by retrying |
| 70 | + terminalMessage := *resourceName + " status reached terminal state: " + terminalStatus + ". Check the FailureReason." |
| 71 | + ackcondition.SetTerminal(r, corev1.ConditionTrue, &terminalMessage, nil) |
| 72 | + |
| 73 | + return true |
| 74 | +} |
0 commit comments