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 | |
| 17 | // The reconciler ensures that a base set of K8s resources is always available in the cluster. These are necessary to |
| 18 | // ensure correct out-of-the-box functionality. All resources containing the smalltown.com/builtin=true label are assumed |
| 19 | // to be managed by the reconciler. |
| 20 | // It currently does not revert modifications made by admins, it is planned to create an admission plugin prohibiting |
| 21 | // such modifications to resources with the smalltown.com/builtin label to deal with that problem. This would also solve a |
| 22 | // potential issue where you could delete resources just by adding the smalltown.com/builtin=true label. |
| 23 | package reconciler |
| 24 | |
| 25 | import ( |
| 26 | "context" |
| 27 | "fmt" |
| 28 | "time" |
| 29 | |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 30 | meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 31 | "k8s.io/client-go/kubernetes" |
Serge Bazanski | 77cb6c5 | 2020-12-19 00:09:22 +0100 | [diff] [blame^] | 32 | |
| 33 | "git.monogon.dev/source/nexantic.git/metropolis/node/common/supervisor" |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | // Sad workaround for all the pointer booleans in K8s specs |
| 37 | func True() *bool { |
| 38 | val := true |
| 39 | return &val |
| 40 | } |
| 41 | func False() *bool { |
| 42 | val := false |
| 43 | return &val |
| 44 | } |
| 45 | |
| 46 | const ( |
| 47 | // BuiltinLabelKey is used as a k8s label to mark built-in objects (ie., managed by the reconciler) |
| 48 | BuiltinLabelKey = "smalltown.com/builtin" |
| 49 | // BuiltinLabelValue is used as a k8s label value, under the BuiltinLabelKey key. |
| 50 | BuiltinLabelValue = "true" |
| 51 | // BuiltinRBACPrefix is used to prefix all built-in objects that are part of the rbac/v1 API (eg. |
| 52 | // {Cluster,}Role{Binding,} objects). This corresponds to the colon-separated 'namespaces' notation used by |
| 53 | // Kubernetes system (system:) objects. |
| 54 | BuiltinRBACPrefix = "smalltown:" |
| 55 | ) |
| 56 | |
| 57 | // builtinLabels makes a kubernetes-compatible label dictionary (key->value) that is used to mark objects that are |
| 58 | // built-in into Smalltown (ie., managed by the reconciler). These are then subsequently retrieved by listBuiltins. |
| 59 | // The extra argument specifies what other labels are to be merged into the the labels dictionary, for convenience. If |
| 60 | // nil or empty, no extra labels will be applied. |
| 61 | func builtinLabels(extra map[string]string) map[string]string { |
| 62 | l := map[string]string{ |
| 63 | BuiltinLabelKey: BuiltinLabelValue, |
| 64 | } |
| 65 | if extra != nil { |
| 66 | for k, v := range extra { |
| 67 | l[k] = v |
| 68 | } |
| 69 | } |
| 70 | return l |
| 71 | } |
| 72 | |
| 73 | // listBuiltins returns a k8s client ListOptions structure that allows to retrieve all objects that are built-in into |
| 74 | // Smalltown currently present in the API server (ie., ones that are to be managed by the reconciler). These are created |
| 75 | // by applying builtinLabels to their metadata labels. |
| 76 | var listBuiltins = meta.ListOptions{ |
| 77 | LabelSelector: fmt.Sprintf("%s=%s", BuiltinLabelKey, BuiltinLabelValue), |
| 78 | } |
| 79 | |
| 80 | // builtinRBACName returns a name that is compatible with colon-delimited 'namespaced' objects, a la system:*. |
| 81 | // These names are to be used by all builtins created as part of the rbac/v1 Kubernetes API. |
| 82 | func builtinRBACName(name string) string { |
| 83 | return BuiltinRBACPrefix + name |
| 84 | } |
| 85 | |
| 86 | // resource is a type of resource to be managed by the reconciler. All builti-ins/reconciled objects must implement |
| 87 | // this interface to be managed correctly by the reconciler. |
| 88 | type resource interface { |
| 89 | // List returns a list of names of objects current present on the target (ie. k8s API server). |
| 90 | List(ctx context.Context) ([]string, error) |
| 91 | // Create creates an object on the target. The el interface{} argument is the black box object returned by the |
| 92 | // Expected() call. |
| 93 | Create(ctx context.Context, el interface{}) error |
| 94 | // Delete delete an object, by name, from the target. |
| 95 | Delete(ctx context.Context, name string) error |
| 96 | // Expected returns a map of all objects expected to be present on the target. The keys are names (which must |
| 97 | // correspond to the names returned by List() and used by Delete(), and the values are blackboxes that will then |
| 98 | // be passed to the Create() call if their corresponding key (name) does not exist on the target. |
| 99 | Expected() map[string]interface{} |
| 100 | } |
| 101 | |
| 102 | func allResources(clientSet kubernetes.Interface) map[string]resource { |
| 103 | return map[string]resource{ |
| 104 | "psps": resourcePodSecurityPolicies{clientSet}, |
| 105 | "clusterroles": resourceClusterRoles{clientSet}, |
| 106 | "clusterrolebindings": resourceClusterRoleBindings{clientSet}, |
| 107 | "storageclasses": resourceStorageClasses{clientSet}, |
| 108 | "csidrivers": resourceCSIDrivers{clientSet}, |
Lorenz Brun | 5e4fc2d | 2020-09-22 18:35:15 +0200 | [diff] [blame] | 109 | "runtimeclasses": resourceRuntimeClasses{clientSet}, |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | func Run(clientSet kubernetes.Interface) supervisor.Runnable { |
| 114 | return func(ctx context.Context) error { |
| 115 | log := supervisor.Logger(ctx) |
| 116 | resources := allResources(clientSet) |
| 117 | t := time.NewTicker(10 * time.Second) |
| 118 | reconcileAll := func() { |
| 119 | for name, resource := range resources { |
| 120 | if err := reconcile(ctx, resource); err != nil { |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 121 | log.Warningf("Failed to reconcile built-in resources %s: %v", name, err) |
Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 126 | reconcileAll() |
| 127 | for { |
| 128 | select { |
| 129 | case <-t.C: |
| 130 | reconcileAll() |
| 131 | case <-ctx.Done(): |
| 132 | return nil |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func reconcile(ctx context.Context, r resource) error { |
| 139 | present, err := r.List(ctx) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | presentSet := make(map[string]bool) |
| 144 | for _, el := range present { |
| 145 | presentSet[el] = true |
| 146 | } |
| 147 | expectedMap := r.Expected() |
| 148 | for name, el := range expectedMap { |
| 149 | if !presentSet[name] { |
| 150 | if err := r.Create(ctx, el); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | for name, _ := range presentSet { |
| 156 | if _, ok := expectedMap[name]; !ok { |
| 157 | if err := r.Delete(ctx, name); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | return nil |
| 163 | } |