Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: Upgrade open-component-model/ocm to v0.4.0 #1808

Merged
merged 12 commits into from
Oct 31, 2023
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return nil is not expected, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, the flow just got inverted since this is the last step it can be skipped if there is no reg given

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if no regstry given, this create module command is meaningless, right? It can't push image to remote.

Copy link
Member Author

@lindnerby lindnerby Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess it's like this because the command could also be used for just persisting the archive locally to inspect layers? also the registry flag is also not mandatory which is weird if the command should be meaningless without it. i don't know the intentions/requirements when this command was implemented, but agree that this should have a second look at some point.
and it's not only the registry flow that seems inconsistent: also the generation of the template could be interesting to be used without registry for debugging purposes, but at the moment the flow ends there

}

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