|
| 1 | +/* |
| 2 | +Copyright 2020 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + appsv1 "k8s.io/api/apps/v1" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + "knative.dev/pkg/apis" |
| 23 | + duckv1 "knative.dev/pkg/apis/duck/v1" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + // IntegrationSinkConditionReady has status True when the IntegrationSink is ready to send events. |
| 28 | + IntegrationSinkConditionReady = apis.ConditionReady |
| 29 | + |
| 30 | + IntegrationSinkConditionAddressable apis.ConditionType = "Addressable" |
| 31 | + |
| 32 | + // IntegrationSinkConditionDeploymentReady has status True when the IntegrationSink has been configured with a deployment. |
| 33 | + IntegrationSinkConditionDeploymentReady apis.ConditionType = "DeploymentReady" |
| 34 | + |
| 35 | + // IntegrationSinkConditionEventPoliciesReady has status True when all the applying EventPolicies for this |
| 36 | + // IntegrationSink are ready. |
| 37 | + IntegrationSinkConditionEventPoliciesReady apis.ConditionType = "EventPoliciesReady" |
| 38 | +) |
| 39 | + |
| 40 | +var IntegrationSinkCondSet = apis.NewLivingConditionSet( |
| 41 | + IntegrationSinkConditionAddressable, |
| 42 | + IntegrationSinkConditionDeploymentReady, |
| 43 | + IntegrationSinkConditionEventPoliciesReady, |
| 44 | +) |
| 45 | + |
| 46 | +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. |
| 47 | +func (*IntegrationSink) GetConditionSet() apis.ConditionSet { |
| 48 | + return IntegrationSinkCondSet |
| 49 | +} |
| 50 | + |
| 51 | +// GetCondition returns the condition currently associated with the given type, or nil. |
| 52 | +func (s *IntegrationSinkStatus) GetCondition(t apis.ConditionType) *apis.Condition { |
| 53 | + return IntegrationSinkCondSet.Manage(s).GetCondition(t) |
| 54 | +} |
| 55 | + |
| 56 | +// GetTopLevelCondition returns the top level Condition. |
| 57 | +func (ps *IntegrationSinkStatus) GetTopLevelCondition() *apis.Condition { |
| 58 | + return IntegrationSinkCondSet.Manage(ps).GetTopLevelCondition() |
| 59 | +} |
| 60 | + |
| 61 | +// IsReady returns true if the resource is ready overall. |
| 62 | +func (s *IntegrationSinkStatus) IsReady() bool { |
| 63 | + return IntegrationSinkCondSet.Manage(s).IsHappy() |
| 64 | +} |
| 65 | + |
| 66 | +// InitializeConditions sets relevant unset conditions to Unknown state. |
| 67 | +func (s *IntegrationSinkStatus) InitializeConditions() { |
| 68 | + IntegrationSinkCondSet.Manage(s).InitializeConditions() |
| 69 | +} |
| 70 | + |
| 71 | +// MarkAddressableReady marks the Addressable condition to True. |
| 72 | +func (s *IntegrationSinkStatus) MarkAddressableReady() { |
| 73 | + IntegrationSinkCondSet.Manage(s).MarkTrue(IntegrationSinkConditionAddressable) |
| 74 | +} |
| 75 | + |
| 76 | +// MarkEventPoliciesFailed marks the EventPoliciesReady condition to False with the given reason and message. |
| 77 | +func (s *IntegrationSinkStatus) MarkEventPoliciesFailed(reason, messageFormat string, messageA ...interface{}) { |
| 78 | + IntegrationSinkCondSet.Manage(s).MarkFalse(IntegrationSinkConditionEventPoliciesReady, reason, messageFormat, messageA...) |
| 79 | +} |
| 80 | + |
| 81 | +// MarkEventPoliciesUnknown marks the EventPoliciesReady condition to Unknown with the given reason and message. |
| 82 | +func (s *IntegrationSinkStatus) MarkEventPoliciesUnknown(reason, messageFormat string, messageA ...interface{}) { |
| 83 | + IntegrationSinkCondSet.Manage(s).MarkUnknown(IntegrationSinkConditionEventPoliciesReady, reason, messageFormat, messageA...) |
| 84 | +} |
| 85 | + |
| 86 | +// MarkEventPoliciesTrue marks the EventPoliciesReady condition to True. |
| 87 | +func (s *IntegrationSinkStatus) MarkEventPoliciesTrue() { |
| 88 | + IntegrationSinkCondSet.Manage(s).MarkTrue(IntegrationSinkConditionEventPoliciesReady) |
| 89 | +} |
| 90 | + |
| 91 | +// MarkEventPoliciesTrueWithReason marks the EventPoliciesReady condition to True with the given reason and message. |
| 92 | +func (s *IntegrationSinkStatus) MarkEventPoliciesTrueWithReason(reason, messageFormat string, messageA ...interface{}) { |
| 93 | + IntegrationSinkCondSet.Manage(s).MarkTrueWithReason(IntegrationSinkConditionEventPoliciesReady, reason, messageFormat, messageA...) |
| 94 | +} |
| 95 | + |
| 96 | +func (s *IntegrationSinkStatus) PropagateDeploymentStatus(d *appsv1.DeploymentStatus) { |
| 97 | + deploymentAvailableFound := false |
| 98 | + for _, cond := range d.Conditions { |
| 99 | + if cond.Type == appsv1.DeploymentAvailable { |
| 100 | + deploymentAvailableFound = true |
| 101 | + if cond.Status == corev1.ConditionTrue { |
| 102 | + IntegrationSinkCondSet.Manage(s).MarkTrue(IntegrationSinkConditionDeploymentReady) |
| 103 | + } else if cond.Status == corev1.ConditionFalse { |
| 104 | + IntegrationSinkCondSet.Manage(s).MarkFalse(IntegrationSinkConditionDeploymentReady, cond.Reason, cond.Message) |
| 105 | + } else if cond.Status == corev1.ConditionUnknown { |
| 106 | + IntegrationSinkCondSet.Manage(s).MarkUnknown(IntegrationSinkConditionDeploymentReady, cond.Reason, cond.Message) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + if !deploymentAvailableFound { |
| 111 | + IntegrationSinkCondSet.Manage(s).MarkUnknown(IntegrationSinkConditionDeploymentReady, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (s *IntegrationSinkStatus) SetAddress(address *duckv1.Addressable) { |
| 116 | + s.Address = address |
| 117 | + if address == nil || address.URL.IsEmpty() { |
| 118 | + IntegrationSinkCondSet.Manage(s).MarkFalse(IntegrationSinkConditionAddressable, "EmptyHostname", "hostname is the empty string") |
| 119 | + } else { |
| 120 | + IntegrationSinkCondSet.Manage(s).MarkTrue(IntegrationSinkConditionAddressable) |
| 121 | + |
| 122 | + } |
| 123 | +} |
0 commit comments