1
    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)
    }

    func deployments(namespace string)(*"k8s.io/api/apps/v1".DeploymentList, error){
        return  clientset.AppsV1().Deployments(namespace).List(v1.ListOptions{})
    }

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

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

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

4

2 回答 2

2

在导入中添加这一行

v2 "k8s.io/api/apps/v1"

并更新函数参数如下

func deployments(namespace string)(*v2.DeploymentList, error){

如果你在寻找 podslist/namespacelist

import  v3 "k8s.io/api/core/v1"

和更新功能为,

 func func_name (input paramteters)(*v3.NamespaceList/PodList , error){
于 2020-02-20T09:26:12.500 回答
0

这是无效的语法:

func deployments(namespace string)(*"k8s.io/api/apps/v1".DeploymentList, error){

这是正确的语法:

func deployments(namespace string)(*v1.DeploymentList, error){

但是,您已经v1导入了另一个包,因此您必须使用不同的别名导入该包,并使用该名称,

于 2020-02-20T06:53:38.733 回答