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
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
-h, --help help for build
|
-h, --help help for build
|
||||||
|
--progress show progress
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options inherited from parent commands
|
### 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/cluster-api v0.3.5
|
||||||
sigs.k8s.io/controller-runtime v0.5.2
|
sigs.k8s.io/controller-runtime v0.5.2
|
||||||
sigs.k8s.io/kustomize/api v0.5.1
|
sigs.k8s.io/kustomize/api v0.5.1
|
||||||
|
sigs.k8s.io/kustomize/kyaml v0.6.0
|
||||||
sigs.k8s.io/yaml v1.2.0
|
sigs.k8s.io/yaml v1.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,9 +16,11 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
|
|
||||||
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
||||||
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
|
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
|
||||||
|
"opendev.org/airship/airshipctl/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -31,6 +33,8 @@ const (
|
|||||||
dotReplacer = "$$$$"
|
dotReplacer = "$$$$"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ plugtypes.Plugin = &plugin{}
|
||||||
|
|
||||||
type plugin struct {
|
type plugin struct {
|
||||||
*airshipv1.ReplacementTransformer
|
*airshipv1.ReplacementTransformer
|
||||||
}
|
}
|
||||||
@ -110,6 +114,10 @@ func (p *plugin) Transform(m resmap.ResMap) error {
|
|||||||
return nil
|
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) {
|
func getReplacement(m resmap.ResMap, objRef *types.Target, fieldRef string) (interface{}, error) {
|
||||||
s := types.Selector{
|
s := types.Selector{
|
||||||
Gvk: objRef.Gvk,
|
Gvk: objRef.Gvk,
|
||||||
|
@ -15,18 +15,23 @@
|
|||||||
package templater
|
package templater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/Masterminds/sprig"
|
"github.com/Masterminds/sprig"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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"
|
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
||||||
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
|
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ plugtypes.Plugin = &plugin{}
|
||||||
|
|
||||||
type plugin struct {
|
type plugin struct {
|
||||||
*airshipv1.Templater
|
*airshipv1.Templater
|
||||||
}
|
}
|
||||||
@ -54,6 +59,29 @@ func (t *plugin) Run(_ io.Reader, out io.Writer) error {
|
|||||||
return tmpl.Execute(out, t.Values)
|
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
|
// Render input yaml as output yaml
|
||||||
// This function is from the Helm project:
|
// This function is from the Helm project:
|
||||||
// https://github.com/helm/helm
|
// https://github.com/helm/helm
|
||||||
|
@ -20,6 +20,8 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
"opendev.org/airship/airshipctl/pkg/document/plugin/templater"
|
"opendev.org/airship/airshipctl/pkg/document/plugin/templater"
|
||||||
@ -53,8 +55,7 @@ template: |
|
|||||||
spec:
|
spec:
|
||||||
bootMACAddress: {{ .macAddress }}
|
bootMACAddress: {{ .macAddress }}
|
||||||
{{ end -}}`,
|
{{ end -}}`,
|
||||||
expectedOut: `---
|
expectedOut: `apiVersion: metal3.io/v1alpha1
|
||||||
apiVersion: metal3.io/v1alpha1
|
|
||||||
kind: BareMetalHost
|
kind: BareMetalHost
|
||||||
metadata:
|
metadata:
|
||||||
name: node-1
|
name: node-1
|
||||||
@ -77,6 +78,8 @@ metadata:
|
|||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
values:
|
values:
|
||||||
test:
|
test:
|
||||||
|
someKey:
|
||||||
|
anotherKey: value
|
||||||
of:
|
of:
|
||||||
- toYaml
|
- toYaml
|
||||||
template: |
|
template: |
|
||||||
@ -85,6 +88,8 @@ template: |
|
|||||||
expectedOut: `test:
|
expectedOut: `test:
|
||||||
of:
|
of:
|
||||||
- toYaml
|
- toYaml
|
||||||
|
someKey:
|
||||||
|
anotherKey: value
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -121,10 +126,12 @@ template: |
|
|||||||
plugin, err := templater.New(cfg)
|
plugin, err := templater.New(cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
err = plugin.Run(nil, buf)
|
nodes, err := plugin.Filter(nil)
|
||||||
if tc.expectedErr != "" {
|
if tc.expectedErr != "" {
|
||||||
assert.EqualError(t, err, 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())
|
assert.Equal(t, tc.expectedOut, buf.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,13 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Plugin interface for airship document plugins
|
// Plugin interface for airship document plugins
|
||||||
type Plugin interface {
|
type Plugin interface {
|
||||||
|
kio.Filter
|
||||||
Run(io.Reader, io.Writer) error
|
Run(io.Reader, io.Writer) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user