| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Deprecated: DatePrefixer will be removed with kustomize/api v1. |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
|
| 11 |
"sigs.k8s.io/kustomize/api/builtins" |
| 12 |
"sigs.k8s.io/kustomize/api/resmap" |
| 13 |
"sigs.k8s.io/kustomize/api/types" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 15 |
"sigs.k8s.io/yaml" |
| 16 |
) |
| 17 |
|
| 18 |
// Add a date prefix to the name. |
| 19 |
// A plugin that adapts another plugin. |
| 20 |
type plugin struct { |
| 21 |
t resmap.Transformer |
| 22 |
} |
| 23 |
|
| 24 |
var KustomizePlugin plugin //nolint:gochecknoglobals |
| 25 |
|
| 26 |
func (p *plugin) makePrefixPluginConfig() ([]byte, error) { |
| 27 |
var s struct { |
| 28 |
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"` |
| 29 |
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` |
| 30 |
} |
| 31 |
s.Prefix = getDate() + "-" |
| 32 |
s.FieldSpecs = []types.FieldSpec{ |
| 33 |
{Path: "metadata/name"}, |
| 34 |
} |
| 35 |
return yaml.Marshal(s) |
| 36 |
} |
| 37 |
|
| 38 |
func (p *plugin) Config(h *resmap.PluginHelpers, _ []byte) error { |
| 39 |
// Ignore the incoming c, compute new config. |
| 40 |
c, err := p.makePrefixPluginConfig() |
| 41 |
if err != nil { |
| 42 |
return errors.WrapPrefixf( |
| 43 |
err, "dateprefixer makeconfig") |
| 44 |
} |
| 45 |
prefixer := builtins.NewPrefixTransformerPlugin() |
| 46 |
err = prefixer.Config(h, c) |
| 47 |
if err != nil { |
| 48 |
return errors.WrapPrefixf( |
| 49 |
err, "prefix configure") |
| 50 |
} |
| 51 |
p.t = prefixer |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
// Returns a constant, rather than |
| 56 |
// |
| 57 |
// time.Now().Format("2006-01-02") |
| 58 |
// |
| 59 |
// to make tests happy. |
| 60 |
// This is just an example. |
| 61 |
func getDate() string { |
| 62 |
return "2018-05-11" |
| 63 |
} |
| 64 |
|
| 65 |
func (p *plugin) Transform(m resmap.ResMap) error { |
| 66 |
_, err := fmt.Fprintln(os.Stderr, "Deprecated: DatePrefixer will be removed with kustomize/api v1.") |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
return p.t.Transform(m) |
| 71 |
} |