/
apply.go
156 lines (128 loc) · 4.65 KB
/
apply.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
//
// DISCLAIMER
//
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package crd
import (
"context"
authorization "k8s.io/api/authorization/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/crd/crds"
"github.com/arangodb/kube-arangodb/pkg/logging"
kresources "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
)
var logger = logging.Global().RegisterAndGetLogger("crd", logging.Info)
// Deprecated: use EnsureCRDWithOptions instead
func EnsureCRD(ctx context.Context, client kclient.Client, ignoreErrors bool) error {
return EnsureCRDWithOptions(ctx, client, EnsureCRDOptions{
IgnoreErrors: ignoreErrors,
})
}
type EnsureCRDOptions struct {
// IgnoreErrors do not return errors if could not apply CRD
IgnoreErrors bool
// ForceUpdate if true, CRD will be updated even if definitions versions are the same
ForceUpdate bool
// CRDOptions defines options per each CRD
CRDOptions map[string]crds.CRDOptions
}
func EnsureCRDWithOptions(ctx context.Context, client kclient.Client, opts EnsureCRDOptions) error {
crdsLock.Lock()
defer crdsLock.Unlock()
for crdName, crdReg := range registeredCRDs {
getAccess := verifyCRDAccess(ctx, client, crdName, "get")
if !getAccess.Allowed {
logger.Str("crd", crdName).Info("Get Operations is not allowed. Continue")
continue
}
var opt *crds.CRDOptions
if o, ok := opts.CRDOptions[crdName]; ok {
opt = &o
}
def := crdReg.getter(opt)
err := tryApplyCRD(ctx, client, def, opts.ForceUpdate)
if !opts.IgnoreErrors && err != nil {
return err
}
}
return nil
}
func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition, forceUpdate bool) error {
crdDefinitions := client.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions()
crdName := def.CRD.Name
c, err := crdDefinitions.Get(ctx, crdName, meta.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
logger.Err(err).Str("crd", crdName).Warn("Get Operations is not allowed due to error")
return err
}
createAccess := verifyCRDAccess(ctx, client, crdName, "create")
if !createAccess.Allowed {
logger.Str("crd", crdName).Info("Create Operations is not allowed but CRD is missing. Continue")
return nil
}
c = &apiextensions.CustomResourceDefinition{
ObjectMeta: meta.ObjectMeta{
Name: crdName,
Labels: map[string]string{
Version: string(def.Version),
},
},
Spec: def.CRD.Spec,
}
if _, err := crdDefinitions.Create(ctx, c, meta.CreateOptions{}); err != nil {
logger.Err(err).Str("crd", crdName).Warn("Create Operations is not allowed due to error")
return err
}
logger.Str("crd", crdName).Info("CRD Created")
return nil
}
updateAccess := verifyCRDAccess(ctx, client, crdName, "update")
if !updateAccess.Allowed {
logger.Str("crd", crdName).Info("Update Operations is not allowed. Continue")
return nil
}
if c.ObjectMeta.Labels == nil {
c.ObjectMeta.Labels = map[string]string{}
}
if v, ok := c.ObjectMeta.Labels[Version]; ok && v != "" {
if !forceUpdate && !isUpdateRequired(def.Version, driver.Version(v)) {
logger.Str("crd", crdName).Info("CRD Update not required")
return nil
}
}
c.ObjectMeta.Labels[Version] = string(def.Version)
c.Spec = def.CRD.Spec
if _, err := crdDefinitions.Update(ctx, c, meta.UpdateOptions{}); err != nil {
logger.Err(err).Str("crd", crdName).Warn("Failed to update CRD definition")
return err
}
logger.Str("crd", crdName).Info("CRD Updated")
return nil
}
func verifyCRDAccess(ctx context.Context, client kclient.Client, crd string, verb string) authorization.SubjectAccessReviewStatus {
if c := verifyCRDAccessForTests; c != nil {
return *c
}
return kresources.VerifyAccessRequestStatus(ctx, client.Kubernetes(), verb, "apiextensions.k8s.io", "v1", "customresourcedefinitions", "", crd, "")
}
var verifyCRDAccessForTests *authorization.SubjectAccessReviewStatus