<p>I created an operator for my application and want to create a service monitor for it.
The Prometheus operator was created.
The monitoring Prometheus library was imported and the service monitor CRD was created in my k8s cluster.
Here is the Go code for this object:</p>
<pre class="lang-go prettyprint-override"><code>package controllers

import (
    &quot;context&quot;
    &quot;fmt&quot;

    appsv1alpha1 &quot;k8s-operator/api/v1alpha1&quot;

    monitoring &quot;github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1&quot;
    &quot;gopkg.in/yaml.v2&quot;
    &quot;k8s.io/apimachinery/pkg/api/errors&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    &quot;k8s.io/apimachinery/pkg/types&quot;
    &quot;sigs.k8s.io/controller-runtime/pkg/controller/controllerutil&quot;
    &quot;sigs.k8s.io/controller-runtime/pkg/reconcile&quot;
)

// ensureSvcMonitor ensures SvcMonitor is Running in a namespace.
func (r *MyappReconciler) ensureSvcMonitor(request reconcile.Request,
    instance *appsv1alpha1.Myapp,
    svcmonitor *monitoring.ServiceMonitor,
) (*reconcile.Result, error) {

    // See if SvcMonitor already exists and create if it doesn't
    found := &amp;monitoring.ServiceMonitor{}
    err := r.Get(context.TODO(), types.NamespacedName{
        Name:      svcmonitor.Name,
        Namespace: instance.Namespace,
    }, found)
    if err != nil &amp;&amp; errors.IsNotFound(err) {

        // Create the SvcMonitor
        err = r.Create(context.TODO(), svcmonitor)

        if err != nil {
            // SvcMonitor creation failed
            return &amp;reconcile.Result{}, err
        } else {
            // SvcMonitor creation was successful
            return nil, nil
        }
    } else if err != nil {
        // Error that isn't due to the SvcMonitor not existing
        return &amp;reconcile.Result{}, err
    }

    return nil, nil
}

// backendSvcMonitor is a code for creating a SvcMonitor
func (r *MyappReconciler) backendSvcMonitor(v *appsv1alpha1.Myapp) *monitoring.ServiceMonitor {

    svcmonitor := &amp;monitoring.ServiceMonitor{
        TypeMeta: metav1.TypeMeta{
            Kind:       &quot;ServiceMonitor&quot;,
            APIVersion: &quot;monitoring.coreos.com/v1&quot;,
        },
        ObjectMeta: metav1.ObjectMeta{
            Name:      v.Spec.Name + &quot;-svcmonitor&quot;,
            Namespace: v.Namespace},
        Spec: monitoring.ServiceMonitorSpec{
            Endpoints: []monitoring.Endpoint{{
                Port: v.Spec.Name,
            }},
            Selector: metav1.LabelSelector{
                MatchLabels: labels(v),
            },
        },
    }

    controllerutil.SetControllerReference(v, svcmonitor, r.Scheme)
    yamlData, _ := yaml.Marshal(&amp;svcmonitor)
    fmt.Println(string(yamlData))
    return svcmonitor
}
</code></pre>
<p>Here is the part which I call these function to create this object in my controller:</p>
<pre class="lang-go prettyprint-override"><code>if instance.Spec.Servicemonitorenable {
        result, err = r.ensureSvcMonitor(req, instance, r.backendSvcMonitor(instance))
        if result != nil {
            log.Error(err, &quot;Servicemonitor Not ready&quot;)
            return *result, err
        }
    }
</code></pre>
<p>But when I create the custom resource instance I get this error:</p>
<pre class="lang-none prettyprint-override"><code>&quot;error&quot;: &quot;no kind is registered for the type v1.ServiceMonitor in scheme \&quot;pkg/runtime/scheme.go:100\&quot;&quot;}
</code></pre>

mona moghadampanah
<p>In your <code>main.go</code>, you need to add <code>monitoring/v1</code> to the <code>scheme</code> injected into <code>controller-runtime</code> i.e.:</p>
<pre class="lang-golang prettyprint-override"><code>// main.go
package main

import (
    &quot;os&quot;
    ctrl &quot;sigs.k8s.io/controller-runtime&quot;
    monitoring &quot;github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1&quot;
    &quot;k8s.io/apimachinery/pkg/runtime&quot;
)

var (
    scheme = runtime.NewScheme()
)

func init() {
    monitoring.AddToScheme(scheme)
}

func main() {
    mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
        Scheme: scheme,
        // ... other options here
    })

    // Start Manager
    if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
        os.Exit(1)
    }
}
</code></pre>
