Convert inventory transformer to plugin, reduce k8sdeps.

This commit is contained in:
Jeffrey Regan
2019-05-31 13:46:35 -07:00
committed by jregan
parent 115a0bc560
commit 81c98c855f
14 changed files with 491 additions and 328 deletions

View File

@@ -1,18 +1,5 @@
/*
Copyright 2018 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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package accumulator
@@ -29,7 +16,8 @@ import (
)
// ResAccumulator accumulates resources and the rules
// used to customize those resources.
// used to customize those resources. It's a ResMap
// plus stuff needed to modify the ResMap.
type ResAccumulator struct {
resMap resmap.ResMap
tConfig *config.TransformerConfig

View File

@@ -5,18 +5,13 @@
package transformer
import (
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/types"
)
// Factory makes transformers
// Factory makes transformers that require k8sdeps.
type Factory interface {
MakePatchTransformer(slice []*resource.Resource, rf *resource.Factory) (transformers.Transformer, error)
MakeInventoryTransformer(
p *types.Inventory,
ldr ifc.Loader,
namespace string,
gp types.GarbagePolicy) transformers.Transformer
MakePatchTransformer(
slice []*resource.Resource,
rf *resource.Factory) (transformers.Transformer, error)
}

View File

@@ -127,14 +127,11 @@ func (kt *KustTarget) makeCustomizedResMap(
if err != nil {
return nil, err
}
// This must be done last, and not as part of
// The following steps must be done last, not as part of
// the recursion implicit in AccumulateTarget.
p := builtin.NewHashTransformerPlugin()
err = kt.configureBuiltinPlugin(p, nil, "hash")
if err != nil {
return nil, err
}
err = ra.Transform(p)
err = kt.addHashesToNames(ra)
if err != nil {
return nil, err
}
@@ -145,22 +142,60 @@ func (kt *KustTarget) makeCustomizedResMap(
if err != nil {
return nil, err
}
// With all the back references fixed, it's OK to resolve Vars.
err = ra.ResolveVars()
if err != nil {
return nil, err
}
rm := ra.ResMap()
pt := kt.tFactory.MakeInventoryTransformer(
kt.kustomization.Inventory, kt.ldr,
kt.kustomization.Namespace,
garbagePolicy)
err = pt.Transform(rm)
err = kt.computeInventory(ra, garbagePolicy)
if err != nil {
return nil, err
}
return rm, nil
return ra.ResMap(), nil
}
func (kt *KustTarget) addHashesToNames(
ra *accumulator.ResAccumulator) error {
p := builtin.NewHashTransformerPlugin()
err := kt.configureBuiltinPlugin(p, nil, "hash")
if err != nil {
return err
}
return ra.Transform(p)
}
func (kt *KustTarget) computeInventory(
ra *accumulator.ResAccumulator, garbagePolicy types.GarbagePolicy) error {
inv := kt.kustomization.Inventory
if inv == nil {
return nil
}
if inv.Type != "ConfigMap" {
return fmt.Errorf("don't know how to do that")
}
if inv.ConfigMap.Namespace != kt.kustomization.Namespace {
return fmt.Errorf("namespace mismatch")
}
p := builtin.NewInventoryTransformerPlugin()
var c struct {
Policy string
Name string
Namespace string
}
c.Name = inv.ConfigMap.Name
c.Namespace = inv.ConfigMap.Namespace
c.Policy = garbagePolicy.String()
err := kt.configureBuiltinPlugin(p, c, "inventory")
if err != nil {
return err
}
return ra.Transform(p)
}
func (kt *KustTarget) shouldAddHashSuffixesToGeneratedResources() bool {

View File

@@ -0,0 +1,25 @@
// Code generated by "stringer -type=GarbagePolicy"; DO NOT EDIT.
package types
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[GarbageIgnore-1]
_ = x[GarbageCollect-2]
}
const _GarbagePolicy_name = "GarbageIgnoreGarbageCollect"
var _GarbagePolicy_index = [...]uint8{0, 13, 27}
func (i GarbagePolicy) String() string {
i -= 1
if i < 0 || i >= GarbagePolicy(len(_GarbagePolicy_index)-1) {
return "GarbagePolicy(" + strconv.FormatInt(int64(i+1), 10) + ")"
}
return _GarbagePolicy_name[_GarbagePolicy_index[i]:_GarbagePolicy_index[i+1]]
}

View File

@@ -146,11 +146,11 @@ type Kustomization struct {
Inventory *Inventory `json:"inventory,omitempty" yaml:"inventory:omitempty"`
}
//go:generate stringer -type=GarbagePolicy
type GarbagePolicy int
const (
GarbageUnknown GarbagePolicy = iota
GarbageIgnore
GarbageIgnore GarbagePolicy = iota + 1
GarbageCollect
)