-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathkubeConfig.go
More file actions
312 lines (287 loc) · 11.7 KB
/
kubeConfig.go
File metadata and controls
312 lines (287 loc) · 11.7 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package helm
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
//"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/go-homedir"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
//clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// Struct holding k8s client config, burst limit for api requests, and mutex for sync
type KubeConfig struct {
ClientConfig clientcmd.ClientConfig
Burst int
sync.Mutex
}
// Converting KubeConfig to a REST config, which will be used to create k8s clients
func (k *KubeConfig) ToRESTConfig() (*rest.Config, error) {
config, err := k.ToRawKubeConfigLoader().ClientConfig()
return config, err
}
// Converting KubeConfig to a discovery client, which will be used to find api resources
func (k *KubeConfig) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
config, err := k.ToRESTConfig()
if err != nil {
return nil, err
}
config.Burst = k.Burst
return memory.NewMemCacheClient(discovery.NewDiscoveryClientForConfigOrDie(config)), nil
}
// Converting KubeConfig to a REST mapper, which will be used to map REST resources to their API obj
func (k *KubeConfig) ToRESTMapper() (meta.RESTMapper, error) {
discoveryClient, err := k.ToDiscoveryClient()
if err != nil {
return nil, err
}
// Using the appropriate types for the arguments
mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
warningHandler := func(warning string) {
fmt.Printf("Warning: %s\n", warning)
}
// Pass the warning handler to the NewShortcutExpander function
expander := restmapper.NewShortcutExpander(mapper, discoveryClient, warningHandler)
return expander, nil
}
// Function returning raw k8s client config
func (k *KubeConfig) ToRawKubeConfigLoader() clientcmd.ClientConfig {
return k.ClientConfig
}
// Generates a k8s client config, based on providers settings and namespace, which this config will be used to interact with the k8s cluster
func (m *Meta) NewKubeConfig(ctx context.Context, namespace string) (*KubeConfig, error) {
overrides := &clientcmd.ConfigOverrides{}
loader := &clientcmd.ClientConfigLoadingRules{}
configPaths := []string{}
if m == nil || m.Data == nil || m.Data.Kubernetes.IsNull() || m.Data.Kubernetes.IsUnknown() {
fmt.Println("Debug - One or more structural elements are nil")
return nil, fmt.Errorf("configuration error: missing required structural data")
}
// Extract the first element from the Kubernetes list
var kubernetesConfig KubernetesConfigModel
var kubernetesConfigs []KubernetesConfigModel
// TODO look into this next
diags := m.Data.Kubernetes.ElementsAs(ctx, &kubernetesConfigs, true)
if diags.HasError() {
fmt.Println("Error extracting Kubernetes config", diags[0])
return nil, fmt.Errorf("configuration error: unable to extract Kubernetes config %#v", diags[0])
}
if len(kubernetesConfigs) > 0 {
kubernetesConfig = kubernetesConfigs[0]
}
// Check ConfigPath
tflog.Debug(ctx, "Debug - m.Data.Kubernetes", map[string]interface{}{"Kubernetes": m.Data.Kubernetes})
if !kubernetesConfig.ConfigPath.IsNull() {
if v := kubernetesConfig.ConfigPath.ValueString(); v != "" {
configPaths = []string{v}
fmt.Println("Debug - ConfigPath:", kubernetesConfig.ConfigPath.ValueString())
tflog.Debug(ctx, "Debug - ConfigPath", map[string]interface{}{"ConfigPath": kubernetesConfig.ConfigPath.ValueString()})
}
}
if !kubernetesConfig.ConfigPaths.IsNull() {
additionalPaths := expandStringSlice(kubernetesConfig.ConfigPaths.Elements())
configPaths = append(configPaths, additionalPaths...)
}
if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" {
configPaths = filepath.SplitList(v)
}
fmt.Println("Initial configPaths:", configPaths)
tflog.Debug(ctx, "Initial configPaths", map[string]interface{}{"configPaths": configPaths})
fmt.Println("Debug - loader struct1:", loader)
if len(configPaths) > 0 {
fmt.Println("Processing config paths:", configPaths)
tflog.Debug(ctx, "Processing config paths", map[string]interface{}{
"configPaths": configPaths,
})
expandedPaths := []string{}
for _, p := range configPaths {
path, err := homedir.Expand(p)
if err != nil {
fmt.Println("Error expanding home directory:", p, "Error:", err)
tflog.Error(ctx, "Error expanding home directory", map[string]interface{}{
"path": p,
"error": err,
})
return nil, err
}
fmt.Println("Using kubeconfig path:", path)
tflog.Debug(ctx, "Using kubeconfig", map[string]interface{}{
"path": path,
})
expandedPaths = append(expandedPaths, path)
}
if len(expandedPaths) == 1 {
loader.ExplicitPath = expandedPaths[0]
} else {
loader.Precedence = expandedPaths
}
tflog.Debug(ctx, "Debug - loader struct2", map[string]interface{}{
"loader": loader,
})
// Check ConfigContext
if !kubernetesConfig.ConfigContext.IsNull() {
overrides.CurrentContext = kubernetesConfig.ConfigContext.ValueString()
fmt.Println("Setting config context:", overrides.CurrentContext)
tflog.Debug(ctx, "Setting config context", map[string]interface{}{
"configContext": overrides.CurrentContext,
})
}
if !kubernetesConfig.ConfigContextAuthInfo.IsNull() {
overrides.Context.AuthInfo = kubernetesConfig.ConfigContextAuthInfo.ValueString()
fmt.Println("Setting config context auth info:", overrides.Context.AuthInfo)
tflog.Debug(ctx, "Setting config context auth info", map[string]interface{}{
"configContextAuthInfo": overrides.Context.AuthInfo,
})
}
if !kubernetesConfig.ConfigContextCluster.IsNull() {
overrides.Context.Cluster = kubernetesConfig.ConfigContextCluster.ValueString()
fmt.Println("Setting config context cluster:", overrides.Context.Cluster)
tflog.Debug(ctx, "Setting config context cluster", map[string]interface{}{
"configContextCluster": overrides.Context.Cluster,
})
}
}
// Check and assign remaining fields
if !kubernetesConfig.Insecure.IsNull() {
overrides.ClusterInfo.InsecureSkipTLSVerify = kubernetesConfig.Insecure.ValueBool()
fmt.Println("Setting insecure skip TLS verify:", overrides.ClusterInfo.InsecureSkipTLSVerify)
tflog.Debug(ctx, "Setting insecure skip TLS verify", map[string]interface{}{
"insecureSkipTLSVerify": overrides.ClusterInfo.InsecureSkipTLSVerify,
})
}
if !kubernetesConfig.TLSServerName.IsNull() {
overrides.ClusterInfo.TLSServerName = kubernetesConfig.TLSServerName.ValueString()
fmt.Println("Setting TLS server name:", overrides.ClusterInfo.TLSServerName)
tflog.Debug(ctx, "Setting TLS server name", map[string]interface{}{
"tlsServerName": overrides.ClusterInfo.TLSServerName,
})
}
if !kubernetesConfig.ClusterCACertificate.IsNull() {
overrides.ClusterInfo.CertificateAuthorityData = []byte(kubernetesConfig.ClusterCACertificate.ValueString())
fmt.Println("Setting cluster CA certificate")
tflog.Debug(ctx, "Setting cluster CA certificate")
}
if !kubernetesConfig.ClientCertificate.IsNull() {
overrides.AuthInfo.ClientCertificateData = []byte(kubernetesConfig.ClientCertificate.ValueString())
fmt.Println("Setting client certificate")
tflog.Debug(ctx, "Setting client certificate")
}
if !kubernetesConfig.Host.IsNull() && kubernetesConfig.Host.ValueString() != "" {
hasCA := len(overrides.ClusterInfo.CertificateAuthorityData) != 0
hasCert := len(overrides.AuthInfo.ClientCertificateData) != 0
defaultTLS := hasCA || hasCert || overrides.ClusterInfo.InsecureSkipTLSVerify
host, _, err := rest.DefaultServerURL(kubernetesConfig.Host.ValueString(), "", schema.GroupVersion{}, defaultTLS)
if err != nil {
fmt.Println("Error setting host:", kubernetesConfig.Host.ValueString(), "Error:", err)
tflog.Error(ctx, "Error setting host", map[string]interface{}{
"host": kubernetesConfig.Host.ValueString(),
"error": err,
})
return nil, err
}
overrides.ClusterInfo.Server = host.String()
fmt.Println("Setting host:", overrides.ClusterInfo.Server)
tflog.Debug(ctx, "Setting host", map[string]interface{}{
"host": overrides.ClusterInfo.Server,
})
}
if !kubernetesConfig.Username.IsNull() {
overrides.AuthInfo.Username = kubernetesConfig.Username.ValueString()
fmt.Println("Setting username:", overrides.AuthInfo.Username)
tflog.Debug(ctx, "Setting username", map[string]interface{}{
"username": overrides.AuthInfo.Username,
})
}
if !kubernetesConfig.Password.IsNull() {
overrides.AuthInfo.Password = kubernetesConfig.Password.ValueString()
fmt.Println("Setting password")
tflog.Debug(ctx, "Setting password")
}
if !kubernetesConfig.ClientKey.IsNull() {
overrides.AuthInfo.ClientKeyData = []byte(kubernetesConfig.ClientKey.ValueString())
fmt.Println("Setting client key")
tflog.Debug(ctx, "Setting client key")
}
if !kubernetesConfig.Token.IsNull() {
overrides.AuthInfo.Token = kubernetesConfig.Token.ValueString()
fmt.Println("Setting token:", overrides.AuthInfo.Token)
tflog.Debug(ctx, "Setting token", map[string]interface{}{
"token": overrides.AuthInfo.Token,
})
}
if !kubernetesConfig.ProxyURL.IsNull() {
overrides.ClusterDefaults.ProxyURL = kubernetesConfig.ProxyURL.ValueString()
fmt.Println("Setting proxy URL:", overrides.ClusterDefaults.ProxyURL)
tflog.Debug(ctx, "Setting proxy URL", map[string]interface{}{
"proxyURL": overrides.ClusterDefaults.ProxyURL,
})
}
// Extract the first element from the Exec list
// var execConfig *ExecConfigModel
// if !kubernetesConfig.Exec.IsUnknown() {
// var execConfigs []ExecConfigModel
// diags := kubernetesConfig.Exec.ElementsAs(ctx, &execConfigs, false)
// if diags.HasError() {
// fmt.Println("Error extracting Exec config")
// return nil, fmt.Errorf("configuration error: unable to extract Exec config")
// }
// if len(execConfigs) > 0 {
// execConfig = &execConfigs[0]
// }
// }
// if execConfig != nil {
// args := execConfig.Args.Elements()
// env := execConfig.Env.Elements()
// exec := &clientcmdapi.ExecConfig{
// APIVersion: execConfig.ApiVersion.ValueString(),
// Command: execConfig.Command.ValueString(),
// Args: expandStringSlice(args),
// }
// for k, v := range env {
// exec.Env = append(exec.Env, clientcmdapi.ExecEnvVar{Name: k, Value: v.(basetypes.StringValue).ValueString()})
// }
// overrides.AuthInfo.Exec = exec
// fmt.Println("Setting exec configuration:", exec)
// tflog.Debug(ctx, "Setting exec configuration", map[string]interface{}{
// "execConfig": exec,
// })
// }
overrides.Context.Namespace = "default"
if namespace != "" {
overrides.Context.Namespace = namespace
fmt.Println("Setting namespace:", overrides.Context.Namespace)
tflog.Debug(ctx, "Setting namespace", map[string]interface{}{
"namespace": overrides.Context.Namespace,
})
}
// Creating the k8s client config, using the loaded and overrides.
burstLimit := int(m.Data.BurstLimit.ValueInt64())
client := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loader, overrides)
if client == nil {
fmt.Println("Failed to initialize kubernetes config")
tflog.Error(ctx, "Failed to initialize kubernetes config")
return nil, fmt.Errorf("failed to initialize kubernetes config")
}
fmt.Println("Successfully initialized kubernetes config")
tflog.Info(ctx, "Successfully initialized kubernetes config")
fmt.Printf("ClientConfig: %+v\n", client)
fmt.Printf("BurstLimit: %d\n", burstLimit)
return &KubeConfig{ClientConfig: client, Burst: burstLimit}, nil
}
func expandStringSlice(input []attr.Value) []string {
result := make([]string, len(input))
for i, v := range input {
result[i] = v.(types.String).ValueString()
}
return result
}