Skip to content

Commit

Permalink
kubernetes make target to generate custom data (#159)
Browse files Browse the repository at this point in the history
* kubernetes make target to generate custom data

* removed go.mod
  • Loading branch information
jadarsie authored Nov 5, 2019
1 parent 5eb2ded commit 5a8612a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 109 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/.DS_Store
**/_out
go.mod
68 changes: 0 additions & 68 deletions .pipeline/assets/encoding/encode_cse_script.ps1

This file was deleted.

39 changes: 0 additions & 39 deletions .pipeline/assets/encoding/encode_cse_script.sh

This file was deleted.

11 changes: 11 additions & 0 deletions kubernetes/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: build-encoder
build-encoder:
go build -o _out/encoder hack/encode/main.go

.PHONY: clean
clean:
rm -rf _out

.PHONY: generate
generate: clean build-encoder
hack/generate.sh
3 changes: 3 additions & 0 deletions kubernetes/hack/encode/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/msazurestackworkloads/azurestack-gallery/kubernetes

go 1.12
63 changes: 63 additions & 0 deletions kubernetes/hack/encode/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
)

func main() {
scriptstring := getBase64CustomScript(os.Args[1])
customerString := getSingleLine("hack/encode/template.yaml", scriptstring)
fmt.Print(fmt.Sprintf("[base64(concat('%s'))]", customerString))
}

// getBase64CustomScript will return a base64 of the CSE
func getSingleLine(csFilename string, scriptstring string) string {
b, err := ioutil.ReadFile(csFilename)
if err != nil {
// this should never happen and this is a bug
panic(fmt.Sprintf("BUG: %s", err.Error()))
}
// translate the parameters
csStr := string(b)
csStr = strings.Replace(csStr, "SCRIPT_PLACEHOLDER", scriptstring, -1)
csStr = strings.Replace(csStr, "\r\n", "\n", -1)
return escapeSingleLine(csStr)
}

func escapeSingleLine(escapedStr string) string {
// template.JSEscapeString leaves undesirable chars that don't work with pretty print
escapedStr = strings.Replace(escapedStr, "\\", "\\\\", -1)
escapedStr = strings.Replace(escapedStr, "\r\n", "\\n", -1)
escapedStr = strings.Replace(escapedStr, "\n", "\\n", -1)
escapedStr = strings.Replace(escapedStr, "!!binary |", "!!binary \\|", -1)
escapedStr = strings.Replace(escapedStr, "\"", "\\\"", -1)
return escapedStr
}

// getBase64CustomScript will return a base64 of the CSE
func getBase64CustomScript(csFilename string) string {
b, err := ioutil.ReadFile(csFilename)
if err != nil {
// this should never happen and this is a bug
panic(fmt.Sprintf("BUG: %s", err.Error()))
}
// translate the parameters
csStr := string(b)
csStr = strings.Replace(csStr, "\r\n", "\n", -1)
return getBase64CustomScriptFromStr(csStr)
}

// getBase64CustomScript will return a base64 of the CSE
func getBase64CustomScriptFromStr(str string) string {
var gzipB bytes.Buffer
w := gzip.NewWriter(&gzipB)
w.Write([]byte(str))
w.Close()
return base64.StdEncoding.EncodeToString(gzipB.Bytes())
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#cloud-config

write_files:
- path: "/opt/azure/containers/script.sh"
- path: /opt/azure/containers/script.sh
permissions: "0744"
encoding: gzip
owner: "root"
owner: root:root
content: !!binary |
SCRIPT_PLACEHOLDER
4 changes: 4 additions & 0 deletions kubernetes/hack/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

mkdir -p _out
_out/encoder template/DeploymentTemplates/script.sh > _out/customdata.txt

0 comments on commit 5a8612a

Please sign in to comment.