Files
kustomize/plugin/someteam.example.com/v1/dateprefixer/DatePrefixer.go
Julian 3dbc88bf94 Refactor the PrefixSuffixTransformer into separate prefix- and suffix transformers (#4318)
* Refactor prefix filter into its own filter, decoupled from the prefixsuffix filter

* Refactor prefix transformer into its own transformer, decoupled from the prefixsuffix transformer

* Refactor suffix filter into its own filter, decoupled from the prefixsuffix filter

* Refactor suffix transformer into its own transformer, decoupled from the prefixsuffix transformer

* Add a default nameSuffix field spec in addition to the namePrefix

* Remove the PrefixSuffixTransformer from the list of builtin transformers

* Add a multi-transformer to builtinhelpers.TransformFactories

* Remove the implementation of the prefixsuffixtransformer.PrefixSuffixTransformer

* Resolve style and format related feedback from the pull request

* Add test to test the legacy PrefixSuffixTransformer for BC purposes
2021-12-22 12:01:06 -08:00

72 lines
1.7 KiB
Go

// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Deprecated: DatePrefixer will be removed with kustomize/api v1.
package main
import (
"fmt"
"os"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/builtins"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml"
)
// Add a date prefix to the name.
// A plugin that adapts another plugin.
type plugin struct {
t resmap.Transformer
}
//nolint: golint
//noinspection GoUnusedGlobalVariable
var KustomizePlugin plugin
func (p *plugin) makePrefixPluginConfig() ([]byte, error) {
var s struct {
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
}
s.Prefix = getDate() + "-"
s.FieldSpecs = []types.FieldSpec{
{Path: "metadata/name"},
}
return yaml.Marshal(s)
}
func (p *plugin) Config(h *resmap.PluginHelpers, _ []byte) error {
// Ignore the incoming c, compute new config.
c, err := p.makePrefixPluginConfig()
if err != nil {
return errors.Wrapf(
err, "dateprefixer makeconfig")
}
prefixer := builtins.NewPrefixTransformerPlugin()
err = prefixer.Config(h, c)
if err != nil {
return errors.Wrapf(
err, "prefix configure")
}
p.t = prefixer
return nil
}
// Returns a constant, rather than
// time.Now().Format("2006-01-02")
// to make tests happy.
// This is just an example.
func getDate() string {
return "2018-05-11"
}
func (p *plugin) Transform(m resmap.ResMap) error {
_, err := fmt.Fprintln(os.Stderr, "Deprecated: DatePrefixer will be removed with kustomize/api v1.")
if err != nil {
return err
}
return p.t.Transform(m)
}