Skip to content

Commit 5f36953

Browse files
committed
cache mlflow crd status
Signed-off-by: Alyssa Goins <agoins@redhat.com>
1 parent 8c0d8c6 commit 5f36953

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

main.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import (
4343
"k8s.io/apimachinery/pkg/selection"
4444
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4545
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
46+
"k8s.io/client-go/discovery"
47+
"k8s.io/client-go/rest"
4648

4749
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
4850
// to ensure that exec-entrypoint and run can make use of them.
@@ -153,6 +155,15 @@ func initConfig(configPath string) error {
153155
return nil
154156
}
155157

158+
func isCRDAvailable(cfg *rest.Config, groupVersion string) bool {
159+
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
160+
if err != nil {
161+
return false
162+
}
163+
_, err = dc.ServerResourcesForGroupVersion(groupVersion)
164+
return err == nil
165+
}
166+
156167
func main() {
157168
var metricsAddr string
158169
var enableLeaderElection bool
@@ -207,7 +218,9 @@ func main() {
207218
dspSelector := labels.NewSelector().Add(*dspLabelReq)
208219
dspFilter := cache.ByObject{Label: dspSelector}
209220

210-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
221+
restCfg := ctrl.GetConfigOrDie()
222+
223+
mgrOpts := ctrl.Options{
211224
Scheme: scheme,
212225
Metrics: metricsserver.Options{
213226
BindAddress: metricsAddr,
@@ -247,22 +260,32 @@ func main() {
247260
// (odh-trusted-ca-bundle, service-ca secrets) that lack the dsp-version
248261
// label. Strip data payloads instead of filtering by label so the
249262
// informer still detects changes to those external resources.
250-
&corev1.ConfigMap{}: {Transform: stripConfigMapData},
251-
&corev1.Secret{}: {Transform: stripSecretData},
263+
&corev1.ConfigMap{}: {Transform: stripConfigMapData},
264+
&corev1.Secret{}: {Transform: stripSecretData},
252265
},
253266
},
254267
// ConfigMap and Secret client reads bypass the cache entirely for
255268
// direct API reads, since the cache strips data payloads.
256269
Client: client.Options{
257270
Cache: &client.CacheOptions{
258-
DisableFor: []client.Object{
259-
&corev1.ConfigMap{},
260-
&corev1.Secret{},
261-
&mlflowv1.MLflow{},
262-
},
271+
DisableFor: []client.Object{
272+
&corev1.ConfigMap{},
273+
&corev1.Secret{},
274+
},
263275
},
264276
},
265-
})
277+
}
278+
279+
if isCRDAvailable(restCfg, mlflowv1.GroupVersion.String()) {
280+
mlflowSyncPeriod := 5 * time.Minute
281+
mgrOpts.Cache.ByObject[&mlflowv1.MLflow{}] = cache.ByObject{SyncPeriod: &mlflowSyncPeriod}
282+
setupLog.Info("MLflow CRD detected, caching MLflow resources", "syncPeriod", mlflowSyncPeriod)
283+
} else {
284+
mgrOpts.Client.Cache.DisableFor = append(mgrOpts.Client.Cache.DisableFor, &mlflowv1.MLflow{})
285+
setupLog.Info("MLflow CRD not found, MLflow cache disabled")
286+
}
287+
288+
mgr, err := ctrl.NewManager(restCfg, mgrOpts)
266289
if err != nil {
267290
setupLog.Error(err, "unable to start manager")
268291
os.Exit(1)

0 commit comments

Comments
 (0)