Skip to content

Commit c09d1bb

Browse files
committed
Added Support mapping of group field in CRDs
Signed-off-by: Abhijeet Dey <[email protected]>
1 parent 1dbf953 commit c09d1bb

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

generators/artifacthub/package.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,21 @@ func (pkg AhPackage) GenerateComponents(group string) ([]_component.ComponentDef
7676
}
7777
comp.Model.Metadata.AdditionalProperties["source_uri"] = pkg.ChartUrl
7878
comp.Model.Version = pkg.Version
79-
comp.Model.Name = pkg.Name
79+
80+
// Derive model from the CRD's API group when available; otherwise, fallback to package name
81+
group := component.ExtractGroupFromAPIVersion(comp.Component.Version)
82+
modelName, displayName := component.GroupToModel(group, pkg.Name)
83+
if strings.TrimSpace(modelName) == "" {
84+
modelName = pkg.Name
85+
}
86+
if strings.TrimSpace(displayName) == "" {
87+
displayName = manifests.FormatToReadableString(modelName)
88+
}
89+
comp.Model.Name = modelName
8090
comp.Model.Category = category.CategoryDefinition{
8191
Name: "",
8292
}
83-
comp.Model.DisplayName = manifests.FormatToReadableString(comp.Model.Name)
93+
comp.Model.DisplayName = displayName
8494
components = append(components, comp)
8595
}
8696
return components, nil

generators/github/package.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"bytes"
55
"os"
6+
"strings"
67

78
"github.com/meshery/meshkit/utils"
89
"github.com/meshery/meshkit/utils/component"
@@ -81,11 +82,22 @@ func (gp GitHubPackage) GenerateComponents(group string) ([]_component.Component
8182

8283
comp.Model.Metadata.AdditionalProperties["source_uri"] = gp.SourceURL
8384
comp.Model.Version = gp.version
84-
comp.Model.Name = gp.Name
85+
86+
// Derive model from the CRD's API group when available; otherwise, fallback to package name
87+
group := component.ExtractGroupFromAPIVersion(comp.Component.Version)
88+
modelName, displayName := component.GroupToModel(group, gp.Name)
89+
// Avoid empty names
90+
if strings.TrimSpace(modelName) == "" {
91+
modelName = gp.Name
92+
}
93+
if strings.TrimSpace(displayName) == "" {
94+
displayName = manifests.FormatToReadableString(modelName)
95+
}
96+
comp.Model.Name = modelName
8597
comp.Model.Category = category.CategoryDefinition{
8698
Name: "",
8799
}
88-
comp.Model.DisplayName = manifests.FormatToReadableString(comp.Model.Name)
100+
comp.Model.DisplayName = displayName
89101
components = append(components, comp)
90102
}
91103

utils/component/generator.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"strings"
78

89
"cuelang.org/go/cue"
910
"github.com/meshery/meshkit/utils"
@@ -58,6 +59,34 @@ var OpenAPISpecPathConfig = CuePathConfig{
5859

5960
var Configs = []CuePathConfig{DefaultPathConfig, DefaultPathConfig2}
6061

62+
// GroupToModel determines the model name and display name from a CRD/OpenAPI group value.
63+
// - If group is non-empty, model name is the exact group (e.g., "monitor.azure.com").
64+
// Display name is a title-cased, dot-separated host converted to words (e.g., "Monitor Azure Com").
65+
// - If group is empty, fallbackName and its formatted variant are used.
66+
func GroupToModel(group, fallbackName string) (modelName, displayName string) {
67+
if strings.TrimSpace(group) != "" {
68+
// Title case each dot-separated part and join with space for display name
69+
parts := strings.Split(group, ".")
70+
for i := range parts {
71+
if parts[i] == "" {
72+
continue
73+
}
74+
parts[i] = strings.ToUpper(parts[i][:1]) + strings.ToLower(parts[i][1:])
75+
}
76+
return group, strings.Join(parts, " ")
77+
}
78+
return fallbackName, manifests.FormatToReadableString(fallbackName)
79+
}
80+
81+
// ExtractGroupFromAPIVersion returns the API group from a k8s apiVersion string like "group/version".
82+
// If apiVersion does not contain '/', it returns an empty string.
83+
func ExtractGroupFromAPIVersion(apiVersion string) string {
84+
if strings.Contains(apiVersion, "/") {
85+
return strings.SplitN(apiVersion, "/", 2)[0]
86+
}
87+
return ""
88+
}
89+
6190
func IncludeComponentBasedOnGroup(resource string, groupFilter string) (bool, error) {
6291
if groupFilter == "" {
6392
return true, nil

utils/component/generator_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,32 @@ func TestGenerate(t *testing.T) {
155155
})
156156
}
157157
}
158+
159+
func TestGroupToModel_WithGroup(t *testing.T) {
160+
name, display := GroupToModel("monitor.azure.com", "fallback")
161+
if name != "monitor.azure.com" {
162+
t.Fatalf("expected model name 'monitor.azure.com', got '%s'", name)
163+
}
164+
if display != "Monitor Azure Com" {
165+
t.Fatalf("expected display name 'Monitor Azure Com', got '%s'", display)
166+
}
167+
}
168+
169+
func TestGroupToModel_EmptyGroup(t *testing.T) {
170+
name, display := GroupToModel("", "fooBarBaz")
171+
if name != "fooBarBaz" {
172+
t.Fatalf("expected fallback model name 'fooBarBaz', got '%s'", name)
173+
}
174+
if display != "Foo Bar Baz" {
175+
t.Fatalf("expected display name 'Foo Bar Baz', got '%s'", display)
176+
}
177+
}
178+
179+
func TestExtractGroupFromAPIVersion(t *testing.T) {
180+
if g := ExtractGroupFromAPIVersion("apps/v1"); g != "apps" {
181+
t.Fatalf("expected 'apps', got '%s'", g)
182+
}
183+
if g := ExtractGroupFromAPIVersion("v1"); g != "" {
184+
t.Fatalf("expected '', got '%s'", g)
185+
}
186+
}

utils/component/openapi_generator.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo
121121
}
122122
}
123123

124+
// Decide model name/display based on group when present
125+
modelName := pkg.GetName()
126+
displayModelName := manifests.FormatToReadableString(pkg.GetName())
127+
if g, _ := groupCue.String(); g != "" {
128+
modelName, displayModelName = GroupToModel(g, pkg.GetName())
129+
}
130+
124131
c := component.ComponentDefinition{
125132
SchemaVersion: v1beta1.ComponentSchemaVersion,
126133
Format: component.JSON,
@@ -138,8 +145,8 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo
138145
Model: model.Model{
139146
Version: pkg.GetVersion(),
140147
},
141-
Name: pkg.GetName(),
142-
DisplayName: manifests.FormatToReadableString(pkg.GetName()),
148+
Name: modelName,
149+
DisplayName: displayModelName,
143150
Metadata: &model.ModelDefinition_Metadata{
144151
AdditionalProperties: map[string]interface{}{
145152
"source_uri": pkg.GetSourceURL(),

0 commit comments

Comments
 (0)