| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"sigs.k8s.io/kustomize/api/ifc" |
| 13 |
"sigs.k8s.io/kustomize/api/internal/generators" |
| 14 |
"sigs.k8s.io/kustomize/api/internal/kusterr" |
| 15 |
"sigs.k8s.io/kustomize/api/konfig" |
| 16 |
"sigs.k8s.io/kustomize/api/types" |
| 17 |
"sigs.k8s.io/kustomize/kyaml/kio" |
| 18 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 19 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 20 |
) |
| 21 |
|
| 22 |
// Factory makes instances of Resource. |
| 23 |
type Factory struct { |
| 24 |
hasher ifc.KustHasher |
| 25 |
|
| 26 |
// When set to true, IncludeLocalConfigs indicates |
| 27 |
// that Factory should include resources with the |
| 28 |
// annotation 'config.kubernetes.io/local-config'. |
| 29 |
// By default these resources are ignored. |
| 30 |
IncludeLocalConfigs bool |
| 31 |
} |
| 32 |
|
| 33 |
// NewFactory makes an instance of Factory. |
| 34 |
func NewFactory(h ifc.KustHasher) *Factory { |
| 35 |
return &Factory{hasher: h} |
| 36 |
} |
| 37 |
|
| 38 |
// Hasher returns an ifc.KustHasher |
| 39 |
func (rf *Factory) Hasher() ifc.KustHasher { |
| 40 |
return rf.hasher |
| 41 |
} |
| 42 |
|
| 43 |
// FromMap returns a new instance of Resource. |
| 44 |
func (rf *Factory) FromMap(m map[string]interface{}) (*Resource, error) { |
| 45 |
res, err := rf.FromMapAndOption(m, nil) |
| 46 |
if err != nil { |
| 47 |
return nil, fmt.Errorf("failed to create resource from map: %w", err) |
| 48 |
} |
| 49 |
return res, nil |
| 50 |
} |
| 51 |
|
| 52 |
// FromMapWithName returns a new instance with the given "original" name. |
| 53 |
func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) (*Resource, error) { |
| 54 |
return rf.FromMapWithNamespaceAndName(resid.DefaultNamespace, n, m) |
| 55 |
} |
| 56 |
|
| 57 |
// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace. |
| 58 |
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) (*Resource, error) { |
| 59 |
r, err := rf.FromMapAndOption(m, nil) |
| 60 |
if err != nil { |
| 61 |
return nil, fmt.Errorf("failed to create resource from map: %w", err) |
| 62 |
} |
| 63 |
return r.setPreviousId(ns, n, r.GetKind()), nil |
| 64 |
} |
| 65 |
|
| 66 |
// FromMapAndOption returns a new instance of Resource with given options. |
| 67 |
func (rf *Factory) FromMapAndOption( |
| 68 |
m map[string]interface{}, args *types.GeneratorArgs) (*Resource, error) { |
| 69 |
n, err := yaml.FromMap(m) |
| 70 |
if err != nil { |
| 71 |
return nil, fmt.Errorf("failed to convert map to YAML node: %w", err) |
| 72 |
} |
| 73 |
return rf.makeOne(n, args), nil |
| 74 |
} |
| 75 |
|
| 76 |
// makeOne returns a new instance of Resource. |
| 77 |
func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GeneratorArgs) *Resource { |
| 78 |
if rn == nil { |
| 79 |
log.Fatal("RNode must not be null") |
| 80 |
} |
| 81 |
resource := &Resource{RNode: *rn} |
| 82 |
if o != nil { |
| 83 |
if o.Options == nil || !o.Options.DisableNameSuffixHash { |
| 84 |
resource.EnableHashSuffix() |
| 85 |
} |
| 86 |
resource.SetBehavior(types.NewGenerationBehavior(o.Behavior)) |
| 87 |
} |
| 88 |
|
| 89 |
return resource |
| 90 |
} |
| 91 |
|
| 92 |
// SliceFromPatches returns a slice of resources given a patch path |
| 93 |
// slice from a kustomization file. |
| 94 |
func (rf *Factory) SliceFromPatches( |
| 95 |
ldr ifc.Loader, paths []types.PatchStrategicMerge) ([]*Resource, error) { |
| 96 |
var result []*Resource |
| 97 |
for _, path := range paths { |
| 98 |
content, err := ldr.Load(string(path)) |
| 99 |
if err != nil { |
| 100 |
return nil, err |
| 101 |
} |
| 102 |
res, err := rf.SliceFromBytes(content) |
| 103 |
if err != nil { |
| 104 |
return nil, kusterr.Handler(err, string(path)) |
| 105 |
} |
| 106 |
result = append(result, res...) |
| 107 |
} |
| 108 |
return result, nil |
| 109 |
} |
| 110 |
|
| 111 |
// FromBytes unmarshalls bytes into one Resource. |
| 112 |
func (rf *Factory) FromBytes(in []byte) (*Resource, error) { |
| 113 |
result, err := rf.SliceFromBytes(in) |
| 114 |
if err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
if len(result) != 1 { |
| 118 |
return nil, fmt.Errorf( |
| 119 |
"expected 1 resource, found %d in %v", len(result), in) |
| 120 |
} |
| 121 |
return result[0], nil |
| 122 |
} |
| 123 |
|
| 124 |
// SliceFromBytes unmarshals bytes into a Resource slice. |
| 125 |
func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) { |
| 126 |
nodes, err := rf.RNodesFromBytes(in) |
| 127 |
if err != nil { |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
return rf.resourcesFromRNodes(nodes), nil |
| 131 |
} |
| 132 |
|
| 133 |
// DropLocalNodes removes the local nodes by default. Local nodes are detected via the annotation `config.kubernetes.io/local-config: "true"` |
| 134 |
func (rf *Factory) DropLocalNodes(nodes []*yaml.RNode) ([]*Resource, error) { |
| 135 |
var result []*yaml.RNode |
| 136 |
for _, node := range nodes { |
| 137 |
if node.IsNilOrEmpty() { |
| 138 |
continue |
| 139 |
} |
| 140 |
md, err := node.GetValidatedMetadata() |
| 141 |
if err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
|
| 145 |
if rf.IncludeLocalConfigs { |
| 146 |
result = append(result, node) |
| 147 |
continue |
| 148 |
} |
| 149 |
localConfig, exist := md.ObjectMeta.Annotations[konfig.IgnoredByKustomizeAnnotation] |
| 150 |
if !exist || localConfig == "false" { |
| 151 |
result = append(result, node) |
| 152 |
} |
| 153 |
} |
| 154 |
return rf.resourcesFromRNodes(result), nil |
| 155 |
} |
| 156 |
|
| 157 |
// ResourcesFromRNodes converts RNodes to Resources. |
| 158 |
func (rf *Factory) ResourcesFromRNodes( |
| 159 |
nodes []*yaml.RNode) (result []*Resource, err error) { |
| 160 |
return rf.DropLocalNodes(nodes) |
| 161 |
} |
| 162 |
|
| 163 |
// resourcesFromRNode assumes all nodes are good. |
| 164 |
func (rf *Factory) resourcesFromRNodes( |
| 165 |
nodes []*yaml.RNode) (result []*Resource) { |
| 166 |
for _, n := range nodes { |
| 167 |
result = append(result, rf.makeOne(n, nil)) |
| 168 |
} |
| 169 |
return |
| 170 |
} |
| 171 |
|
| 172 |
func (rf *Factory) RNodesFromBytes(b []byte) ([]*yaml.RNode, error) { |
| 173 |
nodes, err := kio.FromBytes(b) |
| 174 |
if err != nil { |
| 175 |
return nil, err |
| 176 |
} |
| 177 |
nodes, err = rf.dropBadNodes(nodes) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
return rf.inlineAnyEmbeddedLists(nodes) |
| 182 |
} |
| 183 |
|
| 184 |
// inlineAnyEmbeddedLists scans the RNode slice for nodes named FooList. |
| 185 |
// Such nodes are expected to be lists of resources, each of type Foo. |
| 186 |
// These lists are replaced in the result by their inlined resources. |
| 187 |
func (rf *Factory) inlineAnyEmbeddedLists( |
| 188 |
nodes []*yaml.RNode) (result []*yaml.RNode, err error) { |
| 189 |
var n0 *yaml.RNode |
| 190 |
for len(nodes) > 0 { |
| 191 |
n0, nodes = nodes[0], nodes[1:] |
| 192 |
kind := n0.GetKind() |
| 193 |
if !strings.HasSuffix(kind, "List") { |
| 194 |
result = append(result, n0) |
| 195 |
continue |
| 196 |
} |
| 197 |
// Convert a FooList into a slice of Foo. |
| 198 |
var m map[string]interface{} |
| 199 |
m, err = n0.Map() |
| 200 |
if err != nil { |
| 201 |
return nil, fmt.Errorf("trouble expanding list of %s; %w", kind, err) |
| 202 |
} |
| 203 |
items, ok := m["items"] |
| 204 |
if !ok { |
| 205 |
// Items field is not present. |
| 206 |
// This is not a collections resource. |
| 207 |
// read more https://kubernetes.io/docs/reference/using-api/api-concepts/#collections |
| 208 |
result = append(result, n0) |
| 209 |
continue |
| 210 |
} |
| 211 |
slice, ok := items.([]interface{}) |
| 212 |
if !ok { |
| 213 |
if items == nil { |
| 214 |
// an empty list |
| 215 |
continue |
| 216 |
} |
| 217 |
return nil, fmt.Errorf( |
| 218 |
"expected array in %s/items, but found %T", kind, items) |
| 219 |
} |
| 220 |
innerNodes, err := rf.convertObjectSliceToNodeSlice(slice) |
| 221 |
if err != nil { |
| 222 |
return nil, err |
| 223 |
} |
| 224 |
nodes = append(nodes, innerNodes...) |
| 225 |
} |
| 226 |
return result, nil |
| 227 |
} |
| 228 |
|
| 229 |
// convertObjectSlice converts a list of objects to a list of RNode. |
| 230 |
func (rf *Factory) convertObjectSliceToNodeSlice( |
| 231 |
objects []interface{}) (result []*yaml.RNode, err error) { |
| 232 |
var bytes []byte |
| 233 |
var nodes []*yaml.RNode |
| 234 |
for _, obj := range objects { |
| 235 |
bytes, err = json.Marshal(obj) |
| 236 |
if err != nil { |
| 237 |
return |
| 238 |
} |
| 239 |
nodes, err = kio.FromBytes(bytes) |
| 240 |
if err != nil { |
| 241 |
return |
| 242 |
} |
| 243 |
nodes, err = rf.dropBadNodes(nodes) |
| 244 |
if err != nil { |
| 245 |
return |
| 246 |
} |
| 247 |
result = append(result, nodes...) |
| 248 |
} |
| 249 |
return |
| 250 |
} |
| 251 |
|
| 252 |
// dropBadNodes may drop some nodes from its input argument. |
| 253 |
func (rf *Factory) dropBadNodes(nodes []*yaml.RNode) ([]*yaml.RNode, error) { |
| 254 |
var result []*yaml.RNode |
| 255 |
for _, n := range nodes { |
| 256 |
if n.IsNilOrEmpty() { |
| 257 |
continue |
| 258 |
} |
| 259 |
if _, err := n.GetValidatedMetadata(); err != nil { |
| 260 |
return nil, err |
| 261 |
} |
| 262 |
if foundNil, path := n.HasNilEntryInList(); foundNil { |
| 263 |
return nil, fmt.Errorf("empty item at %v in object %v", path, n) |
| 264 |
} |
| 265 |
result = append(result, n) |
| 266 |
} |
| 267 |
return result, nil |
| 268 |
} |
| 269 |
|
| 270 |
// SliceFromBytesWithNames unmarshals bytes into a Resource slice with specified original |
| 271 |
// name. |
| 272 |
func (rf *Factory) SliceFromBytesWithNames(names []string, in []byte) ([]*Resource, error) { |
| 273 |
result, err := rf.SliceFromBytes(in) |
| 274 |
if err != nil { |
| 275 |
return nil, err |
| 276 |
} |
| 277 |
if len(names) != len(result) { |
| 278 |
return nil, fmt.Errorf("number of names doesn't match number of resources") |
| 279 |
} |
| 280 |
for i, res := range result { |
| 281 |
res.setPreviousId(resid.DefaultNamespace, names[i], res.GetKind()) |
| 282 |
} |
| 283 |
return result, nil |
| 284 |
} |
| 285 |
|
| 286 |
// MakeConfigMap makes an instance of Resource for ConfigMap |
| 287 |
func (rf *Factory) MakeConfigMap(kvLdr ifc.KvLoader, args *types.ConfigMapArgs) (*Resource, error) { |
| 288 |
rn, err := generators.MakeConfigMap(kvLdr, args) |
| 289 |
if err != nil { |
| 290 |
return nil, err |
| 291 |
} |
| 292 |
return rf.makeOne(rn, &args.GeneratorArgs), nil |
| 293 |
} |
| 294 |
|
| 295 |
// MakeSecret makes an instance of Resource for Secret |
| 296 |
func (rf *Factory) MakeSecret(kvLdr ifc.KvLoader, args *types.SecretArgs) (*Resource, error) { |
| 297 |
rn, err := generators.MakeSecret(kvLdr, args) |
| 298 |
if err != nil { |
| 299 |
return nil, err |
| 300 |
} |
| 301 |
return rf.makeOne(rn, &args.GeneratorArgs), nil |
| 302 |
} |