Skip to content

Commit

Permalink
Embed scripts as strings rather than []byte
Browse files Browse the repository at this point in the history
This is to reduce the amount of type conversions needed.
  • Loading branch information
s-fairchild committed Aug 15, 2024
1 parent 7f65006 commit 374af12
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pkg/deploy/assets/gateway-production.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/deploy/assets/rp-production.json

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions pkg/deploy/generator/resources_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,9 @@ func (g *generator) devProxyVMSS() *arm.Resource {
)
}

var sb strings.Builder

sb.WriteString(string(scriptDevProxyVMSS))

trailer := base64.StdEncoding.EncodeToString([]byte(sb.String()))

trailer := base64.StdEncoding.EncodeToString([]byte(scriptDevProxyVMSS))
parts = append(parts, "'\n'", fmt.Sprintf("base64ToString('%s')", trailer))

script := fmt.Sprintf("[base64(concat(%s))]", strings.Join(parts, ","))
customScript := fmt.Sprintf("[base64(concat(%s))]", strings.Join(parts, ","))

return &arm.Resource{
Resource: &mgmtcompute.VirtualMachineScaleSet{
Expand Down Expand Up @@ -217,7 +211,7 @@ func (g *generator) devProxyVMSS() *arm.Resource {
AutoUpgradeMinorVersion: to.BoolPtr(true),
Settings: map[string]interface{}{},
ProtectedSettings: map[string]interface{}{
"script": script,
"script": customScript,
},
},
},
Expand Down
22 changes: 8 additions & 14 deletions pkg/deploy/generator/resources_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,17 @@ func (g *generator) gatewayVMSS() *arm.Resource {
"''')\n'",
)

var sb strings.Builder

// VMSS extensions only support one custom script
// Because of this, the util-*.sh scripts are prefixed to the bootstrapping script
// main is called at the end of the bootstrapping script, so appending them will not work
sb.WriteString(string(scriptUtilCommon))
sb.WriteString(string(scriptUtilPackages))
sb.WriteString(string(scriptUtilServices))
sb.WriteString(string(scriptUtilSystem))
sb.WriteString("\n#Start of gatewayVMSS.sh\n")
sb.WriteString(string(scriptGatewayVMSS))

trailer := base64.StdEncoding.EncodeToString([]byte(sb.String()))

bootstrapScript := scriptUtilCommon +
scriptUtilPackages +
scriptUtilServices +
scriptUtilSystem +
scriptGatewayVMSS
trailer := base64.StdEncoding.EncodeToString([]byte(bootstrapScript))
parts = append(parts, "'\n'", fmt.Sprintf("base64ToString('%s')", trailer))

script := fmt.Sprintf("[base64(concat(%s))]", strings.Join(parts, ","))
customScript := fmt.Sprintf("[base64(concat(%s))]", strings.Join(parts, ","))

return &arm.Resource{
Resource: &mgmtcompute.VirtualMachineScaleSet{
Expand Down Expand Up @@ -362,7 +356,7 @@ func (g *generator) gatewayVMSS() *arm.Resource {
AutoUpgradeMinorVersion: to.BoolPtr(true),
Settings: map[string]interface{}{},
ProtectedSettings: map[string]interface{}{
"script": script,
"script": customScript,
},
},
},
Expand Down
22 changes: 8 additions & 14 deletions pkg/deploy/generator/resources_rp.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,23 +441,17 @@ func (g *generator) rpVMSS() *arm.Resource {
"''')\n'",
)

var sb strings.Builder

// VMSS extensions only support one custom script
// Because of this, the util-*.sh scripts are prefixed to the bootstrapping script
// main is called at the end of the bootstrapping script, so appending them will not work
sb.WriteString(string(scriptUtilCommon))
sb.WriteString(string(scriptUtilPackages))
sb.WriteString(string(scriptUtilServices))
sb.WriteString(string(scriptUtilSystem))
sb.WriteString("\n#Start of rpVMSS.sh\n")
sb.WriteString(string(scriptRpVMSS))

trailer := base64.StdEncoding.EncodeToString([]byte(sb.String()))

bootstrapScript := scriptUtilCommon +
scriptUtilPackages +
scriptUtilServices +
scriptUtilSystem +
scriptRpVMSS
trailer := base64.StdEncoding.EncodeToString([]byte(bootstrapScript))
parts = append(parts, "'\n'", fmt.Sprintf("base64ToString('%s')", trailer))

script := fmt.Sprintf("[base64(concat(%s))]", strings.Join(parts, ","))
customScript := fmt.Sprintf("[base64(concat(%s))]", strings.Join(parts, ","))

return &arm.Resource{
Resource: &mgmtcompute.VirtualMachineScaleSet{
Expand Down Expand Up @@ -560,7 +554,7 @@ func (g *generator) rpVMSS() *arm.Resource {
AutoUpgradeMinorVersion: to.BoolPtr(true),
Settings: map[string]interface{}{},
ProtectedSettings: map[string]interface{}{
"script": script,
"script": customScript,
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/deploy/generator/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
)

//go:embed scripts/devProxyVMSS.sh
var scriptDevProxyVMSS []byte
var scriptDevProxyVMSS string

//go:embed scripts/gatewayVMSS.sh
var scriptGatewayVMSS []byte
var scriptGatewayVMSS string

//go:embed scripts/rpVMSS.sh
var scriptRpVMSS []byte
var scriptRpVMSS string

//go:embed scripts/util-system.sh
var scriptUtilSystem []byte
var scriptUtilSystem string

//go:embed scripts/util-services.sh
var scriptUtilServices []byte
var scriptUtilServices string

//go:embed scripts/util-packages.sh
var scriptUtilPackages []byte
var scriptUtilPackages string

//go:embed scripts/util-common.sh
var scriptUtilCommon []byte
var scriptUtilCommon string

0 comments on commit 374af12

Please sign in to comment.