Skip to content

Commit 3aebeda

Browse files
authored
Refactor extensions handling (#2427)
1 parent 2ad5da7 commit 3aebeda

File tree

4 files changed

+95
-85
lines changed

4 files changed

+95
-85
lines changed

internal/cmd/alpha/alpha.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewAlphaCMD() *cobra.Command {
4343
"registry_config": config.NewConfigCMD,
4444
"registry_image-import": imageimport.NewImportCMD,
4545
"function_init": function.NewInitCmd,
46-
}, cmd, kymaConfig)
46+
}, cmd)
4747

4848
kymaConfig.DisplayExtensionsErrors(cmd.ErrOrStderr())
4949

internal/cmdcommon/extension.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ func newExtensionsConfig(config *KymaConfig) *KymaExtensionsConfig {
3232

3333
if getBoolFlagValue("--skip-extensions") {
3434
// skip extensions fetching
35-
return &KymaExtensionsConfig{
36-
kymaConfig: config,
37-
}
35+
return extensionsConfig
3836
}
3937

4038
extensionsConfig.extensions, extensionsConfig.parseErrors = loadExtensionsFromCluster(config.Ctx, config.KubeClientConfig)
@@ -52,37 +50,31 @@ func (kec *KymaExtensionsConfig) GetRawExtensions() ExtensionList {
5250
return kec.extensions
5351
}
5452

55-
func (kec *KymaExtensionsConfig) BuildExtensions(availableTemplateCommands *TemplateCommandsList, availableCoreCommands CoreCommandsMap, cmd *cobra.Command, config *KymaConfig) []*cobra.Command {
53+
func (kec *KymaExtensionsConfig) BuildExtensions(availableTemplateCommands *TemplateCommandsList, availableCoreCommands CoreCommandsMap, cmd *cobra.Command) []*cobra.Command {
5654
var cmds []*cobra.Command
5755

58-
var cms, cmsError = getExtensionConfigMaps(config.Ctx, config.KubeClientConfig)
59-
if cmsError != nil {
60-
kec.parseErrors = cmsError
61-
return nil
62-
}
63-
6456
existingCommands := make(map[string]bool)
6557
for _, baseCmd := range cmd.Commands() {
6658
existingCommands[baseCmd.Name()] = true
6759
}
6860

69-
for _, cm := range cms.Items {
70-
extension, _ := parseResourceExtension(cm.Data)
61+
for _, extensionItem := range kec.extensions {
62+
extension := extensionItem.Extension
7163
if existingCommands[extension.RootCommand.Name] {
7264
kec.parseErrors = errors.Join(
7365
kec.parseErrors,
7466
fmt.Errorf("failed to validate configmap '%s/%s': base command with name='%s' already exists",
75-
cm.GetNamespace(), cm.GetName(), extension.RootCommand.Name),
67+
extensionItem.ConfigMapNamespace, extensionItem.ConfigMapName, extension.RootCommand.Name),
7668
)
7769
continue
7870
}
7971

80-
extensionCommands, err := buildCommandFromExtension(kec.kymaConfig, extension, availableTemplateCommands, availableCoreCommands)
72+
extensionCommands, err := buildCommandFromExtension(kec.kymaConfig, &extension, availableTemplateCommands, availableCoreCommands)
8173
if err != nil {
8274
kec.parseErrors = errors.Join(
8375
kec.parseErrors,
8476
fmt.Errorf("failed to build extensions from configmap '%s/%s': %s",
85-
cm.GetNamespace(), cm.GetName(), err.Error()),
77+
extensionItem.ConfigMapNamespace, extensionItem.ConfigMapName, err.Error()),
8678
)
8779
continue
8880
}
@@ -124,13 +116,13 @@ func getExtensionConfigMaps(ctx context.Context, clientConfig *KubeClientConfig)
124116
return cms, nil
125117
}
126118

127-
func loadExtensionsFromCluster(ctx context.Context, clientConfig *KubeClientConfig) ([]Extension, error) {
119+
func loadExtensionsFromCluster(ctx context.Context, clientConfig *KubeClientConfig) (ExtensionList, error) {
128120
var cms, cmsError = getExtensionConfigMaps(ctx, clientConfig)
129121
if cmsError != nil {
130122
return nil, cmsError
131123
}
132124

133-
var extensions []Extension
125+
var extensionsItems ExtensionList
134126
var parseErrors error
135127
for _, cm := range cms.Items {
136128
extension, err := parseResourceExtension(cm.Data)
@@ -144,8 +136,8 @@ func loadExtensionsFromCluster(ctx context.Context, clientConfig *KubeClientConf
144136
continue
145137
}
146138

147-
if slices.ContainsFunc(extensions, func(e Extension) bool {
148-
return e.RootCommand.Name == extension.RootCommand.Name
139+
if slices.ContainsFunc(extensionsItems, func(e ExtensionItem) bool {
140+
return e.Extension.RootCommand.Name == extension.RootCommand.Name
149141
}) {
150142
parseErrors = errors.Join(
151143
parseErrors,
@@ -155,10 +147,14 @@ func loadExtensionsFromCluster(ctx context.Context, clientConfig *KubeClientConf
155147
continue
156148
}
157149

158-
extensions = append(extensions, *extension)
150+
extensionsItems = append(extensionsItems, ExtensionItem{
151+
ConfigMapName: cm.GetName(),
152+
ConfigMapNamespace: cm.GetNamespace(),
153+
Extension: *extension,
154+
})
159155
}
160156

161-
return extensions, parseErrors
157+
return extensionsItems, parseErrors
162158
}
163159

164160
func parseResourceExtension(cmData map[string]string) (*Extension, error) {

internal/cmdcommon/extension_test.go

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,14 @@ descriptionLong: test-description-long
129129

130130
want := ExtensionList{
131131
{
132-
RootCommand: types.RootCommand{
133-
Name: "test-command",
134-
Description: "test-description",
135-
DescriptionLong: "test-description-long",
132+
ConfigMapName: "bad-data",
133+
ConfigMapNamespace: "",
134+
Extension: Extension{
135+
RootCommand: types.RootCommand{
136+
Name: "test-command",
137+
Description: "test-description",
138+
DescriptionLong: "test-description-long",
139+
},
136140
},
137141
},
138142
}
@@ -251,70 +255,74 @@ create:
251255
}
252256
}
253257

254-
func fixTestExtension(name string) Extension {
255-
return Extension{
256-
RootCommand: types.RootCommand{
257-
Name: name,
258-
Description: "test-description",
259-
DescriptionLong: "test-description-long",
260-
},
261-
Resource: &types.ResourceInfo{
262-
Scope: types.NamespaceScope,
263-
Kind: "TestKind",
264-
Group: "test.group",
265-
Version: "v1",
266-
},
267-
TemplateCommands: &TemplateCommands{
268-
GetCommand: &types.GetCommand{
269-
Description: "test-get-description",
270-
DescriptionLong: "test-get-description-long",
271-
Parameters: []types.Parameter{
272-
{
273-
Path: ".metadata.generation",
274-
Name: "generation",
275-
},
276-
},
277-
},
278-
ExplainCommand: &types.ExplainCommand{
258+
func fixTestExtension(name string) ExtensionItem {
259+
return ExtensionItem{
260+
ConfigMapName: name,
261+
ConfigMapNamespace: "",
262+
Extension: Extension{
263+
RootCommand: types.RootCommand{
264+
Name: name,
279265
Description: "test-description",
280266
DescriptionLong: "test-description-long",
281-
Output: "test-explain-output",
282267
},
283-
DeleteCommand: &types.DeleteCommand{
284-
Description: "test-delete-description",
285-
DescriptionLong: "test-delete-description-long",
268+
Resource: &types.ResourceInfo{
269+
Scope: types.NamespaceScope,
270+
Kind: "TestKind",
271+
Group: "test.group",
272+
Version: "v1",
286273
},
287-
CreateCommand: &types.CreateCommand{
288-
Description: "create test resource",
289-
DescriptionLong: "use this command to create test resource",
290-
CustomFlags: []types.CustomFlag{
291-
{
292-
Type: types.StringCustomFlagType,
293-
Name: "test-flag",
294-
Description: "test-flag description",
295-
Shorthand: "t",
296-
Path: ".spec.test.field",
297-
DefaultValue: "test-default",
298-
Required: true,
274+
TemplateCommands: &TemplateCommands{
275+
GetCommand: &types.GetCommand{
276+
Description: "test-get-description",
277+
DescriptionLong: "test-get-description-long",
278+
Parameters: []types.Parameter{
279+
{
280+
Path: ".metadata.generation",
281+
Name: "generation",
282+
},
299283
},
300-
{
301-
Type: types.PathCustomFlagType,
302-
Name: "test-flag-2",
303-
Description: "test-flag-2 description",
304-
Shorthand: "f",
305-
Path: ".spec.test.field2",
306-
DefaultValue: "test-default2",
307-
Required: false,
284+
},
285+
ExplainCommand: &types.ExplainCommand{
286+
Description: "test-description",
287+
DescriptionLong: "test-description-long",
288+
Output: "test-explain-output",
289+
},
290+
DeleteCommand: &types.DeleteCommand{
291+
Description: "test-delete-description",
292+
DescriptionLong: "test-delete-description-long",
293+
},
294+
CreateCommand: &types.CreateCommand{
295+
Description: "create test resource",
296+
DescriptionLong: "use this command to create test resource",
297+
CustomFlags: []types.CustomFlag{
298+
{
299+
Type: types.StringCustomFlagType,
300+
Name: "test-flag",
301+
Description: "test-flag description",
302+
Shorthand: "t",
303+
Path: ".spec.test.field",
304+
DefaultValue: "test-default",
305+
Required: true,
306+
},
307+
{
308+
Type: types.PathCustomFlagType,
309+
Name: "test-flag-2",
310+
Description: "test-flag-2 description",
311+
Shorthand: "f",
312+
Path: ".spec.test.field2",
313+
DefaultValue: "test-default2",
314+
Required: false,
315+
},
308316
},
309317
},
310318
},
311-
},
312-
CoreCommands: []CoreCommandInfo{
313-
{
314-
ActionID: "test-action-id-1",
315-
},
316-
{
317-
ActionID: "test-action-id-2",
319+
CoreCommands: []CoreCommandInfo{
320+
{
321+
ActionID: "test-action-id-1",
322+
},
323+
{
324+
ActionID: "test-action-id-2",
325+
},
318326
},
319327
},
320328
}

internal/cmdcommon/extension_types.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ type TemplateCommandsList struct {
2727
Delete func(templates.KubeClientGetter, *templates.DeleteOptions) *cobra.Command
2828
}
2929

30-
type ExtensionList []Extension
30+
type ExtensionList []ExtensionItem
31+
32+
type ExtensionItem struct {
33+
ConfigMapName string
34+
ConfigMapNamespace string
35+
Extension Extension
36+
}
3137

3238
func (el *ExtensionList) ContainResource(kind string) bool {
33-
for _, extension := range *el {
34-
if extension.Resource.Kind == kind {
39+
for _, item := range *el {
40+
if item.Extension.Resource.Kind == kind {
3541
return true
3642
}
3743
}

0 commit comments

Comments
 (0)