feat: create a KustomizeRenderer
The KustomizeRenderer is used to invoke Kustomize to the generated renderer.yaml.
This commit is contained in:
@@ -11,6 +11,7 @@ require (
|
|||||||
github.com/dokku/dokku/plugins/logs v0.0.0-20250618161309-8d0c35f1333c
|
github.com/dokku/dokku/plugins/logs v0.0.0-20250618161309-8d0c35f1333c
|
||||||
github.com/dokku/dokku/plugins/nginx-vhosts v0.0.0-20250618161309-8d0c35f1333c
|
github.com/dokku/dokku/plugins/nginx-vhosts v0.0.0-20250618161309-8d0c35f1333c
|
||||||
github.com/fatih/color v1.18.0
|
github.com/fatih/color v1.18.0
|
||||||
|
github.com/fluxcd/pkg/kustomize v1.18.0
|
||||||
github.com/go-openapi/jsonpointer v0.21.1
|
github.com/go-openapi/jsonpointer v0.21.1
|
||||||
github.com/go-resty/resty/v2 v2.16.5
|
github.com/go-resty/resty/v2 v2.16.5
|
||||||
github.com/gofrs/flock v0.12.1
|
github.com/gofrs/flock v0.12.1
|
||||||
@@ -30,6 +31,7 @@ require (
|
|||||||
k8s.io/kubernetes v1.33.2
|
k8s.io/kubernetes v1.33.2
|
||||||
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
|
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
|
||||||
mvdan.cc/sh/v3 v3.12.0
|
mvdan.cc/sh/v3 v3.12.0
|
||||||
|
sigs.k8s.io/kustomize/api v0.20.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -168,7 +170,6 @@ require (
|
|||||||
sigs.k8s.io/controller-runtime v0.21.0 // indirect
|
sigs.k8s.io/controller-runtime v0.21.0 // indirect
|
||||||
sigs.k8s.io/gateway-api v1.3.0 // indirect
|
sigs.k8s.io/gateway-api v1.3.0 // indirect
|
||||||
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
|
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
|
||||||
sigs.k8s.io/kustomize/api v0.20.0 // indirect
|
|
||||||
sigs.k8s.io/kustomize/kyaml v0.20.0 // indirect
|
sigs.k8s.io/kustomize/kyaml v0.20.0 // indirect
|
||||||
sigs.k8s.io/randfill v1.0.0 // indirect
|
sigs.k8s.io/randfill v1.0.0 // indirect
|
||||||
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
|
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
|
||||||
|
|||||||
@@ -89,6 +89,8 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
|||||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||||
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||||
|
github.com/fluxcd/pkg/kustomize v1.18.0 h1:wWK+qYwmBmba3N3VAqZ9ijnfVGGaIjcaHWo033URZTw=
|
||||||
|
github.com/fluxcd/pkg/kustomize v1.18.0/go.mod h1:Ij9722MdWIE6B1EPg2ZJUf6npycgfRfN4Lohi7D/Kic=
|
||||||
github.com/foxcpp/go-mockdns v1.1.0 h1:jI0rD8M0wuYAxL7r/ynTrCQQq0BVqfB99Vgk7DlmewI=
|
github.com/foxcpp/go-mockdns v1.1.0 h1:jI0rD8M0wuYAxL7r/ynTrCQQq0BVqfB99Vgk7DlmewI=
|
||||||
github.com/foxcpp/go-mockdns v1.1.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk=
|
github.com/foxcpp/go-mockdns v1.1.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk=
|
||||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"github.com/dokku/dokku/plugins/common"
|
||||||
|
"github.com/fluxcd/pkg/kustomize/filesys"
|
||||||
"github.com/gofrs/flock"
|
"github.com/gofrs/flock"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"helm.sh/helm/v3/pkg/action"
|
"helm.sh/helm/v3/pkg/action"
|
||||||
@@ -21,9 +22,13 @@ import (
|
|||||||
"helm.sh/helm/v3/pkg/cli"
|
"helm.sh/helm/v3/pkg/cli"
|
||||||
"helm.sh/helm/v3/pkg/getter"
|
"helm.sh/helm/v3/pkg/getter"
|
||||||
"helm.sh/helm/v3/pkg/kube"
|
"helm.sh/helm/v3/pkg/kube"
|
||||||
|
"helm.sh/helm/v3/pkg/postrender"
|
||||||
"helm.sh/helm/v3/pkg/release"
|
"helm.sh/helm/v3/pkg/release"
|
||||||
"helm.sh/helm/v3/pkg/repo"
|
"helm.sh/helm/v3/pkg/repo"
|
||||||
"helm.sh/helm/v3/pkg/storage/driver"
|
"helm.sh/helm/v3/pkg/storage/driver"
|
||||||
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
|
"sigs.k8s.io/kustomize/api/krusty"
|
||||||
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DevNullPrinter = func(format string, v ...interface{}) {}
|
var DevNullPrinter = func(format string, v ...interface{}) {}
|
||||||
@@ -337,16 +342,6 @@ func (h *HelmAgent) ListRevisions(input ListRevisionsInput) ([]Release, error) {
|
|||||||
return releases, nil
|
return releases, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type DebugRenderer struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *DebugRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
|
|
||||||
for _, line := range strings.Split(renderedManifests.String(), "\n") {
|
|
||||||
common.LogWarn(line)
|
|
||||||
}
|
|
||||||
return renderedManifests, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HelmAgent) UpgradeChart(ctx context.Context, input ChartInput) error {
|
func (h *HelmAgent) UpgradeChart(ctx context.Context, input ChartInput) error {
|
||||||
namespace := input.Namespace
|
namespace := input.Namespace
|
||||||
if namespace == "" {
|
if namespace == "" {
|
||||||
@@ -417,3 +412,77 @@ func (h *HelmAgent) UninstallChart(releaseName string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DebugRenderer struct {
|
||||||
|
Renderer *postrender.PostRenderer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DebugRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
|
||||||
|
if p.Renderer != nil {
|
||||||
|
var err error
|
||||||
|
renderer := *p.Renderer
|
||||||
|
renderedManifests, err = renderer.Run(renderedManifests)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(renderedManifests.String(), "\n") {
|
||||||
|
common.LogWarn(line)
|
||||||
|
}
|
||||||
|
return renderedManifests, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type KustomizeRenderer struct {
|
||||||
|
Renderer *postrender.PostRenderer
|
||||||
|
KustomizeRootPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *KustomizeRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
|
||||||
|
if p.KustomizeRootPath == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !common.DirectoryExists(p.KustomizeRootPath) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fs, err := filesys.MakeFsOnDiskSecureBuild(p.KustomizeRootPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error creating filesystem: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var kfile string
|
||||||
|
for _, f := range konfig.RecognizedKustomizationFileNames() {
|
||||||
|
if kf := filepath.Join(p.KustomizeRootPath, f); fs.Exists(kf) {
|
||||||
|
kfile = kf
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if kfile == "" {
|
||||||
|
return nil, fmt.Errorf("%s not found", konfig.DefaultKustomizationFileName())
|
||||||
|
}
|
||||||
|
|
||||||
|
renderedYamlPath := filepath.Join(p.KustomizeRootPath, "rendered.yaml")
|
||||||
|
if err := os.WriteFile(renderedYamlPath, renderedManifests.Bytes(), 0644); err != nil {
|
||||||
|
return nil, fmt.Errorf("Error writing rendered.yaml: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buildOptions := &krusty.Options{
|
||||||
|
LoadRestrictions: types.LoadRestrictionsNone,
|
||||||
|
PluginConfig: types.DisabledPluginConfig(),
|
||||||
|
}
|
||||||
|
|
||||||
|
k := krusty.MakeKustomizer(buildOptions)
|
||||||
|
m, err := k.Run(fs, p.KustomizeRootPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resources, err := m.AsYaml()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes.NewBuffer(resources), nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user