Skip to content

Commit 5b2ae0d

Browse files
committed
use build in SetGeneratedName to set a unique name
Signed-off-by: Michael Shitrit <[email protected]>
1 parent bbce1d0 commit 5b2ae0d

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

controllers/resources/manager.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package resources
22

33
import (
44
"context"
5-
"crypto/rand"
6-
"fmt"
7-
"math/big"
85
"strings"
96
"time"
107

@@ -127,14 +124,14 @@ func (m *manager) generateRemediationCR(name string, healthCheckOwnerRef *metav1
127124
// can't go wrong, we already checked for correct spec
128125
templateSpec, _, _ := unstructured.NestedMap(template.Object, "spec", "template", "spec")
129126
unstructured.SetNestedField(remediationCR.Object, templateSpec, "spec")
130-
crName := name
127+
131128
if annotationutils.HasMultipleTemplatesAnnotation(template) {
132-
//first part would be the node name and second part is a unique suffix seperated by '-'
133-
crName = generateUniqueRemediationName(name)
129+
remediationCR.SetGenerateName(name)
134130
remediationCR.SetAnnotations(map[string]string{annotationutils.NodeNameAnnotation: name, annotationutils.TemplateNameAnnotation: template.GetName()})
131+
} else {
132+
remediationCR.SetName(name)
135133
}
136134

137-
remediationCR.SetName(crName)
138135
remediationCR.SetNamespace(template.GetNamespace())
139136
remediationCR.SetResourceVersion("")
140137
remediationCR.SetFinalizers(nil)
@@ -415,15 +412,3 @@ func createOwnerRef(obj client.Object) *metav1.OwnerReference {
415412
BlockOwnerDeletion: nil,
416413
}
417414
}
418-
419-
func generateUniqueRemediationName(name string) string {
420-
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
421-
length := 6
422-
bytes := make([]byte, length)
423-
for i := range bytes {
424-
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
425-
bytes[i] = charset[n.Int64()]
426-
}
427-
uniqueSuffix := string(bytes)
428-
return fmt.Sprintf("%s-%s", name, uniqueSuffix)
429-
}

0 commit comments

Comments
 (0)