| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package policy |
| 16 |
|
| 17 |
import ( |
| 18 |
"go.yaml.in/yaml/v3" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func K8sTestTagHandler() TagVisitor { |
| 24 |
return k8sAdmissionTagHandler{TagVisitor: DefaultTagVisitor()} |
| 25 |
} |
| 26 |
|
| 27 |
type k8sAdmissionTagHandler struct { |
| 28 |
TagVisitor |
| 29 |
} |
| 30 |
|
| 31 |
func (k8sAdmissionTagHandler) PolicyTag(ctx ParserContext, id int64, tagName string, node *yaml.Node, policy *Policy) { |
| 32 |
switch tagName { |
| 33 |
case "kind": |
| 34 |
policy.SetMetadata("kind", ctx.NewString(node).Value) |
| 35 |
case "metadata": |
| 36 |
m := k8sMetadata{} |
| 37 |
if err := node.Decode(&m); err != nil { |
| 38 |
ctx.ReportErrorAtID(id, "invalid yaml metadata node: %v, error: %w", node, err) |
| 39 |
return |
| 40 |
} |
| 41 |
case "spec": |
| 42 |
spec := ctx.ParseRule(ctx, policy, node) |
| 43 |
policy.SetRule(spec) |
| 44 |
default: |
| 45 |
ctx.ReportErrorAtID(id, "unsupported policy tag: %s", tagName) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func (k8sAdmissionTagHandler) RuleTag(ctx ParserContext, id int64, tagName string, node *yaml.Node, policy *Policy, r *Rule) { |
| 50 |
switch tagName { |
| 51 |
case "failurePolicy": |
| 52 |
policy.SetMetadata(tagName, ctx.NewString(node).Value) |
| 53 |
case "matchConstraints": |
| 54 |
m := k8sMatchConstraints{} |
| 55 |
if err := node.Decode(&m); err != nil { |
| 56 |
ctx.ReportErrorAtID(id, "invalid yaml matchConstraints node: %v, error: %w", node, err) |
| 57 |
return |
| 58 |
} |
| 59 |
case "validations": |
| 60 |
id := ctx.CollectMetadata(node) |
| 61 |
if node.LongTag() != "tag:yaml.org,2002:seq" { |
| 62 |
ctx.ReportErrorAtID(id, "invalid 'validations' type, expected list got: %s", node.LongTag()) |
| 63 |
return |
| 64 |
} |
| 65 |
for _, val := range node.Content { |
| 66 |
r.AddMatch(ctx.ParseMatch(ctx, policy, val)) |
| 67 |
} |
| 68 |
default: |
| 69 |
ctx.ReportErrorAtID(id, "unsupported rule tag: %s", tagName) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func (k8sAdmissionTagHandler) MatchTag(ctx ParserContext, id int64, tagName string, node *yaml.Node, policy *Policy, m *Match) { |
| 74 |
if m.Output().Value == "" { |
| 75 |
m.SetOutput(ValueString{Value: "'invalid admission request'"}) |
| 76 |
} |
| 77 |
switch tagName { |
| 78 |
case "expression": |
| 79 |
|
| 80 |
condition := ctx.NewString(node) |
| 81 |
condition.Value = "!(" + condition.Value + ")" |
| 82 |
m.SetCondition(condition) |
| 83 |
case "messageExpression": |
| 84 |
m.SetOutput(ctx.NewString(node)) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
type k8sMetadata struct { |
| 89 |
Name string `yaml:"name"` |
| 90 |
} |
| 91 |
|
| 92 |
type k8sMatchConstraints struct { |
| 93 |
ResourceRules []k8sResourceRule `yaml:"resourceRules"` |
| 94 |
} |
| 95 |
|
| 96 |
type k8sResourceRule struct { |
| 97 |
APIGroups []string `yaml:"apiGroups"` |
| 98 |
APIVersions []string `yaml:"apiVersions"` |
| 99 |
Operations []string `yaml:"operations"` |
| 100 |
} |