Skip to content

Commit b5565ba

Browse files
authored
add option to specify additional managed types (#225)
* add option to specify additional managed types (1) * add option to specify additional managed types (2) * add generate markers, generate * add option to specify additional managed types (3) * add option to specify additional managed types (4) * add option to specify additional managed types (5) * update docs * improve apply handing for APIService objects
1 parent 9b53ecf commit b5565ba

File tree

13 files changed

+262
-45
lines changed

13 files changed

+262
-45
lines changed

clm/cmd/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func newDeleteCmd() *cobra.Command {
6161
return err
6262
}
6363

64-
if ok, msg, err := reconciler.IsDeletionAllowed(context.TODO(), &release.Inventory); err != nil {
64+
if ok, msg, err := reconciler.IsDeletionAllowed(context.TODO(), &release.Inventory, ownerId); err != nil {
6565
return err
6666
} else if !ok {
6767
return fmt.Errorf(msg)

clm/cmd/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func isEphmeralError(err error) bool {
6767
}
6868

6969
func formatTimestamp(t time.Time) string {
70-
d := time.Now().Sub(t)
70+
d := time.Since(t)
7171
if d > 24*time.Hour {
7272
return fmt.Sprintf("%dd", d/24*time.Hour)
7373
} else if d > time.Hour {

pkg/component/component.go

+16
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ func assertPolicyConfiguration[T Component](component T) (PolicyConfiguration, b
109109
return nil, false
110110
}
111111

112+
// Check if given component or its spec implements TypeConfiguration (and return it).
113+
func assertTypeConfiguration[T Component](component T) (TypeConfiguration, bool) {
114+
if typeConfiguration, ok := Component(component).(TypeConfiguration); ok {
115+
return typeConfiguration, true
116+
}
117+
if typeConfiguration, ok := getSpec(component).(TypeConfiguration); ok {
118+
return typeConfiguration, true
119+
}
120+
return nil, false
121+
}
122+
112123
// Calculate digest of given component, honoring annotations, spec, and references.
113124
func calculateComponentDigest[T Component](component T) string {
114125
digestData := make(map[string]any)
@@ -218,6 +229,11 @@ func (s *PolicySpec) GetMissingNamespacesPolicy() reconciler.MissingNamespacesPo
218229
return s.MissingNamespacesPolicy
219230
}
220231

232+
// Implement the TypeConfiguration interface.
233+
func (s *TypeSpec) GetAdditionalManagedTypes() []reconciler.TypeInfo {
234+
return s.AdditionalManagedTypes
235+
}
236+
221237
// Check if state is Ready.
222238
func (s *Status) IsReady() bool {
223239
// caveat: this operates only on the status, so it does not check that observedGeneration == generation

pkg/component/reconciler.go

+3
Original file line numberDiff line numberDiff line change
@@ -730,5 +730,8 @@ func (r *Reconciler[T]) getOptionsForComponent(component T) reconciler.Reconcile
730730
options.MissingNamespacesPolicy = &missingNamespacesPolicy
731731
}
732732
}
733+
if typeConfiguration, ok := assertTypeConfiguration(component); ok {
734+
options.AdditionalManagedTypes = typeConfiguration.GetAdditionalManagedTypes()
735+
}
733736
return options
734737
}

pkg/component/target.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func (t *reconcileTarget[T]) Delete(ctx context.Context, component T) (bool, err
8282

8383
func (t *reconcileTarget[T]) IsDeletionAllowed(ctx context.Context, component T) (bool, string, error) {
8484
// log := log.FromContext(ctx)
85+
ownerId := t.reconcilerId + "/" + component.GetNamespace() + "/" + component.GetName()
8586
status := component.GetStatus()
8687

87-
return t.reconciler.IsDeletionAllowed(ctx, &status.Inventory)
88+
return t.reconciler.IsDeletionAllowed(ctx, &status.Inventory, ownerId)
8889
}

pkg/component/types.go

+21
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ type PolicyConfiguration interface {
101101
GetMissingNamespacesPolicy() reconciler.MissingNamespacesPolicy
102102
}
103103

104+
// The TypeConfiguration interface is meant to be implemented by compoments (or their spec) which allow
105+
// to specify additional managed types.
106+
type TypeConfiguration interface {
107+
// Get additional managed types; instances of these types are handled differently during
108+
// apply and delete; foreign instances of these types will block deletion of the component.
109+
// The fields of the returned TypeInfo structs can be concrete api groups, kinds,
110+
// or wildcards ("*"); in addition, groups can be specified as a pattern of the form "*.<suffix>"",
111+
// where the wildcard matches one or multiple dns labels.
112+
GetAdditionalManagedTypes() []reconciler.TypeInfo
113+
}
114+
104115
// +kubebuilder:object:generate=true
105116

106117
// Legacy placement spec. Components may include this into their spec.
@@ -201,6 +212,16 @@ var _ PolicyConfiguration = &PolicySpec{}
201212

202213
// +kubebuilder:object:generate=true
203214

215+
// TypeSpec allows to specify additional managed types, which are not explicitly part of the component's manifests.
216+
// Components providing TypeConfiguration may include this into their spec.
217+
type TypeSpec struct {
218+
AdditionalManagedTypes []reconciler.TypeInfo `json:"additionalManagedTypes,omitempty"`
219+
}
220+
221+
var _ TypeConfiguration = &TypeSpec{}
222+
223+
// +kubebuilder:object:generate=true
224+
204225
// Component Status. Components must include this into their status.
205226
type Status struct {
206227
ObservedGeneration int64 `json:"observedGeneration"`

pkg/component/zz_generated.deepcopy.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/reconciler/inventory.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func (i *InventoryItem) GetObjectKind() schema.ObjectKind {
1818

1919
// Get inventory item's GroupVersionKind.
2020
func (i InventoryItem) GroupVersionKind() schema.GroupVersionKind {
21-
return schema.GroupVersionKind(i.TypeInfo)
21+
return schema.GroupVersionKind(i.TypeVersionInfo)
2222
}
2323

2424
// Set inventory item's GroupVersionKind.
2525
func (i *InventoryItem) SetGroupVersionKind(gvk schema.GroupVersionKind) {
26-
i.TypeInfo = TypeInfo(gvk)
26+
i.TypeVersionInfo = TypeVersionInfo(gvk)
2727
}
2828

2929
// Get inventory item's namespace.

0 commit comments

Comments
 (0)