Skip to content
Open
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: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@
"filename": "infra/feast-operator/internal/controller/services/services.go",
"hashed_secret": "36dc326eb15c7bdd8d91a6b87905bcea20b637d1",
"is_verified": false,
"line_number": 176
"line_number": 179
}
],
"infra/feast-operator/internal/controller/services/tls_test.go": [
Expand Down Expand Up @@ -1539,5 +1539,5 @@
}
]
},
"generated_at": "2026-03-18T08:09:25Z"
"generated_at": "2026-03-18T13:51:43Z"
}
11 changes: 11 additions & 0 deletions infra/feast-operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ rules:
- get
- patch
- update
- apiGroups:
- monitoring.coreos.com
resources:
- servicemonitors
verbs:
- create
- delete
- get
- list
- patch
- watch
- apiGroups:
- policy
resources:
Expand Down
11 changes: 11 additions & 0 deletions infra/feast-operator/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20396,6 +20396,17 @@ rules:
- get
- patch
- update
- apiGroups:
- monitoring.coreos.com
resources:
- servicemonitors
verbs:
- create
- delete
- get
- list
- patch
- watch
- apiGroups:
- policy
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -69,6 +71,7 @@ type FeatureStoreReconciler struct {
// +kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=policy,resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;list;watch;create;patch;delete

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -244,6 +247,15 @@ func (r *FeatureStoreReconciler) SetupWithManager(mgr ctrl.Manager) error {
if services.IsOpenShift() {
bldr = bldr.Owns(&routev1.Route{})
}
if services.HasServiceMonitorCRD() {
sm := &unstructured.Unstructured{}
sm.SetGroupVersionKind(schema.GroupVersionKind{
Group: "monitoring.coreos.com",
Version: "v1",
Kind: "ServiceMonitor",
})
bldr = bldr.Owns(sm)
}

return bldr.Complete(r)

Expand Down
124 changes: 124 additions & 0 deletions infra/feast-operator/internal/controller/services/service_monitor.go
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{}{

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?

Copy link
Member Author

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 ?

"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),
},
},
},
}
}
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())
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (feast *FeastServices) Deploy() error {
if err := feast.deployCronJob(); err != nil {
return err
}
if err := feast.createOrDeleteServiceMonitor(); err != nil {
return err
}

return nil
}
Expand Down
Loading
Loading