@@ -2,8 +2,6 @@ package dashboard
22
33import (
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- }
0 commit comments