forked from juju/juju
/
namespaces.go
174 lines (156 loc) · 5.59 KB
/
namespaces.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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"context"
"fmt"
"github.com/juju/errors"
core "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/informers"
"github.com/DavinZhang/juju/caas/kubernetes/provider/constants"
"github.com/DavinZhang/juju/caas/kubernetes/provider/utils"
k8sannotations "github.com/DavinZhang/juju/core/annotations"
"github.com/DavinZhang/juju/core/watcher"
)
func checkNamespaceOwnedByJuju(ns *core.Namespace, annotationMap map[string]string) error {
if ns == nil {
return nil
}
if k8sannotations.New(ns.GetAnnotations()).HasAll(annotationMap) {
return nil
}
return errors.NotValidf(
"namespace %q is not owned by Juju, it has annotation %v, but required %v",
ns.GetName(), ns.GetAnnotations(), annotationMap,
)
}
// Namespaces returns names of the namespaces on the cluster.
func (k *kubernetesClient) Namespaces() ([]string, error) {
namespaces := k.client().CoreV1().Namespaces()
ns, err := namespaces.List(context.TODO(), v1.ListOptions{})
if err != nil {
return nil, errors.Annotate(err, "listing namespaces")
}
result := make([]string, len(ns.Items))
for i, n := range ns.Items {
if err := checkNamespaceOwnedByJuju(&n, k.annotations); err != nil {
continue
}
result[i] = n.Name
}
return result, nil
}
// GetNamespace returns the namespace for the specified name.
func (k *kubernetesClient) GetNamespace(name string) (*core.Namespace, error) {
ns, err := k.getNamespaceByName(name)
if err != nil {
return nil, errors.Trace(err)
}
if err := checkNamespaceOwnedByJuju(ns, k.annotations); err != nil {
return nil, errors.NotFoundf("namespace %q", name)
}
return ns, nil
}
// getNamespaceByName is used internally for bootstrap.
// Note: it should be never used by something else. "GetNamespace" is what you should use.
func (k *kubernetesClient) getNamespaceByName(name string) (*core.Namespace, error) {
ns, err := k.client().CoreV1().Namespaces().Get(context.TODO(), name, v1.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, errors.NotFoundf("namespace %q", name)
}
if err != nil {
return nil, errors.Annotatef(err, "getting namespace %q", name)
}
return ns, nil
}
// listNamespacesByAnnotations filters namespaces by annotations.
func (k *kubernetesClient) listNamespacesByAnnotations(annotations k8sannotations.Annotation) ([]core.Namespace, error) {
namespaces, err := k.client().CoreV1().Namespaces().List(context.TODO(), v1.ListOptions{})
if err != nil {
return nil, errors.Trace(err)
}
var matchedNS []core.Namespace
annotationMap := annotations
for _, ns := range namespaces.Items {
if err := checkNamespaceOwnedByJuju(&ns, annotationMap); err == nil {
matchedNS = append(matchedNS, ns)
}
}
if len(matchedNS) > 0 {
doLog := logger.Debugf
if len(matchedNS) > 1 {
// this should never happen before we enable multi controller in single cluster.
doLog = logger.Warningf
}
doLog("found %d matched namespaces with annotations %v", len(matchedNS), annotationMap)
return matchedNS, nil
}
return nil, errors.NotFoundf("namespace for %v", k.annotations)
}
// GetCurrentNamespace returns current namespace name.
func (k *kubernetesClient) GetCurrentNamespace() string {
return k.namespace
}
func (k *kubernetesClient) ensureNamespaceAnnotations(ns *core.Namespace) error {
annotations := k8sannotations.New(ns.GetAnnotations()).Merge(k.annotations)
err := annotations.CheckKeysNonEmpty(utils.AnnotationControllerUUIDKey(false), utils.AnnotationModelUUIDKey(false))
if err != nil {
return errors.Trace(err)
}
ns.SetAnnotations(annotations)
return nil
}
// createNamespace creates a namespace with the input name.
func (k *kubernetesClient) createNamespace(name string) error {
ns := &core.Namespace{ObjectMeta: v1.ObjectMeta{Name: name}}
ns.SetLabels(utils.LabelsMerge(
ns.GetLabels(),
utils.LabelsForModel(k.CurrentModel(), false),
utils.LabelsJuju))
if err := k.ensureNamespaceAnnotations(ns); err != nil {
if errors.IsNotFound(err) {
return errors.NewAlreadyExists(nil, fmt.Sprintf("namespace %q may already be in use", name))
}
return errors.Trace(err)
}
_, err := k.client().CoreV1().Namespaces().Create(context.TODO(), ns, v1.CreateOptions{})
if k8serrors.IsAlreadyExists(err) {
return errors.AlreadyExistsf("namespace %q", name)
}
return errors.Trace(err)
}
func (k *kubernetesClient) deleteNamespace() error {
// deleteNamespace is used as a means to implement Destroy().
// All model resources are provisioned in the namespace;
// deleting the namespace will also delete those resources.
ns, err := k.GetNamespace(k.namespace)
if errors.IsNotFound(err) {
return nil
}
if err != nil {
return errors.Trace(err)
}
if err := checkNamespaceOwnedByJuju(ns, k.annotations); err != nil {
return errors.Trace(err)
}
err = k.client().CoreV1().Namespaces().Delete(context.TODO(), k.namespace, v1.DeleteOptions{
PropagationPolicy: constants.DefaultPropagationPolicy(),
})
if k8serrors.IsNotFound(err) {
return nil
}
return errors.Trace(err)
}
// WatchNamespace returns a watcher which notifies when there
// are changes to current namespace.
func (k *kubernetesClient) WatchNamespace() (watcher.NotifyWatcher, error) {
factory := informers.NewSharedInformerFactoryWithOptions(k.client(), 0,
informers.WithTweakListOptions(func(o *v1.ListOptions) {
o.FieldSelector = fields.OneTermEqualSelector("metadata.name", k.namespace).String()
}),
)
return k.newWatcher(factory.Core().V1().Namespaces().Informer(), k.namespace, k.clock)
}