forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.go
262 lines (232 loc) · 7.91 KB
/
k8s.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2016-2018 Tigera, Inc. All rights reserved.
// 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 k8s
import (
"fmt"
"strings"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Import all auth providers.
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
capi "github.com/projectcalico/libcalico-go/lib/apis/v1"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/upgrade/migrator/clients/v1/k8s/custom"
"github.com/projectcalico/libcalico-go/lib/upgrade/migrator/clients/v1/k8s/resources"
)
type KubeClient struct {
// Main Kubernetes clients.
clientSet *kubernetes.Clientset
// Client for interacting with CustomResourceDefinition.
crdClientV1 *rest.RESTClient
// Clients for interacting with Calico resources.
nodeBgpPeerClient resources.K8sResourceClient
globalBgpConfigClient resources.K8sResourceClient
globalFelixConfigClient resources.K8sResourceClient
nodeConfigClient resources.K8sResourceClient
}
func NewKubeClient(kc *capi.KubeConfig) (*KubeClient, error) {
// Use the kubernetes client code to load the kubeconfig file and combine it with the overrides.
configOverrides := &clientcmd.ConfigOverrides{}
var overridesMap = []struct {
variable *string
value string
}{
{&configOverrides.ClusterInfo.Server, kc.K8sAPIEndpoint},
{&configOverrides.AuthInfo.ClientCertificate, kc.K8sCertFile},
{&configOverrides.AuthInfo.ClientKey, kc.K8sKeyFile},
{&configOverrides.ClusterInfo.CertificateAuthority, kc.K8sCAFile},
{&configOverrides.AuthInfo.Token, kc.K8sAPIToken},
}
// Set an explicit path to the kubeconfig if one
// was provided.
loadingRules := clientcmd.ClientConfigLoadingRules{}
if kc.Kubeconfig != "" {
loadingRules.ExplicitPath = kc.Kubeconfig
}
// Using the override map above, populate any non-empty values.
for _, override := range overridesMap {
if override.value != "" {
*override.variable = override.value
}
}
if kc.K8sInsecureSkipTLSVerify {
configOverrides.ClusterInfo.InsecureSkipTLSVerify = true
}
// A kubeconfig file was provided. Use it to load a config, passing through
// any overrides.
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&loadingRules, configOverrides).ClientConfig()
if err != nil {
return nil, resources.K8sErrorToCalico(err, nil)
}
// Create the clientset
cs, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, resources.K8sErrorToCalico(err, nil)
}
log.Debugf("Created k8s clientSet: %+v", cs)
crdClientV1, err := buildCRDClientV1(*config)
if err != nil {
return nil, fmt.Errorf("Failed to build V1 CRD client: %s", err)
}
kubeClient := &KubeClient{
clientSet: cs,
crdClientV1: crdClientV1,
}
// Create the Calico sub-clients.
kubeClient.nodeBgpPeerClient = resources.NewNodeBGPPeerClient(cs)
kubeClient.globalBgpConfigClient = resources.NewGlobalBGPConfigClient(cs, crdClientV1)
kubeClient.globalFelixConfigClient = resources.NewGlobalFelixConfigClient(cs, crdClientV1)
return kubeClient, nil
}
func (c *KubeClient) IsKDD() bool {
return true
}
// waitForClusterType polls until GlobalFelixConfig is ready, or until 30 seconds have passed.
func (c *KubeClient) waitForClusterType() error {
return wait.PollImmediate(1*time.Second, 30*time.Second, func() (bool, error) {
return c.ensureClusterType()
})
}
// ensureClusterType ensures that the ClusterType is configured.
func (c *KubeClient) ensureClusterType() (bool, error) {
k := model.GlobalConfigKey{
Name: "ClusterType",
}
value := "KDD"
// See if a cluster type has been set. If so, append
// any existing values to it.
ct, err := c.Get(k)
if err != nil {
if _, ok := err.(errors.ErrorResourceDoesNotExist); !ok {
// Resource exists but we got another error.
return false, err
}
// Resource does not exist.
}
rv := ""
if ct != nil {
existingValue := ct.Value.(string)
if !strings.Contains(existingValue, "KDD") {
existingValue = fmt.Sprintf("%s,KDD", existingValue)
}
value = existingValue
rv = ct.Revision
}
log.WithField("value", value).Debug("Setting ClusterType")
_, err = c.Apply(&model.KVPair{
Key: k,
Value: value,
Revision: rv,
})
if err != nil {
// Don't return an error, but indicate that we need
// to retry.
log.Warnf("Failed to apply ClusterType: %s", err)
return false, nil
}
return true, nil
}
// buildCRDClientV1 builds a RESTClient configured to interact with Calico CustomResourceDefinitions
func buildCRDClientV1(cfg rest.Config) (*rest.RESTClient, error) {
// Generate config using the base config.
cfg.GroupVersion = &schema.GroupVersion{
Group: "crd.projectcalico.org",
Version: "v1",
}
cfg.APIPath = "/apis"
cfg.ContentType = runtime.ContentTypeJSON
cfg.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
cli, err := rest.RESTClientFor(&cfg)
if err != nil {
return nil, err
}
// We also need to register resources.
schemeBuilder := runtime.NewSchemeBuilder(
func(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(
*cfg.GroupVersion,
&custom.GlobalFelixConfig{},
&custom.GlobalFelixConfigList{},
&custom.GlobalBGPConfig{},
&custom.GlobalBGPConfigList{},
)
return nil
})
schemeBuilder.AddToScheme(scheme.Scheme)
return cli, nil
}
// Update an existing entry in the datastore. This errors if the entry does
// not exist. (Not implemented for KDD.)
func (c *KubeClient) Update(d *model.KVPair) (*model.KVPair, error) {
log.Warn("Attempt to 'Update' using kubernetes backend is not supported.")
return nil, errors.ErrorOperationNotSupported{
Identifier: d.Key,
Operation: "Update",
}
}
// Set an existing entry in the datastore. This ignores whether an entry already
// exists.
func (c *KubeClient) Apply(d *model.KVPair) (*model.KVPair, error) {
log.Warn("Attempt to 'Apply' using kubernetes backend is not supported.")
return nil, errors.ErrorOperationNotSupported{
Identifier: d.Key,
Operation: "Apply",
}
}
// Get an entry from the datastore. This errors if the entry does not exist.
func (c *KubeClient) Get(k model.Key) (*model.KVPair, error) {
log.Debugf("Performing 'Get' for %+v", k)
switch k.(type) {
case model.GlobalConfigKey:
return c.globalFelixConfigClient.Get(k)
case model.NodeBGPPeerKey:
return c.nodeBgpPeerClient.Get(k)
case model.GlobalBGPConfigKey:
return c.globalBgpConfigClient.Get(k)
default:
return nil, errors.ErrorOperationNotSupported{
Identifier: k,
Operation: "Get",
}
}
}
// List entries in the datastore. This may return an empty list if there are
// no entries matching the request in the ListInterface.
func (c *KubeClient) List(l model.ListInterface) ([]*model.KVPair, error) {
log.Debugf("Performing 'List' for %+v", l)
switch l.(type) {
case model.NodeBGPPeerListOptions:
k, _, err := c.nodeBgpPeerClient.List(l)
return k, err
case model.GlobalConfigListOptions:
k, _, err := c.globalFelixConfigClient.List(l)
return k, err
case model.GlobalBGPConfigListOptions:
k, _, err := c.globalBgpConfigClient.List(l)
return k, err
default:
return nil, errors.ErrorOperationNotSupported{
Identifier: l,
Operation: "List",
}
}
}