-
Notifications
You must be signed in to change notification settings - Fork 17
/
kustomize.go
73 lines (66 loc) · 2.32 KB
/
kustomize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package kustomize
import (
"context"
"fmt"
argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/reposerver/repository"
"github.com/argoproj/argo-cd/v2/util/git"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/akuity/kargo-render/internal/manifests"
"github.com/akuity/kargo-render/internal/strings"
)
// Render delegates, in-process to the Argo CD repo server to render plain YAML
// manifests from a directory containing a kustomization.yaml file. This
// function also accepts a list of images (address/name + tag) that will be
// substituted for older versions of the same image. Because of this capability,
// this function is used for last-mile rendering, even when a configuration
// management tool other than Kustomize is used for pre-rendering.
func Render(
ctx context.Context,
path string,
images []string,
) ([]byte, error) {
kustomizeImages := make(argoappv1.KustomizeImages, len(images))
for i, image := range images {
addr, _, _ := strings.SplitLast(image, ":")
kustomizeImages[i] =
argoappv1.KustomizeImage(fmt.Sprintf("%s=%s", addr, image))
}
res, err := repository.GenerateManifests(
ctx,
path,
// Seems ok for these next two arguments to be empty strings. If this is
// last mile rendering, we might be doing this in a directory outside of any
// repo. And event for regular rendering, we have already checked the
// revision we want.
"", // Repo root
"", // Revision
&apiclient.ManifestRequest{
// Both of these fields need to be non-nil
Repo: &argoappv1.Repository{},
ApplicationSource: &argoappv1.ApplicationSource{
Kustomize: &argoappv1.ApplicationSourceKustomize{
Images: kustomizeImages,
},
},
},
true,
&git.NoopCredsStore{}, // No need for this
// TODO: Don't completely understand this next arg, but @alexmt says this is
// right. Something to do with caching?
resource.MustParse("0"),
nil,
)
if err != nil {
return nil,
fmt.Errorf("error generating manifests using Argo CD repo server: %w", err)
}
// res.Manifests contains JSON manifests. We want YAML.
yamlManifests, err := manifests.JSONStringsToYAMLBytes(res.Manifests)
if err != nil {
return nil, err
}
// Glue the manifests together
return manifests.CombineYAML(yamlManifests), nil
}