File: seqfilter.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,768 kB
  • sloc: makefile: 206; sh: 67
file content (60 lines) | stat: -rw-r--r-- 1,514 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package nameref

import (
	"fmt"

	"sigs.k8s.io/kustomize/kyaml/yaml"
)

type setFn func(*yaml.RNode) error

type seqFilter struct {
	setScalarFn  setFn
	setMappingFn setFn
}

func (sf seqFilter) Filter(node *yaml.RNode) (*yaml.RNode, error) {
	if yaml.IsMissingOrNull(node) {
		return node, nil
	}
	switch node.YNode().Kind {
	case yaml.ScalarNode:
		// Kind: Role/ClusterRole
		// FieldSpec is rules.resourceNames
		err := sf.setScalarFn(node)
		return node, err
	case yaml.MappingNode:
		// Kind: RoleBinding/ClusterRoleBinding
		// FieldSpec is subjects
		// Note: The corresponding fieldSpec had been changed from
		// from path: subjects/name to just path: subjects. This is
		// what get mutatefield to request the mapping of the whole
		// map containing namespace and name instead of just a simple
		// string field containing the name
		err := sf.setMappingFn(node)
		return node, err
	default:
		return node, fmt.Errorf(
			"%#v is expected to be either a string or a map of string", node)
	}
}

// applyFilterToSeq will apply the filter to each element in the sequence node
func applyFilterToSeq(filter yaml.Filter, node *yaml.RNode) error {
	if node.YNode().Kind != yaml.SequenceNode {
		return fmt.Errorf("expect a sequence node but got %v", node.YNode().Kind)
	}

	for _, elem := range node.Content() {
		rnode := yaml.NewRNode(elem)
		err := rnode.PipeE(filter)
		if err != nil {
			return err
		}
	}

	return nil
}