forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
61 lines (53 loc) · 1.92 KB
/
label.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
package certrotation
import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
)
const (
// ManagedCertificateTypeLabelName marks config map or secret as object that contains managed certificates.
// This groups all objects that store certs and allow easy query to get them all.
// The value of this label should be set to "true".
ManagedCertificateTypeLabelName = "auth.openshift.io/managed-certificate-type"
)
type CertificateType string
var (
CertificateTypeCABundle CertificateType = "ca-bundle"
CertificateTypeSigner CertificateType = "signer"
CertificateTypeTarget CertificateType = "target"
CertificateTypeUnknown CertificateType = "unknown"
)
// LabelAsManagedConfigMap add label indicating the given config map contains certificates
// that are managed.
func LabelAsManagedConfigMap(config *v1.ConfigMap, certificateType CertificateType) {
if config.Labels == nil {
config.Labels = map[string]string{}
}
config.Labels[ManagedCertificateTypeLabelName] = string(certificateType)
}
// LabelAsManagedConfigMap add label indicating the given secret contains certificates
// that are managed.
func LabelAsManagedSecret(secret *v1.Secret, certificateType CertificateType) {
if secret.Labels == nil {
secret.Labels = map[string]string{}
}
secret.Labels[ManagedCertificateTypeLabelName] = string(certificateType)
}
// CertificateTypeFromObject returns the CertificateType based on the annotations of the object.
func CertificateTypeFromObject(obj runtime.Object) (CertificateType, error) {
accesor, err := meta.Accessor(obj)
if err != nil {
return "", err
}
actualLabels := accesor.GetLabels()
if actualLabels == nil {
return CertificateTypeUnknown, nil
}
t := CertificateType(actualLabels[ManagedCertificateTypeLabelName])
switch t {
case CertificateTypeCABundle, CertificateTypeSigner, CertificateTypeTarget:
return t, nil
default:
return CertificateTypeUnknown, nil
}
}