-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
42 lines (35 loc) · 1.13 KB
/
client.go
File metadata and controls
42 lines (35 loc) · 1.13 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
package controller
import (
"fmt"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)
type Client struct {
client dynamic.Interface
mapper *restmapper.DeferredDiscoveryRESTMapper
}
// NewClient
// This creates a wrapper which gives you an initialized and connected kubernetes client
// It then has a number of helper functions
func NewClient() (*Client, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to load client config: %w", err)
}
client, err := dynamic.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to create dynamic client: %w", err)
}
dc := discovery.NewDiscoveryClientForConfigOrDie(config)
mc := memory.NewMemCacheClient(dc)
mapper := restmapper.NewDeferredDiscoveryRESTMapper(mc)
return &Client{
client: client,
mapper: mapper,
}, nil
}