return 使用 Kubernetes go 客户端输入部署列表
return type for deployments list using Kubernetes go client
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{})
}
当我尝试 运行 此代码时出现错误:
syntax error: unexpected literal "k8s.io/api/apps/v1", expecting type
我想使用 deploymnets return 部署列表 function.i 不知道要使用什么 return 类型以便我成功 return 部署列表。
这是无效语法:
func deployments(namespace string)(*"k8s.io/api/apps/v1".DeploymentList, error){
这是正确的语法:
func deployments(namespace string)(*v1.DeploymentList, error){
但是,您已经导入了另一个 v1
包,因此您必须使用不同的别名导入该包,并使用该名称,
在导入中添加这一行
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){
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{})
}
当我尝试 运行 此代码时出现错误:
syntax error: unexpected literal "k8s.io/api/apps/v1", expecting type
我想使用 deploymnets return 部署列表 function.i 不知道要使用什么 return 类型以便我成功 return 部署列表。
这是无效语法:
func deployments(namespace string)(*"k8s.io/api/apps/v1".DeploymentList, error){
这是正确的语法:
func deployments(namespace string)(*v1.DeploymentList, error){
但是,您已经导入了另一个 v1
包,因此您必须使用不同的别名导入该包,并使用该名称,
在导入中添加这一行
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){