Skip to content

Commit 72a67f1

Browse files
authored
Replace text/template when generating Kptfile CRD (#3681)
* Add simple test for buildKptfile Sets us up to replace the use of text/template * Replace text/template when generating Kptfile CRD Makes code a bit easier to follow.
1 parent a641385 commit 72a67f1

File tree

2 files changed

+89
-59
lines changed

2 files changed

+89
-59
lines changed

internal/testutil/pkgbuilder/builder.go

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
package pkgbuilder
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"os"
2120
"path/filepath"
2221
"regexp"
2322
"strconv"
2423
"testing"
25-
"text/template"
2624

2725
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
2826
rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1"
@@ -561,53 +559,6 @@ func buildRGFile(pkg *pkg) string {
561559
return string(b)
562560
}
563561

564-
// TODO: Consider using the Kptfile struct for this instead of a template.
565-
var kptfileTemplate = `apiVersion: kpt.dev/v1
566-
kind: Kptfile
567-
metadata:
568-
name: {{.PkgName}}
569-
{{- if .Pkg.Kptfile.Upstream }}
570-
upstream:
571-
type: git
572-
git:
573-
repo: {{.Pkg.Kptfile.Upstream.Repo}}
574-
directory: {{.Pkg.Kptfile.Upstream.Dir}}
575-
ref: {{.Pkg.Kptfile.Upstream.Ref}}
576-
updateStrategy: {{.Pkg.Kptfile.Upstream.Strategy}}
577-
{{- end }}
578-
{{- if .Pkg.Kptfile.UpstreamLock }}
579-
upstreamLock:
580-
type: git
581-
git:
582-
repo: {{.Pkg.Kptfile.UpstreamLock.Repo}}
583-
directory: {{.Pkg.Kptfile.UpstreamLock.Dir}}
584-
ref: {{.Pkg.Kptfile.UpstreamLock.Ref}}
585-
commit: {{.Pkg.Kptfile.UpstreamLock.Commit}}
586-
{{- end }}
587-
{{- if .Pkg.Kptfile.Pipeline }}
588-
pipeline:
589-
mutators:
590-
{{- range .Pkg.Kptfile.Pipeline.Functions }}
591-
- image: {{ .Image }}
592-
{{- if .ConfigPath }}
593-
- configPath: {{ .ConfigPath }}
594-
{{- end }}
595-
{{- end }}
596-
{{- end }}
597-
{{- if .Pkg.Kptfile.Inventory }}
598-
inventory:
599-
{{- if .Pkg.Kptfile.Inventory.Name }}
600-
name: {{ .Pkg.Kptfile.Inventory.Name }}
601-
{{- end }}
602-
{{- if .Pkg.Kptfile.Inventory.Namespace }}
603-
namespace: {{ .Pkg.Kptfile.Inventory.Namespace }}
604-
{{- end }}
605-
{{- if .Pkg.Kptfile.Inventory.ID }}
606-
inventoryID: {{ .Pkg.Kptfile.Inventory.ID }}
607-
{{- end }}
608-
{{- end }}
609-
`
610-
611562
type ReposInfo interface {
612563
ResolveRepoRef(repoRef string) (string, bool)
613564
ResolveCommitIndex(repoRef string, index int) (string, bool)
@@ -635,20 +586,62 @@ func buildKptfile(pkg *pkg, pkgName string, reposInfo ReposInfo) string {
635586
pkg.Kptfile.UpstreamLock.Ref = newRef
636587
}
637588
}
638-
tmpl, err := template.New("test").Parse(kptfileTemplate)
639-
if err != nil {
640-
panic(err)
589+
590+
kptfile := &kptfilev1.KptFile{}
591+
kptfile.APIVersion, kptfile.Kind = kptfilev1.KptFileGVK().ToAPIVersionAndKind()
592+
kptfile.ObjectMeta.Name = pkgName
593+
if pkg.Kptfile.Upstream != nil {
594+
kptfile.Upstream = &kptfilev1.Upstream{
595+
Type: "git",
596+
Git: &kptfilev1.Git{
597+
Repo: pkg.Kptfile.Upstream.Repo,
598+
Directory: pkg.Kptfile.Upstream.Dir,
599+
Ref: pkg.Kptfile.Upstream.Ref,
600+
},
601+
UpdateStrategy: kptfilev1.UpdateStrategyType(pkg.Kptfile.Upstream.Strategy),
602+
}
641603
}
642-
var buf bytes.Buffer
643-
err = tmpl.Execute(&buf, map[string]interface{}{
644-
"Pkg": pkg,
645-
"PkgName": pkgName,
646-
})
604+
if pkg.Kptfile.UpstreamLock != nil {
605+
kptfile.UpstreamLock = &kptfilev1.UpstreamLock{
606+
Type: "git",
607+
Git: &kptfilev1.GitLock{
608+
Repo: pkg.Kptfile.UpstreamLock.Repo,
609+
Directory: pkg.Kptfile.UpstreamLock.Dir,
610+
Ref: pkg.Kptfile.UpstreamLock.Ref,
611+
Commit: pkg.Kptfile.UpstreamLock.Commit,
612+
},
613+
}
614+
}
615+
if pkg.Kptfile.Pipeline != nil {
616+
kptfile.Pipeline = &kptfilev1.Pipeline{}
617+
for _, fn := range pkg.Kptfile.Pipeline.Functions {
618+
mutator := kptfilev1.Function{
619+
Image: fn.Image,
620+
}
621+
if fn.ConfigPath != "" {
622+
mutator.ConfigPath = fn.ConfigPath
623+
}
624+
kptfile.Pipeline.Mutators = append(kptfile.Pipeline.Mutators, mutator)
625+
}
626+
}
627+
628+
if inventory := pkg.Kptfile.Inventory; inventory != nil {
629+
kptfile.Inventory = &kptfilev1.Inventory{}
630+
if inventory.Name != "" {
631+
kptfile.Inventory.Name = inventory.Name
632+
}
633+
if inventory.Namespace != "" {
634+
kptfile.Inventory.Namespace = inventory.Namespace
635+
}
636+
if inventory.ID != "" {
637+
kptfile.Inventory.InventoryID = inventory.ID
638+
}
639+
}
640+
b, err := yaml.Marshal(kptfile)
647641
if err != nil {
648642
panic(err)
649643
}
650-
result := buf.String()
651-
return result
644+
return string(b)
652645
}
653646

654647
// resolveRepoRef looks up the repo path for a repo from the reposInfo
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package pkgbuilder
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestBuildKptfile(t *testing.T) {
10+
var repos ReposInfo
11+
pkg := &pkg{}
12+
pkg.Kptfile = &Kptfile{
13+
Pipeline: &Pipeline{
14+
Functions: []Function{
15+
{Image: "example.com/fn1"},
16+
{ConfigPath: "config1"},
17+
{Image: "example.com/fn2"},
18+
},
19+
},
20+
}
21+
22+
got := buildKptfile(pkg, "test1", repos)
23+
want := `apiVersion: kpt.dev/v1
24+
kind: Kptfile
25+
metadata:
26+
name: test1
27+
pipeline:
28+
mutators:
29+
- image: example.com/fn1
30+
- configPath: config1
31+
- image: example.com/fn2
32+
`
33+
34+
if diff := cmp.Diff(want, got); diff != "" {
35+
t.Errorf("buildKptfile returned unexpected diff (-want +got):\n%s", diff)
36+
}
37+
}

0 commit comments

Comments
 (0)