Skip to content

Commit

Permalink
feat: add retrieval of expected modules
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ragot committed Feb 23, 2024
1 parent d79552b commit 8dad6ea
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/operator/api/formance.com/v1beta1/stack_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type StackSpec struct {
// StackStatus defines the observed state of Stack
type StackStatus struct {
CommonStatus `json:",inline"`
Modules []string `json:"modules,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -53,6 +54,7 @@ type StackStatus struct {
//+kubebuilder:printcolumn:name="Version",type="string",JSONPath=".spec.version",description="Stack Version"
//+kubebuilder:printcolumn:name="Versions From file",type="string",JSONPath=".spec.versionsFromFile",description="Stack Version From File"
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.ready",description="Is stack ready"
//+kubebuilder:printcolumn:name="Modules",type=[]string,JSONPath=".status.modules",description="Modules List Registered"
//+kubebuilder:printcolumn:name="Info",type=string,JSONPath=".status.info",description="Info"

// Stack is the Schema for the stacks API
Expand Down
52 changes: 52 additions & 0 deletions components/operator/internal/resources/stacks/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,49 @@ func areDependentReady(ctx Context, stack *v1beta1.Stack) error {
return nil
}

func RetrieveReferenceModules(ctx Context, stack *v1beta1.Stack) error {
setKind := map[string]interface{}{}
for _, rtype := range ctx.GetScheme().AllKnownTypes() {
v := reflect.New(rtype).Interface()
r, ok := v.(v1beta1.Module)
if !ok {
continue
}

gvk, err := apiutil.GVKForObject(r, ctx.GetScheme())
if err != nil {
return err
}
l := &unstructured.UnstructuredList{}
l.SetGroupVersionKind(gvk)
if err := ctx.GetClient().List(ctx, l, client.MatchingFields{
"stack": stack.Name,
}); err != nil {
return err
}

for _, item := range l.Items {
content := item.UnstructuredContent()
if content["kind"] != nil {
kind := content["kind"].(string)
if setKind[kind] == nil {
setKind[kind] = []string{}
}
}
}

}

modules := []string{}
for k := range setKind {
modules = append(modules, k)
}

stack.Status.Modules = modules

return nil
}

func Reconcile(ctx Context, stack *v1beta1.Stack) error {
_, _, err := CreateOrUpdate[*corev1.Namespace](ctx, types.NamespacedName{
Name: stack.Name,
Expand All @@ -115,6 +158,15 @@ func Reconcile(ctx Context, stack *v1beta1.Stack) error {
return err
}

err = RetrieveReferenceModules(ctx, stack)
if err != nil {
return err
}

if err := ctx.GetClient().Status().Update(ctx, stack); err != nil {
return err
}

err = areDependentReady(ctx, stack)
if err != nil {
return err
Expand Down

0 comments on commit 8dad6ea

Please sign in to comment.