| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package nameref |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"sigs.k8s.io/kustomize/api/filters/fieldspec" |
| 11 |
"sigs.k8s.io/kustomize/api/resmap" |
| 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 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 18 |
) |
| 19 |
|
| 20 |
// Filter updates a name references. |
| 21 |
type Filter struct { |
| 22 |
// Referrer refers to another resource X by X's name. |
| 23 |
// E.g. A Deployment can refer to a ConfigMap. |
| 24 |
// The Deployment is the Referrer, |
| 25 |
// the ConfigMap is the ReferralTarget. |
| 26 |
// This filter seeks to repair the reference in Deployment, given |
| 27 |
// that the ConfigMap's name may have changed. |
| 28 |
Referrer *resource.Resource |
| 29 |
|
| 30 |
// NameFieldToUpdate is the field in the Referrer |
| 31 |
// that holds the name requiring an update. |
| 32 |
// This is the field to write. |
| 33 |
NameFieldToUpdate types.FieldSpec |
| 34 |
|
| 35 |
// ReferralTarget is the source of the new value for |
| 36 |
// the name, always in the 'metadata/name' field. |
| 37 |
// This is the field to read. |
| 38 |
ReferralTarget resid.Gvk |
| 39 |
|
| 40 |
// Set of resources to scan to find the ReferralTarget. |
| 41 |
ReferralCandidates resmap.ResMap |
| 42 |
} |
| 43 |
|
| 44 |
// At time of writing, in practice this is called with a slice with only |
| 45 |
// one entry, the node also referred to be the resource in the Referrer field. |
| 46 |
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) { |
| 47 |
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes) |
| 48 |
} |
| 49 |
|
| 50 |
// The node passed in here is the same node as held in Referrer; |
| 51 |
// that's how the referrer's name field is updated. |
| 52 |
// Currently, however, this filter still needs the extra methods on Referrer |
| 53 |
// to consult things like the resource Id, its namespace, etc. |
| 54 |
// TODO(3455): No filter should use the Resource api; all information |
| 55 |
// about names should come from annotations, with helper methods |
| 56 |
// on the RNode object. Resource should get stupider, RNode smarter. |
| 57 |
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) { |
| 58 |
if err := f.confirmNodeMatchesReferrer(node); err != nil { |
| 59 |
// sanity check. |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
f.NameFieldToUpdate.Gvk = f.Referrer.GetGvk() |
| 63 |
if err := node.PipeE(fieldspec.Filter{ |
| 64 |
FieldSpec: f.NameFieldToUpdate, |
| 65 |
SetValue: f.set, |
| 66 |
}); err != nil { |
| 67 |
return nil, errors.WrapPrefixf( |
| 68 |
err, "updating name reference in '%s' field of '%s'", |
| 69 |
f.NameFieldToUpdate.Path, f.Referrer.CurId().String()) |
| 70 |
} |
| 71 |
return node, nil |
| 72 |
} |
| 73 |
|
| 74 |
// This function is called on the node found at FieldSpec.Path. |
| 75 |
// It's some node in the Referrer. |
| 76 |
func (f Filter) set(node *yaml.RNode) error { |
| 77 |
if yaml.IsMissingOrNull(node) { |
| 78 |
return nil |
| 79 |
} |
| 80 |
switch node.YNode().Kind { |
| 81 |
case yaml.ScalarNode: |
| 82 |
return f.setScalar(node) |
| 83 |
case yaml.MappingNode: |
| 84 |
return f.setMapping(node) |
| 85 |
case yaml.SequenceNode: |
| 86 |
return applyFilterToSeq(seqFilter{ |
| 87 |
setScalarFn: f.setScalar, |
| 88 |
setMappingFn: f.setMapping, |
| 89 |
}, node) |
| 90 |
default: |
| 91 |
return fmt.Errorf("node must be a scalar, sequence or map") |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// This method used when NameFieldToUpdate doesn't lead to |
| 96 |
// one scalar field (typically called 'name'), but rather |
| 97 |
// leads to a map field (called anything). In this case we |
| 98 |
// must complete the field path, looking for both a 'name' |
| 99 |
// and a 'namespace' field to help select the proper |
| 100 |
// ReferralTarget to read the name and namespace from. |
| 101 |
func (f Filter) setMapping(node *yaml.RNode) error { |
| 102 |
if node.YNode().Kind != yaml.MappingNode { |
| 103 |
return fmt.Errorf("expect a mapping node") |
| 104 |
} |
| 105 |
nameNode, err := node.Pipe(yaml.FieldMatcher{Name: "name"}) |
| 106 |
if err != nil { |
| 107 |
return errors.WrapPrefixf(err, "trying to match 'name' field") |
| 108 |
} |
| 109 |
if nameNode == nil { |
| 110 |
// This is a _configuration_ error; the field path |
| 111 |
// specified in NameFieldToUpdate.Path doesn't resolve |
| 112 |
// to a map with a 'name' field, so we have no idea what |
| 113 |
// field to update with a new name. |
| 114 |
return fmt.Errorf("path config error; no 'name' field in node") |
| 115 |
} |
| 116 |
candidates, err := f.filterMapCandidatesByNamespace(node) |
| 117 |
if err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
oldName := nameNode.YNode().Value |
| 121 |
// use allNamesAndNamespacesAreTheSame to compare referral candidates for functional identity, |
| 122 |
// because we source both name and namespace values from the referral in this case. |
| 123 |
referral, err := f.selectReferral(oldName, candidates, allNamesAndNamespacesAreTheSame) |
| 124 |
if err != nil || referral == nil { |
| 125 |
// Nil referral means nothing to do. |
| 126 |
return err |
| 127 |
} |
| 128 |
f.recordTheReferral(referral) |
| 129 |
if referral.GetName() == oldName && referral.GetNamespace() == "" { |
| 130 |
// The name has not changed, nothing to do. |
| 131 |
return nil |
| 132 |
} |
| 133 |
if err = node.PipeE(yaml.FieldSetter{ |
| 134 |
Name: "name", |
| 135 |
StringValue: referral.GetName(), |
| 136 |
}); err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
if referral.GetNamespace() == "" { |
| 140 |
// Don't write an empty string into the namespace field, as |
| 141 |
// it should not replace the value "default". The empty |
| 142 |
// string is handled as a wild card here, not as an implicit |
| 143 |
// specification of the "default" k8s namespace. |
| 144 |
return nil |
| 145 |
} |
| 146 |
return node.PipeE(yaml.FieldSetter{ |
| 147 |
Name: "namespace", |
| 148 |
StringValue: referral.GetNamespace(), |
| 149 |
}) |
| 150 |
} |
| 151 |
|
| 152 |
func (f Filter) filterMapCandidatesByNamespace( |
| 153 |
node *yaml.RNode) ([]*resource.Resource, error) { |
| 154 |
namespaceNode, err := node.Pipe(yaml.FieldMatcher{Name: "namespace"}) |
| 155 |
if err != nil { |
| 156 |
return nil, errors.WrapPrefixf(err, "trying to match 'namespace' field") |
| 157 |
} |
| 158 |
if namespaceNode == nil { |
| 159 |
return f.ReferralCandidates.Resources(), nil |
| 160 |
} |
| 161 |
namespace := namespaceNode.YNode().Value |
| 162 |
nsMap := f.ReferralCandidates.GroupedByOriginalNamespace() |
| 163 |
if candidates, ok := nsMap[namespace]; ok { |
| 164 |
return candidates, nil |
| 165 |
} |
| 166 |
nsMap = f.ReferralCandidates.GroupedByCurrentNamespace() |
| 167 |
// This could be nil, or an empty list. |
| 168 |
return nsMap[namespace], nil |
| 169 |
} |
| 170 |
|
| 171 |
func (f Filter) setScalar(node *yaml.RNode) error { |
| 172 |
// use allNamesAreTheSame to compare referral candidates for functional identity, |
| 173 |
// because we only source the name from the referral in this case. |
| 174 |
referral, err := f.selectReferral( |
| 175 |
node.YNode().Value, f.ReferralCandidates.Resources(), allNamesAreTheSame) |
| 176 |
if err != nil || referral == nil { |
| 177 |
// Nil referral means nothing to do. |
| 178 |
return err |
| 179 |
} |
| 180 |
f.recordTheReferral(referral) |
| 181 |
if referral.GetName() == node.YNode().Value { |
| 182 |
// The name has not changed, nothing to do. |
| 183 |
return nil |
| 184 |
} |
| 185 |
return node.PipeE(yaml.FieldSetter{StringValue: referral.GetName()}) |
| 186 |
} |
| 187 |
|
| 188 |
// In the resource, make a note that it is referred to by the Referrer. |
| 189 |
func (f Filter) recordTheReferral(referral *resource.Resource) { |
| 190 |
referral.AppendRefBy(f.Referrer.CurId()) |
| 191 |
} |
| 192 |
|
| 193 |
// getRoleRefGvk returns a Gvk in the roleRef field. Return error |
| 194 |
// if the roleRef, roleRef/apiGroup or roleRef/kind is missing. |
| 195 |
func getRoleRefGvk(n *resource.Resource) (*resid.Gvk, error) { |
| 196 |
roleRef, err := n.Pipe(yaml.Lookup("roleRef")) |
| 197 |
if err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
if roleRef.IsNil() { |
| 201 |
return nil, fmt.Errorf("roleRef cannot be found in %s", n.MustString()) |
| 202 |
} |
| 203 |
apiGroup, err := roleRef.Pipe(yaml.Lookup("apiGroup")) |
| 204 |
if err != nil { |
| 205 |
return nil, err |
| 206 |
} |
| 207 |
if apiGroup.IsNil() { |
| 208 |
return nil, fmt.Errorf("apiGroup cannot be found in roleRef %s", roleRef.MustString()) |
| 209 |
} |
| 210 |
kind, err := roleRef.Pipe(yaml.Lookup("kind")) |
| 211 |
if err != nil { |
| 212 |
return nil, err |
| 213 |
} |
| 214 |
if kind.IsNil() { |
| 215 |
return nil, fmt.Errorf("kind cannot be found in roleRef %s", roleRef.MustString()) |
| 216 |
} |
| 217 |
return &resid.Gvk{ |
| 218 |
Group: apiGroup.YNode().Value, |
| 219 |
Kind: kind.YNode().Value, |
| 220 |
}, nil |
| 221 |
} |
| 222 |
|
| 223 |
// sieveFunc returns true if the resource argument satisfies some criteria. |
| 224 |
type sieveFunc func(*resource.Resource) bool |
| 225 |
|
| 226 |
// doSieve uses a function to accept or ignore resources from a list. |
| 227 |
// If list is nil, returns immediately. |
| 228 |
// It's a filter obviously, but that term is overloaded here. |
| 229 |
func doSieve(list []*resource.Resource, fn sieveFunc) (s []*resource.Resource) { |
| 230 |
for _, r := range list { |
| 231 |
if fn(r) { |
| 232 |
s = append(s, r) |
| 233 |
} |
| 234 |
} |
| 235 |
return |
| 236 |
} |
| 237 |
|
| 238 |
func acceptAll(r *resource.Resource) bool { |
| 239 |
return true |
| 240 |
} |
| 241 |
|
| 242 |
func previousNameMatches(name string) sieveFunc { |
| 243 |
return func(r *resource.Resource) bool { |
| 244 |
for _, id := range r.PrevIds() { |
| 245 |
if id.Name == name { |
| 246 |
return true |
| 247 |
} |
| 248 |
} |
| 249 |
return false |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func previousIdSelectedByGvk(gvk *resid.Gvk) sieveFunc { |
| 254 |
return func(r *resource.Resource) bool { |
| 255 |
for _, id := range r.PrevIds() { |
| 256 |
if id.IsSelected(gvk) { |
| 257 |
return true |
| 258 |
} |
| 259 |
} |
| 260 |
return false |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// If the we are updating a 'roleRef/name' field, the 'apiGroup' and 'kind' |
| 265 |
// fields in the same 'roleRef' map must be considered. |
| 266 |
// If either object is cluster-scoped, there can be a referral. |
| 267 |
// E.g. a RoleBinding (which exists in a namespace) can refer |
| 268 |
// to a ClusterRole (cluster-scoped) object. |
| 269 |
// https://kubernetes.io/docs/reference/access-authn-authz/rbac/#role-and-clusterrole |
| 270 |
// Likewise, a ClusterRole can refer to a Secret (in a namespace). |
| 271 |
// Objects in different namespaces generally cannot refer to other |
| 272 |
// with some exceptions (e.g. RoleBinding and ServiceAccount are both |
| 273 |
// namespaceable, but the former can refer to accounts in other namespaces). |
| 274 |
func (f Filter) roleRefFilter() sieveFunc { |
| 275 |
if !strings.HasSuffix(f.NameFieldToUpdate.Path, "roleRef/name") { |
| 276 |
return acceptAll |
| 277 |
} |
| 278 |
roleRefGvk, err := getRoleRefGvk(f.Referrer) |
| 279 |
if err != nil { |
| 280 |
return acceptAll |
| 281 |
} |
| 282 |
return previousIdSelectedByGvk(roleRefGvk) |
| 283 |
} |
| 284 |
|
| 285 |
func prefixSuffixEquals(other resource.ResCtx, allowEmpty bool) sieveFunc { |
| 286 |
return func(r *resource.Resource) bool { |
| 287 |
return r.PrefixesSuffixesEquals(other, allowEmpty) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func (f Filter) sameCurrentNamespaceAsReferrer() sieveFunc { |
| 292 |
referrerCurId := f.Referrer.CurId() |
| 293 |
if referrerCurId.IsClusterScoped() { |
| 294 |
// If the referrer is cluster-scoped, let anything through. |
| 295 |
return acceptAll |
| 296 |
} |
| 297 |
return func(r *resource.Resource) bool { |
| 298 |
if r.CurId().IsClusterScoped() { |
| 299 |
// Allow cluster-scoped through. |
| 300 |
return true |
| 301 |
} |
| 302 |
if r.GetKind() == "ServiceAccount" { |
| 303 |
// Allow service accounts through, even though they |
| 304 |
// are in a namespace. A RoleBinding in another namespace |
| 305 |
// can reference them. |
| 306 |
return true |
| 307 |
} |
| 308 |
return referrerCurId.IsNsEquals(r.CurId()) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// selectReferral picks the best referral from a list of candidates. |
| 313 |
func (f Filter) selectReferral( |
| 314 |
// The name referral that may need to be updated. |
| 315 |
oldName string, |
| 316 |
candidates []*resource.Resource, |
| 317 |
// function that returns whether two referrals are identical for the purposes of the transformation |
| 318 |
candidatesIdentical func(resources []*resource.Resource) bool) (*resource.Resource, error) { |
| 319 |
candidates = doSieve(candidates, previousNameMatches(oldName)) |
| 320 |
candidates = doSieve(candidates, previousIdSelectedByGvk(&f.ReferralTarget)) |
| 321 |
candidates = doSieve(candidates, f.roleRefFilter()) |
| 322 |
candidates = doSieve(candidates, f.sameCurrentNamespaceAsReferrer()) |
| 323 |
if len(candidates) == 1 { |
| 324 |
return candidates[0], nil |
| 325 |
} |
| 326 |
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, true)) |
| 327 |
if len(candidates) > 1 { |
| 328 |
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, false)) |
| 329 |
} |
| 330 |
if len(candidates) == 1 { |
| 331 |
return candidates[0], nil |
| 332 |
} |
| 333 |
if len(candidates) == 0 { |
| 334 |
return nil, nil |
| 335 |
} |
| 336 |
if candidatesIdentical(candidates) { |
| 337 |
// Just take the first one. |
| 338 |
return candidates[0], nil |
| 339 |
} |
| 340 |
ids := getIds(candidates) |
| 341 |
return nil, fmt.Errorf("found multiple possible referrals: %s\n%s", ids, f.failureDetails(candidates)) |
| 342 |
} |
| 343 |
|
| 344 |
func (f Filter) failureDetails(resources []*resource.Resource) string { |
| 345 |
msg := strings.Builder{} |
| 346 |
msg.WriteString(fmt.Sprintf("\n**** Too many possible referral targets to referrer:\n%s\n", f.Referrer.MustYaml())) |
| 347 |
for i, r := range resources { |
| 348 |
msg.WriteString(fmt.Sprintf("--- possible referral %d:\n%s\n", i, r.MustYaml())) |
| 349 |
} |
| 350 |
return msg.String() |
| 351 |
} |
| 352 |
|
| 353 |
func allNamesAreTheSame(resources []*resource.Resource) bool { |
| 354 |
name := resources[0].GetName() |
| 355 |
for i := 1; i < len(resources); i++ { |
| 356 |
if name != resources[i].GetName() { |
| 357 |
return false |
| 358 |
} |
| 359 |
} |
| 360 |
return true |
| 361 |
} |
| 362 |
|
| 363 |
func allNamesAndNamespacesAreTheSame(resources []*resource.Resource) bool { |
| 364 |
name := resources[0].GetName() |
| 365 |
namespace := resources[0].GetNamespace() |
| 366 |
for i := 1; i < len(resources); i++ { |
| 367 |
if name != resources[i].GetName() || namespace != resources[i].GetNamespace() { |
| 368 |
return false |
| 369 |
} |
| 370 |
} |
| 371 |
return true |
| 372 |
} |
| 373 |
|
| 374 |
func getIds(rs []*resource.Resource) string { |
| 375 |
var result []string |
| 376 |
for _, r := range rs { |
| 377 |
result = append(result, r.CurId().String()) |
| 378 |
} |
| 379 |
return strings.Join(result, ", ") |
| 380 |
} |
| 381 |
|
| 382 |
func checkEqual(k, a, b string) error { |
| 383 |
if a != b { |
| 384 |
return fmt.Errorf( |
| 385 |
"node-referrerOriginal '%s' mismatch '%s' != '%s'", |
| 386 |
k, a, b) |
| 387 |
} |
| 388 |
return nil |
| 389 |
} |
| 390 |
|
| 391 |
func (f Filter) confirmNodeMatchesReferrer(node *yaml.RNode) error { |
| 392 |
meta, err := node.GetMeta() |
| 393 |
if err != nil { |
| 394 |
return err |
| 395 |
} |
| 396 |
gvk := f.Referrer.GetGvk() |
| 397 |
if err = checkEqual( |
| 398 |
"APIVersion", meta.APIVersion, gvk.ApiVersion()); err != nil { |
| 399 |
return err |
| 400 |
} |
| 401 |
if err = checkEqual( |
| 402 |
"Kind", meta.Kind, gvk.Kind); err != nil { |
| 403 |
return err |
| 404 |
} |
| 405 |
if err = checkEqual( |
| 406 |
"Name", meta.Name, f.Referrer.GetName()); err != nil { |
| 407 |
return err |
| 408 |
} |
| 409 |
if err = checkEqual( |
| 410 |
"Namespace", meta.Namespace, f.Referrer.GetNamespace()); err != nil { |
| 411 |
return err |
| 412 |
} |
| 413 |
return nil |
| 414 |
} |