Reputation: 141
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{})
}
when i am trying to run this code it is giving error:
syntax error: unexpected literal "k8s.io/api/apps/v1", expecting type
i want to return deploymentslist using deploymnets function.i dont know what return type to use so that i succesfully return deployments list.
Upvotes: 2
Views: 1907
Reputation: 141
in imports add this line
v2 "k8s.io/api/apps/v1"
and update the function parameter as following
func deployments(namespace string)(*v2.DeploymentList, error){
incase if u r looking for podslist/namespacelist
import v3 "k8s.io/api/core/v1"
and update function as,
func func_name (input paramteters)(*v3.NamespaceList/PodList , error){
Upvotes: 2
Reputation: 51577
This is invalid syntax:
func deployments(namespace string)(*"k8s.io/api/apps/v1".DeploymentList, error){
This is the right syntax:
func deployments(namespace string)(*v1.DeploymentList, error){
However, you already have another v1
package imported, so you have to import that package with a different alias, and use that name,
Upvotes: 0