@@ -17,9 +17,13 @@ limitations under the License.
1717package services
1818
1919import (
20+ "encoding/json"
21+
22+ feastdevv1 "github.com/feast-dev/feast/infra/feast-operator/api/v1"
2023 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2124 "k8s.io/apimachinery/pkg/runtime/schema"
22- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
25+ "k8s.io/apimachinery/pkg/types"
26+ "sigs.k8s.io/controller-runtime/pkg/client"
2327 "sigs.k8s.io/controller-runtime/pkg/log"
2428)
2529
@@ -30,32 +34,38 @@ var serviceMonitorGVK = schema.GroupVersionKind{
3034}
3135
3236// createOrDeleteServiceMonitor reconciles the ServiceMonitor for the
33- // FeatureStore's online store metrics endpoint. When the Prometheus Operator
34- // CRD is not present in the cluster, this is a no-op. When metrics are enabled
35- // on the online store, a ServiceMonitor is created; otherwise any existing
36- // ServiceMonitor is deleted.
37+ // FeatureStore's online store metrics endpoint using Server-Side Apply.
38+ // When the Prometheus Operator CRD is not present in the cluster, this is
39+ // a no-op. When metrics are enabled on the online store, a ServiceMonitor
40+ // is applied; otherwise any existing ServiceMonitor is deleted.
3741func (feast * FeastServices ) createOrDeleteServiceMonitor () error {
3842 if ! hasServiceMonitorCRD {
3943 return nil
4044 }
4145
4246 if feast .isOnlineStore () && feast .isMetricsEnabled (OnlineFeastType ) {
43- return feast .createServiceMonitor ()
47+ return feast .applyServiceMonitor ()
4448 }
4549
4650 return feast .deleteServiceMonitor ()
4751}
4852
49- func (feast * FeastServices ) createServiceMonitor () error {
50- logger := log .FromContext (feast .Handler .Context )
53+ func (feast * FeastServices ) applyServiceMonitor () error {
54+ smApply := feast .buildServiceMonitorApplyConfig ()
55+ data , err := json .Marshal (smApply )
56+ if err != nil {
57+ return err
58+ }
59+
5160 sm := feast .initServiceMonitor ()
52- if op , err := controllerutil .CreateOrUpdate (feast .Handler .Context , feast .Handler .Client , sm , controllerutil .MutateFn (func () error {
53- return feast .setServiceMonitor (sm )
54- })); err != nil {
61+ logger := log .FromContext (feast .Handler .Context )
62+ if err := feast .Handler .Client .Patch (feast .Handler .Context , sm ,
63+ client .RawPatch (types .ApplyPatchType , data ),
64+ client .FieldOwner (fieldManager ), client .ForceOwnership ); err != nil {
5565 return err
56- } else if op == controllerutil .OperationResultCreated || op == controllerutil .OperationResultUpdated {
57- logger .Info ("Successfully reconciled" , "ServiceMonitor" , sm .GetName (), "operation" , op )
5866 }
67+ logger .Info ("Successfully applied" , "ServiceMonitor" , sm .GetName ())
68+
5969 return nil
6070}
6171
@@ -72,25 +82,43 @@ func (feast *FeastServices) initServiceMonitor() *unstructured.Unstructured {
7282 return sm
7383}
7484
75- func (feast * FeastServices ) setServiceMonitor (sm * unstructured.Unstructured ) error {
85+ // buildServiceMonitorApplyConfig constructs the fully desired ServiceMonitor
86+ // state for Server-Side Apply.
87+ func (feast * FeastServices ) buildServiceMonitorApplyConfig () map [string ]interface {} {
7688 cr := feast .Handler .FeatureStore
77-
78- sm .SetLabels (feast .getFeastTypeLabels (OnlineFeastType ))
79-
80- sm .Object ["spec" ] = map [string ]interface {}{
81- "endpoints" : []interface {}{
82- map [string ]interface {}{
83- "port" : "metrics" ,
84- "path" : "/metrics" ,
89+ objMeta := feast .GetObjectMetaType (OnlineFeastType )
90+
91+ return map [string ]interface {}{
92+ "apiVersion" : "monitoring.coreos.com/v1" ,
93+ "kind" : "ServiceMonitor" ,
94+ "metadata" : map [string ]interface {}{
95+ "name" : objMeta .Name ,
96+ "namespace" : objMeta .Namespace ,
97+ "labels" : feast .getFeastTypeLabels (OnlineFeastType ),
98+ "ownerReferences" : []interface {}{
99+ map [string ]interface {}{
100+ "apiVersion" : feastdevv1 .GroupVersion .String (),
101+ "kind" : "FeatureStore" ,
102+ "name" : cr .Name ,
103+ "uid" : string (cr .UID ),
104+ "controller" : true ,
105+ "blockOwnerDeletion" : true ,
106+ },
85107 },
86108 },
87- "selector" : map [string ]interface {}{
88- "matchLabels" : map [string ]interface {}{
89- NameLabelKey : cr .Name ,
90- ServiceTypeLabelKey : string (OnlineFeastType ),
109+ "spec" : map [string ]interface {}{
110+ "endpoints" : []interface {}{
111+ map [string ]interface {}{
112+ "port" : "metrics" ,
113+ "path" : "/metrics" ,
114+ },
115+ },
116+ "selector" : map [string ]interface {}{
117+ "matchLabels" : map [string ]interface {}{
118+ NameLabelKey : cr .Name ,
119+ ServiceTypeLabelKey : string (OnlineFeastType ),
120+ },
91121 },
92122 },
93123 }
94-
95- return controllerutil .SetControllerReference (cr , sm , feast .Handler .Scheme )
96124}
0 commit comments