forked from fluxcd/flux
/
kubernetes.go
341 lines (295 loc) · 9.08 KB
/
kubernetes.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package kubernetes
import (
"bytes"
"fmt"
"sync"
k8syaml "github.com/ghodss/yaml"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
fhrclient "github.com/weaveworks/flux/integrations/client/clientset/versioned"
"gopkg.in/yaml.v2"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "k8s.io/client-go/kubernetes"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/ssh"
)
const (
StatusUnknown = "unknown"
StatusReady = "ready"
StatusUpdating = "updating"
)
type coreClient k8sclient.Interface
type fluxHelmClient fhrclient.Interface
type extendedClient struct {
coreClient
fluxHelmClient
}
// --- internal types for keeping track of syncing
type metadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
type apiObject struct {
resource.Resource
Kind string `yaml:"kind"`
Metadata metadata `yaml:"metadata"`
}
// A convenience for getting an minimal object from some bytes.
func parseObj(def []byte) (*apiObject, error) {
obj := apiObject{}
return &obj, yaml.Unmarshal(def, &obj)
}
func (o *apiObject) hasNamespace() bool {
return o.Metadata.Namespace != ""
}
// --- add-ons
// Kubernetes has a mechanism of "Add-ons", whereby manifest files
// left in a particular directory on the Kubernetes master will be
// applied. We can recognise these, because they:
// 1. Must be in the namespace `kube-system`; and,
// 2. Must have one of the labels below set, else the addon manager will ignore them.
//
// We want to ignore add-ons, since they are managed by the add-on
// manager, and attempts to control them via other means will fail.
// k8sObject represents an value from which you can obtain typical
// Kubernetes metadata. These methods are implemented by the
// Kubernetes API resource types.
type k8sObject interface {
GetNamespace() string
GetLabels() map[string]string
GetAnnotations() map[string]string
}
func isAddon(obj k8sObject) bool {
if obj.GetNamespace() != "kube-system" {
return false
}
labels := obj.GetLabels()
if labels["kubernetes.io/cluster-service"] == "true" ||
labels["addonmanager.kubernetes.io/mode"] == "EnsureExists" ||
labels["addonmanager.kubernetes.io/mode"] == "Reconcile" {
return true
}
return false
}
// --- /add ons
// Cluster is a handle to a Kubernetes API server.
// (Typically, this code is deployed into the same cluster.)
type Cluster struct {
client extendedClient
applier Applier
version string // string response for the version command.
logger log.Logger
sshKeyRing ssh.KeyRing
nsWhitelist []string
nsWhitelistLogged map[string]bool // to keep track of whether we've logged a problem with seeing a whitelisted ns
mu sync.Mutex
}
// NewCluster returns a usable cluster.
func NewCluster(clientset k8sclient.Interface,
fluxHelmClientset fhrclient.Interface,
applier Applier,
sshKeyRing ssh.KeyRing,
logger log.Logger,
nsWhitelist []string) *Cluster {
c := &Cluster{
client: extendedClient{
clientset,
fluxHelmClientset,
},
applier: applier,
logger: logger,
sshKeyRing: sshKeyRing,
nsWhitelist: nsWhitelist,
nsWhitelistLogged: map[string]bool{},
}
return c
}
// --- cluster.Cluster
// SomeControllers returns the controllers named, missing out any that don't
// exist in the cluster. They do not necessarily have to be returned
// in the order requested.
func (c *Cluster) SomeControllers(ids []flux.ResourceID) (res []cluster.Controller, err error) {
var controllers []cluster.Controller
for _, id := range ids {
ns, kind, name := id.Components()
resourceKind, ok := resourceKinds[kind]
if !ok {
return nil, fmt.Errorf("Unsupported kind %v", kind)
}
podController, err := resourceKind.getPodController(c, ns, name)
if err != nil {
return nil, err
}
if !isAddon(podController) {
controllers = append(controllers, podController.toClusterController(id))
}
}
return controllers, nil
}
// AllControllers returns all controllers matching the criteria; that is, in
// the namespace (or any namespace if that argument is empty)
func (c *Cluster) AllControllers(namespace string) (res []cluster.Controller, err error) {
namespaces, err := c.getAllowedNamespaces()
if err != nil {
return nil, errors.Wrap(err, "getting namespaces")
}
var allControllers []cluster.Controller
for _, ns := range namespaces {
if namespace != "" && ns.Name != namespace {
continue
}
for kind, resourceKind := range resourceKinds {
podControllers, err := resourceKind.getPodControllers(c, ns.Name)
if err != nil {
if se, ok := err.(*apierrors.StatusError); ok && se.ErrStatus.Reason == meta_v1.StatusReasonNotFound {
// Kind not supported by API server, skip
continue
} else {
return nil, err
}
}
for _, podController := range podControllers {
if !isAddon(podController) {
id := flux.MakeResourceID(ns.Name, kind, podController.name)
allControllers = append(allControllers, podController.toClusterController(id))
}
}
}
}
return allControllers, nil
}
// Sync performs the given actions on resources. Operations are
// asynchronous, but serialised.
func (c *Cluster) Sync(spec cluster.SyncDef) error {
logger := log.With(c.logger, "method", "Sync")
cs := makeChangeSet()
var errs cluster.SyncError
for _, action := range spec.Actions {
stages := []struct {
res resource.Resource
cmd string
}{
{action.Delete, "delete"},
{action.Apply, "apply"},
}
for _, stage := range stages {
if stage.res == nil {
continue
}
obj, err := parseObj(stage.res.Bytes())
if err == nil {
obj.Resource = stage.res
cs.stage(stage.cmd, obj)
} else {
errs = append(errs, cluster.ResourceError{Resource: stage.res, Error: err})
break
}
}
}
c.mu.Lock()
defer c.mu.Unlock()
if applyErrs := c.applier.apply(logger, cs); len(applyErrs) > 0 {
errs = append(errs, applyErrs...)
}
// If `nil`, errs is a cluster.SyncError(nil) rather than error(nil)
if errs != nil {
return errs
}
return nil
}
func (c *Cluster) Ping() error {
_, err := c.client.coreClient.Discovery().ServerVersion()
return err
}
// Export exports cluster resources
func (c *Cluster) Export() ([]byte, error) {
var config bytes.Buffer
namespaces, err := c.getAllowedNamespaces()
if err != nil {
return nil, errors.Wrap(err, "getting namespaces")
}
for _, ns := range namespaces {
err := appendYAML(&config, "v1", "Namespace", ns)
if err != nil {
return nil, errors.Wrap(err, "marshalling namespace to YAML")
}
for _, resourceKind := range resourceKinds {
podControllers, err := resourceKind.getPodControllers(c, ns.Name)
if err != nil {
if se, ok := err.(*apierrors.StatusError); ok && se.ErrStatus.Reason == meta_v1.StatusReasonNotFound {
// Kind not supported by API server, skip
continue
} else {
return nil, err
}
}
for _, pc := range podControllers {
if !isAddon(pc) {
if err := appendYAML(&config, pc.apiVersion, pc.kind, pc.k8sObject); err != nil {
return nil, err
}
}
}
}
}
return config.Bytes(), nil
}
// kind & apiVersion must be passed separately as the object's TypeMeta is not populated
func appendYAML(buffer *bytes.Buffer, apiVersion, kind string, object interface{}) error {
yamlBytes, err := k8syaml.Marshal(object)
if err != nil {
return err
}
buffer.WriteString("---\n")
buffer.WriteString("apiVersion: ")
buffer.WriteString(apiVersion)
buffer.WriteString("\nkind: ")
buffer.WriteString(kind)
buffer.WriteString("\n")
buffer.Write(yamlBytes)
return nil
}
func (c *Cluster) PublicSSHKey(regenerate bool) (ssh.PublicKey, error) {
if regenerate {
if err := c.sshKeyRing.Regenerate(); err != nil {
return ssh.PublicKey{}, err
}
}
publicKey, _ := c.sshKeyRing.KeyPair()
return publicKey, nil
}
// getAllowedNamespaces returns a list of namespaces that the Flux instance is expected
// to have access to and can look for resources inside of.
// It returns a list of all namespaces unless a namespace whitelist has been set on the Cluster
// instance, in which case it returns a list containing the namespaces from the whitelist
// that exist in the cluster.
func (c *Cluster) getAllowedNamespaces() ([]apiv1.Namespace, error) {
if len(c.nsWhitelist) > 0 {
nsList := []apiv1.Namespace{}
for _, name := range c.nsWhitelist {
ns, err := c.client.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
switch {
case err == nil:
c.nsWhitelistLogged[name] = false // reset, so if the namespace goes away we'll log it again
nsList = append(nsList, *ns)
case apierrors.IsUnauthorized(err) || apierrors.IsForbidden(err) || apierrors.IsNotFound(err):
if !c.nsWhitelistLogged[name] {
c.logger.Log("warning", "whitelisted namespace inaccessible", "namespace", name, "err", err)
c.nsWhitelistLogged[name] = true
}
default:
return nil, err
}
}
return nsList, nil
}
namespaces, err := c.client.CoreV1().Namespaces().List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
return namespaces.Items, nil
}