| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
//go:generate pluginator |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"sigs.k8s.io/kustomize/api/ifc" |
| 11 |
"sigs.k8s.io/kustomize/api/resmap" |
| 12 |
) |
| 13 |
|
| 14 |
type plugin struct { |
| 15 |
hasher ifc.KustHasher |
| 16 |
} |
| 17 |
|
| 18 |
var KustomizePlugin plugin //nolint:gochecknoglobals |
| 19 |
|
| 20 |
func (p *plugin) Config( |
| 21 |
h *resmap.PluginHelpers, _ []byte) (err error) { |
| 22 |
p.hasher = h.ResmapFactory().RF().Hasher() |
| 23 |
return nil |
| 24 |
} |
| 25 |
|
| 26 |
// Transform appends hash to generated resources. |
| 27 |
func (p *plugin) Transform(m resmap.ResMap) error { |
| 28 |
for _, res := range m.Resources() { |
| 29 |
if res.NeedHashSuffix() { |
| 30 |
h, err := res.Hash(p.hasher) |
| 31 |
if err != nil { |
| 32 |
return err |
| 33 |
} |
| 34 |
res.StorePreviousId() |
| 35 |
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h)) |
| 36 |
} |
| 37 |
} |
| 38 |
return nil |
| 39 |
} |