猿问

使用 Kubernetes Go 客户端的部署列表的返回类型

  package main


    import (

          "fmt"

        "html/template"

        "net/http"

        "os"

        log "github.com/kubernetes/klog"

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

        "k8s.io/client-go/kubernetes"

        "k8s.io/client-go/tools/clientcmd"

    )



    type NamespaceDetails struct { //namespace details struct

        Namespace []string

    }



    var templates = template.Must(template.ParseGlob("./*.html"))         

    var microservice = "/microservice/"

    var detailed_view = "/detailed/"

    var kube_config_path = os.Getenv("HOME")+"/.kube/config"

    var config, _ = clientcmd.BuildConfigFromFlags("", kube_config_path)

    var clientset,_ = kubernetes.NewForConfig(config)                              

   var NamespaceClient, _ = clientset.CoreV1().Namespaces().List(v1.ListOptions{}) 


    func main() {

        http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))

        http.Handle("/jpeg/", http.StripPrefix("/jpeg/", http.FileServer(http.Dir("css"))))

        http.HandleFunc("/", Homepage)             // calling homepage function at '/' url

        http.HandleFunc(microservice, Deployments) //calling Deployments function at '/microserivce/' url

        http.HandleFunc(detailed_view, DetailedView)

        // http.HandleFunc("/onlyme", onlyme)

        http.ListenAndServe(":8801", nil) // server runs at this port

    }



    func Homepage(w http.ResponseWriter, r *http.Request) {

        NamespaceStruct := NamespaceDetails{}

        for _, Namespaces := range NamespaceClient.Items {

            log.V(5).Info("inside namespace items loop in homepage")

            NamespaceStruct.Namespace = append(NamespaceStruct.Namespace, Namespaces.Name)

        }

                templates.ExecuteTemplate(w, "homepage2.html", NamespaceStruct)

    }


当我尝试运行此代码时,它给出了错误:


语法错误:意外文字“k8s.io/api/apps/v1”,预期类型


我想使用 deploymnets 函数返回部署列表。我不知道要使用什么返回类型,以便我成功返回部署列表。


Cats萌萌
浏览 128回答 2
2回答

宝慕林4294392

在导入中添加这一行v2 "k8s.io/api/apps/v1"并更新函数参数如下func deployments(namespace string)(*v2.DeploymentList, error){如果你在寻找 podslist/namespacelistimport  v3 "k8s.io/api/core/v1"和更新功能为, func func_name (input paramteters)(*v3.NamespaceList/PodList , error){

慕姐4208626

这是无效的语法:func deployments(namespace string)(*"k8s.io/api/apps/v1".DeploymentList, error){这是正确的语法:func deployments(namespace string)(*v1.DeploymentList, error){但是,您已经v1导入了另一个包,因此您必须使用不同的别名导入该包,并使用该名称,
随时随地看视频慕课网APP

相关分类

Go
我要回答