|
| 1 | +/* |
| 2 | +Copyright 2019 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 | + "knative.dev/eventing/pkg/apis/duck" |
| 22 | + "knative.dev/pkg/apis" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + // SampleConditionReady has status True when the SampleSource is ready to send events. |
| 27 | + SampleConditionReady = apis.ConditionReady |
| 28 | + |
| 29 | + // SampleConditionSinkProvided has status True when the SampleSource has been configured with a sink target. |
| 30 | + SampleConditionSinkProvided apis.ConditionType = "SinkProvided" |
| 31 | + |
| 32 | + // SampleConditionDeployed has status True when the SampleSource has had it's deployment created. |
| 33 | + SampleConditionDeployed apis.ConditionType = "Deployed" |
| 34 | +) |
| 35 | + |
| 36 | +var SampleCondSet = apis.NewLivingConditionSet( |
| 37 | + SampleConditionSinkProvided, |
| 38 | + SampleConditionDeployed, |
| 39 | +) |
| 40 | + |
| 41 | +// GetCondition returns the condition currently associated with the given type, or nil. |
| 42 | +func (s *SampleSourceStatus) GetCondition(t apis.ConditionType) *apis.Condition { |
| 43 | + return SampleCondSet.Manage(s).GetCondition(t) |
| 44 | +} |
| 45 | + |
| 46 | +// InitializeConditions sets relevant unset conditions to Unknown state. |
| 47 | +func (s *SampleSourceStatus) InitializeConditions() { |
| 48 | + SampleCondSet.Manage(s).InitializeConditions() |
| 49 | +} |
| 50 | + |
| 51 | +// GetConditionSet returns SampleSource ConditionSet. |
| 52 | +func (*SampleSource) GetConditionSet() apis.ConditionSet { |
| 53 | + return SampleCondSet |
| 54 | +} |
| 55 | + |
| 56 | +// MarkSink sets the condition that the source has a sink configured. |
| 57 | +func (s *SampleSourceStatus) MarkSink(uri *apis.URL) { |
| 58 | + s.SinkURI = uri |
| 59 | + if len(uri.String()) > 0 { |
| 60 | + SampleCondSet.Manage(s).MarkTrue(SampleConditionSinkProvided) |
| 61 | + } else { |
| 62 | + SampleCondSet.Manage(s).MarkUnknown(SampleConditionSinkProvided, "SinkEmpty", "Sink has resolved to empty.") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// MarkNoSink sets the condition that the source does not have a sink configured. |
| 67 | +func (s *SampleSourceStatus) MarkNoSink(reason, messageFormat string, messageA ...interface{}) { |
| 68 | + SampleCondSet.Manage(s).MarkFalse(SampleConditionSinkProvided, reason, messageFormat, messageA...) |
| 69 | +} |
| 70 | + |
| 71 | +// PropagateDeploymentAvailability uses the availability of the provided Deployment to determine if |
| 72 | +// SampleConditionDeployed should be marked as true or false. |
| 73 | +func (s *SampleSourceStatus) PropagateDeploymentAvailability(d *appsv1.Deployment) { |
| 74 | + if duck.DeploymentIsAvailable(&d.Status, false) { |
| 75 | + SampleCondSet.Manage(s).MarkTrue(SampleConditionDeployed) |
| 76 | + } else { |
| 77 | + // I don't know how to propagate the status well, so just give the name of the Deployment |
| 78 | + // for now. |
| 79 | + SampleCondSet.Manage(s).MarkFalse(SampleConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// IsReady returns true if the resource is ready overall. |
| 84 | +func (s *SampleSourceStatus) IsReady() bool { |
| 85 | + return SampleCondSet.Manage(s).IsHappy() |
| 86 | +} |
0 commit comments