| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resmap |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
|
| 11 |
"sigs.k8s.io/kustomize/api/filters/annotations" |
| 12 |
"sigs.k8s.io/kustomize/api/resource" |
| 13 |
"sigs.k8s.io/kustomize/api/types" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 15 |
"sigs.k8s.io/kustomize/kyaml/kio" |
| 16 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 17 |
kyaml "sigs.k8s.io/kustomize/kyaml/yaml" |
| 18 |
) |
| 19 |
|
| 20 |
// resWrangler implements ResMap. |
| 21 |
type resWrangler struct { |
| 22 |
// Resource list maintained in load (append) order. |
| 23 |
// This is important for transformers, which must |
| 24 |
// be performed in a specific order, and for users |
| 25 |
// who for whatever reasons wish the order they |
| 26 |
// specify in kustomizations to be maintained and |
| 27 |
// available as an option for final YAML rendering. |
| 28 |
rList []*resource.Resource |
| 29 |
} |
| 30 |
|
| 31 |
func newOne() *resWrangler { |
| 32 |
result := &resWrangler{} |
| 33 |
result.Clear() |
| 34 |
return result |
| 35 |
} |
| 36 |
|
| 37 |
// Clear implements ResMap. |
| 38 |
func (m *resWrangler) Clear() { |
| 39 |
m.rList = nil |
| 40 |
} |
| 41 |
|
| 42 |
// DropEmpties quickly drops empty resources. |
| 43 |
// It doesn't use Append, which checks for Id collisions. |
| 44 |
func (m *resWrangler) DropEmpties() { |
| 45 |
var rList []*resource.Resource |
| 46 |
for _, r := range m.rList { |
| 47 |
if !r.IsNilOrEmpty() { |
| 48 |
rList = append(rList, r) |
| 49 |
} |
| 50 |
} |
| 51 |
m.rList = rList |
| 52 |
} |
| 53 |
|
| 54 |
// Size implements ResMap. |
| 55 |
func (m *resWrangler) Size() int { |
| 56 |
return len(m.rList) |
| 57 |
} |
| 58 |
|
| 59 |
func (m *resWrangler) indexOfResource(other *resource.Resource) int { |
| 60 |
for i, r := range m.rList { |
| 61 |
if r == other { |
| 62 |
return i |
| 63 |
} |
| 64 |
} |
| 65 |
return -1 |
| 66 |
} |
| 67 |
|
| 68 |
// Resources implements ResMap. |
| 69 |
func (m *resWrangler) Resources() []*resource.Resource { |
| 70 |
tmp := make([]*resource.Resource, len(m.rList)) |
| 71 |
copy(tmp, m.rList) |
| 72 |
return tmp |
| 73 |
} |
| 74 |
|
| 75 |
// Append implements ResMap. |
| 76 |
func (m *resWrangler) Append(res *resource.Resource) error { |
| 77 |
id := res.CurId() |
| 78 |
if r := m.GetMatchingResourcesByCurrentId(id.Equals); len(r) > 0 { |
| 79 |
return fmt.Errorf( |
| 80 |
"may not add resource with an already registered id: %s", id) |
| 81 |
} |
| 82 |
m.append(res) |
| 83 |
return nil |
| 84 |
} |
| 85 |
|
| 86 |
// append appends without performing an Id check |
| 87 |
func (m *resWrangler) append(res *resource.Resource) { |
| 88 |
m.rList = append(m.rList, res) |
| 89 |
} |
| 90 |
|
| 91 |
// Remove implements ResMap. |
| 92 |
func (m *resWrangler) Remove(adios resid.ResId) error { |
| 93 |
var rList []*resource.Resource |
| 94 |
for _, r := range m.rList { |
| 95 |
if r.CurId() != adios { |
| 96 |
rList = append(rList, r) |
| 97 |
} |
| 98 |
} |
| 99 |
if len(rList) != m.Size()-1 { |
| 100 |
return fmt.Errorf("id %s not found in removal", adios) |
| 101 |
} |
| 102 |
m.rList = rList |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
// Replace implements ResMap. |
| 107 |
func (m *resWrangler) Replace(res *resource.Resource) (int, error) { |
| 108 |
id := res.CurId() |
| 109 |
i, err := m.GetIndexOfCurrentId(id) |
| 110 |
if err != nil { |
| 111 |
return -1, errors.WrapPrefixf(err, "in Replace") |
| 112 |
} |
| 113 |
if i < 0 { |
| 114 |
return -1, fmt.Errorf("cannot find resource with id %s to replace", id) |
| 115 |
} |
| 116 |
m.rList[i] = res |
| 117 |
return i, nil |
| 118 |
} |
| 119 |
|
| 120 |
// AllIds implements ResMap. |
| 121 |
func (m *resWrangler) AllIds() (ids []resid.ResId) { |
| 122 |
ids = make([]resid.ResId, m.Size()) |
| 123 |
for i, r := range m.rList { |
| 124 |
ids[i] = r.CurId() |
| 125 |
} |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
// Debug implements ResMap. |
| 130 |
func (m *resWrangler) Debug(title string) { |
| 131 |
fmt.Println("--------------------------- " + title) |
| 132 |
firstObj := true |
| 133 |
for i, r := range m.rList { |
| 134 |
if firstObj { |
| 135 |
firstObj = false |
| 136 |
} else { |
| 137 |
fmt.Println("---") |
| 138 |
} |
| 139 |
fmt.Printf("# %d %s\n%s\n", i, r.OrgId(), r.String()) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
type IdMatcher func(resid.ResId) bool |
| 144 |
|
| 145 |
// GetByIndex implements ResMap. |
| 146 |
func (m *resWrangler) GetByIndex(i int) *resource.Resource { |
| 147 |
if i < 0 || i >= m.Size() { |
| 148 |
return nil |
| 149 |
} |
| 150 |
return m.rList[i] |
| 151 |
} |
| 152 |
|
| 153 |
// GetIndexOfCurrentId implements ResMap. |
| 154 |
func (m *resWrangler) GetIndexOfCurrentId(id resid.ResId) (int, error) { |
| 155 |
count := 0 |
| 156 |
result := -1 |
| 157 |
for i, r := range m.rList { |
| 158 |
if id.Equals(r.CurId()) { |
| 159 |
count++ |
| 160 |
result = i |
| 161 |
} |
| 162 |
} |
| 163 |
if count > 1 { |
| 164 |
return -1, fmt.Errorf("id matched %d resources", count) |
| 165 |
} |
| 166 |
return result, nil |
| 167 |
} |
| 168 |
|
| 169 |
type IdFromResource func(r *resource.Resource) resid.ResId |
| 170 |
|
| 171 |
func GetCurrentId(r *resource.Resource) resid.ResId { return r.CurId() } |
| 172 |
|
| 173 |
// GetMatchingResourcesByCurrentId implements ResMap. |
| 174 |
func (m *resWrangler) GetMatchingResourcesByCurrentId( |
| 175 |
matches IdMatcher) []*resource.Resource { |
| 176 |
return m.filteredById(matches, GetCurrentId) |
| 177 |
} |
| 178 |
|
| 179 |
// GetMatchingResourcesByAnyId implements ResMap. |
| 180 |
func (m *resWrangler) GetMatchingResourcesByAnyId( |
| 181 |
matches IdMatcher) []*resource.Resource { |
| 182 |
var result []*resource.Resource |
| 183 |
for _, r := range m.rList { |
| 184 |
if r.RNode.IsNilOrEmpty() { |
| 185 |
continue |
| 186 |
} |
| 187 |
|
| 188 |
for _, id := range append(r.PrevIds(), r.CurId()) { |
| 189 |
if matches(id) { |
| 190 |
result = append(result, r) |
| 191 |
break |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
return result |
| 196 |
} |
| 197 |
|
| 198 |
func (m *resWrangler) filteredById( |
| 199 |
matches IdMatcher, idGetter IdFromResource) []*resource.Resource { |
| 200 |
var result []*resource.Resource |
| 201 |
for _, r := range m.rList { |
| 202 |
if matches(idGetter(r)) { |
| 203 |
result = append(result, r) |
| 204 |
} |
| 205 |
} |
| 206 |
return result |
| 207 |
} |
| 208 |
|
| 209 |
// GetByCurrentId implements ResMap. |
| 210 |
func (m *resWrangler) GetByCurrentId( |
| 211 |
id resid.ResId) (*resource.Resource, error) { |
| 212 |
return demandOneMatch(m.GetMatchingResourcesByCurrentId, id, "Current") |
| 213 |
} |
| 214 |
|
| 215 |
// GetById implements ResMap. |
| 216 |
func (m *resWrangler) GetById( |
| 217 |
id resid.ResId) (*resource.Resource, error) { |
| 218 |
r, err := demandOneMatch(m.GetMatchingResourcesByAnyId, id, "Id") |
| 219 |
if err != nil { |
| 220 |
return nil, fmt.Errorf( |
| 221 |
"%s; failed to find unique target for patch %s", |
| 222 |
err.Error(), id.String()) |
| 223 |
} |
| 224 |
return r, nil |
| 225 |
} |
| 226 |
|
| 227 |
type resFinder func(IdMatcher) []*resource.Resource |
| 228 |
|
| 229 |
func demandOneMatch( |
| 230 |
f resFinder, id resid.ResId, s string) (*resource.Resource, error) { |
| 231 |
r := f(id.Equals) |
| 232 |
if len(r) == 1 { |
| 233 |
return r[0], nil |
| 234 |
} |
| 235 |
if len(r) > 1 { |
| 236 |
return nil, fmt.Errorf("multiple matches for %s %s", s, id) |
| 237 |
} |
| 238 |
return nil, fmt.Errorf("no matches for %s %s", s, id) |
| 239 |
} |
| 240 |
|
| 241 |
// GroupedByCurrentNamespace implements ResMap. |
| 242 |
func (m *resWrangler) GroupedByCurrentNamespace() map[string][]*resource.Resource { |
| 243 |
items := m.groupedByCurrentNamespace() |
| 244 |
delete(items, resid.TotallyNotANamespace) |
| 245 |
return items |
| 246 |
} |
| 247 |
|
| 248 |
// ClusterScoped implements ResMap. |
| 249 |
func (m *resWrangler) ClusterScoped() []*resource.Resource { |
| 250 |
return m.groupedByCurrentNamespace()[resid.TotallyNotANamespace] |
| 251 |
} |
| 252 |
|
| 253 |
func (m *resWrangler) groupedByCurrentNamespace() map[string][]*resource.Resource { |
| 254 |
byNamespace := make(map[string][]*resource.Resource) |
| 255 |
for _, res := range m.rList { |
| 256 |
namespace := res.CurId().EffectiveNamespace() |
| 257 |
if _, found := byNamespace[namespace]; !found { |
| 258 |
byNamespace[namespace] = []*resource.Resource{} |
| 259 |
} |
| 260 |
byNamespace[namespace] = append(byNamespace[namespace], res) |
| 261 |
} |
| 262 |
return byNamespace |
| 263 |
} |
| 264 |
|
| 265 |
// GroupedByOriginalNamespace implements ResMap. |
| 266 |
func (m *resWrangler) GroupedByOriginalNamespace() map[string][]*resource.Resource { |
| 267 |
items := m.groupedByOriginalNamespace() |
| 268 |
delete(items, resid.TotallyNotANamespace) |
| 269 |
return items |
| 270 |
} |
| 271 |
|
| 272 |
func (m *resWrangler) groupedByOriginalNamespace() map[string][]*resource.Resource { |
| 273 |
byNamespace := make(map[string][]*resource.Resource) |
| 274 |
for _, res := range m.rList { |
| 275 |
namespace := res.OrgId().EffectiveNamespace() |
| 276 |
if _, found := byNamespace[namespace]; !found { |
| 277 |
byNamespace[namespace] = []*resource.Resource{} |
| 278 |
} |
| 279 |
byNamespace[namespace] = append(byNamespace[namespace], res) |
| 280 |
} |
| 281 |
return byNamespace |
| 282 |
} |
| 283 |
|
| 284 |
// AsYaml implements ResMap. |
| 285 |
func (m *resWrangler) AsYaml() ([]byte, error) { |
| 286 |
firstObj := true |
| 287 |
var b []byte |
| 288 |
buf := bytes.NewBuffer(b) |
| 289 |
for _, res := range m.rList { |
| 290 |
out, err := res.AsYAML() |
| 291 |
if err != nil { |
| 292 |
m, _ := res.Map() |
| 293 |
return nil, errors.WrapPrefixf(err, "%#v", m) |
| 294 |
} |
| 295 |
if firstObj { |
| 296 |
firstObj = false |
| 297 |
} else { |
| 298 |
if _, err = buf.WriteString("---\n"); err != nil { |
| 299 |
return nil, err |
| 300 |
} |
| 301 |
} |
| 302 |
if _, err = buf.Write(out); err != nil { |
| 303 |
return nil, err |
| 304 |
} |
| 305 |
} |
| 306 |
return buf.Bytes(), nil |
| 307 |
} |
| 308 |
|
| 309 |
// ErrorIfNotEqualSets implements ResMap. |
| 310 |
func (m *resWrangler) ErrorIfNotEqualSets(other ResMap) error { |
| 311 |
m2, ok := other.(*resWrangler) |
| 312 |
if !ok { |
| 313 |
return fmt.Errorf("bad cast to resWrangler 1") |
| 314 |
} |
| 315 |
if m.Size() != m2.Size() { |
| 316 |
return fmt.Errorf( |
| 317 |
"lists have different number of entries: %#v doesn't equal %#v", |
| 318 |
m.rList, m2.rList) |
| 319 |
} |
| 320 |
seen := make(map[int]bool) |
| 321 |
for _, r1 := range m.rList { |
| 322 |
id := r1.CurId() |
| 323 |
others := m2.GetMatchingResourcesByCurrentId(id.Equals) |
| 324 |
if len(others) == 0 { |
| 325 |
return fmt.Errorf( |
| 326 |
"id in self missing from other; id: %s", id) |
| 327 |
} |
| 328 |
if len(others) > 1 { |
| 329 |
return fmt.Errorf( |
| 330 |
"id in self matches %d in other; id: %s", len(others), id) |
| 331 |
} |
| 332 |
r2 := others[0] |
| 333 |
if !reflect.DeepEqual(r1.RNode, r2.RNode) { |
| 334 |
return fmt.Errorf( |
| 335 |
"nodes unequal: \n -- %s,\n -- %s\n\n--\n%#v\n------\n%#v\n", |
| 336 |
r1, r2, r1, r2) |
| 337 |
} |
| 338 |
seen[m2.indexOfResource(r2)] = true |
| 339 |
} |
| 340 |
if len(seen) != m.Size() { |
| 341 |
return fmt.Errorf("counting problem %d != %d", len(seen), m.Size()) |
| 342 |
} |
| 343 |
return nil |
| 344 |
} |
| 345 |
|
| 346 |
// ErrorIfNotEqualLists implements ResMap. |
| 347 |
func (m *resWrangler) ErrorIfNotEqualLists(other ResMap) error { |
| 348 |
m2, ok := other.(*resWrangler) |
| 349 |
if !ok { |
| 350 |
return fmt.Errorf("bad cast to resWrangler 2") |
| 351 |
} |
| 352 |
if m.Size() != m2.Size() { |
| 353 |
return fmt.Errorf( |
| 354 |
"lists have different number of entries: %#v doesn't equal %#v", |
| 355 |
m.rList, m2.rList) |
| 356 |
} |
| 357 |
for i, r1 := range m.rList { |
| 358 |
r2 := m2.rList[i] |
| 359 |
if err := r1.ErrIfNotEquals(r2); err != nil { |
| 360 |
return err |
| 361 |
} |
| 362 |
} |
| 363 |
return nil |
| 364 |
} |
| 365 |
|
| 366 |
type resCopier func(r *resource.Resource) *resource.Resource |
| 367 |
|
| 368 |
// ShallowCopy implements ResMap. |
| 369 |
func (m *resWrangler) ShallowCopy() ResMap { |
| 370 |
return m.makeCopy( |
| 371 |
func(r *resource.Resource) *resource.Resource { |
| 372 |
return r |
| 373 |
}) |
| 374 |
} |
| 375 |
|
| 376 |
// DeepCopy implements ResMap. |
| 377 |
func (m *resWrangler) DeepCopy() ResMap { |
| 378 |
return m.makeCopy( |
| 379 |
func(r *resource.Resource) *resource.Resource { |
| 380 |
return r.DeepCopy() |
| 381 |
}) |
| 382 |
} |
| 383 |
|
| 384 |
// makeCopy copies the ResMap. |
| 385 |
func (m *resWrangler) makeCopy(copier resCopier) ResMap { |
| 386 |
result := &resWrangler{} |
| 387 |
result.rList = make([]*resource.Resource, m.Size()) |
| 388 |
for i, r := range m.rList { |
| 389 |
result.rList[i] = copier(r) |
| 390 |
} |
| 391 |
return result |
| 392 |
} |
| 393 |
|
| 394 |
// SubsetThatCouldBeReferencedByResource implements ResMap. |
| 395 |
func (m *resWrangler) SubsetThatCouldBeReferencedByResource( |
| 396 |
referrer *resource.Resource) (ResMap, error) { |
| 397 |
referrerId := referrer.CurId() |
| 398 |
if referrerId.IsClusterScoped() { |
| 399 |
// A cluster scoped resource can refer to anything. |
| 400 |
return m, nil |
| 401 |
} |
| 402 |
result := newOne() |
| 403 |
roleBindingNamespaces, err := getNamespacesForRoleBinding(referrer) |
| 404 |
if err != nil { |
| 405 |
return nil, err |
| 406 |
} |
| 407 |
for _, possibleTarget := range m.rList { |
| 408 |
id := possibleTarget.CurId() |
| 409 |
if id.IsClusterScoped() { |
| 410 |
// A cluster-scoped resource can be referred to by anything. |
| 411 |
result.append(possibleTarget) |
| 412 |
continue |
| 413 |
} |
| 414 |
if id.IsNsEquals(referrerId) { |
| 415 |
// The two objects are in the same namespace. |
| 416 |
result.append(possibleTarget) |
| 417 |
continue |
| 418 |
} |
| 419 |
// The two objects are namespaced (not cluster-scoped), AND |
| 420 |
// are in different namespaces. |
| 421 |
// There's still a chance they can refer to each other. |
| 422 |
if roleBindingNamespaces[possibleTarget.GetNamespace()] { |
| 423 |
result.append(possibleTarget) |
| 424 |
} |
| 425 |
} |
| 426 |
return result, nil |
| 427 |
} |
| 428 |
|
| 429 |
// getNamespacesForRoleBinding returns referenced ServiceAccount namespaces |
| 430 |
// if the resource is a RoleBinding |
| 431 |
func getNamespacesForRoleBinding(r *resource.Resource) (map[string]bool, error) { |
| 432 |
result := make(map[string]bool) |
| 433 |
if r.GetKind() != "RoleBinding" { |
| 434 |
return result, nil |
| 435 |
} |
| 436 |
subjects, err := r.GetSlice("subjects") |
| 437 |
if err != nil || subjects == nil { |
| 438 |
return result, nil |
| 439 |
} |
| 440 |
for _, s := range subjects { |
| 441 |
subject := s.(map[string]interface{}) |
| 442 |
if ns, ok1 := subject["namespace"]; ok1 { |
| 443 |
if kind, ok2 := subject["kind"]; ok2 { |
| 444 |
if kind.(string) == "ServiceAccount" { |
| 445 |
if n, ok3 := ns.(string); ok3 { |
| 446 |
result[n] = true |
| 447 |
} else { |
| 448 |
return nil, errors.Errorf("Invalid Input: namespace is blank for resource %q\n", r.CurId()) |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
return result, nil |
| 455 |
} |
| 456 |
|
| 457 |
// AppendAll implements ResMap. |
| 458 |
func (m *resWrangler) AppendAll(other ResMap) error { |
| 459 |
if other == nil { |
| 460 |
return nil |
| 461 |
} |
| 462 |
m2, ok := other.(*resWrangler) |
| 463 |
if !ok { |
| 464 |
return fmt.Errorf("bad cast to resWrangler 3") |
| 465 |
} |
| 466 |
return m.appendAll(m2.rList) |
| 467 |
} |
| 468 |
|
| 469 |
// appendAll appends all the resources, error on Id collision. |
| 470 |
func (m *resWrangler) appendAll(list []*resource.Resource) error { |
| 471 |
for _, res := range list { |
| 472 |
if err := m.Append(res); err != nil { |
| 473 |
return err |
| 474 |
} |
| 475 |
} |
| 476 |
return nil |
| 477 |
} |
| 478 |
|
| 479 |
// AbsorbAll implements ResMap. |
| 480 |
func (m *resWrangler) AbsorbAll(other ResMap) error { |
| 481 |
if other == nil { |
| 482 |
return nil |
| 483 |
} |
| 484 |
m2, ok := other.(*resWrangler) |
| 485 |
if !ok { |
| 486 |
return fmt.Errorf("bad cast to resWrangler 4") |
| 487 |
} |
| 488 |
for _, r := range m2.rList { |
| 489 |
err := m.appendReplaceOrMerge(r) |
| 490 |
if err != nil { |
| 491 |
return err |
| 492 |
} |
| 493 |
} |
| 494 |
return nil |
| 495 |
} |
| 496 |
|
| 497 |
// AddOriginAnnotation implements ResMap. |
| 498 |
func (m *resWrangler) AddOriginAnnotation(origin *resource.Origin) error { |
| 499 |
if origin == nil { |
| 500 |
return nil |
| 501 |
} |
| 502 |
for _, res := range m.rList { |
| 503 |
or, err := res.GetOrigin() |
| 504 |
if or != nil || err != nil { |
| 505 |
// if any resources already have an origin annotation, |
| 506 |
// skip it |
| 507 |
continue |
| 508 |
} |
| 509 |
if err := res.SetOrigin(origin); err != nil { |
| 510 |
return err |
| 511 |
} |
| 512 |
} |
| 513 |
return nil |
| 514 |
} |
| 515 |
|
| 516 |
// RemoveOriginAnnotation implements ResMap |
| 517 |
func (m *resWrangler) RemoveOriginAnnotations() error { |
| 518 |
for _, res := range m.rList { |
| 519 |
if err := res.SetOrigin(nil); err != nil { |
| 520 |
return err |
| 521 |
} |
| 522 |
} |
| 523 |
return nil |
| 524 |
} |
| 525 |
|
| 526 |
// AddTransformerAnnotation implements ResMap |
| 527 |
func (m *resWrangler) AddTransformerAnnotation(origin *resource.Origin) error { |
| 528 |
for _, res := range m.rList { |
| 529 |
or, err := res.GetOrigin() |
| 530 |
if err != nil { |
| 531 |
return err |
| 532 |
} |
| 533 |
if or == nil { |
| 534 |
// the resource does not have an origin annotation, so |
| 535 |
// we assume that the transformer generated the resource |
| 536 |
// rather than modifying it |
| 537 |
err = res.SetOrigin(origin) |
| 538 |
} else { |
| 539 |
// the resource already has an origin annotation, so we |
| 540 |
// record the provided origin as a transformation |
| 541 |
err = res.AddTransformation(origin) |
| 542 |
} |
| 543 |
if err != nil { |
| 544 |
return err |
| 545 |
} |
| 546 |
} |
| 547 |
return nil |
| 548 |
} |
| 549 |
|
| 550 |
// RemoveTransformerAnnotations implements ResMap |
| 551 |
func (m *resWrangler) RemoveTransformerAnnotations() error { |
| 552 |
for _, res := range m.rList { |
| 553 |
if err := res.ClearTransformations(); err != nil { |
| 554 |
return err |
| 555 |
} |
| 556 |
} |
| 557 |
return nil |
| 558 |
} |
| 559 |
|
| 560 |
func (m *resWrangler) appendReplaceOrMerge(res *resource.Resource) error { |
| 561 |
id := res.CurId() |
| 562 |
matches := m.GetMatchingResourcesByAnyId(id.Equals) |
| 563 |
switch len(matches) { |
| 564 |
case 0: |
| 565 |
switch res.Behavior() { |
| 566 |
case types.BehaviorMerge, types.BehaviorReplace: |
| 567 |
return fmt.Errorf( |
| 568 |
"id %#v does not exist; cannot merge or replace", id) |
| 569 |
default: |
| 570 |
// presumably types.BehaviorCreate |
| 571 |
return m.Append(res) |
| 572 |
} |
| 573 |
case 1: |
| 574 |
old := matches[0] |
| 575 |
if old == nil { |
| 576 |
return fmt.Errorf("id lookup failure") |
| 577 |
} |
| 578 |
index := m.indexOfResource(old) |
| 579 |
if index < 0 { |
| 580 |
return fmt.Errorf("indexing problem") |
| 581 |
} |
| 582 |
switch res.Behavior() { |
| 583 |
case types.BehaviorReplace: |
| 584 |
res.CopyMergeMetaDataFieldsFrom(old) |
| 585 |
case types.BehaviorMerge: |
| 586 |
// ensure the origin annotation doesn't get overwritten |
| 587 |
orig, err := old.GetOrigin() |
| 588 |
if err != nil { |
| 589 |
return err |
| 590 |
} |
| 591 |
res.CopyMergeMetaDataFieldsFrom(old) |
| 592 |
res.MergeDataMapFrom(old) |
| 593 |
res.MergeBinaryDataMapFrom(old) |
| 594 |
if orig != nil { |
| 595 |
res.SetOrigin(orig) |
| 596 |
} |
| 597 |
|
| 598 |
default: |
| 599 |
return fmt.Errorf( |
| 600 |
"id %#v exists; can not use behavior: '%s', behavior must be merge or replace", id, res.Behavior()) |
| 601 |
} |
| 602 |
i, err := m.Replace(res) |
| 603 |
if err != nil { |
| 604 |
return err |
| 605 |
} |
| 606 |
if i != index { |
| 607 |
return fmt.Errorf("unexpected target index in replacement") |
| 608 |
} |
| 609 |
return nil |
| 610 |
default: |
| 611 |
return fmt.Errorf( |
| 612 |
"found multiple objects %v that could accept merge of %v", |
| 613 |
matches, id) |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
// AnnotateAll implements ResMap |
| 618 |
func (m *resWrangler) AnnotateAll(key string, value string) error { |
| 619 |
return m.ApplyFilter(annotations.Filter{ |
| 620 |
Annotations: map[string]string{ |
| 621 |
key: value, |
| 622 |
}, |
| 623 |
FsSlice: []types.FieldSpec{{ |
| 624 |
Path: "metadata/annotations", |
| 625 |
CreateIfNotPresent: true, |
| 626 |
}}, |
| 627 |
}) |
| 628 |
} |
| 629 |
|
| 630 |
// Select returns a list of resources that |
| 631 |
// are selected by a Selector |
| 632 |
func (m *resWrangler) Select(s types.Selector) ([]*resource.Resource, error) { |
| 633 |
var result []*resource.Resource |
| 634 |
sr, err := types.NewSelectorRegex(&s) |
| 635 |
if err != nil { |
| 636 |
return nil, err |
| 637 |
} |
| 638 |
for _, r := range m.rList { |
| 639 |
curId := r.CurId() |
| 640 |
orgId := r.OrgId() |
| 641 |
|
| 642 |
// It first tries to match with the original namespace |
| 643 |
// then matches with the current namespace |
| 644 |
if !sr.MatchNamespace(orgId.EffectiveNamespace()) && |
| 645 |
!sr.MatchNamespace(curId.EffectiveNamespace()) { |
| 646 |
continue |
| 647 |
} |
| 648 |
|
| 649 |
// It first tries to match with the original name |
| 650 |
// then matches with the current name |
| 651 |
if !sr.MatchName(orgId.Name) && |
| 652 |
!sr.MatchName(curId.Name) { |
| 653 |
continue |
| 654 |
} |
| 655 |
|
| 656 |
// matches the GVK |
| 657 |
if !sr.MatchGvk(r.GetGvk()) { |
| 658 |
continue |
| 659 |
} |
| 660 |
|
| 661 |
// matches the label selector |
| 662 |
matched, err := r.MatchesLabelSelector(s.LabelSelector) |
| 663 |
if err != nil { |
| 664 |
return nil, err |
| 665 |
} |
| 666 |
if !matched { |
| 667 |
continue |
| 668 |
} |
| 669 |
|
| 670 |
// matches the annotation selector |
| 671 |
matched, err = r.MatchesAnnotationSelector(s.AnnotationSelector) |
| 672 |
if err != nil { |
| 673 |
return nil, err |
| 674 |
} |
| 675 |
if !matched { |
| 676 |
continue |
| 677 |
} |
| 678 |
result = append(result, r) |
| 679 |
} |
| 680 |
return result, nil |
| 681 |
} |
| 682 |
|
| 683 |
// ToRNodeSlice returns a copy of the resources as RNodes. |
| 684 |
func (m *resWrangler) ToRNodeSlice() []*kyaml.RNode { |
| 685 |
result := make([]*kyaml.RNode, len(m.rList)) |
| 686 |
for i := range m.rList { |
| 687 |
result[i] = m.rList[i].Copy() |
| 688 |
} |
| 689 |
return result |
| 690 |
} |
| 691 |
|
| 692 |
// DeAnchor implements ResMap. |
| 693 |
func (m *resWrangler) DeAnchor() (err error) { |
| 694 |
for i := range m.rList { |
| 695 |
if err = m.rList[i].DeAnchor(); err != nil { |
| 696 |
return err |
| 697 |
} |
| 698 |
} |
| 699 |
return nil |
| 700 |
} |
| 701 |
|
| 702 |
// ApplySmPatch applies the patch, and errors on Id collisions. |
| 703 |
func (m *resWrangler) ApplySmPatch(selectedSet *resource.IdSet, patch *resource.Resource) error { |
| 704 |
var list []*resource.Resource |
| 705 |
for _, res := range m.rList { |
| 706 |
if selectedSet.Contains(res.CurId()) { |
| 707 |
patchCopy := patch.DeepCopy() |
| 708 |
patchCopy.CopyMergeMetaDataFieldsFrom(patch) |
| 709 |
patchCopy.SetGvk(res.GetGvk()) |
| 710 |
patchCopy.SetKind(patch.GetKind()) |
| 711 |
if err := res.ApplySmPatch(patchCopy); err != nil { |
| 712 |
return err |
| 713 |
} |
| 714 |
} |
| 715 |
if !res.IsNilOrEmpty() { |
| 716 |
list = append(list, res) |
| 717 |
} |
| 718 |
} |
| 719 |
m.Clear() |
| 720 |
return m.appendAll(list) |
| 721 |
} |
| 722 |
|
| 723 |
func (m *resWrangler) RemoveBuildAnnotations() { |
| 724 |
for _, r := range m.rList { |
| 725 |
r.RemoveBuildAnnotations() |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
// ApplyFilter implements ResMap. |
| 730 |
func (m *resWrangler) ApplyFilter(f kio.Filter) error { |
| 731 |
reverseLookup := make(map[*kyaml.RNode]*resource.Resource, len(m.rList)) |
| 732 |
nodes := make([]*kyaml.RNode, len(m.rList)) |
| 733 |
for i, r := range m.rList { |
| 734 |
ptr := &(r.RNode) |
| 735 |
nodes[i] = ptr |
| 736 |
reverseLookup[ptr] = r |
| 737 |
} |
| 738 |
// The filter can modify nodes, but also delete and create them. |
| 739 |
// The filtered list might be smaller or larger than the nodes list. |
| 740 |
filtered, err := f.Filter(nodes) |
| 741 |
if err != nil { |
| 742 |
return err |
| 743 |
} |
| 744 |
// Rebuild the resmap from the filtered RNodes. |
| 745 |
var nRList []*resource.Resource |
| 746 |
for _, rn := range filtered { |
| 747 |
if rn.IsNilOrEmpty() { |
| 748 |
// A node might make it through the filter as an object, |
| 749 |
// but still be empty. Drop such entries. |
| 750 |
continue |
| 751 |
} |
| 752 |
res, ok := reverseLookup[rn] |
| 753 |
if !ok { |
| 754 |
// A node was created; make a Resource to wrap it. |
| 755 |
res = &resource.Resource{ |
| 756 |
RNode: *rn, |
| 757 |
// Leave remaining fields empty. |
| 758 |
// At at time of writing, seeking to eliminate those fields. |
| 759 |
// Alternatively, could just return error on creation attempt |
| 760 |
// until remaining fields eliminated. |
| 761 |
} |
| 762 |
} |
| 763 |
nRList = append(nRList, res) |
| 764 |
} |
| 765 |
m.rList = nRList |
| 766 |
return nil |
| 767 |
} |