Skip to content

Commit

Permalink
chore: Remove Module Validation from create module command (kyma-proj…
Browse files Browse the repository at this point in the history
…ect#1909)

* Remove module Validation

* Fix
  • Loading branch information
nesmabadr authored Jan 15, 2024
1 parent f7cc8d2 commit 539e188
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 768 deletions.
44 changes: 8 additions & 36 deletions cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package module

import (
"context"
"errors"
"fmt"
"maps"
Expand Down Expand Up @@ -112,7 +111,7 @@ Build a Kubebuilder module my-domain/modC in version 3.2.1 and push it to a loca
kyma alpha create module --name my-domain/modC --version 3.2.1 --path /path/to/module --registry http://localhost:5001/unsigned --insecure
`,
RunE: func(cobraCmd *cobra.Command, args []string) error { return c.Run(cobraCmd.Context()) },
RunE: func(cobraCmd *cobra.Command, args []string) error { return c.Run() },
Aliases: []string{"mod"},
}

Expand Down Expand Up @@ -224,14 +223,9 @@ func configureLegacyFlags(cmd *cobra.Command, o *Options) *cobra.Command {
return cmd
}

type validator interface {
GetCrd() []byte
Run(ctx context.Context, log *zap.SugaredLogger) error
}

const kcpSystemNamespace = "kcp-system"

func (cmd *command) Run(ctx context.Context) error {
func (cmd *command) Run() error {
osFS := osfs.New()

if cmd.opts.CI {
Expand Down Expand Up @@ -275,9 +269,9 @@ func (cmd *command) Run(ctx context.Context) error {
}
cmd.CurrentStep.Successf("Module built")

var crValidator validator
if crValidator, err = cmd.validateDefaultCR(ctx, modDef, l); err != nil {
return err
crd, err := module.GetCrdFromModuleDef(cmd.opts.KubebuilderProject, modDef)
if err != nil {
return nil
}

var archiveFS vfs.FileSystem
Expand Down Expand Up @@ -424,7 +418,7 @@ func (cmd *command) Run(ctx context.Context) error {
}

labels := cmd.getModuleTemplateLabels(modCnf)
annotations := cmd.getModuleTemplateAnnotations(modCnf, crValidator)
annotations := cmd.getModuleTemplateAnnotations(modCnf, crd)

template, err := module.Template(componentVersionAccess, resourceName, namespace,
channel, modDef.DefaultCR, labels, annotations, modDef.CustomStateChecks, mandatoryModule)
Expand Down Expand Up @@ -458,7 +452,7 @@ func (cmd *command) getModuleTemplateLabels(modCnf *Config) map[string]string {
return labels
}

func (cmd *command) getModuleTemplateAnnotations(modCnf *Config, crValidator validator) map[string]string {
func (cmd *command) getModuleTemplateAnnotations(modCnf *Config, crd []byte) map[string]string {
annotations := map[string]string{}
moduleVersion := cmd.opts.Version
if modCnf != nil {
Expand All @@ -467,7 +461,7 @@ func (cmd *command) getModuleTemplateAnnotations(modCnf *Config, crValidator val
moduleVersion = modCnf.Version
}

isClusterScoped := isCrdClusterScoped(crValidator.GetCrd())
isClusterScoped := isCrdClusterScoped(crd)
if isClusterScoped {
annotations[shared.IsClusterScopedAnnotation] = shared.EnableLabelValue
} else {
Expand All @@ -477,28 +471,6 @@ func (cmd *command) getModuleTemplateAnnotations(modCnf *Config, crValidator val
return annotations
}

func (cmd *command) validateDefaultCR(ctx context.Context, modDef *module.Definition, l *zap.SugaredLogger) (validator,
error) {
cmd.NewStep("Validating Default CR")

var crValidator validator
if cmd.opts.KubebuilderProject {
crValidator = module.NewDefaultCRValidator(modDef.DefaultCR, modDef.Source)
} else {
crValidator = module.NewSingleManifestFileCRValidator(modDef.DefaultCR, modDef.SingleManifestPath)
}

if err := crValidator.Run(ctx, l); err != nil {
if errors.Is(err, module.ErrEmptyCR) {
cmd.CurrentStep.Successf("Default CR validation skipped - no default CR")
return crValidator, nil
}
return crValidator, err
}
cmd.CurrentStep.Successf("Default CR validation succeeded")
return crValidator, nil
}

func (cmd *command) getRemote(nameMapping module.NameMapping) (*module.Remote, error) {
res := &module.Remote{
Registry: cmd.opts.RegistryURL,
Expand Down
19 changes: 6 additions & 13 deletions cmd/kyma/alpha/create/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/kyma-project/lifecycle-manager/api/shared"

"github.com/kyma-project/cli/internal/cli"
"github.com/kyma-project/cli/pkg/module"
)

//go:embed testdata/clusterScopedCRD.yaml
Expand Down Expand Up @@ -120,8 +119,8 @@ func Test_command_getModuleTemplateAnnotations(t *testing.T) {
opts *Options
}
type args struct {
modCnf *Config
crValidator validator
modCnf *Config
crd []byte
}
tests := []struct {
name string
Expand All @@ -143,9 +142,7 @@ func Test_command_getModuleTemplateAnnotations(t *testing.T) {
},
Version: "1.1.1",
},
crValidator: &module.SingleManifestFileCRValidator{
Crd: namespacedScopedCrd,
},
crd: namespacedScopedCrd,
},
want: map[string]string{
"annotation1": "value1",
Expand All @@ -167,9 +164,7 @@ func Test_command_getModuleTemplateAnnotations(t *testing.T) {
},
Version: "1.1.1",
},
crValidator: &module.SingleManifestFileCRValidator{
Crd: clusterScopedCrd,
},
crd: clusterScopedCrd,
},
want: map[string]string{
"annotation1": "value1",
Expand All @@ -184,9 +179,7 @@ func Test_command_getModuleTemplateAnnotations(t *testing.T) {
opts: &Options{Version: "1.0.0"},
},
args: args{
crValidator: &module.SingleManifestFileCRValidator{
Crd: namespacedScopedCrd,
},
crd: namespacedScopedCrd,
},
want: map[string]string{
shared.ModuleVersionAnnotation: "1.0.0",
Expand All @@ -200,7 +193,7 @@ func Test_command_getModuleTemplateAnnotations(t *testing.T) {
Command: tt.fields.Command,
opts: tt.fields.opts,
}
if got := cmd.getModuleTemplateAnnotations(tt.args.modCnf, tt.args.crValidator); !reflect.DeepEqual(got,
if got := cmd.getModuleTemplateAnnotations(tt.args.modCnf, tt.args.crd); !reflect.DeepEqual(got,
tt.want) {
t.Errorf("getModuleTemplateAnnotations() = %v, want %v", got, tt.want)
}
Expand Down
100 changes: 0 additions & 100 deletions internal/cli/setup/envtest/setup.go

This file was deleted.

53 changes: 0 additions & 53 deletions internal/envtest/envtest.go

This file was deleted.

Loading

0 comments on commit 539e188

Please sign in to comment.