From d8c0d8c961f1492d6cc708b4a0cefc44a4298548 Mon Sep 17 00:00:00 2001 From: Viktor Voytovych Date: Tue, 17 Nov 2026 15:02:15 +0200 Subject: [PATCH] fix: declare the owner of compute's activity policies The activity processor reads a policy's owner from its meta.datumapis.com/service and meta.datumapis.com/team labels, and the alerts for a failing policy take their service and team from them. Compute's four policies set neither, so their alerts fall back to the platform team. The policies' kustomization now sets both labels, compute and build, as the platform's service catalog lists this repository. A policy added to the folder takes them too. A new test fails when the labels are missing or wrong, or when a policy file is not in the kustomization's resources. --- .../milo/activity/policies/kustomization.yaml | 8 ++ test/activitypolicy/owners_test.go | 99 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 test/activitypolicy/owners_test.go diff --git a/config/milo/activity/policies/kustomization.yaml b/config/milo/activity/policies/kustomization.yaml index 5bf14789..b397bd4a 100644 --- a/config/milo/activity/policies/kustomization.yaml +++ b/config/milo/activity/policies/kustomization.yaml @@ -1,6 +1,14 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization +# The owner the activity processor reads to route a failing policy's alerts. +# The platform's service catalog lists this repository under the compute +# service, team build. +labels: + - pairs: + meta.datumapis.com/service: compute + meta.datumapis.com/team: build + resources: - instance-policy.yaml - instanceconsolesession-policy.yaml diff --git a/test/activitypolicy/owners_test.go b/test/activitypolicy/owners_test.go new file mode 100644 index 00000000..cb77be55 --- /dev/null +++ b/test/activitypolicy/owners_test.go @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +package activitypolicy_test + +import ( + "os" + "path/filepath" + "sort" + "strings" + "testing" + + "sigs.k8s.io/yaml" +) + +// The owner the activity processor reads from a policy's labels to route the +// alerts of a failing policy. The platform's service catalog lists this +// repository under the compute service, team build. +var policyOwner = map[string]string{ + "meta.datumapis.com/service": "compute", + "meta.datumapis.com/team": "build", +} + +type policyKustomization struct { + Resources []string `json:"resources"` + Labels []struct { + Pairs map[string]string `json:"pairs"` + } `json:"labels"` +} + +func readPolicyKustomization(t *testing.T) policyKustomization { + t.Helper() + raw, err := os.ReadFile(filepath.Join(policyDir, "kustomization.yaml")) + if err != nil { + t.Fatal(err) + } + var k policyKustomization + if err := yaml.Unmarshal(raw, &k); err != nil { + t.Fatal(err) + } + return k +} + +// declaredLabels returns the labels the kustomization sets on its policies, +// by label name. +func declaredLabels(k policyKustomization) map[string]string { + labels := map[string]string{} + for _, l := range k.Labels { + for name, value := range l.Pairs { + labels[name] = value + } + } + return labels +} + +// policyFiles returns the YAML files in the policy directory other than its +// kustomization. +func policyFiles(t *testing.T) []string { + t.Helper() + entries, err := os.ReadDir(policyDir) + if err != nil { + t.Fatal(err) + } + var files []string + for _, e := range entries { + if !e.IsDir() && strings.HasSuffix(e.Name(), ".yaml") && e.Name() != "kustomization.yaml" { + files = append(files, e.Name()) + } + } + sort.Strings(files) + return files +} + +// TestEveryPolicyDeclaresItsOwner fails when the policies' kustomization sets +// no owner on its policies, or when a policy file sits outside its resources, +// where the owner labels would not reach it. +func TestEveryPolicyDeclaresItsOwner(t *testing.T) { + k := readPolicyKustomization(t) + + declared := declaredLabels(k) + for name, want := range policyOwner { + if got := declared[name]; got != want { + t.Errorf("the policies' kustomization sets %s to %q on its policies, want %q", name, got, want) + } + } + + files := policyFiles(t) + if len(files) == 0 { + t.Fatalf("%s holds no policy files", policyDir) + } + listed := map[string]bool{} + for _, r := range k.Resources { + listed[r] = true + } + for _, f := range files { + if !listed[f] { + t.Errorf("%s: not in the policies' resources, so the owner labels never reach it", f) + } + } +}