forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
81 lines (64 loc) · 2.62 KB
/
helper.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
/*
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 kubernetes
import (
"fmt"
"regexp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/sets"
providerconfig "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
)
var tokenValidator = regexp.MustCompile(`[bcdfghjklmnpqrstvwxz2456789]{6}\.[bcdfghjklmnpqrstvwxz2456789]{16}`)
// HasFinalizer tells if a object has all the given finalizers
func HasFinalizer(o metav1.Object, names ...string) bool {
return sets.NewString(o.GetFinalizers()...).HasAll(names...)
}
func HasAnyFinalizer(o metav1.Object, names ...string) bool {
return sets.NewString(o.GetFinalizers()...).HasAny(names...)
}
// HasOnlyFinalizer tells if an object has only the given finalizer
func HasOnlyFinalizer(o metav1.Object, name string) bool {
set := sets.NewString(o.GetFinalizers()...)
return set.Has(name) && set.Len() == 1
}
// RemoveFinalizer removes the given finalizer from the object
func RemoveFinalizer(obj metav1.Object, toRemove string) {
set := sets.NewString(obj.GetFinalizers()...)
set.Delete(toRemove)
obj.SetFinalizers(set.List())
}
// AddFinalizer will add the given finalizer to the object. It uses a StringSet to avoid duplicates
func AddFinalizer(obj metav1.Object, finalizers ...string) {
set := sets.NewString(obj.GetFinalizers()...)
set.Insert(finalizers...)
obj.SetFinalizers(set.List())
}
// GenerateToken generates a new, random token that can be used
// as an admin and kubelet token.
func GenerateToken() string {
return fmt.Sprintf("%s.%s", rand.String(6), rand.String(16))
}
// ValidateKubernetesToken checks if a given token is syntactically correct.
func ValidateKubernetesToken(token string) error {
if !tokenValidator.MatchString(token) {
return fmt.Errorf("token is malformed, must match %s", tokenValidator.String())
}
return nil
}
func ValidateSecretKeySelector(selector *providerconfig.GlobalSecretKeySelector, key string) error {
if selector.Name == "" || selector.Namespace == "" || key == "" {
return fmt.Errorf("%q cannot be empty", key)
}
return nil
}