@@ -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
3637const (
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
581567func (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
914930func (n * NemoCustomizer ) GetIngressAnnotations () map [string ]string {
915931 NemoCustomizerAnnotations := n .GetNemoCustomizerAnnotations ()
0 commit comments