Skip to content

Commit

Permalink
deps: Upgrade open-component-model/ocm to v0.4.0 (#1808)
Browse files Browse the repository at this point in the history
* Bump ocm dep

* Use new identity pkg

* Remove unnecessary register calls

* don't close the archive

* fix test

* offer init component descriptor

* create ComponentDescriptor before CreateArchive

* Remove unecessary git source code. Refactor module.go

* fix test

* wrap test cases

* apply review suggestions

* apply review suggestions

---------

Co-authored-by: Xin Ruan <xin.ruan@sap.com>
  • Loading branch information
lindnerby and ruanxin committed Oct 31, 2023
1 parent ae3e63b commit f6ad108
Show file tree
Hide file tree
Showing 14 changed files with 788 additions and 388 deletions.
155 changes: 74 additions & 81 deletions cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/mandelsoft/vfs/pkg/vfs"
"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/repositories/comparch"
"github.com/spf13/cobra"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -275,7 +274,6 @@ func (cmd *command) Run(ctx context.Context) error {
return err
}

cmd.NewStep("Creating module archive")
var archiveFS vfs.FileSystem
if cmd.opts.PersistentArchive {
archiveFS = osFS
Expand All @@ -285,7 +283,14 @@ func (cmd *command) Run(ctx context.Context) error {
l.Info("using in-memory archive")
}

var archive *comparch.ComponentArchive
cmd.NewStep("Creating component descriptor")
componentDescriptor, err := module.InitComponentDescriptor(modDef)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("component descriptor created")

gitPath, err := files.SearchForTargetDirByName(modDef.Source, ".git")
if gitPath == "" || err != nil {
l.Warnf("could not find git repository root, using %s directory", modDef.Source)
Expand All @@ -301,120 +306,108 @@ func (cmd *command) Run(ctx context.Context) error {
}
}

archive, err = module.CreateArchive(archiveFS, cmd.opts.ModuleArchivePath, cmd.opts.GitRemote, modDef, false)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
} else {
l.Infof("found git repository root at %s", gitPath)
l.Infof("adding sources to the layer")
l.Infof("found git repository root at %s: adding git sources to the layer", gitPath)
modDef.Source = gitPath // set the source to the git root
archive, err = module.CreateArchive(archiveFS, cmd.opts.ModuleArchivePath, cmd.opts.GitRemote, modDef, true)
if err != nil {
if err := module.AddGitSources(componentDescriptor, modDef, cmd.opts.GitRemote); err != nil {
cmd.CurrentStep.Failure()
return err
}
}

cmd.CurrentStep.Successf("Module archive created")

cmd.NewStep("Adding layers to archive...")

if err := module.AddResources(archive, modDef, l, osFS, cmd.opts.RegistryCredSelector); err != nil {
cmd.CurrentStep.Failure()
return err
}

cmd.CurrentStep.Success()

// Security Scan
if cmd.opts.SecurityScanConfig != "" && gitPath != "" { // security scan is only supported for target git repositories
cmd.NewStep("Configuring security scanning...")
if files.IsFileExists(cmd.opts.SecurityScanConfig) {
err = module.AddSecurityScanningMetadata(archive.GetDescriptor(), cmd.opts.SecurityScanConfig)
err = module.AddSecurityScanningMetadata(componentDescriptor, cmd.opts.SecurityScanConfig)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
if err := archive.Update(); err != nil {
return fmt.Errorf("could not write security scanning configuration into archive: %w", err)
}
cmd.CurrentStep.Successf("Security scanning configured")
} else {
l.Warnf("Security scanning configuration was skipped")
cmd.CurrentStep.Failure()
}
}

/* -- PUSH & TEMPLATE -- */
cmd.NewStep("Creating module archive...")
archive, err := module.CreateArchive(archiveFS, cmd.opts.ModuleArchivePath, componentDescriptor)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Module archive created")

if cmd.opts.RegistryURL != "" {
cmd.NewStep("Adding layers to archive...")
if err = module.AddResources(archive, modDef, l, osFS, cmd.opts.RegistryCredSelector); err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Layers successfully added to archive")

cmd.NewStep(fmt.Sprintf("Pushing image to %q", cmd.opts.RegistryURL))
remote, err := cmd.getRemote(modDef.NameMappingMode)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
if cmd.opts.RegistryURL == "" {
return nil
}

componentVersionAccess, err := remote.Push(archive, cmd.opts.ArchiveVersionOverwrite)
cmd.NewStep(fmt.Sprintf("Pushing image to %q", cmd.opts.RegistryURL))
remote, err := cmd.getRemote(modDef.NameMappingMode)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
componentVersionAccess, err := remote.Push(archive, cmd.opts.ArchiveVersionOverwrite)
if err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Module successfully pushed")

if err != nil {
if cmd.opts.PrivateKeyPath != "" {
cmd.NewStep("Fetching and signing component descriptor...")
signConfig := &module.ComponentSignConfig{
Name: modDef.Name,
Version: modDef.Version,
KeyPath: cmd.opts.PrivateKeyPath,
}
if err = module.Sign(signConfig, remote); err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Module successfully pushed to %q", cmd.opts.RegistryURL)

if cmd.opts.PrivateKeyPath != "" {
signCfg := &module.ComponentSignConfig{
Name: modDef.Name,
Version: modDef.Version,
KeyPath: cmd.opts.PrivateKeyPath,
}

cmd.NewStep("Fetching and signing component descriptor...")
if err = module.Sign(signCfg, remote); err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Success()
}

cmd.NewStep("Generating module template")

var resourceName = ""
if modCnf != nil {
resourceName = modCnf.ResourceName
}
cmd.CurrentStep.Success()
}

var channel = cmd.opts.Channel
if modCnf != nil {
channel = modCnf.Channel
}
cmd.NewStep("Generating module template...")
var resourceName = ""
if modCnf != nil {
resourceName = modCnf.ResourceName
}

var namespace = cmd.opts.Namespace
if modCnf != nil && modCnf.Namespace != "" {
namespace = modCnf.Namespace
}
var channel = cmd.opts.Channel
if modCnf != nil {
channel = modCnf.Channel
}

labels := cmd.getModuleTemplateLabels(modCnf)
annotations := cmd.getModuleTemplateAnnotations(modCnf, crValidator)
var namespace = cmd.opts.Namespace
if modCnf != nil && modCnf.Namespace != "" {
namespace = modCnf.Namespace
}

t, err := module.Template(componentVersionAccess, resourceName, namespace,
channel, modDef.DefaultCR, labels, annotations, modDef.CustomStateChecks)
labels := cmd.getModuleTemplateLabels(modCnf)
annotations := cmd.getModuleTemplateAnnotations(modCnf, crValidator)

if err != nil {
cmd.CurrentStep.Failure()
return err
}
template, err := module.Template(componentVersionAccess, resourceName, namespace,
channel, modDef.DefaultCR, labels, annotations, modDef.CustomStateChecks)
if err != nil {
cmd.CurrentStep.Failure()
return err
}

if err := vfs.WriteFile(osFS, cmd.opts.TemplateOutput, t, os.ModePerm); err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Template successfully generated at %s", cmd.opts.TemplateOutput)
if err := vfs.WriteFile(osFS, cmd.opts.TemplateOutput, template, os.ModePerm); err != nil {
cmd.CurrentStep.Failure()
return err
}
cmd.CurrentStep.Successf("Template successfully generated at %s", cmd.opts.TemplateOutput)

return nil
}
Expand Down
Loading

0 comments on commit f6ad108

Please sign in to comment.