@@ -16,6 +16,7 @@ import (
16
16
"github.com/ory/viper"
17
17
corev1 "k8s.io/api/core/v1"
18
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
+ "k8s.io/apimachinery/pkg/types"
19
20
"k8s.io/client-go/kubernetes"
20
21
"k8s.io/client-go/rest"
21
22
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -93,6 +94,15 @@ func ParseHealthCheckOptionsFromAnnotations(annotations map[string]string) *crus
93
94
return healthCheckOptions
94
95
}
95
96
97
+ func isNodeReady (node * corev1.Node ) bool {
98
+ for _ , condition := range node .Status .Conditions {
99
+ if condition .Type == corev1 .NodeReady && condition .Status == corev1 .ConditionTrue {
100
+ return true
101
+ }
102
+ }
103
+ return false
104
+ }
105
+
96
106
func (r * ServiceReconciler ) parseListenPortsAndBackends (ctx context.Context , svc * corev1.Service , logger logr.Logger ) []crusoeapi.ListenPortAndBackend {
97
107
listenPortsAndBackends := []crusoeapi.ListenPortAndBackend {}
98
108
@@ -103,9 +113,17 @@ func (r *ServiceReconciler) parseListenPortsAndBackends(ctx context.Context, svc
103
113
return listenPortsAndBackends
104
114
}
105
115
116
+ // Filter out only Ready nodes
117
+ var readyNodes []corev1.Node
118
+ for _ , node := range nodeList .Items {
119
+ if isNodeReady (& node ) {
120
+ readyNodes = append (readyNodes , node )
121
+ }
122
+ }
123
+
106
124
// Extract internal IPs of all nodes
107
125
internalIPs := []string {}
108
- for _ , node := range nodeList . Items {
126
+ for _ , node := range readyNodes {
109
127
for _ , address := range node .Status .Addresses {
110
128
if address .Type == corev1 .NodeInternalIP {
111
129
internalIPs = append (internalIPs , address .Address )
@@ -114,18 +132,37 @@ func (r *ServiceReconciler) parseListenPortsAndBackends(ctx context.Context, svc
114
132
}
115
133
logger .Info ("Retrieved internal IPs of nodes" , "internalIPs" , internalIPs )
116
134
135
+ // 1. Retrieve the NodePort Service from the cluster
136
+ nodePortSvc := & corev1.Service {}
137
+ err := r .Client .Get (ctx , types.NamespacedName {Name : utils .GenerateNodePortServiceName (svc .Name ), Namespace : svc .Namespace }, nodePortSvc )
138
+ if err != nil {
139
+ logger .Error (err , "Failed to get NodePort service" )
140
+ }
141
+
142
+ // 2. Extract the NodePort from its Spec
143
+ var nodePorts []int32
144
+ for _ , port := range nodePortSvc .Spec .Ports {
145
+ if port .NodePort != 0 {
146
+ nodePorts = append (nodePorts , port .NodePort )
147
+ }
148
+ }
149
+ logger .Info ("Discovered NodePorts" , "nodePorts" , nodePorts )
150
+
117
151
// Map backends using the retrieved internal IPs
118
152
for _ , port := range svc .Spec .Ports {
119
153
backends := []crusoeapi.Backend {}
120
154
121
- for _ , ip := range internalIPs {
122
- backend := crusoeapi.Backend {
123
- Ip : ip ,
124
- Port : int64 (port .TargetPort .IntVal ), // Use the TargetPort from service spec
155
+ for _ , nodePortVal := range nodePorts {
156
+ for _ , ip := range internalIPs {
157
+ backends = append (backends , crusoeapi.Backend {
158
+ Ip : ip ,
159
+ Port : int64 (nodePortVal ),
160
+ })
125
161
}
126
- backends = append (backends , backend )
127
162
}
128
163
164
+ logger .Info ("Discovered backends" , "backends" , backends , "listenport" , port .Port )
165
+
129
166
// Append to the list of ListenPortAndBackend
130
167
listenPortsAndBackends = append (listenPortsAndBackends , crusoeapi.ListenPortAndBackend {
131
168
ListenPort : int64 (port .Port ), // Map the port exposed by the service
0 commit comments