-
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?
Conversation
shivamerla
commented
Feb 7, 2025
- Default named port of "api" will be added with legacy .spec.port attribute
- Ability to setup multiple named ports "api", "metrics" etc.
- auto detection of metrics and api ports for probes and metrics collection
* Default named port of "api" will be added with legacy .spec.port attribute * Ability to setup multiple named ports "api", "metrics" etc. * auto detection of metrics and api ports for probes and metrics collection Signed-off-by: Shiva Krishna, Merla <[email protected]>
} | ||
|
||
// getProbePort determines the appropriate port for probes based on the service spec. | ||
func getProbePort(serviceSpec Service) intstr.IntOrString { |
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 the ServiceParams.Ports
list
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.
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.
…onfigurable Signed-off-by: Shiva Krishna, Merla <[email protected]>
Type: intstr.Type(0), | ||
IntVal: n.Spec.Expose.Service.Port, | ||
}, | ||
Port: getProbePort(n.Spec.Expose.Service), |
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.
This implies that .spec.expose.service
is required, but it is not currently.
Maybe we should make it a required field or add a default value?
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.
yes, we can make this as a required field, earlier there was just single port param and default for that, so it was always present.
api/apps/v1alpha1/common_types.go
Outdated
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 != "" { | ||
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 comment
The 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:
- Prefer "api"
- Otherwise, pick first port
Isn't this the case? Defaulting to "api" when other options exist doesnt seem right to me.
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 != "" { | |
return intstr.FromString(portName) | |
} | |
// Default when multiple ports exist | |
return intstr.FromString("api") | |
} | |
} | |
switch len(serviceSpec.Ports) { | |
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 != "" { | |
return intstr.FromString(portName) | |
} | |
// Default when multiple ports exist | |
port := serviceSpec.Ports[0] | |
if port.Name != "" { | |
return intstr.FromString(port.Name) | |
} | |
return intstr.FromInt(int(port.Port)) | |
} | |
} |
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 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.
api/apps/v1alpha1/common_types.go
Outdated
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 comment
The 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 comment
The 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.
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 comment
The 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 api
like the test case in L63?
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.
when say user provides input as below:
expose:
service:
type: ClusterIP
ports:
- port: 8000
protocol: TCP
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.
Signed-off-by: Shiva Krishna, Merla <[email protected]>