|
15 | 15 | package pkgbuilder
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "bytes" |
19 | 18 | "fmt"
|
20 | 19 | "os"
|
21 | 20 | "path/filepath"
|
22 | 21 | "regexp"
|
23 | 22 | "strconv"
|
24 | 23 | "testing"
|
25 |
| - "text/template" |
26 | 24 |
|
27 | 25 | kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
|
28 | 26 | rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1"
|
@@ -561,53 +559,6 @@ func buildRGFile(pkg *pkg) string {
|
561 | 559 | return string(b)
|
562 | 560 | }
|
563 | 561 |
|
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 |
| - |
611 | 562 | type ReposInfo interface {
|
612 | 563 | ResolveRepoRef(repoRef string) (string, bool)
|
613 | 564 | ResolveCommitIndex(repoRef string, index int) (string, bool)
|
@@ -635,20 +586,62 @@ func buildKptfile(pkg *pkg, pkgName string, reposInfo ReposInfo) string {
|
635 | 586 | pkg.Kptfile.UpstreamLock.Ref = newRef
|
636 | 587 | }
|
637 | 588 | }
|
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 | + } |
641 | 603 | }
|
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) |
647 | 641 | if err != nil {
|
648 | 642 | panic(err)
|
649 | 643 | }
|
650 |
| - result := buf.String() |
651 |
| - return result |
| 644 | + return string(b) |
652 | 645 | }
|
653 | 646 |
|
654 | 647 | // resolveRepoRef looks up the repo path for a repo from the reposInfo
|
|
0 commit comments