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 函数返回部署列表。我不知道要使用什么返回类型,以便我成功返回部署列表。