Unable to add 3rd party Resource to Operator

196 views
Skip to first unread message

Brian Ngo

unread,
Jul 22, 2024, 11:52:49 AM7/22/24
to Operator Framework
Hey everyone,

I'm following this guide to add a PodMonitor in my operator; however, it doesn't seem like this resource is registering properly in the scheme. Here's snippets from main.go
import (
"flag"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"os"

// 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"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
//+kubebuilder:scaffold:imports
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(monitoringv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
 

And here's the error:
"error": "no kind is registered for the type v1.PodMonitor in scheme \"k8s.io/client-go/kubernetes/scheme/register.go:81\""

Version Info:
operator-sdk version: "v1.33.0"
go version go1.22.5 darwin/arm64

Does anyone have any tips on what I might be missing?

Thanks for the help!

Varsha Prasad Narsing

unread,
Jul 23, 2024, 4:46:21 AM7/23/24
to Brian Ngo, Operator Framework
Hi Brian

Could you please verify if you're passing the right registered scheme to the controller through its options. Assuming the code looks like this:

```
var 
scheme = runtime.NewScheme() 
) 

func init() 
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) 
utilruntime.Must(monitoringv1.AddToScheme(scheme))//+kubebuilder:scaffold:scheme 
}
```
Could you check, if controller options have this scheme passed, ie:

```
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ 
            Scheme: scheme, // Scheme initialized before with registered types.
            MetricsBindAddress: metricsAddr,  
            .....
})
```

If it is still the case, and error persists - I'd suggest verifying the following:
1. Ensure that the path being specified for the monitoring scheme is right, and does contain `PodMonitor` registered. 
2. Ensure that the scheme is initialized properly. 
3. Verify that you are testing against the right CRD version. 

Hope this helps!

Best,
--
Varsha Prasad
Senior Software Engineer
Operator Framework, Openshift
Red Hat Inc. 

Brian Ngo

unread,
Jul 23, 2024, 2:59:27 PM7/23/24
to Varsha Prasad Narsing, Operator Framework
Hey Varsha,

Thank you for your help with this.

The code in my main.go matches yours as far as I can tell. It's untouched from initial project setup except the additional line in the init function but here's a few snippets:


```
var (
    scheme   = runtime.NewScheme()
    setupLog = ctrl.Log.WithName("setup")

)
```

```
func init() {
    utilruntime.Must(clientgoscheme.AddToScheme(scheme))
    utilruntime.Must(monitoringv1.AddToScheme(scheme))
    //+kubebuilder:scaffold:scheme
}
```

```
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
    Scheme:                 scheme,
```

For your other questions:
> 1. Ensure that the path being specified for the monitoring scheme is right, and does contain `PodMonitor` registered. 
If I'm reading docs correctly, the code should panic if scheme registration fails. Is there a way to confirm if the scheme is right?

> 2. Ensure that the scheme is initialized properly. 
The code in main.go is untouched from initial project setup except the additional line in the init function for monitoringv1 registration. So as far as I can tell, the scheme should be initialized properly. Is there a way to confirm this?


> 3. Verify that you are testing against the right CRD version. 
I've only got one version of the CRD I'm working on right now so this shouldn't be an issue.

Thanks again for your help!
Reply all
Reply to author
Forward
0 new messages