-
Notifications
You must be signed in to change notification settings - Fork 16
/
interfaces.go
117 lines (95 loc) · 2.72 KB
/
interfaces.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
package common
import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/scheme"
)
// ObjectHelper is a generic interface to implement actions
// that are to be executed on an object
type ObjectHelper interface {
Handle() error
}
// KubeClientUser is an interface to use kubeclients on a specific namespace
type KubeClientUser interface {
DynamicClient() dynamic.Interface
Clientset() kubernetes.Interface
GetConfig() *rest.Config
GetNamespace() string
}
func NewKubeClientUser(cfgflags *genericclioptions.ConfigFlags, ns string) (KubeClientUser, error) {
var err error
kuser := &kubeClientUserImp{}
kuser.cfg, err = cfgflags.ToRESTConfig()
if err != nil {
panic(err)
}
// NOTE(jaosorior): workaround for https://github.com/kubernetes/client-go/issues/657
kuser.cfg.GroupVersion = &schema.GroupVersion{Group: "", Version: "v1"}
kuser.cfg.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
// NOTE(jaosorior): This is needed so the appropriate api path is used.
kuser.cfg.APIPath = "/api"
kuser.clientset, err = kubernetes.NewForConfig(kuser.cfg)
if err != nil {
panic(err)
}
kuser.dynclient, err = dynamic.NewForConfig(kuser.cfg)
if err != nil {
panic(err)
}
rawConfig, err := cfgflags.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return nil, err
}
// Takes precedence
if ns != "" {
kuser.namespace = ns
} else if currentContext, exists := rawConfig.Contexts[rawConfig.CurrentContext]; exists {
if currentContext.Namespace != "" {
kuser.namespace = currentContext.Namespace
}
}
return kuser, nil
}
type kubeClientUserImp struct {
dynclient dynamic.Interface
clientset kubernetes.Interface
cfg *rest.Config
namespace string
}
func (kuser *kubeClientUserImp) DynamicClient() dynamic.Interface {
return kuser.dynclient
}
func (kuser *kubeClientUserImp) Clientset() kubernetes.Interface {
return kuser.clientset
}
func (kuser *kubeClientUserImp) GetConfig() *rest.Config {
return kuser.cfg
}
func (kuser *kubeClientUserImp) GetNamespace() string {
return kuser.namespace
}
type CommandContext struct {
ConfigFlags *genericclioptions.ConfigFlags
Kuser KubeClientUser
Helper ObjectHelper
Args []string
genericclioptions.IOStreams
}
// Complete sets all information required for updating the current context
func (o *CommandContext) Complete(cmd *cobra.Command, args []string) error {
o.Args = args
// Takes precedence
givenNamespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
o.Kuser, err = NewKubeClientUser(o.ConfigFlags, givenNamespace)
if err != nil {
return err
}
return nil
}