Skip to content

Commit

Permalink
fix: Allow creating module for existing versions (#1865)
Browse files Browse the repository at this point in the history
* Fix logging

* Implementation for the difference detection

* Adjust E2E test

* Fix E2E test

* Add OCI interface

* Start unit test

* Fix mock

* Unit test

* Code review comments
  • Loading branch information
nesmabadr authored Nov 30, 2023
1 parent 6182b47 commit c4cc821
Show file tree
Hide file tree
Showing 11 changed files with 587 additions and 75 deletions.
24 changes: 21 additions & 3 deletions .github/workflows/test-e2e-create-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ jobs:
- name: Run create module with kubebuilder-project
if: ${{ matrix.e2e-test == 'test-kubebuilder-module-creation' }}
run: |
cd ./template-operator
kyma alpha create module \
--name kyma-project.io/module/template-operator \
--path ./template-operator \
--path . \
--registry http://k3d-oci.localhost:5001 \
--insecure \
--kubebuilder-project \
--version $MODULE_TEMPLATE_VERSION -v \
--output /tmp/kubebuilder-template.yaml \
--sec-scanners-config ./template-operator/sec-scanners-config.yaml
--sec-scanners-config sec-scanners-config.yaml
echo "MODULE_TEMPLATE_PATH=/tmp/kubebuilder-template.yaml" >> "$GITHUB_ENV"
- name: Run create module with module-config
if: ${{ matrix.e2e-test == 'test-moduleconfig-module-creation' || matrix.e2e-test == 'test-same-version-module-creation'}}
Expand All @@ -75,8 +76,25 @@ jobs:
--insecure \
--module-config-file ./module-config.yaml \
--version $MODULE_TEMPLATE_VERSION -v \
--output /tmp/module-config-template.yaml
--sec-scanners-config sec-scanners-config.yaml \
--output /tmp/module-config-template.yaml
echo "MODULE_TEMPLATE_PATH=/tmp/module-config-template.yaml" >> "$GITHUB_ENV"
- name: Create a different security scanners config file for different layers
if: ${{matrix.e2e-test == 'test-same-version-module-creation'}}
run: |
cd ./template-operator
echo \
"module-name: template-operator
rc-tag: 0.5.0
dev-branch: main
protecode:
- europe-west3-docker.pkg.dev/sap-kyma-jellyfish-dev/template-operator/component-descriptors/kyma-project.io/template-operator:v1.0.0-e2e-warning
whitesource:
language: golang-mod
exclude:
- \"**/test/**\"
- \"**/*_test.go\"" > sec-scanners-config-changed.yaml
cat sec-scanners-config-changed.yaml
- name: Verify module template
if: ${{ matrix.e2e-test == 'test-moduleconfig-module-creation' || matrix.e2e-test == 'test-kubebuilder-module-creation'}}
run: |
Expand Down
57 changes: 44 additions & 13 deletions cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/mandelsoft/vfs/pkg/memoryfs"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
compdescv2 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/versions/v2"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/spf13/cobra"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -128,7 +130,8 @@ Build a Kubebuilder module my-domain/modC in version 3.2.1 and push it to a loca
"Uses the host filesystem instead of in-memory archiving to build the module.",
)

cmd.Flags().BoolVar(&o.ArchiveVersionOverwrite, "module-archive-version-overwrite", false, "Overwrites existing component's versions of the module. If set to false, the push is a No-Op.")
cmd.Flags().BoolVar(&o.ArchiveVersionOverwrite, "module-archive-version-overwrite", false,
"Overwrites existing component's versions of the module. If set to false, the push is a No-Op.")

cmd.Flags().StringVar(
&o.GitRemote, "git-remote", "origin",
Expand Down Expand Up @@ -184,7 +187,8 @@ Build a Kubebuilder module my-domain/modC in version 3.2.1 and push it to a loca
&o.PrivateKeyPath, "key", "", "Specifies the path where a private key is used for signing.",
)

cmd.Flags().BoolVar(&o.KubebuilderProject, "kubebuilder-project", false, "Specifies provided module is a Kubebuilder Project.")
cmd.Flags().BoolVar(&o.KubebuilderProject, "kubebuilder-project", false,
"Specifies provided module is a Kubebuilder Project.")

configureLegacyFlags(cmd, o)

Expand Down Expand Up @@ -213,7 +217,8 @@ func configureLegacyFlags(cmd *cobra.Command, o *Options) *cobra.Command {

cmd.Flags().StringVar(&o.Channel, "channel", "regular", "Channel to use for the module template.")

cmd.Flags().StringVar(&o.Namespace, "namespace", kcpSystemNamespace, "Specifies the namespace where the ModuleTemplate is deployed.")
cmd.Flags().StringVar(&o.Namespace, "namespace", kcpSystemNamespace,
"Specifies the namespace where the ModuleTemplate is deployed.")

return cmd
}
Expand Down Expand Up @@ -356,12 +361,36 @@ func (cmd *command) Run(ctx context.Context) error {
cmd.CurrentStep.Failure()
return err
}
componentVersionAccess, err := remote.Push(archive, cmd.opts.ArchiveVersionOverwrite)

repo, err := remote.GetRepository(cpi.DefaultContext())
if err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Module successfully pushed")

var componentVersionAccess ocm.ComponentVersionAccess
shouldPushArchive, err := remote.ShouldPushArchive(repo, archive, cmd.opts.ArchiveVersionOverwrite)
if err != nil {
cmd.CurrentStep.Failure()
return err
}

if shouldPushArchive {
componentVersionAccess, err = remote.Push(repo, archive, cmd.opts.ArchiveVersionOverwrite)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Module successfully pushed")
} else {
componentVersionAccess, err = remote.GetComponentVersion(archive, repo)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf(fmt.Sprintf("Module already exists. Retrieved image from %q",
cmd.opts.RegistryURL))
}

if cmd.opts.PrivateKeyPath != "" {
cmd.NewStep("Fetching and signing component descriptor...")
Expand Down Expand Up @@ -447,7 +476,8 @@ 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) {
func (cmd *command) validateDefaultCR(ctx context.Context, modDef *module.Definition, l *zap.SugaredLogger) (validator,
error) {
cmd.NewStep("Validating Default CR")

var crValidator validator
Expand All @@ -470,11 +500,12 @@ func (cmd *command) validateDefaultCR(ctx context.Context, modDef *module.Defini

func (cmd *command) getRemote(nameMapping module.NameMapping) (*module.Remote, error) {
res := &module.Remote{
Registry: cmd.opts.RegistryURL,
NameMapping: nameMapping,
Credentials: cmd.opts.Credentials,
Token: cmd.opts.Token,
Insecure: cmd.opts.Insecure,
Registry: cmd.opts.RegistryURL,
NameMapping: nameMapping,
Credentials: cmd.opts.Credentials,
Token: cmd.opts.Token,
Insecure: cmd.opts.Insecure,
OciRepoAccess: &module.OciRepo{},
}

if strings.HasPrefix(strings.ToLower(cmd.opts.RegistryURL), "https:") {
Expand Down Expand Up @@ -509,7 +540,7 @@ func (cmd *command) moduleDefinitionFromOptions() (*module.Definition, *Config,
np := nice.Nice{}
np.PrintImportant("WARNING: The Kubebuilder support is DEPRECATED. Use the simple mode by providing the \"--module-config-file\" flag instead.")

//legacy approach, flag-based
// legacy approach, flag-based
def = &module.Definition{
Name: cmd.opts.Name,
Version: cmd.opts.Version,
Expand All @@ -523,7 +554,7 @@ func (cmd *command) moduleDefinitionFromOptions() (*module.Definition, *Config,
return def, cnf, nil
}

//new approach, config-file based
// new approach, config-file based
moduleConfig, err := ParseConfig(cmd.opts.ModuleConfigFile)
if err != nil {
return nil, nil, err
Expand Down
14 changes: 8 additions & 6 deletions cmd/kyma/alpha/verify/module/module.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package module

import (
"github.com/spf13/cobra"

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

type command struct {
Expand Down Expand Up @@ -80,11 +81,12 @@ func (c *command) Run(_ []string) error {
}

remote := &module.Remote{
Registry: c.opts.RegistryURL,
NameMapping: nameMappingMode,
Credentials: c.opts.Credentials,
Token: c.opts.Token,
Insecure: c.opts.Insecure,
Registry: c.opts.RegistryURL,
NameMapping: nameMappingMode,
Credentials: c.opts.Credentials,
Token: c.opts.Token,
Insecure: c.opts.Insecure,
OciRepoAccess: &module.OciRepo{},
}

if err := module.Verify(signCfg, remote); err != nil {
Expand Down
Loading

0 comments on commit c4cc821

Please sign in to comment.