Skip to content

Commit e6f5f4f

Browse files
committed
Fix available controller assumption about port 443
1 parent c1076ba commit e6f5f4f

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

staging/src/k8s.io/kube-aggregator/pkg/controllers/status/available_controller.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,20 @@ func (c *AvailableConditionController) sync(key string) error {
185185
}
186186

187187
if service.Spec.Type == v1.ServiceTypeClusterIP {
188-
// if we have a cluster IP service, it must be listening on 443 and we can check that
188+
// if we have a cluster IP service, it must be listening on configured port and we can check that
189+
servicePort := apiService.Spec.Service.Port
190+
portName := ""
189191
foundPort := false
190192
for _, port := range service.Spec.Ports {
191-
if port.Port == 443 {
193+
if port.Port == servicePort {
192194
foundPort = true
195+
portName = port.Name
193196
}
194197
}
195198
if !foundPort {
196199
availableCondition.Status = apiregistration.ConditionFalse
197200
availableCondition.Reason = "ServicePortError"
198-
availableCondition.Message = fmt.Sprintf("service/%s in %q is not listening on port 443", apiService.Spec.Service.Name, apiService.Spec.Service.Namespace)
201+
availableCondition.Message = fmt.Sprintf("service/%s in %q is not listening on port %d", apiService.Spec.Service.Name, apiService.Spec.Service.Namespace, apiService.Spec.Service.Port)
199202
apiregistration.SetAPIServiceCondition(apiService, availableCondition)
200203
_, err := updateAPIServiceStatus(c.apiServiceClient, originalAPIService, apiService)
201204
return err
@@ -219,15 +222,19 @@ func (c *AvailableConditionController) sync(key string) error {
219222
}
220223
hasActiveEndpoints := false
221224
for _, subset := range endpoints.Subsets {
222-
if len(subset.Addresses) > 0 {
223-
hasActiveEndpoints = true
224-
break
225+
if len(subset.Addresses) == 0 {
226+
continue
227+
}
228+
for _, endpointPort := range subset.Ports {
229+
if endpointPort.Name == portName {
230+
hasActiveEndpoints = true
231+
}
225232
}
226233
}
227234
if !hasActiveEndpoints {
228235
availableCondition.Status = apiregistration.ConditionFalse
229236
availableCondition.Reason = "MissingEndpoints"
230-
availableCondition.Message = fmt.Sprintf("endpoints for service/%s in %q have no addresses", apiService.Spec.Service.Name, apiService.Spec.Service.Namespace)
237+
availableCondition.Message = fmt.Sprintf("endpoints for service/%s in %q have no addresses with port name %q", apiService.Spec.Service.Name, apiService.Spec.Service.Namespace, portName)
231238
apiregistration.SetAPIServiceCondition(apiService, availableCondition)
232239
_, err := updateAPIServiceStatus(c.apiServiceClient, originalAPIService, apiService)
233240
return err

staging/src/k8s.io/kube-aggregator/pkg/controllers/status/available_controller_test.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package apiserver
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/davecgh/go-spew/spew"
@@ -31,13 +32,18 @@ import (
3132
listers "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion"
3233
)
3334

35+
const (
36+
testServicePort = 1234
37+
testServicePortName = "testPort"
38+
)
39+
3440
func newEndpoints(namespace, name string) *v1.Endpoints {
3541
return &v1.Endpoints{
3642
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name},
3743
}
3844
}
3945

40-
func newEndpointsWithAddress(namespace, name string) *v1.Endpoints {
46+
func newEndpointsWithAddress(namespace, name string, port int32, portName string) *v1.Endpoints {
4147
return &v1.Endpoints{
4248
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name},
4349
Subsets: []v1.EndpointSubset{
@@ -47,18 +53,24 @@ func newEndpointsWithAddress(namespace, name string) *v1.Endpoints {
4753
IP: "val",
4854
},
4955
},
56+
Ports: []v1.EndpointPort{
57+
{
58+
Name: portName,
59+
Port: port,
60+
},
61+
},
5062
},
5163
},
5264
}
5365
}
5466

55-
func newService(namespace, name string) *v1.Service {
67+
func newService(namespace, name string, port int32, portName string) *v1.Service {
5668
return &v1.Service{
5769
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name},
5870
Spec: v1.ServiceSpec{
5971
Type: v1.ServiceTypeClusterIP,
6072
Ports: []v1.ServicePort{
61-
{Port: 443},
73+
{Port: port, Name: portName},
6274
},
6375
},
6476
}
@@ -77,6 +89,7 @@ func newRemoteAPIService(name string) *apiregistration.APIService {
7789
Service: &apiregistration.ServiceReference{
7890
Namespace: "foo",
7991
Name: "bar",
92+
Port: testServicePort,
8093
},
8194
},
8295
}
@@ -107,7 +120,7 @@ func TestSync(t *testing.T) {
107120
name: "no service",
108121
apiServiceName: "remote.group",
109122
apiServices: []*apiregistration.APIService{newRemoteAPIService("remote.group")},
110-
services: []*v1.Service{newService("foo", "not-bar")},
123+
services: []*v1.Service{newService("foo", "not-bar", testServicePort, testServicePortName)},
111124
expectedAvailability: apiregistration.APIServiceCondition{
112125
Type: apiregistration.Available,
113126
Status: apiregistration.ConditionFalse,
@@ -128,19 +141,19 @@ func TestSync(t *testing.T) {
128141
},
129142
},
130143
}},
131-
endpoints: []*v1.Endpoints{newEndpointsWithAddress("foo", "bar")},
144+
endpoints: []*v1.Endpoints{newEndpointsWithAddress("foo", "bar", testServicePort, testServicePortName)},
132145
expectedAvailability: apiregistration.APIServiceCondition{
133146
Type: apiregistration.Available,
134147
Status: apiregistration.ConditionFalse,
135148
Reason: "ServicePortError",
136-
Message: `service/bar in "foo" is not listening on port 443`,
149+
Message: fmt.Sprintf(`service/bar in "foo" is not listening on port %d`, testServicePort),
137150
},
138151
},
139152
{
140153
name: "no endpoints",
141154
apiServiceName: "remote.group",
142155
apiServices: []*apiregistration.APIService{newRemoteAPIService("remote.group")},
143-
services: []*v1.Service{newService("foo", "bar")},
156+
services: []*v1.Service{newService("foo", "bar", testServicePort, testServicePortName)},
144157
expectedAvailability: apiregistration.APIServiceCondition{
145158
Type: apiregistration.Available,
146159
Status: apiregistration.ConditionFalse,
@@ -152,21 +165,34 @@ func TestSync(t *testing.T) {
152165
name: "missing endpoints",
153166
apiServiceName: "remote.group",
154167
apiServices: []*apiregistration.APIService{newRemoteAPIService("remote.group")},
155-
services: []*v1.Service{newService("foo", "bar")},
168+
services: []*v1.Service{newService("foo", "bar", testServicePort, testServicePortName)},
156169
endpoints: []*v1.Endpoints{newEndpoints("foo", "bar")},
157170
expectedAvailability: apiregistration.APIServiceCondition{
158171
Type: apiregistration.Available,
159172
Status: apiregistration.ConditionFalse,
160173
Reason: "MissingEndpoints",
161-
Message: `endpoints for service/bar in "foo" have no addresses`,
174+
Message: `endpoints for service/bar in "foo" have no addresses with port name "testPort"`,
175+
},
176+
},
177+
{
178+
name: "wrong endpoint port name",
179+
apiServiceName: "remote.group",
180+
apiServices: []*apiregistration.APIService{newRemoteAPIService("remote.group")},
181+
services: []*v1.Service{newService("foo", "bar", testServicePort, testServicePortName)},
182+
endpoints: []*v1.Endpoints{newEndpointsWithAddress("foo", "bar", testServicePort, "wrongName")},
183+
expectedAvailability: apiregistration.APIServiceCondition{
184+
Type: apiregistration.Available,
185+
Status: apiregistration.ConditionFalse,
186+
Reason: "MissingEndpoints",
187+
Message: fmt.Sprintf(`endpoints for service/bar in "foo" have no addresses with port name "%s"`, testServicePortName),
162188
},
163189
},
164190
{
165191
name: "remote",
166192
apiServiceName: "remote.group",
167193
apiServices: []*apiregistration.APIService{newRemoteAPIService("remote.group")},
168-
services: []*v1.Service{newService("foo", "bar")},
169-
endpoints: []*v1.Endpoints{newEndpointsWithAddress("foo", "bar")},
194+
services: []*v1.Service{newService("foo", "bar", testServicePort, testServicePortName)},
195+
endpoints: []*v1.Endpoints{newEndpointsWithAddress("foo", "bar", testServicePort, testServicePortName)},
170196
expectedAvailability: apiregistration.APIServiceCondition{
171197
Type: apiregistration.Available,
172198
Status: apiregistration.ConditionTrue,

0 commit comments

Comments
 (0)