-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Post render * Kustomize failing with vague nil pointer dereference error * Add logic for kustomize in postrenderer * Move post render to its own file * Add test for postrender Co-authored-by: DavisFrench <davisdavis27@gmail.com>
- Loading branch information
1 parent
0c14643
commit b36e29d
Showing
11 changed files
with
435 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package chart | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strings" | ||
|
||
"helm.sh/helm/v3/pkg/postrender" | ||
v1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/kustomize/api/krusty" | ||
kTypes "sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
var _ postrender.PostRenderer = &postRender{} | ||
|
||
type postRender struct { | ||
cli client.Client | ||
namespace string | ||
} | ||
|
||
func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *bytes.Buffer, err error) { | ||
|
||
var configMapList v1.ConfigMapList | ||
opts := &client.ListOptions{Namespace: p.namespace} | ||
if err := p.cli.List(context.Background(), &configMapList, opts); err != nil { | ||
return nil, err | ||
} | ||
|
||
fs := filesys.MakeFsInMemory() | ||
if err := fs.Mkdir(p.namespace); err != nil { | ||
return nil, err | ||
} | ||
|
||
var postrenderFound bool | ||
for _, cm := range configMapList.Items { | ||
if !strings.HasSuffix(cm.Name, "-postrender") { | ||
continue | ||
} | ||
postrenderFound = true | ||
|
||
for k, v := range cm.Data { | ||
fileName := p.namespace + "/" + k | ||
if err := fs.WriteFile(fileName, []byte(v)); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
// return original manifests, otherwise begin postrender | ||
if !postrenderFound { | ||
return renderedManifests, nil | ||
} | ||
|
||
if err := fs.WriteFile(p.namespace+"/app.yaml", renderedManifests.Bytes()); err != nil { | ||
return nil, err | ||
} | ||
|
||
kustomizer := krusty.MakeKustomizer(&krusty.Options{ | ||
PluginConfig: &kTypes.PluginConfig{ | ||
HelmConfig: kTypes.HelmConfig{ | ||
Enabled: true, | ||
Command: "helm", | ||
}, | ||
}, | ||
}) | ||
|
||
result, err := kustomizer.Run(fs, p.namespace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
y, err := result.AsYaml() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewBuffer(y), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package chart | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
//go:embed testdata/render_yamls/prerendered-manifests.yaml | ||
var prerender []byte | ||
|
||
//go:embed testdata/render_yamls/kustomization.yaml | ||
var kustomizationYaml string | ||
|
||
//go:embed testdata/render_yamls/nodeaffinity-patch.yaml | ||
var nodeaffinityPatchYaml string | ||
|
||
//go:embed testdata/render_yamls/nodeaffinity-postrender.yaml | ||
var nodeaffinityPostrender string | ||
|
||
func TestPostRenderRun(t *testing.T) { | ||
tc := []struct { | ||
name string | ||
configmap corev1.ConfigMap | ||
expected string | ||
}{ | ||
{ | ||
name: "postrender not found", | ||
configmap: corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "some-other-configmap", | ||
Namespace: "fake", | ||
}, | ||
}, | ||
expected: string(prerender), | ||
}, | ||
{ | ||
name: "postrender found, patch nodeAffinity", | ||
configmap: corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "nodeaffinity-postrender", | ||
Namespace: "fake", | ||
}, | ||
Data: map[string]string{ | ||
"kustomization.yaml": kustomizationYaml, | ||
"patch.yaml": nodeaffinityPatchYaml, | ||
}, | ||
}, | ||
expected: nodeaffinityPostrender, | ||
}, | ||
} | ||
for _, tt := range tc { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client := fake.NewClientBuilder() | ||
client.WithObjects(&tt.configmap) | ||
|
||
pr := postRender{ | ||
namespace: "fake", | ||
cli: client.Build(), | ||
} | ||
result, err := pr.Run(bytes.NewBuffer(prerender)) | ||
fmt.Println(result.String()) | ||
require.Nil(t, err) | ||
require.Equal(t, tt.expected, result.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
resources: | ||
- app.yaml | ||
patches: | ||
- path: patch.yaml | ||
target: | ||
kind: Deployment | ||
name: ".*" |
16 changes: 16 additions & 0 deletions
16
internal/chart/testdata/render_yamls/nodeaffinity-patch.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: doesnotmatter | ||
spec: | ||
template: | ||
spec: | ||
affinity: | ||
nodeAffinity: | ||
requiredDuringSchedulingIgnoredDuringExecution: | ||
nodeSelectorTerms: | ||
- matchExpressions: | ||
- key: asdf | ||
operator: In | ||
values: | ||
- asdf |
Oops, something went wrong.