|
4 | 4 | package v1beta2 |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "slices" |
| 8 | + |
7 | 9 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/labels" |
8 | 11 | "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/runtime/schema" |
9 | 13 |
|
10 | 14 | "github.com/projectcapsule/capsule/pkg/api" |
11 | 15 | "github.com/projectcapsule/capsule/pkg/api/meta" |
| 16 | + capruntime "github.com/projectcapsule/capsule/pkg/api/runtime" |
| 17 | + "github.com/projectcapsule/capsule/pkg/runtime/selectors" |
12 | 18 | tpl "github.com/projectcapsule/capsule/pkg/template" |
13 | 19 | ) |
14 | 20 |
|
@@ -58,6 +64,109 @@ type TenantResourceCommonSpec struct { |
58 | 64 | Cordoned *bool `json:"cordoned,omitempty"` |
59 | 65 | // Defines the rules to select targeting Namespace, along with the objects that must be replicated. |
60 | 66 | Resources []ResourceSpec `json:"resources"` |
| 67 | + // Triggers re-render this resource (near-)immediately when matching cluster |
| 68 | + // objects change, instead of only waiting for resyncPeriod. This lets you keep |
| 69 | + // a high resyncPeriod while still reacting quickly to changes of the objects |
| 70 | + // the rendering depends on (e.g. Secrets or ServiceAccounts referenced through |
| 71 | + // a resource's context). Each trigger installs a metadata-only watch per |
| 72 | + // referenced kind; a watch is torn down automatically once no resource |
| 73 | + // references its kind anymore. |
| 74 | + // +optional |
| 75 | + Triggers []TriggerSpec `json:"triggers,omitempty"` |
| 76 | +} |
| 77 | + |
| 78 | +// TriggerOperation is the object lifecycle event a trigger reacts to. |
| 79 | +// +kubebuilder:validation:Enum=CREATE;UPDATE;DELETE |
| 80 | +type TriggerOperation string |
| 81 | + |
| 82 | +const ( |
| 83 | + // TriggerOperationCreate reacts to creations of matching objects. |
| 84 | + TriggerOperationCreate TriggerOperation = "CREATE" |
| 85 | + // TriggerOperationUpdate reacts to updates of matching objects. |
| 86 | + TriggerOperationUpdate TriggerOperation = "UPDATE" |
| 87 | + // TriggerOperationDelete reacts to deletions of matching objects. |
| 88 | + TriggerOperationDelete TriggerOperation = "DELETE" |
| 89 | +) |
| 90 | + |
| 91 | +// TriggerSpec declares the cluster object kinds whose changes cause the owning |
| 92 | +// TenantResource / GlobalTenantResource to be re-rendered. |
| 93 | +// |
| 94 | +// Wildcards are rejected: every kind selected by a trigger is armed as a |
| 95 | +// dedicated watch, so the selection must be a bounded, concrete set. |
| 96 | +// |
| 97 | +// +kubebuilder:validation:XValidation:rule="!self.kinds.exists(k, k.contains('*'))",message="wildcard kinds are not supported in triggers" |
| 98 | +// +kubebuilder:validation:XValidation:rule="!has(self.apiGroups) || !self.apiGroups.exists(g, g.contains('*'))",message="wildcard apiGroups are not supported in triggers" |
| 99 | +type TriggerSpec struct { |
| 100 | + capruntime.VersionKinds `json:",inline"` |
| 101 | + |
| 102 | + // Operations that cause a re-render. When empty, all operations |
| 103 | + // (CREATE, UPDATE and DELETE) are considered. |
| 104 | + // +optional |
| 105 | + Operations []TriggerOperation `json:"operations,omitempty"` |
| 106 | + // Selector narrows the trigger to objects whose labels match. When omitted, |
| 107 | + // every object of the referenced kind matches. |
| 108 | + // +optional |
| 109 | + Selector *metav1.LabelSelector `json:"selector,omitempty"` |
| 110 | + // NamespaceSelector narrows the trigger to objects living in namespaces whose |
| 111 | + // labels match. It is only honored for the cluster-scoped GlobalTenantResource; |
| 112 | + // for the namespaced TenantResource it is ignored, as the trigger is always |
| 113 | + // scoped to the namespaces of the owning Tenant. |
| 114 | + // +optional |
| 115 | + NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"` |
| 116 | +} |
| 117 | + |
| 118 | +// MatchesOperation reports whether the trigger reacts to the given operation. |
| 119 | +// An empty operation list matches every operation. |
| 120 | +func (t TriggerSpec) MatchesOperation(op TriggerOperation) bool { |
| 121 | + if len(t.Operations) == 0 { |
| 122 | + return true |
| 123 | + } |
| 124 | + |
| 125 | + return slices.Contains(t.Operations, op) |
| 126 | +} |
| 127 | + |
| 128 | +// Matches reports whether the trigger reacts to a change of the given kind and |
| 129 | +// operation whose object carries the given labels. Namespace scoping |
| 130 | +// (NamespaceSelector, tenant scoping) is consumer policy and not evaluated here. |
| 131 | +func (t TriggerSpec) Matches(gvk schema.GroupVersionKind, op TriggerOperation, lbls map[string]string) bool { |
| 132 | + if !t.MatchesGroupVersionKind(gvk) || !t.MatchesOperation(op) { |
| 133 | + return false |
| 134 | + } |
| 135 | + |
| 136 | + if t.Selector == nil { |
| 137 | + return true |
| 138 | + } |
| 139 | + |
| 140 | + ok, err := selectors.MatchesSelector(labels.Set(lbls), *t.Selector) |
| 141 | + |
| 142 | + return err == nil && ok |
| 143 | +} |
| 144 | + |
| 145 | +// TriggerVersionKinds returns the de-duplicated set of kind selectors |
| 146 | +// referenced by the resource's triggers. Selectors without a concrete version |
| 147 | +// (e.g. apiGroups: ["apps"]) are resolved to a watchable GroupVersionKind by |
| 148 | +// the trigger watch manager via the REST mapper. |
| 149 | +func (s *TenantResourceCommonSpec) TriggerVersionKinds() []capruntime.VersionKind { |
| 150 | + seen := make(map[capruntime.VersionKind]struct{}, len(s.Triggers)) |
| 151 | + out := make([]capruntime.VersionKind, 0, len(s.Triggers)) |
| 152 | + |
| 153 | + for _, t := range s.Triggers { |
| 154 | + for _, vk := range t.VersionKinds.VersionKinds() { |
| 155 | + if vk.Kind == "" { |
| 156 | + continue |
| 157 | + } |
| 158 | + |
| 159 | + if _, ok := seen[vk]; ok { |
| 160 | + continue |
| 161 | + } |
| 162 | + |
| 163 | + seen[vk] = struct{}{} |
| 164 | + |
| 165 | + out = append(out, vk) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return out |
61 | 170 | } |
62 | 171 |
|
63 | 172 | type TenantResourceCommonSpecSettings struct { |
|
0 commit comments