add generator plugins

This commit is contained in:
Jingfang Liu
2019-04-05 13:51:56 -07:00
parent 4f1a2350ce
commit 7493732176
4 changed files with 250 additions and 1 deletions

View File

@@ -0,0 +1,73 @@
/*
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 target_test
import (
"os"
"path/filepath"
"testing"
"sigs.k8s.io/kustomize/pkg/pgmconfig"
"sigs.k8s.io/kustomize/pkg/types"
)
func writeGenerator(th *KustTestHarness, path string) {
th.writeF(path, `
apiVersion: strings.microwoosh.com/v1
kind: ServiceGenerator
metadata:
name: myServiceGenerator
service: my-service
port: "12345"
`)
}
func TestGeneratorPlugin(t *testing.T) {
dir, err := filepath.Abs("../../..")
if err != nil {
t.Errorf("unexpected error %v", err)
}
os.Setenv(pgmconfig.XDG_CONFIG_HOME, dir)
defer os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
err = buildGoPlugins(dir, "ServiceGenerator")
if err != nil {
t.Errorf("unexpected error %v", err)
}
th := NewKustTestHarnessWithPluginConfig(
t, "/app", types.PluginConfig{GoEnabled: true})
th.writeK("/app", `
generators:
- serviceGenerator.yaml
`)
writeGenerator(th, "/app/serviceGenerator.yaml")
m, err := th.makeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.assertActualEqualsExpected(m, `
apiVersion: v1
kind: Service
metadata:
labels:
app: dev
name: my-service
spec:
ports:
- port: 12345
selector:
app: dev
`)
}

View File

@@ -155,7 +155,7 @@ func (kt *KustTarget) shouldAddHashSuffixesToGeneratedResources() bool {
// holding customized resources and the data/rules used
// to do so. The name back references and vars are
// not yet fixed.
func (kt *KustTarget) AccumulateTarget() (
func (kt *KustTarget) AccumulateTarget() ( // nolint: gocyclo
ra *accumulator.ResAccumulator, err error) {
// TODO(monopole): Get rid of the KustomizationErrors accumulator.
// It's not consistently used, and complicates tests.
@@ -173,6 +173,17 @@ func (kt *KustTarget) AccumulateTarget() (
if err != nil {
errs.Append(errors.Wrap(err, "MergeResourcesWithErrorOnIdCollision"))
}
resourceFromGenerators, err := kt.loadGeneratorPlugins()
if err != nil {
errs.Append(errors.Wrap(err, "failed to load resources from generators"))
}
if len(errs.Get()) > 0 {
return ra, errs
}
err = ra.MergeResourcesWithErrorOnIdCollision(resourceFromGenerators)
if err != nil {
errs.Append(errors.Wrap(err, "MergeResourcesWithErrorOnIdCollision"))
}
tConfig, err := config.MakeTransformerConfig(
kt.ldr, kt.kustomization.Configurations)
if err != nil {
@@ -340,3 +351,13 @@ func (kt *KustTarget) loadTransformerPlugins() ([]transformers.Transformer, erro
tl := plugins.NewTransformerLoader(kt.goPluginEnabled)
return tl.Load(transformerPluginConfigs)
}
func (kt *KustTarget) loadGeneratorPlugins() (resmap.ResMap, error) {
generatorPluginConfigs, err := kt.rFactory.FromFiles(
kt.ldr, kt.kustomization.Generators)
if err != nil {
return nil, err
}
gl := plugins.NewGeneratorLoader(kt.goPluginEnabled, kt.rFactory)
return gl.Load(generatorPluginConfigs)
}