-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Add ServiceMonitor auto-generation for Prometheus discovery #6126
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
Open
ntkathole
wants to merge
2
commits into
feast-dev:master
Choose a base branch
from
ntkathole:service_monitor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
infra/feast-operator/internal/controller/services/service_monitor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| Copyright 2026 Feast Community. | ||
|
|
||
| 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 services | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| feastdevv1 "github.com/feast-dev/feast/infra/feast-operator/api/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| var serviceMonitorGVK = schema.GroupVersionKind{ | ||
| Group: "monitoring.coreos.com", | ||
| Version: "v1", | ||
| Kind: "ServiceMonitor", | ||
| } | ||
|
|
||
| // createOrDeleteServiceMonitor reconciles the ServiceMonitor for the | ||
| // FeatureStore's online store metrics endpoint using Server-Side Apply. | ||
| // When the Prometheus Operator CRD is not present in the cluster, this is | ||
| // a no-op. When metrics are enabled on the online store, a ServiceMonitor | ||
| // is applied; otherwise any existing ServiceMonitor is deleted. | ||
| func (feast *FeastServices) createOrDeleteServiceMonitor() error { | ||
| if !hasServiceMonitorCRD { | ||
| return nil | ||
| } | ||
|
|
||
| if feast.isOnlineStore() && feast.isMetricsEnabled(OnlineFeastType) { | ||
| return feast.applyServiceMonitor() | ||
| } | ||
|
|
||
| return feast.deleteServiceMonitor() | ||
| } | ||
|
|
||
| func (feast *FeastServices) applyServiceMonitor() error { | ||
| smApply := feast.buildServiceMonitorApplyConfig() | ||
| data, err := json.Marshal(smApply) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sm := feast.initServiceMonitor() | ||
| logger := log.FromContext(feast.Handler.Context) | ||
| if err := feast.Handler.Client.Patch(feast.Handler.Context, sm, | ||
| client.RawPatch(types.ApplyPatchType, data), | ||
| client.FieldOwner(fieldManager), client.ForceOwnership); err != nil { | ||
| return err | ||
| } | ||
| logger.Info("Successfully applied", "ServiceMonitor", sm.GetName()) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (feast *FeastServices) deleteServiceMonitor() error { | ||
| sm := feast.initServiceMonitor() | ||
| return feast.Handler.DeleteOwnedFeastObj(sm) | ||
| } | ||
|
|
||
| func (feast *FeastServices) initServiceMonitor() *unstructured.Unstructured { | ||
| sm := &unstructured.Unstructured{} | ||
| sm.SetGroupVersionKind(serviceMonitorGVK) | ||
| sm.SetName(feast.GetFeastServiceName(OnlineFeastType)) | ||
| sm.SetNamespace(feast.Handler.FeatureStore.Namespace) | ||
| return sm | ||
| } | ||
|
|
||
| // buildServiceMonitorApplyConfig constructs the fully desired ServiceMonitor | ||
| // state for Server-Side Apply. | ||
| func (feast *FeastServices) buildServiceMonitorApplyConfig() map[string]interface{} { | ||
| cr := feast.Handler.FeatureStore | ||
| objMeta := feast.GetObjectMetaType(OnlineFeastType) | ||
|
|
||
| return map[string]interface{}{ | ||
| "apiVersion": "monitoring.coreos.com/v1", | ||
| "kind": "ServiceMonitor", | ||
| "metadata": map[string]interface{}{ | ||
| "name": objMeta.Name, | ||
| "namespace": objMeta.Namespace, | ||
| "labels": feast.getFeastTypeLabels(OnlineFeastType), | ||
| "ownerReferences": []interface{}{ | ||
| map[string]interface{}{ | ||
| "apiVersion": feastdevv1.GroupVersion.String(), | ||
| "kind": "FeatureStore", | ||
| "name": cr.Name, | ||
| "uid": string(cr.UID), | ||
| "controller": true, | ||
| "blockOwnerDeletion": true, | ||
| }, | ||
| }, | ||
| }, | ||
| "spec": map[string]interface{}{ | ||
| "endpoints": []interface{}{ | ||
| map[string]interface{}{ | ||
| "port": "metrics", | ||
| "path": "/metrics", | ||
| }, | ||
| }, | ||
| "selector": map[string]interface{}{ | ||
| "matchLabels": map[string]interface{}{ | ||
| NameLabelKey: cr.Name, | ||
| ServiceTypeLabelKey: string(OnlineFeastType), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
176 changes: 176 additions & 0 deletions
176
infra/feast-operator/internal/controller/services/service_monitor_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| Copyright 2026 Feast Community. | ||
|
|
||
| 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 services | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| feastdevv1 "github.com/feast-dev/feast/infra/feast-operator/api/v1" | ||
| "github.com/feast-dev/feast/infra/feast-operator/internal/controller/handler" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/utils/ptr" | ||
| ) | ||
|
|
||
| var _ = Describe("ServiceMonitor", func() { | ||
| var ( | ||
| featureStore *feastdevv1.FeatureStore | ||
| feast *FeastServices | ||
| typeNamespacedName types.NamespacedName | ||
| ctx context.Context | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| ctx = context.Background() | ||
| typeNamespacedName = types.NamespacedName{ | ||
| Name: "sm-test-fs", | ||
| Namespace: "default", | ||
| } | ||
|
|
||
| featureStore = &feastdevv1.FeatureStore{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: typeNamespacedName.Name, | ||
| Namespace: typeNamespacedName.Namespace, | ||
| }, | ||
| Spec: feastdevv1.FeatureStoreSpec{ | ||
| FeastProject: "smtestproject", | ||
| Services: &feastdevv1.FeatureStoreServices{ | ||
| OnlineStore: &feastdevv1.OnlineStore{ | ||
| Server: &feastdevv1.ServerConfigs{ | ||
| ContainerConfigs: feastdevv1.ContainerConfigs{ | ||
| DefaultCtrConfigs: feastdevv1.DefaultCtrConfigs{ | ||
| Image: ptr.To("test-image"), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Registry: &feastdevv1.Registry{ | ||
| Local: &feastdevv1.LocalRegistryConfig{ | ||
| Server: &feastdevv1.RegistryServerConfigs{ | ||
| ServerConfigs: feastdevv1.ServerConfigs{ | ||
| ContainerConfigs: feastdevv1.ContainerConfigs{ | ||
| DefaultCtrConfigs: feastdevv1.DefaultCtrConfigs{ | ||
| Image: ptr.To("test-image"), | ||
| }, | ||
| }, | ||
| }, | ||
| GRPC: ptr.To(true), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| Expect(k8sClient.Create(ctx, featureStore)).To(Succeed()) | ||
|
|
||
| feast = &FeastServices{ | ||
| Handler: handler.FeastHandler{ | ||
| Client: k8sClient, | ||
| Context: ctx, | ||
| Scheme: k8sClient.Scheme(), | ||
| FeatureStore: featureStore, | ||
| }, | ||
| } | ||
|
|
||
| Expect(feast.ApplyDefaults()).To(Succeed()) | ||
| applySpecToStatus(featureStore) | ||
| feast.refreshFeatureStore(ctx, typeNamespacedName) | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| testSetHasServiceMonitorCRD(false) | ||
| Expect(k8sClient.Delete(ctx, featureStore)).To(Succeed()) | ||
| }) | ||
|
|
||
| Describe("initServiceMonitor", func() { | ||
| It("should create an unstructured ServiceMonitor with correct GVK and name", func() { | ||
| sm := feast.initServiceMonitor() | ||
| Expect(sm).NotTo(BeNil()) | ||
| Expect(sm.GetKind()).To(Equal("ServiceMonitor")) | ||
| Expect(sm.GetAPIVersion()).To(Equal("monitoring.coreos.com/v1")) | ||
| Expect(sm.GetName()).To(Equal(feast.GetFeastServiceName(OnlineFeastType))) | ||
| Expect(sm.GetNamespace()).To(Equal(featureStore.Namespace)) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("buildServiceMonitorApplyConfig", func() { | ||
| It("should build the correct SSA payload with labels, endpoints, selector, and owner reference", func() { | ||
| sm := feast.buildServiceMonitorApplyConfig() | ||
|
|
||
| Expect(sm["apiVersion"]).To(Equal("monitoring.coreos.com/v1")) | ||
| Expect(sm["kind"]).To(Equal("ServiceMonitor")) | ||
|
|
||
| metadata := sm["metadata"].(map[string]interface{}) | ||
| Expect(metadata["name"]).To(Equal(feast.GetFeastServiceName(OnlineFeastType))) | ||
| Expect(metadata["namespace"]).To(Equal(featureStore.Namespace)) | ||
|
|
||
| labels := metadata["labels"].(map[string]string) | ||
| Expect(labels).To(HaveKeyWithValue(NameLabelKey, featureStore.Name)) | ||
| Expect(labels).To(HaveKeyWithValue(ServiceTypeLabelKey, string(OnlineFeastType))) | ||
|
|
||
| ownerRefs := metadata["ownerReferences"].([]interface{}) | ||
| Expect(ownerRefs).To(HaveLen(1)) | ||
| ownerRef := ownerRefs[0].(map[string]interface{}) | ||
| Expect(ownerRef["apiVersion"]).To(Equal(feastdevv1.GroupVersion.String())) | ||
| Expect(ownerRef["kind"]).To(Equal("FeatureStore")) | ||
| Expect(ownerRef["name"]).To(Equal(featureStore.Name)) | ||
| Expect(ownerRef["controller"]).To(BeTrue()) | ||
| Expect(ownerRef["blockOwnerDeletion"]).To(BeTrue()) | ||
|
|
||
| spec := sm["spec"].(map[string]interface{}) | ||
|
|
||
| endpoints := spec["endpoints"].([]interface{}) | ||
| Expect(endpoints).To(HaveLen(1)) | ||
| ep := endpoints[0].(map[string]interface{}) | ||
| Expect(ep["port"]).To(Equal("metrics")) | ||
| Expect(ep["path"]).To(Equal("/metrics")) | ||
|
|
||
| selector := spec["selector"].(map[string]interface{}) | ||
| matchLabels := selector["matchLabels"].(map[string]interface{}) | ||
| Expect(matchLabels[NameLabelKey]).To(Equal(featureStore.Name)) | ||
| Expect(matchLabels[ServiceTypeLabelKey]).To(Equal(string(OnlineFeastType))) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("createOrDeleteServiceMonitor", func() { | ||
| It("should be a no-op when ServiceMonitor CRD is not available", func() { | ||
| testSetHasServiceMonitorCRD(false) | ||
| Expect(feast.createOrDeleteServiceMonitor()).To(Succeed()) | ||
| }) | ||
|
|
||
| It("should not error when metrics is not enabled and CRD is unavailable", func() { | ||
| testSetHasServiceMonitorCRD(false) | ||
| featureStore.Status.Applied.Services.OnlineStore.Server.Metrics = ptr.To(false) | ||
| Expect(feast.createOrDeleteServiceMonitor()).To(Succeed()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("HasServiceMonitorCRD", func() { | ||
| It("should return false by default", func() { | ||
| testSetHasServiceMonitorCRD(false) | ||
| Expect(HasServiceMonitorCRD()).To(BeFalse()) | ||
| }) | ||
|
|
||
| It("should return true when set", func() { | ||
| testSetHasServiceMonitorCRD(true) | ||
| Expect(HasServiceMonitorCRD()).To(BeTrue()) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe use the apply config from the Prometheus operator client?
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.
There is trade-off, it will make code cleaner but our use is of just one small builder. May be we can avoid coupling (new dependencies, version alignment) to the Prometheus operator's release cycle ?