-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to configure multiple service ports #316
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
autoscalingv2 "k8s.io/api/autoscaling/v2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
corev1 "k8s.io/api/core/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
networkingv1 "k8s.io/api/networking/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"k8s.io/apimachinery/pkg/util/intstr" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Expose defines attributes to expose the service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -115,3 +116,59 @@ type CertConfig struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// MountPath is the path where the certificates should be mounted in the container. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MountPath string `json:"mountPath"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// selectNamedPort returns the first occurrence of a given named port, or an empty string if not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func selectNamedPort(serviceSpec Service, portNames ...string) string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, name := range portNames { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, port := range serviceSpec.Ports { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if port.Name == name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// getProbePort determines the appropriate port for probes based on the service spec. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func getProbePort(serviceSpec Service) intstr.IntOrString { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
switch len(serviceSpec.Ports) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
port := serviceSpec.Ports[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if port.Name != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString(port.Name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromInt(int(port.Port)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Default to "api" as the operator always adds a default named port with 8000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString("api") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Multiple ports: Prefer "api" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if portName := selectNamedPort(serviceSpec, "api"); portName != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
shivamerla marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString(portName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Default when multiple ports exist | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString("api") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case where length is 1 and default case where length > 1 should have similar behavior:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We just can't assume to use the first port when multiple ports are given. For e.g. in case of Customizer, if "api" and "internal" ports are used with different names, we can't assume "internal" ports for liveness/startup probes etc. Hence i was always enforcing that a named port of "api" should be specified in that case. Probably better things is to validate this during reconciliation and error out when required named ports are not provided. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// getMetricsPort determines the appropriate port for metrics based on the service spec. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func getMetricsPort(serviceSpec Service) intstr.IntOrString { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
switch len(serviceSpec.Ports) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
port := serviceSpec.Ports[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if port.Name != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString(port.Name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromInt(int(port.Port)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Default to "api" as the operator always adds a default named port with 8000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString("api") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Multiple ports: Prefer "metrics", fallback to "api" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if portName := selectNamedPort(serviceSpec, "metrics", "api"); portName != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString(portName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Default when multiple ports exist | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return intstr.FromString("metrics") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly here: Shouldn't we be getting the first port from the list if "metrics" and "api" dont exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same logic as above, in documentation we have to specify that named ports for "api" or "metrics" should be provided when they are served differently by the underlying service. Randomly assigning would not work. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2025. | ||
|
||
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 v1alpha1 | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
// assertEqual checks if the actual result matches the expected result. | ||
func assertEqual(t *testing.T, actual, expected intstr.IntOrString) { | ||
if actual != expected { | ||
t.Errorf("Got %v, expected %v", actual, expected) | ||
} | ||
} | ||
|
||
func TestGetProbePort(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
serviceSpec Service | ||
expected intstr.IntOrString | ||
}{ | ||
{"Single named port", Service{Ports: []corev1.ServicePort{{Name: "api", Port: 8080}}}, intstr.FromString("api")}, | ||
{"Single unnamed port", Service{Ports: []corev1.ServicePort{{Port: 9090}}}, intstr.FromInt(9090)}, | ||
{"Multiple ports - prefers 'api'", Service{Ports: []corev1.ServicePort{{Name: "metrics", Port: 9090}, {Name: "api", Port: 8080}}}, intstr.FromString("api")}, | ||
{"No ports - uses legacy Port field", Service{Port: 7070}, intstr.FromString("api")}, | ||
{"No ports at all - defaults to 'api'", Service{}, intstr.FromString("api")}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assertEqual(t, getProbePort(tt.serviceSpec), tt.expected) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetMetricsPort(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
serviceSpec Service | ||
expected intstr.IntOrString | ||
}{ | ||
{"Single named port", Service{Ports: []corev1.ServicePort{{Name: "metrics", Port: 8081}}}, intstr.FromString("metrics")}, | ||
{"Single unnamed port", Service{Ports: []corev1.ServicePort{{Port: 8181}}}, intstr.FromInt(8181)}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trying to understand the logic.. in this case, why it shouldn't produce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when say user provides input as below:
then, in the deployment spec, we don't add any named ports like "api". Hence we use the port number that is given to us. we will add a default named port ("api: 8000" ) when no ports are given in the config at all. |
||
{"Multiple ports - prefers 'metrics'", Service{Ports: []corev1.ServicePort{{Name: "api", Port: 8080}, {Name: "metrics", Port: 9090}}}, intstr.FromString("metrics")}, | ||
{"Multiple ports - no 'metrics', uses 'api'", Service{Ports: []corev1.ServicePort{{Name: "grpc", Port: 5050}, {Name: "api", Port: 8080}}}, intstr.FromString("api")}, | ||
{"No ports - uses legacy Port field", Service{Port: 6060}, intstr.FromString("api")}, | ||
{"No ports at all - defaults to 'metrics'", Service{}, intstr.FromString("api")}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assertEqual(t, getMetricsPort(tt.serviceSpec), tt.expected) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can probably use
rendertypes.ServiceParams
as input here and simplify this to always prefer "api" namedPort from theServiceParams.Ports
listThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each resource is rendered independently during reconciliation. That will add a dependency that we always have to render serviceParams before setting up deployment or servicemonitor.