forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
230 lines (187 loc) · 6.86 KB
/
util.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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.
*/
package common
import (
"context"
"fmt"
"strings"
"github.com/kubermatic/kubermatic/api/pkg/controller/util/predicate"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
operatorv1alpha1 "github.com/kubermatic/kubermatic/api/pkg/crd/operator/v1alpha1"
"github.com/kubermatic/kubermatic/api/pkg/resources"
"github.com/kubermatic/kubermatic/api/pkg/resources/reconciling"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
// OperatorName is used as the value for ManagedBy labels to establish
// a weak ownership to reconciled resources.
OperatorName = "kubermatic-operator"
// ManagedByLabel is the label used to identify the resources
// created by this controller.
ManagedByLabel = "app.kubernetes.io/managed-by"
)
var (
// ManagedByOperatorPredicate is a predicate that matches all resources created by
// the Kubermatic Operator, based on the ManagedBy label.
ManagedByOperatorPredicate = predicate.Factory(func(m metav1.Object, _ runtime.Object) bool {
for _, ref := range m.GetOwnerReferences() {
if isKubermaticConfiguration(ref) || isSeed(ref) {
return true
}
}
return false
})
// ManagedByOperatorSelector is a label selector that matches all resources created by
// the Kubermatic Operator.
ManagedByOperatorSelector, _ = labels.NewRequirement(ManagedByLabel, selection.Equals, []string{OperatorName})
)
func isKubermaticConfiguration(ref metav1.OwnerReference) bool {
return ref.APIVersion == operatorv1alpha1.SchemeGroupVersion.String() && ref.Kind == "KubermaticConfiguration"
}
func isSeed(ref metav1.OwnerReference) bool {
return ref.APIVersion == kubermaticv1.SchemeGroupVersion.String() && ref.Kind == "Seed"
}
// StringifyFeatureGates takes a set of enabled features and returns a comma-separated
// key=value list like "featureA=true,featureB=true,...".
func StringifyFeatureGates(cfg *operatorv1alpha1.KubermaticConfiguration) string {
features := make([]string, 0)
for _, feature := range cfg.Spec.FeatureGates.List() {
features = append(features, fmt.Sprintf("%s=true", feature))
}
return strings.Join(features, ",")
}
// OwnershipModifierFactory is generating a new ObjectModifier that wraps an ObjectCreator
// and takes care of applying the ownership and other labels for all managed objects.
func OwnershipModifierFactory(owner metav1.Object, scheme *runtime.Scheme) reconciling.ObjectModifier {
return func(create reconciling.ObjectCreator) reconciling.ObjectCreator {
return func(existing runtime.Object) (runtime.Object, error) {
obj, err := create(existing)
if err != nil {
return obj, err
}
o, ok := obj.(metav1.Object)
if !ok {
return obj, nil
}
// try to set an owner reference; on shared resources this would fail to set
// the second owner ref, we ignore this error and rely on the existing
// KubermaticConfiguration ownership
err = controllerutil.SetControllerReference(owner, o, scheme)
if err != nil {
if _, ok := err.(*controllerutil.AlreadyOwnedError); !ok {
return obj, fmt.Errorf("failed to set owner reference: %v", err)
}
}
labels := o.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[ManagedByLabel] = OperatorName
o.SetLabels(labels)
return obj, nil
}
}
}
// VolumeRevisionLabelsModifierFactory scans volume mounts for pod templates for ConfigMaps
// and Secrets and will then put new labels for these mounts onto the pod template, causing
// restarts when the volumes changed.
func VolumeRevisionLabelsModifierFactory(ctx context.Context, client ctrlruntimeclient.Client) reconciling.ObjectModifier {
return func(create reconciling.ObjectCreator) reconciling.ObjectCreator {
return func(existing runtime.Object) (runtime.Object, error) {
obj, err := create(existing)
if err != nil {
return obj, err
}
deployment, ok := obj.(*appsv1.Deployment)
if !ok {
return obj, nil
}
volumeLabels, err := resources.VolumeRevisionLabels(ctx, client, deployment.Namespace, deployment.Spec.Template.Spec.Volumes)
if err != nil {
return obj, fmt.Errorf("failed to determine revision labels for volumes: %v", err)
}
// switch to a new map in case the deployment used the same map for selector.matchLabels and labels
oldLabels := deployment.Spec.Template.Labels
deployment.Spec.Template.Labels = volumeLabels
for k, v := range oldLabels {
deployment.Spec.Template.Labels[k] = v
}
return obj, nil
}
}
}
func createSecretData(s *corev1.Secret, data map[string]string) *corev1.Secret {
if s.Data == nil {
s.Data = make(map[string][]byte)
}
for k, v := range data {
s.Data[k] = []byte(v)
}
return s
}
// CleanupClusterResource attempts to find a cluster-wide resource and
// deletes it if it was found. If no resource with the given name exists,
// nil is returned.
func CleanupClusterResource(client ctrlruntimeclient.Client, obj runtime.Object, name string) error {
key := types.NamespacedName{Name: name}
ctx := context.Background()
if err := client.Get(ctx, key, obj); err != nil {
if !kerrors.IsNotFound(err) {
return fmt.Errorf("failed to probe for %s: %v", key, err)
}
return nil
}
if err := client.Delete(ctx, obj); err != nil {
return fmt.Errorf("failed to delete %s: %v", key, err)
}
return nil
}
func ProxyEnvironmentVars(cfg *operatorv1alpha1.KubermaticConfiguration) []corev1.EnvVar {
result := []corev1.EnvVar{}
settings := cfg.Spec.Proxy
if settings.HTTP == "" && settings.HTTPS == "" {
return result
}
if settings.HTTP != "" {
result = append(result, corev1.EnvVar{
Name: "HTTP_PROXY",
Value: settings.HTTP,
})
}
if settings.HTTPS != "" {
result = append(result, corev1.EnvVar{
Name: "HTTPS_PROXY",
Value: settings.HTTPS,
})
}
noProxy := []string{
DefaultNoProxy,
}
if settings.NoProxy != "" {
noProxy = append(noProxy, settings.NoProxy)
}
result = append(result, corev1.EnvVar{
Name: "NO_PROXY",
Value: strings.Join(noProxy, ","),
})
return result
}