-
Notifications
You must be signed in to change notification settings - Fork 30
/
kube.go
109 lines (95 loc) · 2.7 KB
/
kube.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
package kube
import (
"context"
"errors"
"strings"
v1Core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type (
Kube interface {
BuildClient() (*kubernetes.Clientset, error)
BuildConfig() (*rest.Config, error)
EnsureNamespaceExists(ctx context.Context, cs *kubernetes.Clientset) error
}
kube struct {
contextName string
namespace string
pathToKubeConfig string
inCluster bool
dryRun bool
}
Options struct {
ContextName string
Namespace string
PathToKubeConfig string
InCluster bool
DryRun bool
}
)
func New(o *Options) Kube {
return &kube{
contextName: o.ContextName,
namespace: o.Namespace,
pathToKubeConfig: o.PathToKubeConfig,
inCluster: o.InCluster,
dryRun: o.DryRun,
}
}
func (k *kube) BuildClient() (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if k.inCluster {
config, err = rest.InClusterConfig()
} else {
config, err = k.BuildConfig()
if err != nil { // if cannot create from kubeConfigPath, try in-cluster config
config, err = rest.InClusterConfig()
}
}
if err != nil {
return nil, err
}
cs, err := kubernetes.NewForConfig(config)
if err != nil {
if strings.Contains(err.Error(), "exec plugin: invalid apiVersion") {
return nil, errors.New("Kubeconfig user entry is using an invalid API version client.authentication.k8s.io/v1alpha1.\nSee details at https://support.codefresh.io/hc/en-us/articles/6947789386652-Failure-to-perform-actions-on-your-selected-Kubernetes-context")
}
return nil, err
}
return cs, nil
}
func (k *kube) EnsureNamespaceExists(ctx context.Context, cs *kubernetes.Clientset) error {
if k.dryRun {
return nil
}
_, err := cs.CoreV1().Namespaces().Get(ctx, k.namespace, v1.GetOptions{})
if err != nil {
nsSpec := &v1Core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: k.namespace}}
_, err := cs.CoreV1().Namespaces().Create(ctx, nsSpec, v1.CreateOptions{})
if err != nil {
return err
}
}
return nil
}
func (k *kube) BuildConfig() (*rest.Config, error) {
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: k.pathToKubeConfig},
&clientcmd.ConfigOverrides{
CurrentContext: k.contextName,
Context: clientcmdapi.Context{
Namespace: k.namespace,
},
})
cc, err := config.ClientConfig()
if err != nil { // if cannot create from kubeConfigPath, try in-cluster config
return rest.InClusterConfig()
}
return cc, nil
}