-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubeconfig.go
61 lines (50 loc) · 1.53 KB
/
kubeconfig.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
package khota_handler
import (
"context"
b64 "encoding/base64"
"encoding/json"
"net/http"
"os"
"github.com/gorilla/mux"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type KubeConfig struct {
CurrentContext string `json:"current_context"`
Customer string `json:"customer"`
Namespace string `json:"namespace"`
ClusterCA string `json:"cluster_ca"`
ClusterServer string `json:"cluster_server"`
UserTokenValue string `json:"user_token_value"`
}
func NewKubeConfig(
customerName string,
clientset *kubernetes.Clientset,
) (*KubeConfig, error) {
secret, err := clientset.CoreV1().Secrets(customerName).Get(context.TODO(), customerName, metav1.GetOptions{})
if err != nil {
return nil, err
}
return &KubeConfig{
CurrentContext: customerName + "-context",
Customer: customerName,
Namespace: string(secret.Data["namespace"]),
ClusterCA: b64.StdEncoding.EncodeToString([]byte(secret.Data["ca.crt"])),
ClusterServer: os.Getenv("KUBERNETES_CONFIG_SERVER_URL"),
UserTokenValue: string(secret.Data["token"]),
}, nil
}
func GetKubeConfig(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
customerName := vars["customer_name"]
kc, _ := getKubeClientset()
config, err := NewKubeConfig(customerName, kc)
if err != nil {
res := NewResponse(CustomMsg(ConfigGetFail), internal_error, err, http.StatusInternalServerError)
res.SetResponse(&w)
res.LogResponse()
return
}
bytes, _ := json.Marshal(config)
sendJsonResponse(bytes, http.StatusOK, &w)
}