-
Notifications
You must be signed in to change notification settings - Fork 5
/
ingressclass.go
229 lines (204 loc) · 6.89 KB
/
ingressclass.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ingressclass
import (
"context"
"net/http"
"sync"
"time"
"github.com/forbearing/k8s/types"
"github.com/forbearing/k8s/util/client"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/informers"
"k8s.io/client-go/informers/internalinterfaces"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
type Handler struct {
ctx context.Context
kubeconfig string
config *rest.Config
httpClient *http.Client
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
resyncPeriod time.Duration
informerScope string
tweakListOptions internalinterfaces.TweakListOptionsFunc
informerFactory informers.SharedInformerFactory
Options *types.HandlerOptions
l sync.RWMutex
}
// NewOrDie simply call New() to get a ingressclass handler.
// panic if there is any error occurs.
// The kubeconfig precedence is:
// * kubeconfig variable passed.
// * KUBECONFIG environment variable pointing at a file.
// * $HOME/.kube/config if exists.
// * In-cluster config if running in cluster.
func NewOrDie(ctx context.Context, kubeconfig string) *Handler {
handler, err := New(ctx, kubeconfig)
if err != nil {
panic(err)
}
return handler
}
// New returns a ingressclass handler from kubeconfig or in-cluster config.
func New(ctx context.Context, kubeconfig string) (*Handler, error) {
var (
err error
config *rest.Config
httpClient *http.Client
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
informerFactory informers.SharedInformerFactory
)
// create rest config, and config precedence.
// * kubeconfig variable passed.
// * KUBECONFIG environment variable pointing at a file.
// * $HOME/.kube/config if exists.
// * In-cluster config if running in cluster.
if config, err = client.RESTConfig(kubeconfig); err != nil {
return nil, err
}
// setup APIPath, GroupVersion and NegotiatedSerializer before initializing a RESTClient
config.APIPath = "api"
config.GroupVersion = &networkingv1.SchemeGroupVersion
config.NegotiatedSerializer = scheme.Codecs
// create a http client for the given config.
if httpClient, err = rest.HTTPClientFor(config); err != nil {
return nil, err
}
// create a RESTClient for the given config and http client.
if restClient, err = rest.RESTClientForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a Clientset for the given config and http client.
if clientset, err = kubernetes.NewForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a dynamic client for the given config and http client.
if dynamicClient, err = dynamic.NewForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a DiscoveryClient for the given config and http client.
if discoveryClient, err = discovery.NewDiscoveryClientForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a sharedInformerFactory for all namespaces.
informerFactory = informers.NewSharedInformerFactory(clientset, 0)
return &Handler{
ctx: ctx,
kubeconfig: kubeconfig,
config: config,
httpClient: httpClient,
restClient: restClient,
clientset: clientset,
dynamicClient: dynamicClient,
discoveryClient: discoveryClient,
informerFactory: informerFactory,
Options: &types.HandlerOptions{},
}, nil
}
// WithDryRun deep copies a new handler and prints the create/update/apply/delete
// operations, without sending it to apiserver.
func (h *Handler) WithDryRun() *Handler {
handler := h.DeepCopy()
handler.Options.CreateOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.UpdateOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.ApplyOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.PatchOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.DeleteOptions.DryRun = []string{metav1.DryRunAll}
return handler
}
func (in *Handler) DeepCopy() *Handler {
if in == nil {
return nil
}
return &Handler{
ctx: in.ctx,
kubeconfig: in.kubeconfig,
config: in.config,
httpClient: in.httpClient,
restClient: in.restClient,
clientset: in.clientset,
dynamicClient: in.dynamicClient,
discoveryClient: in.discoveryClient,
informerFactory: in.informerFactory,
resyncPeriod: in.resyncPeriod,
informerScope: in.informerScope,
tweakListOptions: in.tweakListOptions,
Options: &types.HandlerOptions{
CreateOptions: *in.Options.CreateOptions.DeepCopy(),
UpdateOptions: *in.Options.UpdateOptions.DeepCopy(),
ApplyOptions: *in.Options.ApplyOptions.DeepCopy(),
DeleteOptions: *in.Options.DeleteOptions.DeepCopy(),
GetOptions: *in.Options.GetOptions.DeepCopy(),
ListOptions: *in.Options.ListOptions.DeepCopy(),
PatchOptions: *in.Options.PatchOptions.DeepCopy(),
},
}
}
func (h *Handler) SetTimeout(timeout int64) {
h.l.Lock()
defer h.l.Unlock()
h.Options.ListOptions.TimeoutSeconds = &timeout
}
func (h *Handler) SetLimit(limit int64) {
h.l.Lock()
defer h.l.Unlock()
h.Options.ListOptions.Limit = limit
}
func (h *Handler) SetForceDelete(force bool) {
h.l.Lock()
defer h.l.Unlock()
if force {
h.Options.DeleteOptions.GracePeriodSeconds = new(int64)
}
}
// RESTConfig returns underlying rest config.
func (h *Handler) RESTConfig() *rest.Config {
return h.config
}
// RESTClient returns underlying rest client.
func (h *Handler) RESTClient() *rest.RESTClient {
return h.restClient
}
// Clientset returns underlying clientset.
func (h *Handler) Clientset() *kubernetes.Clientset {
return h.clientset
}
// DynamicClient returns underlying dynamic client.
func (h *Handler) DynamicClient() dynamic.Interface {
return h.dynamicClient
}
// DiscoveryClient returns underlying discovery client.
func (h *Handler) DiscoveryClient() *discovery.DiscoveryClient {
return h.discoveryClient
}
// GVK contains the Group, Version, Kind name of ingressclass.
var GVK = schema.GroupVersionKind{
Group: networkingv1.SchemeGroupVersion.Group,
Version: networkingv1.SchemeGroupVersion.Version,
Kind: types.KindIngressClass,
}
// GVR contains the Group, Version and Resource name of ingressclass.
var GVR = schema.GroupVersionResource{
Group: networkingv1.SchemeGroupVersion.Group,
Version: networkingv1.SchemeGroupVersion.Version,
Resource: types.ResourceIngressClass,
}
// Kind is the ingressclass Kind name.
var Kind = GVK.Kind
// Group is the ingressclass Group name.
var Group = GVK.Group
// Version is the ingressclass Version name.
var Version = GVK.Version
// Resource is the ingressclass Resource name.
var Resource = GVR.Resource