-
Notifications
You must be signed in to change notification settings - Fork 886
/
args.go
179 lines (160 loc) · 4.47 KB
/
args.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright 2021 The KubeVela 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 common
import (
"fmt"
pkgmulticluster "github.com/kubevela/pkg/multicluster"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/flowcontrol"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/kubevela/workflow/pkg/cue/packages"
"github.com/oam-dev/kubevela/pkg/oam"
)
// Args is args for controller-runtime client
type Args struct {
config *rest.Config
rawConfig *api.Config
Schema *runtime.Scheme
client client.Client
pd *packages.PackageDiscover
dc *discovery.DiscoveryClient
}
// SetConfig insert kubeconfig into Args
func (a *Args) SetConfig(c *rest.Config) error {
if c != nil {
a.config = c
return nil
}
restConf, err := config.GetConfig()
if err != nil {
return err
}
restConf.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(100, 200)
a.config = restConf
return nil
}
// GetConfig get config, if not exist, will create
func (a *Args) GetConfig() (*rest.Config, error) {
if a.config != nil {
return a.config, nil
}
if err := a.SetConfig(nil); err != nil {
return nil, err
}
return a.config, nil
}
// GetRawConfig get raw kubeconfig, if not exist, will create
func (a *Args) GetRawConfig() (*api.Config, error) {
if a.rawConfig != nil {
return a.rawConfig, nil
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
raw, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules, nil).RawConfig()
if err != nil {
return nil, err
}
return &raw, nil
}
// GetNamespaceFromConfig will get namespace from kube config
func (a *Args) GetNamespaceFromConfig() string {
conf, err := a.GetRawConfig()
if err != nil || conf == nil || conf.Contexts == nil {
return ""
}
ctx, ok := conf.Contexts[conf.CurrentContext]
if !ok {
return ""
}
return ctx.Namespace
}
// SetClient set custom client
func (a *Args) SetClient(c client.Client) {
a.client = c
}
// GetClient get client if exist
func (a *Args) GetClient() (client.Client, error) {
if a.client != nil {
return a.client, nil
}
if a.config == nil {
if err := a.SetConfig(nil); err != nil {
return nil, err
}
}
newClient, err := pkgmulticluster.NewClient(a.config,
pkgmulticluster.ClientOptions{
Options: client.Options{Scheme: a.Schema}})
if err != nil {
return nil, err
}
a.client = newClient
return a.client, nil
}
// GetFakeClient returns a fake client with the definition objects preloaded
func (a *Args) GetFakeClient(defs []oam.Object) (client.Client, error) {
if a.client != nil {
return a.client, nil
}
if a.config == nil {
if err := a.SetConfig(nil); err != nil {
return nil, err
}
}
objs := make([]client.Object, 0, len(defs))
for _, def := range defs {
if unstructDef, ok := def.(*unstructured.Unstructured); ok {
objs = append(objs, unstructDef)
}
}
return fake.NewClientBuilder().WithObjects(objs...).WithScheme(a.Schema).Build(), nil
}
// GetPackageDiscover get PackageDiscover client if exist, create if not exist.
func (a *Args) GetPackageDiscover() (*packages.PackageDiscover, error) {
if a.config == nil {
if err := a.SetConfig(nil); err != nil {
return nil, err
}
}
if a.pd != nil {
return a.pd, nil
}
pd, err := packages.NewPackageDiscover(a.config)
if err != nil {
return nil, fmt.Errorf("failed to create CRD discovery for CUE package client %w", err)
}
a.pd = pd
return pd, nil
}
// GetDiscoveryClient return a discovery client from cli args
func (a *Args) GetDiscoveryClient() (*discovery.DiscoveryClient, error) {
if a.dc != nil {
return a.dc, nil
}
cfg, err := a.GetConfig()
if err != nil {
return nil, err
}
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return nil, err
}
return dc, nil
}