Skip to content

Commit b19df2f

Browse files
committed
use regexp to match metadata to replace
1 parent baccf11 commit b19df2f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

controllers/cloudstackmachine_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222
"math/rand"
2323
"reflect"
24-
"strings"
24+
"regexp"
2525

2626
"github.com/pkg/errors"
2727
corev1 "k8s.io/api/core/v1"
@@ -185,8 +185,10 @@ func processUserData(data []byte, r *CloudStackMachineReconciliationRunner) stri
185185
// since cloudstack metadata does not allow custom data added into meta_data, following line is a hack to specify a hostname name
186186
// {{ ds.meta_data.hostname }} is expected to be used as a name when kubelet register a node
187187
// if more custom data needed to injected, this can be refactored into a method -- processCustomMetadata()
188-
userData := strings.ReplaceAll(string(data), "{{ ds.meta_data.hostname }}", r.CAPIMachine.Name)
189-
userData = strings.ReplaceAll(userData, "{{ds.meta_data.failuredomain}}", r.FailureDomain.Spec.Name)
188+
hostnameMatcher := regexp.MustCompile(`\{\{\s*ds\.meta_data\.hostname\s*\}\}`)
189+
failuredomainMatcher := regexp.MustCompile(`\{\{\s*ds\.meta_data\.failuredomain\s*\}\}`)
190+
userData := hostnameMatcher.ReplaceAllString(string(data), r.CAPIMachine.Name)
191+
userData = failuredomainMatcher.ReplaceAllString(userData, r.FailureDomain.Spec.Name)
190192
return userData
191193
}
192194

0 commit comments

Comments
 (0)