1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
// 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"
"sigs.k8s.io/kustomize/api/builtins"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/yaml"
)
// Add a date prefix to the name.
// A plugin that adapts another plugin.
type plugin struct {
t resmap.Transformer
}
var KustomizePlugin plugin //nolint:gochecknoglobals
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.WrapPrefixf(
err, "dateprefixer makeconfig")
}
prefixer := builtins.NewPrefixTransformerPlugin()
err = prefixer.Config(h, c)
if err != nil {
return errors.WrapPrefixf(
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)
}
|