Skip to content

Commit be9370d

Browse files
committed
[dashboard] refactor dashboard configuration
- Refactor code for dashboard resources creation - Move dashboard-config helm chart to dynamic dashboard controller
1 parent bb653e5 commit be9370d

86 files changed

Lines changed: 4530 additions & 6002 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎internal/controller/cozystackresource_controller.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type CozystackResourceDefinitionReconciler struct {
3939
lastHandled time.Time
4040

4141
mem *crdmem.Memory
42+
43+
// Track static resources initialization
44+
staticResourcesInitialized bool
45+
staticResourcesMutex sync.Mutex
4246
}
4347

4448
func (r *CozystackResourceDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
@@ -70,6 +74,13 @@ func (r *CozystackResourceDefinitionReconciler) Reconcile(ctx context.Context, r
7074
return res, derr
7175
}
7276

77+
// After processing CRD, perform cleanup of orphaned resources
78+
// This should be done after cache warming to ensure all current resources are known
79+
if err := mgr.CleanupOrphanedResources(ctx); err != nil {
80+
logger.Error(err, "Failed to cleanup orphaned dashboard resources")
81+
// Don't fail the reconciliation, just log the error
82+
}
83+
7384
r.mu.Lock()
7485
r.lastEvent = time.Now()
7586
r.mu.Unlock()
@@ -87,6 +98,40 @@ func (r *CozystackResourceDefinitionReconciler) Reconcile(ctx context.Context, r
8798
return ctrl.Result{}, nil
8899
}
89100

101+
// initializeStaticResourcesOnce ensures static resources are created only once
102+
func (r *CozystackResourceDefinitionReconciler) initializeStaticResourcesOnce(ctx context.Context) error {
103+
r.staticResourcesMutex.Lock()
104+
defer r.staticResourcesMutex.Unlock()
105+
106+
if r.staticResourcesInitialized {
107+
return nil // Already initialized
108+
}
109+
110+
// Create dashboard manager and initialize static resources
111+
mgr := dashboard.NewManager(
112+
r.Client,
113+
r.Scheme,
114+
dashboard.WithCRDListFunc(func(c context.Context) ([]cozyv1alpha1.CozystackResourceDefinition, error) {
115+
if r.mem != nil {
116+
return r.mem.ListFromCacheOrAPI(c, r.Client)
117+
}
118+
var list cozyv1alpha1.CozystackResourceDefinitionList
119+
if err := r.Client.List(c, &list); err != nil {
120+
return nil, err
121+
}
122+
return list.Items, nil
123+
}),
124+
)
125+
126+
if err := mgr.InitializeStaticResources(ctx); err != nil {
127+
return err
128+
}
129+
130+
r.staticResourcesInitialized = true
131+
log.FromContext(ctx).Info("Static dashboard resources initialized successfully")
132+
return nil
133+
}
134+
90135
func (r *CozystackResourceDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error {
91136
if r.Debounce == 0 {
92137
r.Debounce = 5 * time.Second
@@ -97,6 +142,15 @@ func (r *CozystackResourceDefinitionReconciler) SetupWithManager(mgr ctrl.Manage
97142
if err := r.mem.EnsurePrimingWithManager(mgr); err != nil {
98143
return err
99144
}
145+
146+
// Initialize static resources once during controller startup
147+
go func() {
148+
ctx := context.Background()
149+
if err := r.initializeStaticResourcesOnce(ctx); err != nil {
150+
log.FromContext(ctx).Error(err, "Failed to initialize static resources")
151+
}
152+
}()
153+
100154
return ctrl.NewControllerManagedBy(mgr).
101155
Named("cozystackresource-controller").
102156
For(&cozyv1alpha1.CozystackResourceDefinition{}, builder.WithPredicates()).
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package dashboard
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
9+
dashv1alpha1 "github.com/cozystack/cozystack/api/dashboard/v1alpha1"
10+
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
11+
12+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
14+
)
15+
16+
// ensureBreadcrumb creates or updates a Breadcrumb resource for the given CRD
17+
func (m *Manager) ensureBreadcrumb(ctx context.Context, crd *cozyv1alpha1.CozystackResourceDefinition) error {
18+
_, _, kind := pickGVK(crd)
19+
20+
lowerKind := strings.ToLower(kind)
21+
detailID := fmt.Sprintf("stock-project-factory-%s-details", lowerKind)
22+
23+
obj := &dashv1alpha1.Breadcrumb{}
24+
obj.SetName(detailID)
25+
26+
plural := pickPlural(kind, crd)
27+
28+
// Prefer dashboard.Plural for UI label if provided
29+
labelPlural := titleFromKindPlural(kind, plural)
30+
if crd != nil && crd.Spec.Dashboard != nil && crd.Spec.Dashboard.Plural != "" {
31+
labelPlural = crd.Spec.Dashboard.Plural
32+
}
33+
34+
key := plural // e.g., "virtualmachines"
35+
label := labelPlural
36+
link := fmt.Sprintf("/openapi-ui/{clusterName}/{namespace}/api-table/apps.cozystack.io/v1alpha1/%s", plural)
37+
// If Name is set, change the first breadcrumb item to "Tenant Modules"
38+
// TODO add parameter to this
39+
if crd.Spec.Dashboard.Name != "" {
40+
key = "tenantmodules"
41+
label = "Tenant Modules"
42+
link = "/openapi-ui/{clusterName}/{namespace}/api-table/core.cozystack.io/v1alpha1/tenantmodules"
43+
}
44+
45+
items := []any{
46+
map[string]any{
47+
"key": key,
48+
"label": label,
49+
"link": link,
50+
},
51+
map[string]any{
52+
"key": strings.ToLower(kind), // "etcd"
53+
"label": "{6}", // literal, as in your example
54+
},
55+
}
56+
57+
spec := map[string]any{
58+
"id": detailID,
59+
"breadcrumbItems": items,
60+
}
61+
62+
_, err := controllerutil.CreateOrUpdate(ctx, m.client, obj, func() error {
63+
if err := controllerutil.SetOwnerReference(crd, obj, m.scheme); err != nil {
64+
return err
65+
}
66+
// Add dashboard labels to dynamic resources
67+
m.addDashboardLabels(obj, crd, ResourceTypeDynamic)
68+
b, err := json.Marshal(spec)
69+
if err != nil {
70+
return err
71+
}
72+
obj.Spec = dashv1alpha1.ArbitrarySpec{JSON: apiextv1.JSON{Raw: b}}
73+
return nil
74+
})
75+
return err
76+
}

‎internal/controller/dashboard/customcolumns.go‎

Lines changed: 2 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package dashboard
22

33
import (
44
"context"
5-
"crypto/sha1"
6-
"encoding/hex"
75
"encoding/json"
86
"fmt"
97
"strings"
@@ -12,7 +10,6 @@ import (
1210
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
1311

1412
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
15-
"k8s.io/apimachinery/pkg/runtime/schema"
1613
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1714
)
1815

@@ -38,11 +35,6 @@ func (m *Manager) ensureCustomColumnsOverride(ctx context.Context, crd *cozyv1al
3835
badgeColor := hexColorForKind(kind) // deterministic, dark enough for white text
3936

4037
obj := &dashv1alpha1.CustomColumnsOverride{}
41-
obj.SetGroupVersionKind(schema.GroupVersionKind{
42-
Group: "dashboard.cozystack.io",
43-
Version: "v1alpha1",
44-
Kind: "CustomColumnsOverride",
45-
})
4638
obj.SetName(name)
4739

4840
href := fmt.Sprintf("/openapi-ui/{2}/{reqsJsonPath[0]['.metadata.namespace']['-']}/factory/%s/{reqsJsonPath[0]['.metadata.name']['-']}", detailsSegment)
@@ -158,6 +150,8 @@ func (m *Manager) ensureCustomColumnsOverride(ctx context.Context, crd *cozyv1al
158150
if err := controllerutil.SetOwnerReference(crd, obj, m.scheme); err != nil {
159151
return err
160152
}
153+
// Add dashboard labels to dynamic resources
154+
m.addDashboardLabels(obj, crd, ResourceTypeDynamic)
161155
b, err := json.Marshal(desired["spec"])
162156
if err != nil {
163157
return err
@@ -168,131 +162,3 @@ func (m *Manager) ensureCustomColumnsOverride(ctx context.Context, crd *cozyv1al
168162
// Return OperationResultCreated/Updated is not available here with unstructured; we can mimic Updated when no error.
169163
return controllerutil.OperationResultNone, err
170164
}
171-
172-
// --- helpers ---
173-
174-
// pickGVK tries to read group/version/kind from the CRD. We prefer the "application" section,
175-
// falling back to other likely fields if your schema differs.
176-
func pickGVK(crd *cozyv1alpha1.CozystackResourceDefinition) (group, version, kind string) {
177-
// Best guess based on your examples:
178-
if crd.Spec.Application.Kind != "" {
179-
kind = crd.Spec.Application.Kind
180-
}
181-
182-
// Reasonable fallbacks if any are empty:
183-
if group == "" {
184-
group = "apps.cozystack.io"
185-
}
186-
if version == "" {
187-
version = "v1alpha1"
188-
}
189-
if kind == "" {
190-
kind = "Resource"
191-
}
192-
return
193-
}
194-
195-
// pickPlural prefers a field on the CRD if you have it; otherwise do a simple lowercase + "s".
196-
func pickPlural(kind string, crd *cozyv1alpha1.CozystackResourceDefinition) string {
197-
// If you have crd.Spec.Application.Plural, prefer it. Example:
198-
if crd.Spec.Application.Plural != "" {
199-
return crd.Spec.Application.Plural
200-
}
201-
// naive pluralization
202-
k := strings.ToLower(kind)
203-
if strings.HasSuffix(k, "s") {
204-
return k
205-
}
206-
return k + "s"
207-
}
208-
209-
// initialsFromKind splits CamelCase and returns the first letters in upper case.
210-
// "VirtualMachine" -> "VM"; "Bucket" -> "B".
211-
func initialsFromKind(kind string) string {
212-
parts := splitCamel(kind)
213-
if len(parts) == 0 {
214-
return strings.ToUpper(kind)
215-
}
216-
var b strings.Builder
217-
for _, p := range parts {
218-
if p == "" {
219-
continue
220-
}
221-
b.WriteString(strings.ToUpper(string(p[0])))
222-
// Limit to 3 chars to keep the badge compact (VM, PVC, etc.)
223-
if b.Len() >= 3 {
224-
break
225-
}
226-
}
227-
return b.String()
228-
}
229-
230-
// hexColorForKind returns a dark, saturated color (hex) derived from a stable hash of the kind.
231-
// We map the hash to an HSL hue; fix S/L for consistent readability with white text.
232-
func hexColorForKind(kind string) string {
233-
// Stable short hash (sha1 → bytes → hue)
234-
sum := sha1.Sum([]byte(kind))
235-
// Use first two bytes for hue [0..359]
236-
hue := int(sum[0])<<8 | int(sum[1])
237-
hue = hue % 360
238-
239-
// Fixed S/L chosen to contrast with white text:
240-
// S = 80%, L = 35% (dark enough so #fff is readable)
241-
r, g, b := hslToRGB(float64(hue), 0.80, 0.35)
242-
243-
return fmt.Sprintf("#%02x%02x%02x", r, g, b)
244-
}
245-
246-
// hslToRGB converts HSL (0..360, 0..1, 0..1) to sRGB (0..255).
247-
func hslToRGB(h float64, s float64, l float64) (uint8, uint8, uint8) {
248-
c := (1 - absFloat(2*l-1)) * s
249-
hp := h / 60.0
250-
x := c * (1 - absFloat(modFloat(hp, 2)-1))
251-
var r1, g1, b1 float64
252-
switch {
253-
case 0 <= hp && hp < 1:
254-
r1, g1, b1 = c, x, 0
255-
case 1 <= hp && hp < 2:
256-
r1, g1, b1 = x, c, 0
257-
case 2 <= hp && hp < 3:
258-
r1, g1, b1 = 0, c, x
259-
case 3 <= hp && hp < 4:
260-
r1, g1, b1 = 0, x, c
261-
case 4 <= hp && hp < 5:
262-
r1, g1, b1 = x, 0, c
263-
default:
264-
r1, g1, b1 = c, 0, x
265-
}
266-
m := l - c/2
267-
r := uint8(clamp01(r1+m) * 255.0)
268-
g := uint8(clamp01(g1+m) * 255.0)
269-
b := uint8(clamp01(b1+m) * 255.0)
270-
return r, g, b
271-
}
272-
273-
func absFloat(v float64) float64 {
274-
if v < 0 {
275-
return -v
276-
}
277-
return v
278-
}
279-
280-
func modFloat(a, b float64) float64 {
281-
return a - b*float64(int(a/b))
282-
}
283-
284-
func clamp01(v float64) float64 {
285-
if v < 0 {
286-
return 0
287-
}
288-
if v > 1 {
289-
return 1
290-
}
291-
return v
292-
}
293-
294-
// optional: tiny helper to expose the compact color hash (useful for debugging)
295-
func shortHashHex(s string) string {
296-
sum := sha1.Sum([]byte(s))
297-
return hex.EncodeToString(sum[:4])
298-
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package dashboard
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
9+
dashv1alpha1 "github.com/cozystack/cozystack/api/dashboard/v1alpha1"
10+
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
11+
12+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
14+
)
15+
16+
// ensureCustomFormsOverride creates or updates a CustomFormsOverride resource for the given CRD
17+
func (m *Manager) ensureCustomFormsOverride(ctx context.Context, crd *cozyv1alpha1.CozystackResourceDefinition) error {
18+
g, v, kind := pickGVK(crd)
19+
plural := pickPlural(kind, crd)
20+
21+
name := fmt.Sprintf("%s.%s.%s", g, v, plural)
22+
customizationID := fmt.Sprintf("default-/%s/%s/%s", g, v, plural)
23+
24+
obj := &dashv1alpha1.CustomFormsOverride{}
25+
obj.SetName(name)
26+
27+
// Replicates your Helm includes (system metadata + api + status).
28+
hidden := []any{}
29+
hidden = append(hidden, hiddenMetadataSystem()...)
30+
hidden = append(hidden, hiddenMetadataAPI()...)
31+
hidden = append(hidden, hiddenStatus()...)
32+
33+
// If Name is set, hide metadata
34+
if crd.Spec.Dashboard != nil && strings.TrimSpace(crd.Spec.Dashboard.Name) != "" {
35+
hidden = append([]interface{}{
36+
[]any{"metadata"},
37+
}, hidden...)
38+
}
39+
40+
sort := make([]any, len(crd.Spec.Dashboard.KeysOrder))
41+
for i, v := range crd.Spec.Dashboard.KeysOrder {
42+
sort[i] = v
43+
}
44+
45+
spec := map[string]any{
46+
"customizationId": customizationID,
47+
"hidden": hidden,
48+
"sort": sort,
49+
"schema": map[string]any{}, // {}
50+
"strategy": "merge",
51+
}
52+
53+
_, err := controllerutil.CreateOrUpdate(ctx, m.client, obj, func() error {
54+
if err := controllerutil.SetOwnerReference(crd, obj, m.scheme); err != nil {
55+
return err
56+
}
57+
// Add dashboard labels to dynamic resources
58+
m.addDashboardLabels(obj, crd, ResourceTypeDynamic)
59+
b, err := json.Marshal(spec)
60+
if err != nil {
61+
return err
62+
}
63+
obj.Spec = dashv1alpha1.ArbitrarySpec{JSON: apiextv1.JSON{Raw: b}}
64+
return nil
65+
})
66+
return err
67+
}

0 commit comments

Comments
 (0)