Skip to content
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

Adding support for service monitor on NIM Service #58

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/apps/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -45,8 +46,9 @@ type Metrics struct {

// ServiceMonitor defines attributes to create a service monitor
type ServiceMonitor struct {
Create *bool `json:"enabled,omitempty"`
AdditionalLabels map[string]string `json:"additionalLabels,omitempty"`
Interval promv1.Duration `json:"interval,omitempty"`
ScrapeTimeout promv1.Duration `json:"scrapeTimeout,omitempty"`
}

// Autoscaling defines attributes to automatically scale the service based on metrics
Expand Down
30 changes: 30 additions & 0 deletions api/apps/v1alpha1/nimservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package v1alpha1

import (
"fmt"
"maps"
"os"

rendertypes "github.com/NVIDIA/k8s-nim-operator/internal/render/types"
utils "github.com/NVIDIA/k8s-nim-operator/internal/utils"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -439,6 +441,11 @@ func (n *NIMService) GetHPA() HorizontalPodAutoscalerSpec {
return n.Spec.Scale.HPA
}

// GetServiceMonitor returns the Service Monitor details for the NIMService deployment
func (n *NIMService) GetServiceMonitor() ServiceMonitor {
return n.Spec.Metrics.ServiceMonitor
}

// GetReplicas returns replicas for the NIMService deployment
func (n *NIMService) GetReplicas() int {
if n.IsAutoScalingEnabled() {
Expand Down Expand Up @@ -608,6 +615,7 @@ func (n *NIMService) GetServiceParams() *rendertypes.ServiceParams {

// Set service ports
params.Port = n.GetServicePort()
params.PortName = "open-ai-port"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the metrics port for embedding/reranking if not the open-ai-port. It is a separate port.

return params
}

Expand Down Expand Up @@ -689,6 +697,28 @@ func (n *NIMService) GetSCCParams() *rendertypes.SCCParams {
return params
}

// GetServiceMonitorParams return params to render Service Monitor from templates
func (n *NIMService) GetServiceMonitorParams() *rendertypes.ServiceMonitorParams {
params := &rendertypes.ServiceMonitorParams{}
serviceMonitor := n.GetServiceMonitor()
params.Enabled = n.IsServiceMonitorEnabled()
params.Name = n.GetName()
params.Namespace = n.GetNamespace()
svcLabels := n.GetServiceLabels()
maps.Copy(svcLabels, serviceMonitor.AdditionalLabels)
params.Labels = svcLabels
params.Annotations = n.GetServiceAnnotations()

// Set Service Monitor spec
smSpec := monitoringv1.ServiceMonitorSpec{
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{n.Namespace}},
Selector: metav1.LabelSelector{MatchLabels: n.GetServiceLabels()},
Endpoints: []monitoringv1.Endpoint{{Port: "open-ai-port", ScrapeTimeout: serviceMonitor.ScrapeTimeout, Interval: serviceMonitor.Interval}},
}
params.SMSpec = smSpec
return params
}

func init() {
SchemeBuilder.Register(&NIMService{}, &NIMServiceList{})
}
5 changes: 0 additions & 5 deletions api/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions bundle/manifests/apps.nvidia.com_nimpipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,20 @@ spec:
additionalProperties:
type: string
type: object
enabled:
type: boolean
interval:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
scrapeTimeout:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
type: object
type: object
nimCache:
Expand Down
16 changes: 14 additions & 2 deletions bundle/manifests/apps.nvidia.com_nimservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,20 @@ spec:
additionalProperties:
type: string
type: object
enabled:
type: boolean
interval:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
scrapeTimeout:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
type: object
type: object
nimCache:
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"os"

monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -51,8 +52,8 @@ var (

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(monitoring.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down
16 changes: 14 additions & 2 deletions config/crd/bases/apps.nvidia.com_nimpipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,20 @@ spec:
additionalProperties:
type: string
type: object
enabled:
type: boolean
interval:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
scrapeTimeout:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
type: object
type: object
nimCache:
Expand Down
16 changes: 14 additions & 2 deletions config/crd/bases/apps.nvidia.com_nimservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,20 @@ spec:
additionalProperties:
type: string
type: object
enabled:
type: boolean
interval:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
scrapeTimeout:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
type: object
type: object
nimCache:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,20 @@ spec:
additionalProperties:
type: string
type: object
enabled:
type: boolean
interval:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
scrapeTimeout:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
type: object
type: object
nimCache:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,20 @@ spec:
additionalProperties:
type: string
type: object
enabled:
type: boolean
interval:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
scrapeTimeout:
description: |-
Duration is a valid time duration that can be parsed by Prometheus model.ParseDuration() function.
Supported units: y, w, d, h, m, s, ms
Examples: `30s`, `1m`, `1h20m15s`, `15d`
pattern: ^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$
type: string
type: object
type: object
nimCache:
Expand Down
2 changes: 2 additions & 0 deletions internal/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
ReasonHPAFailed = "HPAFailed"
// ReasonSCCFailed indicates that the creation of scc has failed
ReasonSCCFailed = "SCCFailed"
// ReasonServiceMonitorFailed indicates that the creation of Service Monitor has failed
ReasonServiceMonitorFailed = "ServiceMonitorFailed"
// ReasonDeploymentFailed indicates that the creation of deployment has failed
ReasonDeploymentFailed = "DeploymentFailed"
// ReasonStatefulSetFailed indicates that the creation of statefulset has failed
Expand Down
11 changes: 11 additions & 0 deletions internal/controller/platform/standalone/nimservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/NVIDIA/k8s-nim-operator/internal/shared"
"github.com/NVIDIA/k8s-nim-operator/internal/utils"
"github.com/go-logr/logr"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -132,6 +133,16 @@ func (r *NIMServiceReconciler) reconcileNIMService(ctx context.Context, nimServi
}
}

// Sync Service Monitor
if nimService.IsServiceMonitorEnabled() {
err = r.renderAndSyncResource(ctx, nimService, &renderer, &monitoringv1.ServiceMonitor{}, func() (client.Object, error) {
return renderer.ServiceMonitor(nimService.GetServiceMonitorParams())
}, "servicemonitor", conditions.ReasonServiceMonitorFailed)
if err != nil {
return ctrl.Result{}, err
}
}

deploymentParams := nimService.GetDeploymentParams()
var modelPVC *appsv1alpha1.PersistentVolumeClaim
modelProfile := ""
Expand Down
2 changes: 2 additions & 0 deletions internal/render/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package types

import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -196,4 +197,5 @@ type ServiceMonitorParams struct {
Path string
Interval int32
ScrapeTimeout int32
SMSpec monitoringv1.ServiceMonitorSpec
}
16 changes: 3 additions & 13 deletions manifests/servicemonitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ metadata:
{{- .Annotations | yaml | nindent 4 }}
{{- end }}
spec:
selector:
labels:
{{- if .MatchLabels }}
{{- .MatchLabels | yaml | nindent 6 }}
{{- end }}
namespaceSelector:
matchNames:
- {{ .Namespace }}
endpoints:
- port: {{ .Port }}
path: {{ .Path }}
interval: {{ .Interval }}
scrapeTimeout: {{ .ScrapeTimeout }}
{{- if .SMSpec }}
{{- .SMSpec | yaml | nindent 2 }}
{{- end }}
{{- end }}