| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 17 | // The reconciler ensures that a base set of K8s resources is always available |
| 18 | // in the cluster. These are necessary to ensure correct out-of-the-box |
| 19 | // functionality. All resources containing the |
| 20 | // metropolis.monogon.dev/builtin=true label are assumed to be managed by the |
| 21 | // reconciler. |
| 22 | // It currently does not revert modifications made by admins, it is planned to |
| 23 | // create an admission plugin prohibiting such modifications to resources with |
| 24 | // the metropolis.monogon.dev/builtin label to deal with that problem. This |
| 25 | // would also solve a potential issue where you could delete resources just by |
| 26 | // adding the metropolis.monogon.dev/builtin=true label. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 27 | package reconciler |
| 28 | |
| 29 | import ( |
| 30 | "context" |
| 31 | "fmt" |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 32 | |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 33 | meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 34 | "k8s.io/client-go/kubernetes" |
| 35 | ) |
| 36 | |
| Tim Windelschmidt | 51daf25 | 2024-04-18 23:18:43 +0200 | [diff] [blame] | 37 | // True is a sad workaround for all the pointer booleans in K8s specs |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 38 | func True() *bool { |
| 39 | val := true |
| 40 | return &val |
| 41 | } |
| 42 | func False() *bool { |
| 43 | val := false |
| 44 | return &val |
| 45 | } |
| 46 | |
| 47 | const ( |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 48 | // BuiltinLabelKey is used as a k8s label to mark built-in objects (ie., |
| 49 | // managed by the reconciler) |
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 50 | BuiltinLabelKey = "metropolis.monogon.dev/builtin" |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 51 | // BuiltinLabelValue is used as a k8s label value, under the |
| 52 | // BuiltinLabelKey key. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 53 | BuiltinLabelValue = "true" |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 54 | // BuiltinRBACPrefix is used to prefix all built-in objects that are part |
| 55 | // of the rbac/v1 API (eg. {Cluster,}Role{Binding,} objects). This |
| 56 | // corresponds to the colon-separated 'namespaces' notation used by |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 57 | // Kubernetes system (system:) objects. |
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 58 | BuiltinRBACPrefix = "metropolis:" |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 59 | ) |
| 60 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 61 | // builtinLabels makes a kubernetes-compatible label dictionary (key->value) |
| 62 | // that is used to mark objects that are built-in into Metropolis (ie., managed |
| 63 | // by the reconciler). These are then subsequently retrieved by listBuiltins. |
| 64 | // The extra argument specifies what other labels are to be merged into the the |
| 65 | // labels dictionary, for convenience. If nil or empty, no extra labels will be |
| 66 | // applied. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 67 | func builtinLabels(extra map[string]string) map[string]string { |
| 68 | l := map[string]string{ |
| 69 | BuiltinLabelKey: BuiltinLabelValue, |
| 70 | } |
| Tim Windelschmidt | 24ce66f | 2024-04-18 23:59:24 +0200 | [diff] [blame] | 71 | for k, v := range extra { |
| 72 | l[k] = v |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 73 | } |
| 74 | return l |
| 75 | } |
| 76 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 77 | // listBuiltins returns a k8s client ListOptions structure that allows to |
| 78 | // retrieve all objects that are built-in into Metropolis currently present in |
| 79 | // the API server (ie., ones that are to be managed by the reconciler). These |
| 80 | // are created by applying builtinLabels to their metadata labels. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 81 | var listBuiltins = meta.ListOptions{ |
| 82 | LabelSelector: fmt.Sprintf("%s=%s", BuiltinLabelKey, BuiltinLabelValue), |
| 83 | } |
| 84 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 85 | // builtinRBACName returns a name that is compatible with colon-delimited |
| 86 | // 'namespaced' objects, a la system:*. |
| 87 | // These names are to be used by all builtins created as part of the rbac/v1 |
| 88 | // Kubernetes API. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 89 | func builtinRBACName(name string) string { |
| 90 | return BuiltinRBACPrefix + name |
| 91 | } |
| 92 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 93 | // resource is a type of resource to be managed by the reconciler. All |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 94 | // built-ins/reconciled objects must implement this interface to be managed |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 95 | // correctly by the reconciler. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 96 | type resource interface { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 97 | // List returns a list of objects currently present on the target |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 98 | // (ie. k8s API server). |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 99 | List(ctx context.Context) ([]meta.Object, error) |
| 100 | // Create creates an object on the target. The el argument is |
| 101 | // an object returned by the Expected() call. |
| 102 | Create(ctx context.Context, el meta.Object) error |
| 103 | // Delete deletes an object, by name, from the target. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 104 | Delete(ctx context.Context, name string) error |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 105 | // Expected returns a list of all objects expected to be present on the |
| 106 | // target. Objects are identified by their name, as returned by GetName. |
| 107 | Expected() []meta.Object |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func allResources(clientSet kubernetes.Interface) map[string]resource { |
| 111 | return map[string]resource{ |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 112 | "clusterroles": resourceClusterRoles{clientSet}, |
| 113 | "clusterrolebindings": resourceClusterRoleBindings{clientSet}, |
| 114 | "storageclasses": resourceStorageClasses{clientSet}, |
| 115 | "csidrivers": resourceCSIDrivers{clientSet}, |
| Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 116 | "runtimeclasses": resourceRuntimeClasses{clientSet}, |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| Jan Schär | d20ddcc | 2024-05-08 14:18:29 +0200 | [diff] [blame] | 120 | func reconcileAll(ctx context.Context, clientSet kubernetes.Interface) error { |
| Serge Bazanski | 356cbf3 | 2023-03-16 17:52:20 +0100 | [diff] [blame] | 121 | resources := allResources(clientSet) |
| 122 | for name, resource := range resources { |
| 123 | err := reconcile(ctx, resource) |
| 124 | if err != nil { |
| 125 | return fmt.Errorf("resource %s: %w", name, err) |
| 126 | } |
| 127 | } |
| 128 | return nil |
| 129 | } |
| 130 | |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 131 | func reconcile(ctx context.Context, r resource) error { |
| 132 | present, err := r.List(ctx) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 136 | presentMap := make(map[string]meta.Object) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 137 | for _, el := range present { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 138 | presentMap[el.GetName()] = el |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 139 | } |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 140 | expected := r.Expected() |
| 141 | expectedMap := make(map[string]meta.Object) |
| 142 | for _, el := range expected { |
| 143 | expectedMap[el.GetName()] = el |
| 144 | } |
| 145 | for name, expectedEl := range expectedMap { |
| 146 | if _, ok := presentMap[name]; ok { |
| 147 | // TODO(#288): update the object if it is different than expected. |
| 148 | } else { |
| 149 | if err := r.Create(ctx, expectedEl); err != nil { |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 150 | return err |
| 151 | } |
| 152 | } |
| 153 | } |
| Tim Windelschmidt | 6b6428d | 2024-04-11 01:35:41 +0200 | [diff] [blame] | 154 | for name := range presentMap { |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 155 | if _, ok := expectedMap[name]; !ok { |
| 156 | if err := r.Delete(ctx, name); err != nil { |
| 157 | return err |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return nil |
| 162 | } |