Skip to content

Commit 41b8ae6

Browse files
committed
Update service API to include only necessary parameters
Signed-off-by: Shiva Krishna, Merla <smerla@nvidia.com>
1 parent aba76ed commit 41b8ae6

39 files changed

Lines changed: 487 additions & 1047 deletions

api/apps/v1alpha1/common_types.go

Lines changed: 7 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
autoscalingv2 "k8s.io/api/autoscaling/v2"
2222
corev1 "k8s.io/api/core/v1"
2323
networkingv1 "k8s.io/api/networking/v1"
24-
"k8s.io/apimachinery/pkg/util/intstr"
2524
)
2625

2726
const (
28-
// DefaultAPIPort is the default API port
27+
// DefaultAPIPort is the default api port
2928
DefaultAPIPort = 8000
30-
// DefaultNamedPortAPI is the default named API port
29+
// DefaultNamedPortAPI is the default name for api port
3130
DefaultNamedPortAPI = "api"
32-
// DefaultNamedPortMetrics is the default named Metrics port
31+
// DefaultNamedPortMetrics is the default name for metrics port
3332
DefaultNamedPortMetrics = "metrics"
3433
)
3534

@@ -44,29 +43,11 @@ type Service struct {
4443
Type corev1.ServiceType `json:"type,omitempty"`
4544
// override the default service name
4645
Name string `json:"name,omitempty"`
47-
// Deprecated: Use Ports instead.
48-
// +kubebuilder:deprecatedversion
49-
Port int32 `json:"port,omitempty"`
50-
// Defines multiple ports for the service
51-
Ports []ServicePort `json:"ports,omitempty"`
52-
Annotations map[string]string `json:"annotations,omitempty"`
53-
}
54-
55-
// ServicePort defines attributes to setup the service ports
56-
type ServicePort struct {
57-
// The name of this port within the service.
58-
Name string `json:"name,omitempty"`
59-
60-
// The IP protocol for this port. Supports "TCP", "UDP", and "SCTP".
61-
// Default is TCP.
62-
// +kubebuilder:validation:Enum=TCP;UDP;SCTP
63-
// +kubebuilder:default="TCP"
64-
Protocol corev1.Protocol `json:"protocol,omitempty"`
65-
66-
// The port that will be exposed by this service.
67-
// +kubebuilder:validation:Minimum=1
68-
// +kubebuilder:validation:Maximum=65535
46+
// Port is the main api serving port
6947
Port int32 `json:"port"`
48+
// MetricsPort is the port to be used for the metrics collection (optional)
49+
MetricsPort int32 `json:"metricsPort,omitempty"`
50+
Annotations map[string]string `json:"annotations,omitempty"`
7051
}
7152

7253
// Metrics defines attributes to setup metrics collection
@@ -141,59 +122,3 @@ type CertConfig struct {
141122
// MountPath is the path where the certificates should be mounted in the container.
142123
MountPath string `json:"mountPath"`
143124
}
144-
145-
// selectNamedPort returns the first occurrence of a given named port, or an empty string if not found.
146-
func selectNamedPort(serviceSpec Service, portNames ...string) string {
147-
for _, name := range portNames {
148-
for _, port := range serviceSpec.Ports {
149-
if port.Name == name {
150-
return name
151-
}
152-
}
153-
}
154-
return ""
155-
}
156-
157-
// getProbePort determines the appropriate port for probes based on the service spec.
158-
func getProbePort(serviceSpec Service) intstr.IntOrString {
159-
switch len(serviceSpec.Ports) {
160-
case 1:
161-
port := serviceSpec.Ports[0]
162-
if port.Name != "" {
163-
return intstr.FromString(port.Name)
164-
}
165-
return intstr.FromInt(int(port.Port))
166-
case 0:
167-
// Default to "api" as the operator always adds a default named port with 8000
168-
return intstr.FromString(DefaultNamedPortAPI)
169-
default:
170-
// Multiple ports: Prefer "api"
171-
if portName := selectNamedPort(serviceSpec, DefaultNamedPortAPI); portName != "" {
172-
return intstr.FromString(portName)
173-
}
174-
// Default when multiple ports exist
175-
return intstr.FromString(DefaultNamedPortAPI)
176-
}
177-
}
178-
179-
// getMetricsPort determines the appropriate port for metrics based on the service spec.
180-
func getMetricsPort(serviceSpec Service) intstr.IntOrString {
181-
switch len(serviceSpec.Ports) {
182-
case 1:
183-
port := serviceSpec.Ports[0]
184-
if port.Name != "" {
185-
return intstr.FromString(port.Name)
186-
}
187-
return intstr.FromInt(int(port.Port))
188-
case 0:
189-
// Default to "api" as the operator always adds a default named port with 8000
190-
return intstr.FromString(DefaultNamedPortAPI)
191-
default:
192-
// Multiple ports: Prefer "metrics", fallback to "api"
193-
if portName := selectNamedPort(serviceSpec, DefaultNamedPortMetrics, DefaultNamedPortAPI); portName != "" {
194-
return intstr.FromString(portName)
195-
}
196-
// Default when multiple ports exist
197-
return intstr.FromString(DefaultNamedPortMetrics)
198-
}
199-
}

api/apps/v1alpha1/common_types_test.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

api/apps/v1alpha1/nemo_customizer_types.go

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ import (
3131
networkingv1 "k8s.io/api/networking/v1"
3232
rbacv1 "k8s.io/api/rbac/v1"
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/util/intstr"
3435
)
3536

3637
const (
3738
// CustomizerAPIPort is the default port that customizer serves on
3839
CustomizerAPIPort = 8000
40+
// DefaultNamedPortInternal is the default name for customizer internal port
41+
DefaultNamedPortInternal = "internal"
3942
// CustomizerInternalPort is the default port used for syncing training progress
4043
CustomizerInternalPort = 9009
4144
// NemoCustomizerConditionReady indicates that the NEMO CustomizerService is ready.
@@ -180,7 +183,7 @@ func (n *NemoCustomizer) GetStandardEnv() []corev1.EnvVar {
180183
},
181184
{
182185
Name: "CUSTOMIZATIONS_CALLBACK_URL",
183-
Value: fmt.Sprintf("http://%s.%s.svc.cluster.local:%d", n.GetName(), n.GetNamespace(), n.GetInternalServicePort()),
186+
Value: fmt.Sprintf("http://%s.%s.svc.cluster.local:%d", n.GetName(), n.GetNamespace(), CustomizerInternalPort),
184187
},
185188
{
186189
Name: "LOG_LEVEL",
@@ -446,7 +449,7 @@ func (n *NemoCustomizer) GetDefaultStartupProbe() *corev1.Probe {
446449
ProbeHandler: corev1.ProbeHandler{
447450
HTTPGet: &corev1.HTTPGetAction{
448451
Path: "/v1/health/ready",
449-
Port: getProbePort(n.Spec.Expose.Service),
452+
Port: intstr.FromString(DefaultNamedPortAPI),
450453
},
451454
},
452455
}
@@ -473,7 +476,7 @@ func (n *NemoCustomizer) GetDefaultLivenessProbe() *corev1.Probe {
473476
ProbeHandler: corev1.ProbeHandler{
474477
HTTPGet: &corev1.HTTPGetAction{
475478
Path: "/v1/health/live",
476-
Port: getProbePort(n.Spec.Expose.Service),
479+
Port: intstr.FromString(DefaultNamedPortAPI),
477480
},
478481
},
479482
}
@@ -499,7 +502,7 @@ func (n *NemoCustomizer) GetDefaultReadinessProbe() *corev1.Probe {
499502
ProbeHandler: corev1.ProbeHandler{
500503
HTTPGet: &corev1.HTTPGetAction{
501504
Path: "/v1/health/ready",
502-
Port: getProbePort(n.Spec.Expose.Service),
505+
Port: intstr.FromString(DefaultNamedPortAPI),
503506
},
504507
},
505508
}
@@ -560,23 +563,6 @@ func (n *NemoCustomizer) IsServiceMonitorEnabled() bool {
560563
return n.Spec.Metrics.Enabled != nil && *n.Spec.Metrics.Enabled
561564
}
562565

563-
// GetInternalServicePort returns the internal service port number for the NemoCustomizer deployment
564-
func (n *NemoCustomizer) GetInternalServicePort() int32 {
565-
for _, port := range n.Spec.Expose.Service.Ports {
566-
if port.Name == "internal" {
567-
return port.Port
568-
}
569-
}
570-
571-
// Default to 9009 if no internal port is found
572-
return CustomizerInternalPort
573-
}
574-
575-
// GetServicePorts returns the service ports for the NemoCustomizer deployment
576-
func (n *NemoCustomizer) GetServicePorts() []corev1.ServicePort {
577-
return n.Spec.Expose.Service.Ports
578-
}
579-
580566
// GetServiceType returns the service type for the NemoCustomizer deployment
581567
func (n *NemoCustomizer) GetServiceType() string {
582568
return string(n.Spec.Expose.Service.Type)
@@ -657,21 +643,27 @@ func (n *NemoCustomizer) GetDeploymentParams() *rendertypes.DeploymentParams {
657643
params.RuntimeClassName = n.GetRuntimeClass()
658644

659645
// Setup container ports for customizer
660-
// TODO: set these to use defined values in the service spec
661-
// once that is allowed by the customizer through env variables
662-
containerPorts := []corev1.ContainerPort{
646+
params.Ports = []corev1.ContainerPort{
663647
{
664648
Name: DefaultNamedPortAPI,
665649
Protocol: corev1.ProtocolTCP,
666650
ContainerPort: CustomizerAPIPort,
667651
},
668652
{
669-
Name: "internal",
653+
Name: DefaultNamedPortInternal,
670654
Protocol: corev1.ProtocolTCP,
671655
ContainerPort: CustomizerInternalPort,
672656
},
673657
}
674-
params.Ports = containerPorts
658+
659+
if n.GetMetricsPort() != 0 {
660+
metricsPort := corev1.ContainerPort{
661+
Name: DefaultNamedPortMetrics,
662+
Protocol: corev1.ProtocolTCP,
663+
ContainerPort: n.GetMetricsPort(),
664+
}
665+
params.Ports = append(params.Ports, metricsPort)
666+
}
675667

676668
return params
677669
}
@@ -738,23 +730,30 @@ func (n *NemoCustomizer) GetServiceParams() *rendertypes.ServiceParams {
738730
// Set service type
739731
params.Type = n.GetServiceType()
740732

741-
servicePorts := n.GetServicePorts()
742-
if len(servicePorts) != 0 {
743-
params.Ports = servicePorts
744-
} else {
745-
// Set default ports
746-
params.Ports = []corev1.ServicePort{
747-
{
748-
Name: DefaultNamedPortAPI,
749-
Port: CustomizerAPIPort,
750-
Protocol: corev1.ProtocolTCP,
751-
},
752-
{
753-
Name: "internal",
754-
Port: CustomizerInternalPort,
755-
Protocol: corev1.ProtocolTCP,
756-
},
733+
// Set service ports
734+
params.Ports = []corev1.ServicePort{
735+
{
736+
Name: DefaultNamedPortAPI,
737+
Port: n.GetServicePort(),
738+
TargetPort: intstr.FromString((DefaultNamedPortAPI)),
739+
Protocol: corev1.ProtocolTCP,
740+
},
741+
{
742+
Name: DefaultNamedPortInternal,
743+
Port: CustomizerInternalPort,
744+
TargetPort: intstr.FromString((DefaultNamedPortInternal)),
745+
Protocol: corev1.ProtocolTCP,
746+
},
747+
}
748+
749+
if n.GetMetricsPort() != 0 {
750+
metricsPort := corev1.ServicePort{
751+
Name: DefaultNamedPortMetrics,
752+
Port: n.GetMetricsPort(),
753+
TargetPort: intstr.FromString((DefaultNamedPortMetrics)),
754+
Protocol: corev1.ProtocolTCP,
757755
}
756+
params.Ports = append(params.Ports, metricsPort)
758757
}
759758

760759
return params
@@ -892,15 +891,19 @@ func (n *NemoCustomizer) GetServiceMonitorParams() *rendertypes.ServiceMonitorPa
892891
params.Annotations = n.GetServiceMonitorAnnotations()
893892

894893
// Determine the appropriate port for monitoring
895-
metricsPort := getMetricsPort(n.Spec.Expose.Service)
894+
namedMetricsPort := DefaultNamedPortAPI
895+
if n.GetMetricsPort() != 0 {
896+
// Use the named port for metrics
897+
namedMetricsPort = DefaultNamedPortMetrics
898+
}
896899

897900
// Set Service Monitor spec
898901
smSpec := monitoringv1.ServiceMonitorSpec{
899902
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{n.Namespace}},
900903
Selector: metav1.LabelSelector{MatchLabels: n.GetServiceLabels()},
901904
Endpoints: []monitoringv1.Endpoint{
902905
{
903-
Port: metricsPort.StrVal,
906+
Port: namedMetricsPort,
904907
ScrapeTimeout: serviceMonitor.ScrapeTimeout,
905908
Interval: serviceMonitor.Interval,
906909
},
@@ -910,6 +913,19 @@ func (n *NemoCustomizer) GetServiceMonitorParams() *rendertypes.ServiceMonitorPa
910913
return params
911914
}
912915

916+
// GetServicePort returns the service port for the NemoCustomizer deployment or default port
917+
func (n *NemoCustomizer) GetServicePort() int32 {
918+
if n.Spec.Expose.Service.Port == 0 {
919+
return DefaultAPIPort
920+
}
921+
return n.Spec.Expose.Service.Port
922+
}
923+
924+
// GetMetricsPort returns the metrics port for the NemoCustomizer deployment
925+
func (n *NemoCustomizer) GetMetricsPort() int32 {
926+
return n.Spec.Expose.Service.MetricsPort
927+
}
928+
913929
// GetIngressAnnotations return standard and customized ingress annotations
914930
func (n *NemoCustomizer) GetIngressAnnotations() map[string]string {
915931
NemoCustomizerAnnotations := n.GetNemoCustomizerAnnotations()

0 commit comments

Comments
 (0)