forked from crossplane-contrib/provider-helm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kustomization patch support to helm3 post render hook, resolves c…
- Loading branch information
Showing
12 changed files
with
665 additions
and
31 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 helm | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kustomize/api/filesys" | ||
"sigs.k8s.io/kustomize/api/konfig" | ||
"sigs.k8s.io/kustomize/api/krusty" | ||
"sigs.k8s.io/kustomize/api/types" | ||
) | ||
|
||
type KustomizationRender struct { | ||
patches []types.Patch | ||
} | ||
|
||
const ( | ||
kustomizationFileName = "kustomization.yaml" | ||
helmOutputFileName = "helm-output.yaml" | ||
) | ||
|
||
func (kr KustomizationRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *bytes.Buffer, err error) { | ||
d, err := ioutil.TempDir("", "helm-post-render") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
k := types.Kustomization{ | ||
Resources: []string{helmOutputFileName}, | ||
Patches: kr.patches, | ||
} | ||
|
||
fsys := filesys.MakeFsOnDisk() | ||
kdata, err := json.Marshal(k) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = fsys.WriteFile(filepath.Join(d, kustomizationFileName), kdata) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
err := os.RemoveAll(d) | ||
if err != nil { | ||
// todo: log err | ||
} | ||
}() | ||
|
||
err = fsys.WriteFile(filepath.Join(d, helmOutputFileName), renderedManifests.Bytes()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := &krusty.Options{ | ||
DoLegacyResourceSort: false, | ||
LoadRestrictions: types.LoadRestrictionsRootOnly, | ||
DoPrune: false, | ||
PluginConfig: konfig.DisabledPluginConfig(), | ||
} | ||
|
||
kust := krusty.MakeKustomizer(fsys, opts) | ||
m, err := kust.Run(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
yml, err := m.AsYaml() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bytes.NewBuffer(yml), 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,102 @@ | ||
package helm | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/test" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/pkg/errors" | ||
"sigs.k8s.io/kustomize/api/resid" | ||
"sigs.k8s.io/kustomize/api/types" | ||
) | ||
|
||
const testDeployment = ` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
env: dev | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
env: dev | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 | ||
` | ||
|
||
func TestKustomize(t *testing.T) { | ||
type want struct { | ||
result string | ||
err error | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
base string | ||
patches []types.Patch | ||
want want | ||
}{ | ||
{ | ||
name: "BasicPatch", | ||
base: testDeployment, | ||
patches: []types.Patch{ | ||
{ | ||
Target: &types.Selector{Gvk: resid.Gvk{Kind: "Deployment"}}, | ||
Patch: "- op: add\n path: /spec/template/spec/nodeSelector\n value:\n node.size: really-big\n aws.az: us-west-2a", | ||
}, | ||
}, | ||
want: want{ | ||
result: "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: nginx-deployment\nspec:\n selector:\n matchLabels:\n app: nginx\n env: dev\n template:\n metadata:\n labels:\n app: nginx\n env: dev\n spec:\n containers:\n - image: nginx:1.14.2\n name: nginx\n ports:\n - containerPort: 80\n nodeSelector:\n aws.az: us-west-2a\n node.size: really-big\n", | ||
}, | ||
}, | ||
{ | ||
name: "InvalidPatch", | ||
base: testDeployment, | ||
patches: []types.Patch{ | ||
{ | ||
Target: &types.Selector{Gvk: resid.Gvk{Kind: "Deployment"}}, | ||
Patch: "- bad patch", | ||
}, | ||
}, | ||
want: want{ | ||
result: "", | ||
err: errors.WithStack(fmt.Errorf("trouble configuring builtin PatchTransformer with config: `\npatch: '- bad patch'\ntarget:\n kind: Deployment\n`: unable to parse SM or JSON patch from [- bad patch]")), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
k := KustomizationRender{ | ||
patches: tt.patches, | ||
} | ||
|
||
buf := bytes.NewBuffer([]byte(tt.base)) | ||
resBuffer, gotErr := k.Run(buf) | ||
gotResult := "" | ||
if gotErr == nil { | ||
gotResult = resBuffer.String() | ||
} | ||
|
||
if diff := cmp.Diff(tt.want.err, gotErr, test.EquateErrors()); diff != "" { | ||
t.Errorf("Reconcile() -want error %s, +got error %s:\n%s", reflect.ValueOf(tt.want.err).Type(), reflect.ValueOf(gotErr).Type(), diff) | ||
} | ||
|
||
if diff := cmp.Diff(tt.want.result, gotResult); diff != "" { | ||
t.Errorf("Reconcile() -want, +got:\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
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,41 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/crossplane-contrib/provider-helm/apis/v1alpha1" | ||
"github.com/pkg/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
ktypes "sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
const ( | ||
keyDefaultPatchFrom = "patch.yaml" | ||
errFailedToUnmarshallPatch = "failed to unmarshal patch" | ||
) | ||
|
||
func getPatchesFromSpec(ctx context.Context, kube client.Client, vals []v1alpha1.ValueFromSource) ([]ktypes.Patch, error) { | ||
var base []ktypes.Patch | ||
|
||
for _, vf := range vals { | ||
s, err := getDataValueFromSource(ctx, kube, vf, keyDefaultPatchFrom) | ||
if err != nil { | ||
return nil, errors.Wrap(err, errFailedToGetValueFromSource) | ||
} | ||
|
||
if s == "" { | ||
continue | ||
} | ||
|
||
var p struct { | ||
Patches []ktypes.Patch `json:"patches"` | ||
} | ||
if err = yaml.Unmarshal([]byte(s), &p); err != nil { | ||
return nil, errors.Wrap(err, errFailedToUnmarshallPatch) | ||
} | ||
base = append(base, p.Patches...) | ||
} | ||
|
||
return base, nil | ||
} |
Oops, something went wrong.