-
Notifications
You must be signed in to change notification settings - Fork 582
/
apiserver_lifecycle.go
149 lines (121 loc) · 6.24 KB
/
apiserver_lifecycle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/apis"
)
const (
// ApiServerConditionReady has status True when the ApiServerSource is ready to send events.
ApiServerConditionReady = apis.ConditionReady
// ApiServerConditionSinkProvided has status True when the ApiServerSource has been configured with a sink target.
ApiServerConditionSinkProvided apis.ConditionType = "SinkProvided"
// ApiServerConditionDeployed has status True when the ApiServerSource has had it's deployment created.
ApiServerConditionDeployed apis.ConditionType = "Deployed"
// ApiServerConditionSufficientPermissions has status True when the ApiServerSource has sufficient permissions to access resources.
ApiServerConditionSufficientPermissions apis.ConditionType = "SufficientPermissions"
// ApiServerConditionOIDCIdentityCreated has status True when the ApiServerSource has created an OIDC identity.
ApiServerConditionOIDCIdentityCreated apis.ConditionType = "OIDCIdentityCreated"
)
var apiserverCondSet = apis.NewLivingConditionSet(
ApiServerConditionSinkProvided,
ApiServerConditionDeployed,
ApiServerConditionSufficientPermissions,
ApiServerConditionOIDCIdentityCreated,
)
// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*ApiServerSource) GetConditionSet() apis.ConditionSet {
return apiserverCondSet
}
// GetGroupVersionKind returns the GroupVersionKind.
func (*ApiServerSource) GetGroupVersionKind() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("ApiServerSource")
}
// GetUntypedSpec returns the spec of the ApiServerSource.
func (s *ApiServerSource) GetUntypedSpec() interface{} {
return s.Spec
}
// GetCondition returns the condition currently associated with the given type, or nil.
func (s *ApiServerSourceStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return apiserverCondSet.Manage(s).GetCondition(t)
}
// GetTopLevelCondition returns the top level condition.
func (s *ApiServerSourceStatus) GetTopLevelCondition() *apis.Condition {
return apiserverCondSet.Manage(s).GetTopLevelCondition()
}
// InitializeConditions sets relevant unset conditions to Unknown state.
func (s *ApiServerSourceStatus) InitializeConditions() {
apiserverCondSet.Manage(s).InitializeConditions()
}
// MarkSink sets the condition that the source has a sink configured.
func (s *ApiServerSourceStatus) MarkSink(addr *duckv1.Addressable) {
if addr != nil {
s.SinkURI = addr.URL
s.SinkCACerts = addr.CACerts
s.SinkAudience = addr.Audience
apiserverCondSet.Manage(s).MarkTrue(ApiServerConditionSinkProvided)
} else {
apiserverCondSet.Manage(s).MarkFalse(ApiServerConditionSinkProvided, "SinkEmpty", "Sink has resolved to empty.%s", "")
}
}
// MarkNoSink sets the condition that the source does not have a sink configured.
func (s *ApiServerSourceStatus) MarkNoSink(reason, messageFormat string, messageA ...interface{}) {
apiserverCondSet.Manage(s).MarkFalse(ApiServerConditionSinkProvided, reason, messageFormat, messageA...)
}
// PropagateDeploymentAvailability uses the availability of the provided Deployment to determine if
// ApiServerConditionDeployed should be marked as true or false.
func (s *ApiServerSourceStatus) PropagateDeploymentAvailability(d *appsv1.Deployment) {
deploymentAvailableFound := false
for _, cond := range d.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
deploymentAvailableFound = true
if cond.Status == corev1.ConditionTrue {
apiserverCondSet.Manage(s).MarkTrue(ApiServerConditionDeployed)
} else if cond.Status == corev1.ConditionFalse {
apiserverCondSet.Manage(s).MarkFalse(ApiServerConditionDeployed, cond.Reason, cond.Message)
} else if cond.Status == corev1.ConditionUnknown {
apiserverCondSet.Manage(s).MarkUnknown(ApiServerConditionDeployed, cond.Reason, cond.Message)
}
}
}
if !deploymentAvailableFound {
apiserverCondSet.Manage(s).MarkUnknown(ApiServerConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
}
}
// MarkSufficientPermissions sets the condition that the source has enough permissions to access the resources.
func (s *ApiServerSourceStatus) MarkSufficientPermissions() {
apiserverCondSet.Manage(s).MarkTrue(ApiServerConditionSufficientPermissions)
}
// MarkNoSufficientPermissions sets the condition that the source does not have enough permissions to access the resources
func (s *ApiServerSourceStatus) MarkNoSufficientPermissions(reason, messageFormat string, messageA ...interface{}) {
apiserverCondSet.Manage(s).MarkFalse(ApiServerConditionSufficientPermissions, reason, messageFormat, messageA...)
}
// IsReady returns true if the resource is ready overall.
func (s *ApiServerSourceStatus) IsReady() bool {
return apiserverCondSet.Manage(s).IsHappy()
}
func (s *ApiServerSourceStatus) MarkOIDCIdentityCreatedSucceeded() {
apiserverCondSet.Manage(s).MarkTrue(ApiServerConditionOIDCIdentityCreated)
}
func (s *ApiServerSourceStatus) MarkOIDCIdentityCreatedSucceededWithReason(reason, messageFormat string, messageA ...interface{}) {
apiserverCondSet.Manage(s).MarkTrueWithReason(ApiServerConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}
func (s *ApiServerSourceStatus) MarkOIDCIdentityCreatedFailed(reason, messageFormat string, messageA ...interface{}) {
apiserverCondSet.Manage(s).MarkFalse(ApiServerConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}
func (s *ApiServerSourceStatus) MarkOIDCIdentityCreatedUnknown(reason, messageFormat string, messageA ...interface{}) {
apiserverCondSet.Manage(s).MarkUnknown(ApiServerConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}