-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
179 lines (153 loc) · 4.25 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Autovia GmbH
// SPDX-License-Identifier: Apache-2.0
package structs
import (
"net/http"
clientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
type App struct {
Addr *string
Router *http.ServeMux
KubeConfigPath *string
ApiServerHost *string
ProxyUrl *string
InCluster *bool
FileServer *bool
FileServerPath *string
Client *Client
AuthManager AuthManager
// config
PodLogTailLines *int64
DefaultConfigName *string
TokenExpirationHours *int64
}
func (app *App) LoadKubeContext(context string) error {
var config *rest.Config
var err error
if *app.InCluster {
config, err = rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
} else {
if context == "" {
config, err = clientcmd.BuildConfigFromFlags("", *app.KubeConfigPath)
if err != nil {
return err
}
} else {
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *app.KubeConfigPath},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
if err != nil {
return err
}
}
}
k8sclientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
apiclientset, err := clientset.NewForConfig(config)
if err != nil {
return err
}
app.Client = &Client{
Clientset: k8sclientset,
ApiClient: apiclientset,
Config: config,
}
return nil
}
func (app *App) NewKubeClient(token string) (*Client, error) {
config, err := app.buildConfigFromToken(token)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
return &Client{Clientset: clientset, Discovery: discoveryClient, Config: config}, nil
}
func (app *App) NewApiClient(token string) (*Client, error) {
config, err := app.buildConfigFromToken(token)
if err != nil {
return nil, err
}
apiclientset, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
return &Client{ApiClient: apiclientset, Dynamic: dynamicClient, Discovery: discoveryClient, Config: config}, nil
}
func (app *App) buildConfigFromToken(token string) (*rest.Config, error) {
var clientCfg *rest.Config
var err error
if *app.InCluster {
cfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
apiConfig := api.NewConfig()
apiConfig.Clusters[*app.DefaultConfigName] = &api.Cluster{
Server: cfg.Host,
CertificateAuthority: cfg.TLSClientConfig.CAFile,
CertificateAuthorityData: cfg.TLSClientConfig.CAData,
InsecureSkipTLSVerify: cfg.TLSClientConfig.Insecure,
}
apiConfig.AuthInfos[*app.DefaultConfigName] = &api.AuthInfo{Token: token}
apiConfig.Contexts[*app.DefaultConfigName] = &api.Context{
Cluster: *app.DefaultConfigName,
AuthInfo: *app.DefaultConfigName,
}
apiConfig.CurrentContext = *app.DefaultConfigName
clientConfig := clientcmd.NewDefaultClientConfig(
*apiConfig,
&clientcmd.ConfigOverrides{},
)
clientCfg, err = clientConfig.ClientConfig()
if err != nil {
return nil, err
}
} else {
clientCfg, err = clientcmd.BuildConfigFromFlags("", *app.KubeConfigPath)
if err != nil {
return nil, err
}
}
return clientCfg, nil
}
// Use configmap
func (app *App) LoadConfig() {
// PodLogOptions, the number of lines from the end of the logs to show
var podLogTailLines int64 = 1024
app.PodLogTailLines = &podLogTailLines
// Context for K8s client
var defaultConfigName string = "kubernetes"
app.DefaultConfigName = &defaultConfigName
// jwtToken which contains the K8s token (default expires after 1h)
// Can be changed with "--duration=12h" when using "kubectl create token"
var tokenExpirationHours int64 = 12
app.TokenExpirationHours = &tokenExpirationHours
}