Skip to content

Commit

Permalink
Support ignition userdata for bootstrap tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Aug 26, 2022
1 parent d7e3c5d commit 84592bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions pkg/controller/machine_bootstrap_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"crypto/rand"
"encoding/hex"
"math/big"
"net/url"
"strings"
"time"

Expand All @@ -38,6 +39,8 @@ import (

const placeholder = "<<BOOTSTRAP_TOKEN>>"

var ignitionPlaceholder = url.QueryEscape(placeholder)

func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineName string, driver driver.Driver) error {
userData := driver.GetUserData()
klog.V(4).Infof("Creating bootstrap token!")
Expand All @@ -50,8 +53,16 @@ func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineNam
string(bootstrapTokenSecret.Data[bootstraptokenapi.BootstrapTokenIDKey]),
string(bootstrapTokenSecret.Data[bootstraptokenapi.BootstrapTokenSecretKey]),
)
klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userData = strings.ReplaceAll(userData, placeholder, token)

if strings.Contains(userData, placeholder) {
klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userData = strings.ReplaceAll(userData, placeholder, token)
} else if strings.Contains(userData, ignitionPlaceholder) {
klog.V(4).Infof("replacing ignition placeholder %s with %s in user-data!", ignitionPlaceholder, token)
userData = strings.ReplaceAll(userData, ignitionPlaceholder, url.QueryEscape(token))
} else {
klog.V(4).Info("no bootstrap token placeholder found in user-data, nothing to replace!")
}

driver.SetUserData(userData)
return nil
Expand Down
15 changes: 13 additions & 2 deletions pkg/util/provider/machinecontroller/machine_bootstrap_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"net/url"
"strings"
"time"

Expand All @@ -38,6 +39,8 @@ import (

const placeholder = "<<BOOTSTRAP_TOKEN>>"

var ignitionPlaceholder = url.QueryEscape(placeholder)

func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineName string, secret *corev1.Secret) error {
var (
userDataB []byte
Expand All @@ -61,8 +64,16 @@ func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineNam
string(bootstrapTokenSecret.Data[bootstraptokenapi.BootstrapTokenSecretKey]),
)

klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userDataS = strings.ReplaceAll(userDataS, placeholder, token)
if strings.Contains(userDataS, placeholder) {
klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userDataS = strings.ReplaceAll(userDataS, placeholder, token)
} else if strings.Contains(userDataS, ignitionPlaceholder) {
klog.V(4).Infof("replacing ignition placeholder %s with %s in user-data!", ignitionPlaceholder, token)
userDataS = strings.ReplaceAll(userDataS, ignitionPlaceholder, url.QueryEscape(token))
} else {
klog.V(4).Info("no bootstrap token placeholder found in user-data, nothing to replace!")
}

secret.Data["userData"] = []byte(userDataS)

return nil
Expand Down

0 comments on commit 84592bf

Please sign in to comment.