Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

New internal package for helper functions #588

Merged
merged 2 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
// Package template contains the utility functions that help in rendering
// the go templates.
package template

import (
"bytes"
"text/template"
)

// RenderTemplate applies a parsed template to the specified data object
// Render applies a parsed template to the specified data object
// and returns the output as string or an error.
func RenderTemplate(tmpl string, obj interface{}) (string, error) {
func Render(tmpl string, obj interface{}) (string, error) {
ipochi marked this conversation as resolved.
Show resolved Hide resolved
t, err := template.New("render").Parse(tmpl)
if err != nil {
return "", err
Expand Down
57 changes: 57 additions & 0 deletions internal/template/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 The Lokomotive 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 template_test

import (
"testing"

"github.com/kinvolk/lokomotive/internal/template"
)

type testRenderer struct {
Test string
}

func TestRenderSuccess(t *testing.T) {
tmpl := `Rendered template is: {{ .Test }}`
expected := "Rendered template is: Success"

tr := &testRenderer{Test: "Success"}

output, err := template.Render(tmpl, tr)
if err != nil {
t.Fatalf("error rendering template not expected, got: %q", err)
}

if output != expected {
t.Fatalf("expected: %s, got: %s", expected, output)
}
}

func TestRenderFail(t *testing.T) {
tmpl := `Rendered template is: {{ .UnknownField }}`
expected := "Rendered template is: Success"

tr := &testRenderer{Test: "Fail"}

output, err := template.Render(tmpl, tr)
if err == nil {
t.Fatalf("expected error in rendering template")
}

if output != "" {
t.Fatalf("expected: %s, got: %s", expected, output)
}
}
4 changes: 2 additions & 2 deletions pkg/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/backend"
"github.com/kinvolk/lokomotive/pkg/components/util"
)

type local struct {
Expand All @@ -45,7 +45,7 @@ func NewLocalBackend() *local {

// Render renders the Go template with local backend configuration.
func (l *local) Render() (string, error) {
return util.RenderTemplate(backendConfigTmpl, l)
return template.Render(backendConfigTmpl, l)
}

// Validate validates the local backend configuration.
Expand Down
4 changes: 2 additions & 2 deletions pkg/backend/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/backend"
"github.com/kinvolk/lokomotive/pkg/components/util"
)

type s3 struct {
Expand Down Expand Up @@ -52,7 +52,7 @@ func NewS3Backend() *s3 {

// Render renders the Go template with s3 backend configuration.
func (s *s3) Render() (string, error) {
return util.RenderTemplate(backendConfigTmpl, s)
return template.Render(backendConfigTmpl, s)
}

// Validate validates the s3 backend configuration.
Expand Down
3 changes: 2 additions & 1 deletion pkg/components/cert-manager/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, errors.Wrap(err, "load chart from assets")
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/components/cluster-autoscaler/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/packethost/packngo"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)
Expand Down Expand Up @@ -323,7 +324,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
c.Packet.AuthToken = base64.StdEncoding.EncodeToString([]byte(os.Getenv("PACKET_AUTH_TOKEN")))
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/components/contour/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/kinvolk/lokomotive/pkg/components/util"

internaltemplate "github.com/kinvolk/lokomotive/internal/template"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Why is it imported as internaltemplate? Name conflicts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import it as internaltemplate everywhere else? So it becomes easier to read code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes Suraj, name conflicts with Go library template package.

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)

const (
Expand Down Expand Up @@ -101,7 +102,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, fmt.Errorf("failed to marshal node affinity: %w", err)
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := internaltemplate.Render(chartValuesTmpl, c)
if err != nil {
return nil, fmt.Errorf("rendering values template failed: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/components/dex/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

internaltemplate "github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)

const name = "dex"
Expand Down Expand Up @@ -310,17 +310,17 @@ func (c *component) RenderManifests() (map[string]string, error) {
}
c.StaticClientsRaw = staticClients

configMap, err := util.RenderTemplate(configMapTmpl, c)
configMap, err := internaltemplate.Render(configMapTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "execute template failed")
}

ingressBuf, err := util.RenderTemplate(ingressTmpl, c)
ingressBuf, err := internaltemplate.Render(ingressTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "execute template failed")
}

deployment, err := util.RenderTemplate(deploymentTmpl, c)
deployment, err := internaltemplate.Render(deploymentTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "execute template failed")
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/components/external-dns/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/pkg/errors"
)

const name = "external-dns"
Expand Down Expand Up @@ -128,7 +130,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
c.AwsConfig.SecretAccessKey = secretAccessKey
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/components/metallb/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package metallb
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/pkg/errors"
)

const name = "metallb"
Expand Down Expand Up @@ -85,17 +87,17 @@ func (c *component) RenderManifests() (map[string]string, error) {
}
c.ControllerTolerationsJSON = t

controllerStr, err := util.RenderTemplate(deploymentController, c)
controllerStr, err := template.Render(deploymentController, c)
if err != nil {
return nil, errors.Wrap(err, "render template failed")
}

speakerStr, err := util.RenderTemplate(daemonsetSpeaker, c)
speakerStr, err := template.Render(daemonsetSpeaker, c)
if err != nil {
return nil, errors.Wrap(err, "render template failed")
}

configMapStr, err := util.RenderTemplate(configMap, c)
configMapStr, err := template.Render(configMap, c)
if err != nil {
return nil, errors.Wrap(err, "rendering ConfigMap template failed")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/components/metrics-server/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, errors.Wrap(err, "load chart from assets")
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/components/openebs-operator/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)
Expand Down Expand Up @@ -101,7 +102,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, fmt.Errorf("load chart from assets: %w", err)
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, fmt.Errorf("render chart values template: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/components/prometheus-operator/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/types"
"github.com/kinvolk/lokomotive/pkg/components/util"
Expand Down Expand Up @@ -149,7 +150,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, errors.Wrap(err, "load chart from assets")
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/components/rook-ceph/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

internaltemplate "github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/pkg/errors"
)

const name = "rook-ceph"
Expand Down Expand Up @@ -74,7 +76,7 @@ func (c *component) RenderManifests() (map[string]string, error) {

// Parse template with values
for k, v := range template {
rendered, err := util.RenderTemplate(v, c)
rendered, err := internaltemplate.Render(v, c)
if err != nil {
return nil, fmt.Errorf("template rendering failed for %q: %w", k, err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/components/rook/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)
Expand Down Expand Up @@ -75,7 +77,7 @@ func (c *component) RenderManifests() (map[string]string, error) {

c.RookNodeAffinity = convertNodeSelector(c.NodeSelector)

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, fmt.Errorf("rendering values template failed: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/components/velero/velero.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/components/velero/azure"
Expand Down Expand Up @@ -152,7 +153,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, errors.Wrap(err, "load chart from assets")
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
Expand Down