亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“錯誤”:“方案中沒有為類型 v1.ServiceMonitor 注冊類型 \”

“錯誤”:“方案中沒有為類型 v1.ServiceMonitor 注冊類型 \”

Go
慕雪6442864 2023-02-28 20:27:58
我為我的應用程序創建了一個操作員,并想為它創建一個服務監視器。創建了 Prometheus 運算符。在我的 k8s 集群中導入了監控 Prometheus 庫并創建了服務監控 CRD。這是這個對象的 Go 代碼:package controllersimport (    "context"    "fmt"    appsv1alpha1 "k8s-operator/api/v1alpha1"    monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"    "gopkg.in/yaml.v2"    "k8s.io/apimachinery/pkg/api/errors"    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"    "k8s.io/apimachinery/pkg/types"    "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"    "sigs.k8s.io/controller-runtime/pkg/reconcile")// 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 := &monitoring.ServiceMonitor{}    err := r.Get(context.TODO(), types.NamespacedName{        Name:      svcmonitor.Name,        Namespace: instance.Namespace,    }, found)    if err != nil && errors.IsNotFound(err) {        // Create the SvcMonitor        err = r.Create(context.TODO(), svcmonitor)        if err != nil {            // SvcMonitor creation failed            return &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 &reconcile.Result{}, err    }    return nil, nil}// backendSvcMonitor is a code for creating a SvcMonitorfunc (r *MyappReconciler) backendSvcMonitor(v *appsv1alpha1.Myapp) *monitoring.ServiceMonitor {    svcmonitor := &monitoring.ServiceMonitor{        TypeMeta: metav1.TypeMeta{            Kind:       "ServiceMonitor",            APIVersion: "monitoring.coreos.com/v1",        },
查看完整描述

2 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

在您的 中main.go,您需要添加monitoring/v1到scheme注入的controller-runtimeie 中:


// main.go

package main


import (

    "os"

    ctrl "sigs.k8s.io/controller-runtime"

    monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"

    "k8s.io/apimachinery/pkg/runtime"

)


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)

    }

}


查看完整回答
反對 回復 2023-02-28
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

看起來 crd ServiceMonitor 是monitoring.coreos.com而不是 monitoring.coreos.com/v1 的一部分,所以它應該足以在代碼中更改它 (APIVersion):


package controllers


import (

    "context"

    "fmt"


    appsv1alpha1 "k8s-operator/api/v1alpha1"


    monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"

    "gopkg.in/yaml.v2"

    "k8s.io/apimachinery/pkg/api/errors"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

    "k8s.io/apimachinery/pkg/types"

    "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

    "sigs.k8s.io/controller-runtime/pkg/reconcile"

)


// 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 := &monitoring.ServiceMonitor{}

    err := r.Get(context.TODO(), types.NamespacedName{

        Name:      svcmonitor.Name,

        Namespace: instance.Namespace,

    }, found)

    if err != nil && errors.IsNotFound(err) {


        // Create the SvcMonitor

        err = r.Create(context.TODO(), svcmonitor)


        if err != nil {

            // SvcMonitor creation failed

            return &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 &reconcile.Result{}, err

    }


    return nil, nil

}


// backendSvcMonitor is a code for creating a SvcMonitor

func (r *MyappReconciler) backendSvcMonitor(v *appsv1alpha1.Myapp) *monitoring.ServiceMonitor {


    svcmonitor := &monitoring.ServiceMonitor{

        TypeMeta: metav1.TypeMeta{

            Kind:       "ServiceMonitor",

            APIVersion: "monitoring.coreos.com",

        },

        ObjectMeta: metav1.ObjectMeta{

            Name:      v.Spec.Name + "-svcmonitor",

            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(&svcmonitor)

    fmt.Println(string(yamlData))

    return svcmonitor

}


查看完整回答
反對 回復 2023-02-28
  • 2 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號