Extend plugin interface with execute method
Change introduces evolution of airshipctl document plugins which are compatible to the new approach for kustomize plugins. Change-Id: I422110ba523b97c8a80a0b82cb43b70a4ee8e558 Relates-To: #322
This commit is contained in:
parent
a61554984b
commit
0d475ec6c5
@ -13,7 +13,8 @@ airshipctl image build [flags]
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for build
|
||||
-h, --help help for build
|
||||
--progress show progress
|
||||
```
|
||||
|
||||
### Options inherited from parent commands
|
||||
|
1
go.mod
1
go.mod
@ -34,6 +34,7 @@ require (
|
||||
sigs.k8s.io/cluster-api v0.3.5
|
||||
sigs.k8s.io/controller-runtime v0.5.2
|
||||
sigs.k8s.io/kustomize/api v0.5.1
|
||||
sigs.k8s.io/kustomize/kyaml v0.6.0
|
||||
sigs.k8s.io/yaml v1.2.0
|
||||
)
|
||||
|
||||
|
@ -16,9 +16,11 @@ import (
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
|
||||
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
||||
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
|
||||
"opendev.org/airship/airshipctl/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -31,6 +33,8 @@ const (
|
||||
dotReplacer = "$$$$"
|
||||
)
|
||||
|
||||
var _ plugtypes.Plugin = &plugin{}
|
||||
|
||||
type plugin struct {
|
||||
*airshipv1.ReplacementTransformer
|
||||
}
|
||||
@ -110,6 +114,10 @@ func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return nil, errors.ErrNotImplemented{What: "`Exec` method for replacement transformer"}
|
||||
}
|
||||
|
||||
func getReplacement(m resmap.ResMap, objRef *types.Target, fieldRef string) (interface{}, error) {
|
||||
s := types.Selector{
|
||||
Gvk: objRef.Gvk,
|
||||
|
@ -15,18 +15,23 @@
|
||||
package templater
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/yaml"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
|
||||
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
||||
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
|
||||
)
|
||||
|
||||
var _ plugtypes.Plugin = &plugin{}
|
||||
|
||||
type plugin struct {
|
||||
*airshipv1.Templater
|
||||
}
|
||||
@ -54,6 +59,29 @@ func (t *plugin) Run(_ io.Reader, out io.Writer) error {
|
||||
return tmpl.Execute(out, t.Values)
|
||||
}
|
||||
|
||||
func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
out := &bytes.Buffer{}
|
||||
err := t.Run(nil, out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&kio.ByteReader{Reader: out}},
|
||||
Outputs: []kio.Writer{&kio.PackageBuffer{}},
|
||||
}
|
||||
err = p.Execute()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, ok := p.Outputs[0].(*kio.PackageBuffer)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Output conversion error")
|
||||
}
|
||||
return append(items, res.Nodes...), nil
|
||||
}
|
||||
|
||||
// Render input yaml as output yaml
|
||||
// This function is from the Helm project:
|
||||
// https://github.com/helm/helm
|
||||
|
@ -20,6 +20,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"opendev.org/airship/airshipctl/pkg/document/plugin/templater"
|
||||
@ -53,8 +55,7 @@ template: |
|
||||
spec:
|
||||
bootMACAddress: {{ .macAddress }}
|
||||
{{ end -}}`,
|
||||
expectedOut: `---
|
||||
apiVersion: metal3.io/v1alpha1
|
||||
expectedOut: `apiVersion: metal3.io/v1alpha1
|
||||
kind: BareMetalHost
|
||||
metadata:
|
||||
name: node-1
|
||||
@ -77,6 +78,8 @@ metadata:
|
||||
name: notImportantHere
|
||||
values:
|
||||
test:
|
||||
someKey:
|
||||
anotherKey: value
|
||||
of:
|
||||
- toYaml
|
||||
template: |
|
||||
@ -85,6 +88,8 @@ template: |
|
||||
expectedOut: `test:
|
||||
of:
|
||||
- toYaml
|
||||
someKey:
|
||||
anotherKey: value
|
||||
`,
|
||||
},
|
||||
{
|
||||
@ -121,10 +126,12 @@ template: |
|
||||
plugin, err := templater.New(cfg)
|
||||
require.NoError(t, err)
|
||||
buf := &bytes.Buffer{}
|
||||
err = plugin.Run(nil, buf)
|
||||
nodes, err := plugin.Filter(nil)
|
||||
if tc.expectedErr != "" {
|
||||
assert.EqualError(t, err, tc.expectedErr)
|
||||
}
|
||||
err = kio.ByteWriter{Writer: buf}.Write(nodes)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.expectedOut, buf.String())
|
||||
}
|
||||
}
|
||||
|
@ -16,10 +16,13 @@ package types
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
)
|
||||
|
||||
// Plugin interface for airship document plugins
|
||||
type Plugin interface {
|
||||
kio.Filter
|
||||
Run(io.Reader, io.Writer) error
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user