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