| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"sigs.k8s.io/kustomize/api/kv" |
| 8 |
"sigs.k8s.io/kustomize/api/resmap" |
| 9 |
"sigs.k8s.io/kustomize/api/types" |
| 10 |
"sigs.k8s.io/yaml" |
| 11 |
) |
| 12 |
|
| 13 |
// A secret generator example that gets data |
| 14 |
// from a database (simulated by a hardcoded map). |
| 15 |
type plugin struct { |
| 16 |
h *resmap.PluginHelpers |
| 17 |
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` |
| 18 |
// List of keys to use in database lookups |
| 19 |
Keys []string `json:"keys,omitempty" yaml:"keys,omitempty"` |
| 20 |
} |
| 21 |
|
| 22 |
var KustomizePlugin plugin //nolint:gochecknoglobals |
| 23 |
|
| 24 |
var database = map[string]string{ |
| 25 |
"TREE": "oak", |
| 26 |
"ROCKET": "SaturnV", |
| 27 |
"FRUIT": "apple", |
| 28 |
"VEGETABLE": "carrot", |
| 29 |
"SIMPSON": "homer", |
| 30 |
} |
| 31 |
|
| 32 |
func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error { |
| 33 |
p.h = h |
| 34 |
return yaml.Unmarshal(c, p) |
| 35 |
} |
| 36 |
|
| 37 |
// The plan here is to convert the plugin's input |
| 38 |
// into the format used by the builtin secret generator plugin. |
| 39 |
func (p *plugin) Generate() (resmap.ResMap, error) { |
| 40 |
args := types.SecretArgs{} |
| 41 |
args.Name = p.Name |
| 42 |
args.Namespace = p.Namespace |
| 43 |
for _, k := range p.Keys { |
| 44 |
if v, ok := database[k]; ok { |
| 45 |
args.LiteralSources = append( |
| 46 |
args.LiteralSources, k+"="+v) |
| 47 |
} |
| 48 |
} |
| 49 |
return p.h.ResmapFactory().FromSecretArgs( |
| 50 |
kv.NewLoader(p.h.Loader(), p.h.Validator()), args) |
| 51 |
} |