This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
info.go
79 lines (69 loc) · 1.63 KB
/
info.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
73
74
75
76
77
78
79
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cluster
import "k8s.io/client-go/tools/clientcmd"
//go:generate mockgen -source=info.go -destination=./fake/mock_info_interface.go -package=fake github.com/vmware-tanzu/octant/internal/cluster InfoInterface
// InfoInterface provides connection details for a cluster
type InfoInterface interface {
Context() string
Cluster() string
Server() string
User() string
}
type clusterInfo struct {
clientConfig clientcmd.ClientConfig
}
func newClusterInfo(clientConfig clientcmd.ClientConfig) clusterInfo {
return clusterInfo{clientConfig: clientConfig}
}
func (ci clusterInfo) Context() string {
raw, err := ci.clientConfig.RawConfig()
if err != nil {
return ""
}
return raw.CurrentContext
}
func (ci clusterInfo) Cluster() string {
raw, err := ci.clientConfig.RawConfig()
if err != nil {
return ""
}
ktx, ok := raw.Contexts[raw.CurrentContext]
if !ok || ktx == nil {
return ""
}
return ktx.Cluster
}
func (ci clusterInfo) Server() string {
raw, err := ci.clientConfig.RawConfig()
if err != nil {
return ""
}
ktx, ok := raw.Contexts[raw.CurrentContext]
if !ok || ktx == nil {
return ""
}
c, ok := raw.Clusters[ktx.Cluster]
if !ok || c == nil {
return ""
}
return c.Server
}
func (ci clusterInfo) User() string {
raw, err := ci.clientConfig.RawConfig()
if err != nil {
return ""
}
ktx, ok := raw.Contexts[raw.CurrentContext]
if !ok || ktx == nil {
return ""
}
// auth, ok := raw.AuthInfos[ktx.AuthInfo]
// if !ok || auth == nil {
// return ""
// }
// return auth.Username
return ktx.AuthInfo
}