forked from titansoft-pte-ltd/imagepullsecret-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
72 lines (63 loc) · 1.8 KB
/
secret.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
package main
import (
"io/ioutil"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type verifySecretResult string
const (
// annotation constants
annotationManagedBy = "app.kubernetes.io/managed-by"
annotationAppName = "imagepullsecret-patcher"
// result code for verifySecret
secretOk verifySecretResult = "SecretOk"
secretWrongType verifySecretResult = "SecretWrongType"
secretNoKey verifySecretResult = "SecretNoKey"
secretDataNotMatch verifySecretResult = "SecretDataNotMatch"
)
// getDockerConfigJSON is a dynamic getter for our secret value. It lets us
// dynamically fetch the value from file or return the hard coded value,
// providing a consistent interface for access
func getDockerConfigJSON() (string, error) {
if configDockerConfigJSONPath != "" {
b, ok := ioutil.ReadFile(configDockerConfigJSONPath)
return string(b), ok
}
return configDockerconfigjson, nil
}
func dockerconfigSecret(namespace string) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: configSecretName,
Namespace: namespace,
Annotations: map[string]string{
annotationManagedBy: annotationAppName,
},
},
Data: map[string][]byte{
corev1.DockerConfigJsonKey: []byte(dockerConfigJSON),
},
Type: corev1.SecretTypeDockerConfigJson,
}
}
func verifySecret(secret *corev1.Secret) verifySecretResult {
if secret.Type != corev1.SecretTypeDockerConfigJson {
return secretWrongType
}
b, ok := secret.Data[corev1.DockerConfigJsonKey]
if !ok {
return secretNoKey
}
if string(b) != dockerConfigJSON {
return secretDataNotMatch
}
return secretOk
}
func isManagedSecret(secret *corev1.Secret) bool {
if k, ok := secret.ObjectMeta.Annotations[annotationManagedBy]; ok {
if k == annotationAppName {
return true
}
}
return false
}