|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package crd |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + |
| 27 | + "github.com/arangodb/go-driver" |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/util/kclient" |
| 29 | + "github.com/rs/zerolog" |
| 30 | + authorization "k8s.io/api/authorization/v1" |
| 31 | + apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 32 | + "k8s.io/apimachinery/pkg/api/errors" |
| 33 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | +) |
| 35 | + |
| 36 | +func EnsureCRD(ctx context.Context, log zerolog.Logger, client kclient.Client) { |
| 37 | + crdsLock.Lock() |
| 38 | + defer crdsLock.Unlock() |
| 39 | + |
| 40 | + for crd, spec := range crds { |
| 41 | + getAccess := verifyCRDAccess(ctx, client, crd, "get") |
| 42 | + |
| 43 | + if !getAccess.Allowed { |
| 44 | + log.Info().Str("crd", crd).Msgf("Get Operations is not allowed. Continue") |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + c, err := client.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crd, meta.GetOptions{}) |
| 49 | + if err != nil { |
| 50 | + if !errors.IsNotFound(err) { |
| 51 | + log.Warn().Err(err).Str("crd", crd).Msgf("Get Operations is not allowed due to error. Continue") |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + createAccess := verifyCRDAccess(ctx, client, crd, "create") |
| 56 | + |
| 57 | + if !createAccess.Allowed { |
| 58 | + log.Info().Str("crd", crd).Msgf("Create Operations is not allowed but CRD is missing. Continue") |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + c = &apiextensions.CustomResourceDefinition{ |
| 63 | + ObjectMeta: meta.ObjectMeta{ |
| 64 | + Name: crd, |
| 65 | + Labels: map[string]string{ |
| 66 | + Version: string(spec.version), |
| 67 | + }, |
| 68 | + }, |
| 69 | + Spec: spec.spec, |
| 70 | + } |
| 71 | + |
| 72 | + if _, err := client.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions().Create(ctx, c, meta.CreateOptions{}); err != nil { |
| 73 | + log.Warn().Err(err).Str("crd", crd).Msgf("Create Operations is not allowed due to error. Continue") |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + log.Info().Str("crd", crd).Msgf("CRD Created") |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + updateAccess := verifyCRDAccess(ctx, client, crd, "update") |
| 82 | + |
| 83 | + if !updateAccess.Allowed { |
| 84 | + log.Info().Str("crd", crd).Msgf("Update Operations is not allowed. Continue") |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + if c.ObjectMeta.Labels == nil { |
| 89 | + c.ObjectMeta.Labels = map[string]string{} |
| 90 | + } |
| 91 | + |
| 92 | + if v, ok := c.ObjectMeta.Labels[Version]; ok { |
| 93 | + if v != "" { |
| 94 | + if !isUpdateRequired(spec.version, driver.Version(v)) { |
| 95 | + log.Info().Str("crd", crd).Msgf("CRD Update not required") |
| 96 | + continue |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + c.ObjectMeta.Labels[Version] = string(spec.version) |
| 102 | + |
| 103 | + c.Spec = spec.spec |
| 104 | + |
| 105 | + if _, err := client.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions().Update(ctx, c, meta.UpdateOptions{}); err != nil { |
| 106 | + log.Warn().Err(err).Str("crd", crd).Msgf("Create Operations is not allowed due to error. Continue") |
| 107 | + continue |
| 108 | + } |
| 109 | + log.Info().Str("crd", crd).Msgf("CRD Updated") |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func verifyCRDAccess(ctx context.Context, client kclient.Client, crd string, verb string) authorization.SubjectAccessReviewStatus { |
| 114 | + r, err := verifyCRDAccessRequest(ctx, client, crd, verb) |
| 115 | + if err != nil { |
| 116 | + return authorization.SubjectAccessReviewStatus{ |
| 117 | + Allowed: false, |
| 118 | + Reason: fmt.Sprintf("Unable to check access: %s", err.Error()), |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return r.Status |
| 123 | +} |
| 124 | + |
| 125 | +func verifyCRDAccessRequest(ctx context.Context, client kclient.Client, crd string, verb string) (*authorization.SelfSubjectAccessReview, error) { |
| 126 | + review := authorization.SelfSubjectAccessReview{ |
| 127 | + Spec: authorization.SelfSubjectAccessReviewSpec{ |
| 128 | + ResourceAttributes: &authorization.ResourceAttributes{ |
| 129 | + Verb: verb, |
| 130 | + Group: "apiextensions.k8s.io", |
| 131 | + Version: "v1", |
| 132 | + Resource: "customresourcedefinitions", |
| 133 | + Name: crd, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + return client.Kubernetes().AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, &review, meta.CreateOptions{}) |
| 139 | +} |
0 commit comments