forked from kubeflow/kubeflow
/
dashboard.go
72 lines (57 loc) · 1.79 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
var (
port = os.Getenv("PORT_1")
)
// Call GetClient from client/manager.go to obtain Cluster configs.
// Use client info from GetClient to call SearchKatib from findservice/findservice.go.
// SearchKatib will return a boolean true if Katib is deployed.
func main() {
clientset := GetClient()
isKatibDeployed := SearchKatibPods(clientset)
indexServer := http.FileServer(http.Dir("frontend/layout/"))
if isKatibDeployed {
indexServer = http.FileServer(http.Dir("frontend/index/"))
}
http.Handle("/", indexServer)
log.Println("Listening on", ":"+port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// GetClient gets client information
func GetClient() *kubernetes.Clientset {
config, err := rest.InClusterConfig()
if err != nil {
fmt.Printf("Error loading incluster config: %v", err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("Error creating clientset from clusterconfig: %v", err.Error())
}
return clientset
}
//SearchKatibPods searches across the cluster to see if Katib is deployed.
func SearchKatibPods(clientset *kubernetes.Clientset) bool {
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
fmt.Printf("Error in retrieving pods")
}
for _, pod := range pods.Items {
if strings.HasPrefix(pod.Name, "vizier") == true {
fmt.Printf("Match found\npod namespace: %s\npod name: %s\n", pod.Namespace, pod.Name)
return true
}
}
return false
}