mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-22 18:37:15 +00:00
Adding AppProtocol to Service and Endpoints Ports
This commit is contained in:
@@ -65,6 +65,7 @@ go_test(
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/testing:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
|
||||
"//vendor/k8s.io/utils/pointer:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@@ -444,17 +444,14 @@ func (e *EndpointController) syncService(key string) error {
|
||||
} else {
|
||||
for i := range service.Spec.Ports {
|
||||
servicePort := &service.Spec.Ports[i]
|
||||
|
||||
portName := servicePort.Name
|
||||
portProto := servicePort.Protocol
|
||||
portNum, err := podutil.FindPort(pod, servicePort)
|
||||
if err != nil {
|
||||
klog.V(4).Infof("Failed to find port for service %s/%s: %v", service.Namespace, service.Name, err)
|
||||
continue
|
||||
}
|
||||
epp := endpointPortFromServicePort(servicePort, portNum)
|
||||
|
||||
var readyEps, notReadyEps int
|
||||
epp := &v1.EndpointPort{Name: portName, Port: int32(portNum), Protocol: portProto}
|
||||
subsets, readyEps, notReadyEps = addEndpointSubset(subsets, pod, epa, epp, tolerateUnreadyEndpoints)
|
||||
totalReadyEps = totalReadyEps + readyEps
|
||||
totalNotReadyEps = totalNotReadyEps + notReadyEps
|
||||
@@ -608,3 +605,15 @@ func shouldPodBeInEndpoints(pod *v1.Pod) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func endpointPortFromServicePort(servicePort *v1.ServicePort, portNum int) *v1.EndpointPort {
|
||||
epp := &v1.EndpointPort{
|
||||
Name: servicePort.Name,
|
||||
Port: int32(portNum),
|
||||
Protocol: servicePort.Protocol,
|
||||
}
|
||||
if utilfeature.DefaultFeatureGate.Enabled(features.ServiceAppProtocol) {
|
||||
epp.AppProtocol = servicePort.AppProtocol
|
||||
}
|
||||
return epp
|
||||
}
|
||||
|
@@ -45,6 +45,7 @@ import (
|
||||
"k8s.io/kubernetes/pkg/controller"
|
||||
endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
utilpointer "k8s.io/utils/pointer"
|
||||
)
|
||||
|
||||
var alwaysReady = func() bool { return true }
|
||||
@@ -1947,6 +1948,55 @@ func TestSyncEndpointsServiceNotFound(t *testing.T) {
|
||||
endpointsHandler.ValidateRequest(t, "/api/v1/namespaces/"+ns+"/endpoints/foo", "DELETE", nil)
|
||||
}
|
||||
|
||||
func TestEndpointPortFromServicePort(t *testing.T) {
|
||||
http := utilpointer.StringPtr("http")
|
||||
testCases := map[string]struct {
|
||||
featureGateEnabled bool
|
||||
serviceAppProtocol *string
|
||||
expectedEndpointsAppProtocol *string
|
||||
}{
|
||||
"feature gate disabled, empty app protocol": {
|
||||
featureGateEnabled: false,
|
||||
serviceAppProtocol: nil,
|
||||
expectedEndpointsAppProtocol: nil,
|
||||
},
|
||||
"feature gate disabled, http app protocol": {
|
||||
featureGateEnabled: false,
|
||||
serviceAppProtocol: http,
|
||||
expectedEndpointsAppProtocol: nil,
|
||||
},
|
||||
"feature gate enabled, empty app protocol": {
|
||||
featureGateEnabled: true,
|
||||
serviceAppProtocol: nil,
|
||||
expectedEndpointsAppProtocol: nil,
|
||||
},
|
||||
"feature gate enabled, http app protocol": {
|
||||
featureGateEnabled: true,
|
||||
serviceAppProtocol: http,
|
||||
expectedEndpointsAppProtocol: http,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ServiceAppProtocol, tc.featureGateEnabled)()
|
||||
|
||||
epp := endpointPortFromServicePort(&v1.ServicePort{Name: "test", AppProtocol: tc.serviceAppProtocol}, 80)
|
||||
|
||||
if epp.AppProtocol != tc.expectedEndpointsAppProtocol {
|
||||
t.Errorf("Expected Endpoints AppProtocol to be %s, got %s", stringVal(tc.expectedEndpointsAppProtocol), stringVal(epp.AppProtocol))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func stringVal(str *string) string {
|
||||
if str == nil {
|
||||
return "nil"
|
||||
}
|
||||
return *str
|
||||
}
|
||||
|
||||
func podChangedHelper(oldPod, newPod *v1.Pod, endpointChanged endpointutil.EndpointsMatch) bool {
|
||||
podChanged, _ := endpointutil.PodChanged(oldPod, newPod, endpointChanged)
|
||||
return podChanged
|
||||
|
Reference in New Issue
Block a user