-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathagent_endpoint_conditions.go
More file actions
137 lines (119 loc) · 4.5 KB
/
agent_endpoint_conditions.go
File metadata and controls
137 lines (119 loc) · 4.5 KB
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
package agent
import (
ngrokv1alpha1 "github.com/ngrok/ngrok-operator/api/ngrok/v1alpha1"
domainpkg "github.com/ngrok/ngrok-operator/internal/domain"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Standard condition types for AgentEndpoint
const (
ConditionReady = "Ready"
ConditionEndpointCreated = "EndpointCreated"
ConditionTrafficPolicy = "TrafficPolicyApplied"
)
// Standard condition reasons
const (
ReasonEndpointActive = "EndpointActive"
ReasonTrafficPolicyError = "TrafficPolicyError"
ReasonNgrokAPIError = "NgrokAPIError"
ReasonUpstreamError = "UpstreamError"
ReasonEndpointCreated = "EndpointCreated"
ReasonConfigError = "ConfigurationError"
)
// setReadyCondition sets the Ready condition based on the overall endpoint state
func setReadyCondition(endpoint *ngrokv1alpha1.AgentEndpoint, ready bool, reason, message string) {
status := metav1.ConditionTrue
if !ready {
status = metav1.ConditionFalse
}
condition := metav1.Condition{
Type: ConditionReady,
Status: status,
Reason: reason,
Message: message,
ObservedGeneration: endpoint.Generation,
}
meta.SetStatusCondition(&endpoint.Status.Conditions, condition)
}
// setEndpointCreatedCondition sets the EndpointCreated condition
func setEndpointCreatedCondition(endpoint *ngrokv1alpha1.AgentEndpoint, created bool, reason, message string) {
status := metav1.ConditionTrue
if !created {
status = metav1.ConditionFalse
}
condition := metav1.Condition{
Type: ConditionEndpointCreated,
Status: status,
Reason: reason,
Message: message,
ObservedGeneration: endpoint.Generation,
}
meta.SetStatusCondition(&endpoint.Status.Conditions, condition)
}
// setTrafficPolicyCondition sets the TrafficPolicyApplied condition
func setTrafficPolicyCondition(endpoint *ngrokv1alpha1.AgentEndpoint, applied bool, reason, message string) {
status := metav1.ConditionTrue
if !applied {
status = metav1.ConditionFalse
}
condition := metav1.Condition{
Type: ConditionTrafficPolicy,
Status: status,
Reason: reason,
Message: message,
ObservedGeneration: endpoint.Generation,
}
meta.SetStatusCondition(&endpoint.Status.Conditions, condition)
}
// calculateAgentEndpointReadyCondition calculates the overall Ready condition based on other conditions and domain status
func calculateAgentEndpointReadyCondition(aep *ngrokv1alpha1.AgentEndpoint, domainResult *domainpkg.DomainResult) {
// Check all required conditions
endpointCreatedCondition := meta.FindStatusCondition(aep.Status.Conditions, ConditionEndpointCreated)
endpointCreated := endpointCreatedCondition != nil && endpointCreatedCondition.Status == metav1.ConditionTrue
trafficPolicyCondition := meta.FindStatusCondition(aep.Status.Conditions, ConditionTrafficPolicy)
trafficPolicyReady := true
// If traffic policy condition exists and is False, it's not ready
if trafficPolicyCondition != nil && trafficPolicyCondition.Status == metav1.ConditionFalse {
trafficPolicyReady = false
}
// Check if domain is ready (default to false for safety)
domainReady := false
if domainResult != nil {
domainReady = domainResult.IsReady
}
// Overall ready status - all conditions must be true
ready := endpointCreated && trafficPolicyReady && domainReady
// Determine reason and message based on state
var reason, message string
switch {
case ready:
reason = ReasonEndpointActive
message = "AgentEndpoint is active and ready"
case !domainReady:
// Use the domain's Ready condition reason/message for better context
if domainResult != nil && domainResult.ReadyReason != "" {
reason = domainResult.ReadyReason
message = domainResult.ReadyMessage
} else {
reason = "DomainNotReady"
message = "Domain is not ready"
}
case !endpointCreated:
// If EndpointCreated condition exists and is False, use its reason/message
if endpointCreatedCondition != nil && endpointCreatedCondition.Status == metav1.ConditionFalse {
reason = endpointCreatedCondition.Reason
message = endpointCreatedCondition.Message
} else {
reason = "Pending"
message = "Waiting for endpoint creation"
}
case !trafficPolicyReady:
// Use the traffic policy's condition reason/message
reason = trafficPolicyCondition.Reason
message = trafficPolicyCondition.Message
default:
reason = "Unknown"
message = "AgentEndpoint is not ready"
}
setReadyCondition(aep, ready, reason, message)
}