This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
/
config.go
165 lines (145 loc) · 4.14 KB
/
config.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
// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package kubeutil
// adapted from: https://github.com/bwplotka/mimic/blob/prometheus-kubernetes-example/abstractions/kubernetes/volumes/volumes.go
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DataConfigMap creates a ConfigMap with the given data.
func DataConfigMap(
name, namespace string,
labels, annotations, data map[string]string,
) *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: TypeConfigMapV1,
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
Annotations: annotations,
},
Data: data,
}
}
// ConfigAndMount is a helper struct to create a ConfigMap and a VolumeMount
type ConfigAndMount struct {
metav1.ObjectMeta
VolumeMount corev1.VolumeMount
Data map[string]string
}
// ConfigMap creates a ConfigMap from the ConfigAndMount
func (m ConfigAndMount) ConfigMap() *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: TypeConfigMapV1,
ObjectMeta: m.ObjectMeta,
Data: m.Data,
}
}
// VolumeAndMount creates a VolumeAndMount from the ConfigAndMount
func (m ConfigAndMount) VolumeAndMount() VolumeAndMount {
return VolumeAndMount{
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: m.ObjectMeta.Name,
},
},
},
VolumeMount: m.VolumeMount,
}
}
func (m ConfigAndMount) EnvConfigMapRef(name, key string) corev1.EnvVar {
return corev1.EnvVar{
Name: name,
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
Key: key,
LocalObjectReference: corev1.LocalObjectReference{
Name: m.ObjectMeta.Name,
},
},
},
}
}
// HashEnv creates an environment variable with the hash of the ConfigMap data.
func (m ConfigAndMount) HashEnv(name string) corev1.EnvVar {
h := sha256.New()
if err := json.NewEncoder(h).Encode(m.Data); err != nil {
panic(
fmt.Sprintf(
"failed to JSON encode & hash configMap data for %s, err: %v",
m.VolumeMount.Name,
err,
),
)
}
return corev1.EnvVar{
Name: name,
Value: base64.URLEncoding.EncodeToString(h.Sum(nil)),
}
}
// Hash returns the hash of the ConfigMap data.
func (m ConfigAndMount) Hash() string {
h := sha256.New()
if err := json.NewEncoder(h).Encode(m.Data); err != nil {
panic(
fmt.Sprintf(
"failed to JSON encode & hash configMap data for %s, err: %v",
m.VolumeMount.Name,
err,
),
)
}
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
// HashConfig returns the hash of the ConfigMap data.
func HashConfig(c *corev1.ConfigMap) string {
h := sha256.New()
if err := json.NewEncoder(h).Encode(c.Data); err != nil {
panic(
fmt.Sprintf(
"failed to JSON encode & hash configMap data for %s, err: %v",
c.Name, err,
),
)
}
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
// VolumeAndMount is a helper struct to create a Volume and a VolumeSource
type VolumeAndMount struct {
corev1.VolumeMount
// corev1.Volume has just Name and VolumeSource.
// A name field is already present in the VolumeMount,
// so we just add the VolumeSource field here directly.
VolumeSource corev1.VolumeSource
}
// Volume creates a Volume from the VolumeAndMount
func (vam VolumeAndMount) Volume() corev1.Volume {
return corev1.Volume{
Name: vam.Name,
VolumeSource: vam.VolumeSource,
}
}
// VolumesAndMounts is a helper struct to create a list of Volumes and a list of VolumeSource
type VolumesAndMounts []VolumeAndMount
// Volumes creates a list of Volumes from the VolumesAndMounts
func (vams VolumesAndMounts) Volumes() []corev1.Volume {
volumes := make([]corev1.Volume, 0, len(vams))
for _, vam := range vams {
volumes = append(volumes, vam.Volume())
}
return volumes
}
// VolumeMounts creates a list of VolumesMount from the VolumesAndMounts
func (vams VolumesAndMounts) VolumeMounts() []corev1.VolumeMount {
mounts := make([]corev1.VolumeMount, 0, len(vams))
for _, vam := range vams {
mounts = append(mounts, vam.VolumeMount)
}
return mounts
}