-
Notifications
You must be signed in to change notification settings - Fork 215
/
wirek8s.go
140 lines (119 loc) · 4.34 KB
/
wirek8s.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wirek8s
import (
"strings"
"github.com/google/wire"
"github.com/spf13/pflag"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/cli-experimental/internal/pkg/client"
"sigs.k8s.io/cli-experimental/internal/pkg/configflags"
"sigs.k8s.io/cli-experimental/internal/pkg/util"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
// for connecting to various types of hosted clusters
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
// ProviderSet defines dependencies for initializing Kubernetes objects
var ProviderSet = wire.NewSet(
NewKubernetesClientSet,
NewExtensionsClientSet,
NewConfigFlags,
NewRestConfig,
NewDynamicClient,
NewRestMapper,
NewClient,
)
// Flags registers flags for talkig to a Kubernetes cluster
func Flags(fs *pflag.FlagSet) {
kubeConfigFlags := configflags.NewConfigFlags(false)
kubeConfigFlags.AddFlags(fs)
}
// HelpFlags is a list of flags to strips
var HelpFlags = []string{"-h", "--help"}
// WordSepNormalizeFunc normalizes flags
func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
if strings.Contains(name, "_") {
return pflag.NormalizedName(strings.Replace(name, "_", "-", -1))
}
return pflag.NormalizedName(name)
}
// NewConfigFlags parses flags used to generate the *rest.Config
func NewConfigFlags(ar util.Args) (*configflags.ConfigFlags, error) {
a := CopyStrSlice([]string(ar))
// IMPORTANT: If there is an error parsing flags--continue.
kubeConfigFlagSet := pflag.NewFlagSet("dispatcher-kube-config", pflag.ContinueOnError)
kubeConfigFlagSet.ParseErrorsWhitelist.UnknownFlags = true
kubeConfigFlagSet.SetNormalizeFunc(WordSepNormalizeFunc)
_ = kubeConfigFlagSet.Set("namespace", "default")
unusedParameter := true // Could be either true or false
kubeConfigFlags := configflags.NewConfigFlags(unusedParameter)
kubeConfigFlags.AddFlags(kubeConfigFlagSet)
// Remove help flags, since these are special-cased in pflag.Parse,
args := FilterList(a, HelpFlags)
if err := kubeConfigFlagSet.Parse(args); err != nil {
return nil, err
}
return kubeConfigFlags, nil
}
// FilterList returns a copy of "l" with elements from "toRemove" filtered out.
func FilterList(l []string, rl []string) []string {
c := CopyStrSlice(l)
for _, r := range rl {
c = RemoveAllElements(c, r)
}
return c
}
// RemoveAllElements removes all elements from "s" which match the string "r".
func RemoveAllElements(s []string, r string) []string {
for i, rlen := 0, len(s); i < rlen; i++ {
j := i - (rlen - len(s))
if s[j] == r {
s = append(s[:j], s[j+1:]...)
}
}
return s
}
// CopyStrSlice returns a copy of the slice of strings.
func CopyStrSlice(s []string) []string {
c := make([]string, len(s))
copy(c, s)
return c
}
// NewRestConfig returns a new rest.Config parsed from --kubeconfig and --master
func NewRestConfig(f *configflags.ConfigFlags) (*rest.Config, error) {
return f.ToRESTConfig()
}
// NewKubernetesClientSet provides a clientset for talking to k8s clusters
func NewKubernetesClientSet(c *rest.Config) (*kubernetes.Clientset, error) {
return kubernetes.NewForConfig(c)
}
// NewExtensionsClientSet provides an apiextensions ClientSet
func NewExtensionsClientSet(c *rest.Config) (*clientset.Clientset, error) {
return clientset.NewForConfig(c)
}
// NewDynamicClient returns a Dynamic Client
func NewDynamicClient(c *rest.Config) (dynamic.Interface, error) {
return dynamic.NewForConfig(c)
}
// NewRestMapper provides a Discovery rest mapper
func NewRestMapper(c *rest.Config) (meta.RESTMapper, error) {
return apiutil.NewDiscoveryRESTMapper(c)
}
// NewClient provides a dynamic.Interface
func NewClient(d dynamic.Interface, m meta.RESTMapper) (client.Client, error) {
return client.NewForConfig(d, m)
}