Files
cozystack/internal/shared/appdefowner/appdefowner.go
T
IvanHunters 0b7dd23b99 fix(appdef): let one ApplicationDefinition own each application kind
Nothing enforced that spec.application.kind is unique, and each consumer that
maps a kind back to a definition resolved a duplicate its own way: the
chartRef reconciler let every definition rewrite every release of its kind,
the lineage webhook and the CA projection reconciler took whichever came
first in an unordered list, and cozystack-api registered one resource per
definition. A second definition declaring a kind already in use could
therefore repoint that kind's tenant releases at its own artifact.

Add one ownership rule, the definition created first owns the kind with ties
broken by name, and route every consumer through it. A definition that
arrives later with a kind already in use no longer manages that kind's
releases whatever it is named: the chartRef reconciler skips it with a
Warning event and re-reconciles the next owner when the current one goes,
cozystack-api registers the owner only and logs the rest, its restart hash
moves when ownership moves, and the lineage and CA projection lookups
resolve the owner.

Assisted-by: LLM
Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
2026-09-25 09:39:35 +03:00

49 lines
1.7 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 The Cozystack Authors.
// Package appdefowner decides which ApplicationDefinition owns an application
// kind when more than one declares it. The Go consumers that map a kind back to
// a definition (the cozystack-api resource registry and its restart hash, the
// HelmRelease chartRef reconciler, the lineage webhook and the CA projection
// reconciler) use this one rule, so they settle on the same definition.
//
// The definition created first owns the kind; creation time ties are broken by
// name. Creation time has one-second resolution, so only a definition created
// in a later second is reliably kept out; within one second the name decides.
// A definition that arrives later with a kind already in use therefore does not
// take over the releases the earlier one backs.
package appdefowner
import (
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
)
// Owners maps every non-empty application kind to the name of the definition
// that owns it. The result does not depend on the order of defs.
func Owners(defs []cozyv1alpha1.ApplicationDefinition) map[string]string {
owners := make(map[string]string)
owner := make(map[string]*cozyv1alpha1.ApplicationDefinition)
for i := range defs {
d := &defs[i]
kind := d.Spec.Application.Kind
if kind == "" {
continue
}
if cur, ok := owner[kind]; !ok || before(d, cur) {
owner[kind] = d
}
}
for kind, d := range owner {
owners[kind] = d.Name
}
return owners
}
func before(a, b *cozyv1alpha1.ApplicationDefinition) bool {
at, bt := a.CreationTimestamp.Time, b.CreationTimestamp.Time
if !at.Equal(bt) {
return at.Before(bt)
}
return a.Name < b.Name
}