Skip to content

Commit

Permalink
Take advantage of the custom template functions in the main config re…
Browse files Browse the repository at this point in the history
…ndering

Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 committed Mar 24, 2021
1 parent 07493f8 commit 3b8b3a1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 72 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ jobs:
build:
name: Build
runs-on: ubuntu-latest
environment:
name: kube-burner
strategy:
matrix:
golang-version: [1.14, 1.15]
Expand Down Expand Up @@ -79,8 +77,6 @@ jobs:

lint:
name: Run golangci-lint
environment:
name: kube-burner
runs-on: ubuntu-latest
steps:

Expand All @@ -105,8 +101,6 @@ jobs:

build-container:
name: Build container image
environment:
name: kube-burner
runs-on: ubuntu-latest
steps:

Expand Down
2 changes: 1 addition & 1 deletion pkg/burner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (ex *Executor) replicaHandler(objectIndex int, obj object, ns string, itera
ex.limiter.Wait(context.TODO())
newObject := &unstructured.Unstructured{}
templateData[replica] = r
renderedObj, err := renderTemplate(obj.objectSpec, templateData, missingKeyError)
renderedObj, err := util.RenderTemplate(obj.objectSpec, templateData, util.MissingKeyError)
if err != nil {
log.Fatalf("Template error in %s: %s", obj.objectTemplate, err)
}
Expand Down
60 changes: 7 additions & 53 deletions pkg/burner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
package burner

import (
"bytes"
"context"
"fmt"
"math/rand"
"regexp"
"strings"
"text/template"
"time"

"github.com/cloud-bulldozer/kube-burner/log"
Expand All @@ -33,25 +30,18 @@ import (
"k8s.io/kubectl/pkg/scheme"
)

type templateOption string

const (
// Parameters for retrying with exponential backoff.
retryBackoffInitialDuration = 1 * time.Second
retryBackoffFactor = 3
retryBackoffJitter = 0
retryBackoffSteps = 3
objectLimit = 500
missingKeyError templateOption = "missingkey=error"
retryBackoffInitialDuration = 1 * time.Second
retryBackoffFactor = 3
retryBackoffJitter = 0
retryBackoffSteps = 3
objectLimit = 500
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func prepareTemplate(original []byte) ([]byte, error) {
// Removing all placeholder from template.
// This needs to be done due to placeholders not being valid yaml.
// Removing all placeholders from template.
// This needs to be done due to placeholders not being valid yaml
if isEmpty(original) {
return nil, fmt.Errorf("template is empty")
}
Expand All @@ -63,42 +53,6 @@ func prepareTemplate(original []byte) ([]byte, error) {
return original, nil
}

func renderTemplate(original []byte, data interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
funcMap := template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
"multiply": func(a int, b int) int {
return a * b
},
"rand": func(length int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
},
"sequence": func(start int, end int) []int {
var sequence = []int{}
for i := start; i <= end; i++ {
sequence = append(sequence, i)
}
return sequence
},
}
t, err := template.New("").Option(string(options)).Funcs(funcMap).Parse(string(original))
if err != nil {
return nil, fmt.Errorf("Parsing error: %s", err)
}
err = t.Execute(&rendered, data)
if err != nil {
return nil, fmt.Errorf("Rendering error: %s", err)
}
return rendered.Bytes(), nil
}

func yamlToUnstructured(y []byte, uns *unstructured.Unstructured) (runtime.Object, *schema.GroupVersionKind) {
o, gvr, err := scheme.Codecs.UniversalDeserializer().Decode(y, nil, uns)
if err != nil {
Expand Down
19 changes: 7 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ package config
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"

"github.com/cloud-bulldozer/kube-burner/log"
Expand Down Expand Up @@ -56,16 +54,12 @@ func envToMap() map[string]string {
return envMap
}

func renderConfig(cfg string) (io.Reader, error) {
var rendered bytes.Buffer
t, err := template.New("cfg").Option("missingkey=error").Parse(cfg)
func renderConfig(cfg []byte) ([]byte, error) {
rendered, err := util.RenderTemplate(cfg, envToMap(), util.MissingKeyError)
if err != nil {
return &rendered, fmt.Errorf("Error rendering configuration template: %s", err)
return rendered, fmt.Errorf("Error rendering configuration template: %s", err)
}
if err = t.Execute(&rendered, envToMap()); err != nil {
return &rendered, fmt.Errorf("Error rendering configuration template: %s", err)
}
return &rendered, nil
return rendered, nil
}

// UnmarshalYAML implements Unmarshaller to customize defaults
Expand Down Expand Up @@ -100,11 +94,12 @@ func Parse(c string, jobsRequired bool) error {
if err != nil {
return fmt.Errorf("Error reading configuration file %s: %s", c, err)
}
renderedCfg, err := renderConfig(string(cfg))
renderedCfg, err := renderConfig(cfg)
if err != nil {
return err
}
yamlDec := yaml.NewDecoder(renderedCfg)
cfgReader := bytes.NewReader(renderedCfg)
yamlDec := yaml.NewDecoder(cfgReader)
yamlDec.KnownFields(true)
if err = yamlDec.Decode(&ConfigSpec); err != nil {
return fmt.Errorf("Error decoding configuration file %s: %s", c, err)
Expand Down
70 changes: 70 additions & 0 deletions pkg/util/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 The Kube-burner Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"bytes"
"fmt"
"math/rand"
"text/template"
"time"
)

type templateOption string

const (
MissingKeyError templateOption = "missingkey=error"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

//RenderTemplate renders a go-template and adds several custom functions
func RenderTemplate(original []byte, inputData interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
funcMap := template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
"multiply": func(a int, b int) int {
return a * b
},
"rand": func(length int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
},
"sequence": func(start int, end int) []int {
var sequence = []int{}
for i := start; i <= end; i++ {
sequence = append(sequence, i)
}
return sequence
},
}
t, err := template.New("").Option(string(options)).Funcs(funcMap).Parse(string(original))
if err != nil {
return nil, fmt.Errorf("Parsing error: %s", err)
}
err = t.Execute(&rendered, inputData)
if err != nil {
return nil, fmt.Errorf("Rendering error: %s", err)
}
return rendered.Bytes(), nil
}

0 comments on commit 3b8b3a1

Please sign in to comment.