Skip to content

Commit

Permalink
Merge pull request #946 from Liujingfang1/moreplugins
Browse files Browse the repository at this point in the history
add support for transformer goplugins
  • Loading branch information
k8s-ci-robot authored Apr 5, 2019
2 parents 53f0dee + 4f1a235 commit 9e8d06e
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 20 deletions.
10 changes: 6 additions & 4 deletions pkg/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ url examples:
func NewCmdBuild(
out io.Writer, fs fs.FileSystem,
rf *resmap.Factory,
ptf transformer.Factory) *cobra.Command {
ptf transformer.Factory,
b bool) *cobra.Command {
var o Options

cmd := &cobra.Command{
Expand All @@ -75,7 +76,7 @@ func NewCmdBuild(
if err != nil {
return err
}
return o.RunBuild(out, fs, rf, ptf)
return o.RunBuild(out, fs, rf, ptf, b)
},
}
cmd.Flags().StringVarP(
Expand All @@ -102,13 +103,14 @@ func (o *Options) Validate(args []string) error {
// RunBuild runs build command.
func (o *Options) RunBuild(
out io.Writer, fSys fs.FileSystem,
rf *resmap.Factory, ptf transformer.Factory) error {
rf *resmap.Factory, ptf transformer.Factory,
b bool) error {
ldr, err := loader.NewLoader(o.kustomizationPath, fSys)
if err != nil {
return err
}
defer ldr.Cleanup()
kt, err := target.NewKustTarget(ldr, rf, ptf)
kt, err := target.NewKustTarget(ldr, rf, ptf, b)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ See https://sigs.k8s.io/kustomize
build.NewCmdBuild(
stdOut, fSys,
resmap.NewFactory(resource.NewFactory(uf)),
transformer.NewFactoryImpl()),
transformer.NewFactoryImpl(), genMetaArgs.PluginConfig.GoEnabled),
edit.NewCmdEdit(fSys, validator.NewKustValidator(), uf),
misc.NewCmdConfig(fSys),
misc.NewCmdVersion(stdOut),
Expand Down
3 changes: 2 additions & 1 deletion pkg/pgmconfig/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ var KustomizationFileNames = []string{
}

const (
PgmName = "kustomize"
PgmName = "kustomize"
PluginsDir = "plugins"
)
95 changes: 95 additions & 0 deletions pkg/plugins/transformers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package plugins

import (
"fmt"
"path/filepath"
"plugin"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/pgmconfig"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers"
)

const transformerSymbol = "Transformer"

type Configurable interface {
Config(k ifc.Kunstructured) error
}

type transformerLoader struct {
pluginDir string
enabled bool
}

func NewTransformerLoader(b bool) transformerLoader {
return transformerLoader{
pluginDir: filepath.Join(pgmconfig.ConfigRoot(), pgmconfig.PluginsDir),
enabled: b,
}
}

func (l transformerLoader) Load(rm resmap.ResMap) ([]transformers.Transformer, error) {
if len(rm) == 0 {
return nil, nil
}
if !l.enabled {
return nil, fmt.Errorf("plugin is not enabled")
}
var result []transformers.Transformer
for id, res := range rm {
t, err := l.load(id, res)
if err != nil {
return nil, err
}
result = append(result, t)
}
return result, nil
}

func (l transformerLoader) load(id resid.ResId, res *resource.Resource) (transformers.Transformer, error) {
fileName := filepath.Join(l.pluginDir, id.Gvk().Kind+".so")
goPlugin, err := plugin.Open(fileName)
if err != nil {
return nil, fmt.Errorf("plugin %s file not opened", fileName)
}

symbol, err := goPlugin.Lookup(transformerSymbol)
if err != nil {
return nil, fmt.Errorf("plugin %s fails lookup", fileName)
}

c, ok := symbol.(Configurable)
if !ok {
return nil, fmt.Errorf("plugin %s not configurable", fileName)
}
err = c.Config(res)
if err != nil {
return nil, errors.Wrapf(err, "plugin %s fails configuration", fileName)
}

t, ok := c.(transformers.Transformer)
if !ok {
return nil, fmt.Errorf("plugin %s not a transformer", fileName)
}
return t, nil
}
40 changes: 30 additions & 10 deletions pkg/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
interror "sigs.k8s.io/kustomize/pkg/internal/error"
patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer"
"sigs.k8s.io/kustomize/pkg/pgmconfig"
"sigs.k8s.io/kustomize/pkg/plugins"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers"
Expand All @@ -40,17 +41,19 @@ import (

// KustTarget encapsulates the entirety of a kustomization build.
type KustTarget struct {
kustomization *types.Kustomization
ldr ifc.Loader
rFactory *resmap.Factory
tFactory transformer.Factory
kustomization *types.Kustomization
ldr ifc.Loader
rFactory *resmap.Factory
tFactory transformer.Factory
goPluginEnabled bool
}

// NewKustTarget returns a new instance of KustTarget primed with a Loader.
func NewKustTarget(
ldr ifc.Loader,
rFactory *resmap.Factory,
tFactory transformer.Factory) (*KustTarget, error) {
tFactory transformer.Factory,
b bool) (*KustTarget, error) {
content, err := loadKustFile(ldr)
if err != nil {
return nil, err
Expand All @@ -68,10 +71,11 @@ func NewKustTarget(
strings.Join(errs, "\n"), ldr.Root())
}
return &KustTarget{
kustomization: &k,
ldr: ldr,
rFactory: rFactory,
tFactory: tFactory,
kustomization: &k,
ldr: ldr,
rFactory: rFactory,
tFactory: tFactory,
goPluginEnabled: b,
}, nil
}

Expand Down Expand Up @@ -252,7 +256,7 @@ func (kt *KustTarget) accumulateBases() (
continue
}
subKt, err := NewKustTarget(
ldr, kt.rFactory, kt.tFactory)
ldr, kt.rFactory, kt.tFactory, kt.goPluginEnabled)
if err != nil {
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
ldr.Cleanup()
Expand Down Expand Up @@ -318,5 +322,21 @@ func (kt *KustTarget) newTransformer(
return nil, err
}
r = append(r, t)

tp, err := kt.loadTransformerPlugins()
if err != nil {
return nil, err
}
r = append(r, tp...)
return transformers.NewMultiTransformer(r), nil
}

func (kt *KustTarget) loadTransformerPlugins() ([]transformers.Transformer, error) {
transformerPluginConfigs, err := kt.rFactory.FromFiles(
kt.ldr, kt.kustomization.Transformers)
if err != nil {
return nil, err
}
tl := plugins.NewTransformerLoader(kt.goPluginEnabled)
return tl.Load(transformerPluginConfigs)
}
2 changes: 1 addition & 1 deletion pkg/target/kusttarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestResources(t *testing.T) {
}

func TestKustomizationNotFound(t *testing.T) {
_, err := NewKustTarget(loadertest.NewFakeLoader("/foo"), nil, nil)
_, err := NewKustTarget(loadertest.NewFakeLoader("/foo"), nil, nil, false)
if err == nil {
t.Fatalf("expected an error")
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/target/kusttestharness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type KustTestHarness struct {
t *testing.T
rf *resmap.Factory
ldr loadertest.FakeLoader
b bool
}

func NewKustTestHarness(t *testing.T, path string) *KustTestHarness {
Expand All @@ -55,12 +56,13 @@ func NewKustTestHarnessWithPluginConfig(
rf: resmap.NewFactory(resource.NewFactory(
kunstruct.NewKunstructuredFactoryWithGeneratorArgs(
&types.GeneratorMetaArgs{PluginConfig: config}))),
ldr: loadertest.NewFakeLoader(path)}
ldr: loadertest.NewFakeLoader(path),
b: config.GoEnabled}
}

func (th *KustTestHarness) makeKustTarget() *KustTarget {
kt, err := NewKustTarget(
th.ldr, th.rf, transformer.NewFactoryImpl())
th.ldr, th.rf, transformer.NewFactoryImpl(), th.b)
if err != nil {
th.t.Fatalf("Unexpected construction error %v", err)
}
Expand Down
Loading

0 comments on commit 9e8d06e

Please sign in to comment.