-
Notifications
You must be signed in to change notification settings - Fork 6
/
factory.go
126 lines (109 loc) · 2.91 KB
/
factory.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
package kube
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/jenkins-x/jx-logging/pkg/log"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
type factory struct {
}
func (f *factory) SetKubeContext(context string, config *api.Config) (*api.Config, error) {
ctx := config.Contexts[context]
if ctx == nil {
return nil, fmt.Errorf("could not find Kubernetes context %s", context)
}
newConfig := *config
newConfig.CurrentContext = context
err := clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), newConfig, false)
if err != nil {
return nil, err
}
return &newConfig, nil
}
func (f *factory) SetKubeNamespace(namespace string, config *api.Config) (*api.Config, error) {
newConfig := *config
ctx := f.GetCurrentContext(config)
if ctx == nil {
return nil, errors.New("could not find Kubernetes context")
}
if ctx.Namespace == namespace {
return config, nil
}
ctx.Namespace = namespace
err := clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), newConfig, false)
if err != nil {
return nil, err
}
return &newConfig, nil
}
func (f *factory) SetKubeConfig(config *api.Config) (*api.Config, error) {
newConfig := *config
log.Logger().Debugf("persisting %+v", newConfig)
err := clientcmd.ModifyConfig(clientcmd.NewDefaultPathOptions(), newConfig, false)
if err != nil {
return nil, err
}
return &newConfig, nil
}
func NewKuber() Kuber {
return &factory{}
}
func (f *factory) LoadAPIConfig() (*api.Config, error) {
po := clientcmd.NewDefaultPathOptions()
if po == nil {
return nil, errors.New("unable to get kube config path options")
}
apiConfig, err := po.GetStartingConfig()
if err != nil {
return nil, err
}
return apiConfig, nil
}
func (f *factory) LoadAPIConfigFromPath(path string) (*api.Config, error) {
apiConfig, err := clientcmd.LoadFromFile(path)
if err != nil {
return nil, err
}
return apiConfig, nil
}
func (f *factory) LoadClientConfig() (*rest.Config, error) {
po := clientcmd.NewDefaultPathOptions()
if po == nil {
return nil, errors.New("unable to get kube config path options")
}
restConfig, err := clientcmd.BuildConfigFromFlags("", po.GlobalFile)
if err != nil {
return nil, err
}
// for testing purposes one can enable tracing of Kube REST API calls
traceKubeAPI := os.Getenv("TRACE_KUBE_API")
if traceKubeAPI == "1" || traceKubeAPI == "on" {
restConfig.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return &Tracer{RoundTripper: rt}
}
}
return restConfig, nil
}
func (f *factory) GetCurrentContext(config *api.Config) *api.Context {
if config != nil {
name := config.CurrentContext
if name != "" && config.Contexts != nil {
return config.Contexts[name]
}
}
return nil
}
func (f *factory) GetCurrentNamespace(config *api.Config) string {
ctx := f.GetCurrentContext(config)
if ctx != nil {
n := ctx.Namespace
if n != "" {
return n
}
}
return "default"
}