@@ -12,14 +12,41 @@ import (
1212)
1313
1414const (
15- TFPlanName = "tfplan"
16- SavedPlanSecretAnnotation = "savedPlan"
15+ // Kubernetes Label names associated with Terraform Plans
16+ TFPlanNameLabel = "infra.contrib.fluxcd.io/plan-name"
17+ TFPlanWorkspaceLabel = "infra.contrib.fluxcd.io/plan-workspace"
18+
19+ // Kubernetes Annotation names associated with Terraform Plans
20+ TFPlanFullNameAnnotation = "infra.contrib.fluxcd.io/plan-full-name"
21+ TFPlanChunkAnnotation = "infra.contrib.fluxcd.io/plan-chunk"
22+ TFPlanHashAnnotation = "infra.contrib.fluxcd.io/plan-hash"
23+ TFPlanSavedAnnotation = "savedPlan"
24+
25+ TFPlanName = "tfplan"
1726
1827 // resourceDataMaxSizeBytes defines the maximum size of data
1928 // that can be stored in a Kubernetes Secret or ConfigMap
2029 resourceDataMaxSizeBytes = 1 * 1024 * 1024 // 1MB
2130)
2231
32+ // SafeLabelValue returns a string that is safe to use as a Kubernetes label value.
33+ func SafeLabelValue (value string ) string {
34+ // Values that are equal to or less than 63 characters are already good
35+ if len (value ) <= 63 {
36+ return value
37+ }
38+
39+ // Create haash
40+ checksum := sha256 .Sum256 ([]byte (value ))
41+
42+ // Build a prefix to append to end of truncated value
43+ checksumPrefix := fmt .Sprintf ("-%x" , checksum [:8 ])
44+
45+ prefix := value [:63 - len (checksumPrefix )]
46+
47+ return prefix + checksumPrefix
48+ }
49+
2350type Plan struct {
2451 name string
2552 namespace string
@@ -57,20 +84,20 @@ func NewFromSecrets(name string, namespace string, uuid string, secrets []v1.Sec
5784
5885 // Grab the chunk index from the secret annotation
5986 chunkIndex := 0
60- if idxStr , ok := secret .Annotations ["infra.contrib.fluxcd.io/plan-chunk" ]; ok && idxStr != "" {
87+ if idxStr , ok := secret .Annotations [TFPlanChunkAnnotation ]; ok && idxStr != "" {
6188 var err error
6289 chunkIndex , err = strconv .Atoi (idxStr )
6390 if err != nil {
6491 return nil , fmt .Errorf ("invalid chunk index annotation found on secret %s: %s" , secret .Name , err )
6592 }
6693 }
6794
68- workspaceName , ok = secret .Labels ["infra.contrib.fluxcd.io/plan-workspace" ]
95+ workspaceName , ok = secret .Labels [TFPlanWorkspaceLabel ]
6996 if ! ok {
7097 return nil , fmt .Errorf ("missing plan workspace label on secret %s" , secret .Name )
7198 }
7299
73- planID , ok = secret .Annotations [SavedPlanSecretAnnotation ]
100+ planID , ok = secret .Annotations [TFPlanSavedAnnotation ]
74101 if ! ok {
75102 return nil , fmt .Errorf ("missing plan ID annotation on secret %s" , secret .Name )
76103 }
@@ -120,20 +147,20 @@ func NewFromConfigMaps(name string, namespace string, uuid string, configmaps []
120147
121148 // Grab the chunk index from the configmap annotation
122149 chunkIndex := 0
123- if idxStr , ok := configmap .Annotations ["infra.contrib.fluxcd.io/plan-chunk" ]; ok && idxStr != "" {
150+ if idxStr , ok := configmap .Annotations [TFPlanChunkAnnotation ]; ok && idxStr != "" {
124151 var err error
125152 chunkIndex , err = strconv .Atoi (idxStr )
126153 if err != nil {
127154 return nil , fmt .Errorf ("invalid chunk index annotation found on configmap %s: %s" , configmap .Name , err )
128155 }
129156 }
130157
131- workspaceName , ok = configmap .Labels ["infra.contrib.fluxcd.io/plan-workspace" ]
158+ workspaceName , ok = configmap .Labels [TFPlanWorkspaceLabel ]
132159 if ! ok {
133160 return nil , fmt .Errorf ("missing plan workspace label on configmap %s" , configmap .Name )
134161 }
135162
136- planID , ok = configmap .Annotations [SavedPlanSecretAnnotation ]
163+ planID , ok = configmap .Annotations [TFPlanSavedAnnotation ]
137164 if ! ok {
138165 return nil , fmt .Errorf ("missing plan ID annotation on secret %s" , configmap .Name )
139166 }
@@ -183,13 +210,14 @@ func (p *Plan) ToSecret(suffix string) ([]*v1.Secret, error) {
183210 Name : secretIdentifier ,
184211 Namespace : p .namespace ,
185212 Annotations : map [string ]string {
186- "encoding" : "gzip" ,
187- SavedPlanSecretAnnotation : p .planID ,
188- "infra.contrib.fluxcd.io/plan-hash" : fmt .Sprintf ("%x" , sha256 .Sum256 (encoded )),
213+ "encoding" : "gzip" ,
214+ TFPlanFullNameAnnotation : p .name + suffix ,
215+ TFPlanSavedAnnotation : p .planID ,
216+ TFPlanHashAnnotation : fmt .Sprintf ("%x" , sha256 .Sum256 (p .bytes )),
189217 },
190218 Labels : map [string ]string {
191- "infra.contrib.fluxcd.io/plan-name" : p .name + suffix ,
192- "infra.contrib.fluxcd.io/plan-workspace" : p .workspace ,
219+ TFPlanNameLabel : SafeLabelValue ( p .name + suffix ) ,
220+ TFPlanWorkspaceLabel : p .workspace ,
193221 },
194222 OwnerReferences : []metav1.OwnerReference {
195223 {
@@ -223,14 +251,15 @@ func (p *Plan) ToSecret(suffix string) ([]*v1.Secret, error) {
223251 Name : fmt .Sprintf ("%s-%d" , secretIdentifier , chunk ),
224252 Namespace : p .namespace ,
225253 Annotations : map [string ]string {
226- "encoding" : "gzip" ,
227- SavedPlanSecretAnnotation : p .planID ,
228- "infra.contrib.fluxcd.io/plan-chunk" : fmt .Sprintf ("%d" , chunk ),
229- "infra.contrib.fluxcd.io/plan-hash" : fmt .Sprintf ("%x" , sha256 .Sum256 (planData )),
254+ "encoding" : "gzip" ,
255+ TFPlanFullNameAnnotation : p .name + suffix ,
256+ TFPlanSavedAnnotation : p .planID ,
257+ TFPlanChunkAnnotation : fmt .Sprintf ("%d" , chunk ),
258+ TFPlanHashAnnotation : fmt .Sprintf ("%x" , sha256 .Sum256 (planData )),
230259 },
231260 Labels : map [string ]string {
232- "infra.contrib.fluxcd.io/plan-name" : p .name + suffix ,
233- "infra.contrib.fluxcd.io/plan-workspace" : p .workspace ,
261+ TFPlanNameLabel : SafeLabelValue ( p .name + suffix ) ,
262+ TFPlanWorkspaceLabel : p .workspace ,
234263 },
235264 OwnerReferences : []metav1.OwnerReference {
236265 {
@@ -268,12 +297,13 @@ func (p *Plan) ToConfigMap(suffix string) ([]*v1.ConfigMap, error) {
268297 Name : configMapIdentifier ,
269298 Namespace : p .namespace ,
270299 Annotations : map [string ]string {
271- SavedPlanSecretAnnotation : p .planID ,
272- "infra.contrib.fluxcd.io/plan-hash" : fmt .Sprintf ("%x" , sha256 .Sum256 (p .bytes )),
300+ TFPlanFullNameAnnotation : p .name + suffix ,
301+ TFPlanSavedAnnotation : p .planID ,
302+ TFPlanHashAnnotation : fmt .Sprintf ("%x" , sha256 .Sum256 (p .bytes )),
273303 },
274304 Labels : map [string ]string {
275- "infra.contrib.fluxcd.io/plan-name" : p .name + suffix ,
276- "infra.contrib.fluxcd.io/plan-workspace" : p .workspace ,
305+ TFPlanNameLabel : SafeLabelValue ( p .name + suffix ) ,
306+ TFPlanWorkspaceLabel : p .workspace ,
277307 },
278308 OwnerReferences : []metav1.OwnerReference {
279309 {
@@ -307,13 +337,14 @@ func (p *Plan) ToConfigMap(suffix string) ([]*v1.ConfigMap, error) {
307337 Name : fmt .Sprintf ("%s-%d" , configMapIdentifier , chunk ),
308338 Namespace : p .namespace ,
309339 Annotations : map [string ]string {
310- SavedPlanSecretAnnotation : p .planID ,
311- "infra.contrib.fluxcd.io/plan-chunk" : fmt .Sprintf ("%d" , chunk ),
312- "infra.contrib.fluxcd.io/plan-hash" : fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (planData ))),
340+ TFPlanFullNameAnnotation : p .name + suffix ,
341+ TFPlanSavedAnnotation : p .planID ,
342+ TFPlanChunkAnnotation : fmt .Sprintf ("%d" , chunk ),
343+ TFPlanHashAnnotation : fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (planData ))),
313344 },
314345 Labels : map [string ]string {
315- "infra.contrib.fluxcd.io/plan-name" : p .name + suffix ,
316- "infra.contrib.fluxcd.io/plan-workspace" : p .workspace ,
346+ TFPlanNameLabel : SafeLabelValue ( p .name + suffix ) ,
347+ TFPlanWorkspaceLabel : p .workspace ,
317348 },
318349 OwnerReferences : []metav1.OwnerReference {
319350 {
0 commit comments