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
122 changes: 59 additions & 63 deletions cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ func (cmd *command) Run(ctx context.Context) error {
}

cmd.NewStep("Creating component descriptor")

componentDescriptor, err := module.InitComponentDescriptor(modDef)
if err != nil {
cmd.CurrentStep.Failure()
Expand All @@ -308,10 +307,9 @@ func (cmd *command) Run(ctx context.Context) error {
}

} 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
if err := module.AddSources(componentDescriptor, modDef, cmd.opts.GitRemote); err != nil {
if err := module.AddGitSources(componentDescriptor, modDef, cmd.opts.GitRemote); err != nil {
cmd.CurrentStep.Failure()
return err
}
Expand All @@ -333,85 +331,83 @@ func (cmd *command) Run(ctx context.Context) error {
}
}

cmd.NewStep("Creating module archive")

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")

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

if err := module.AddResources(archive, modDef, l, osFS, cmd.opts.RegistryCredSelector); err != nil {
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.CurrentStep.Success()
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

}

if cmd.opts.RegistryURL != "" {
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)
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
27 changes: 2 additions & 25 deletions internal/files/files.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// package files provides all functionality to manage kyma CLI local files.
package files

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/user"
"path/filepath"

"github.com/mandelsoft/vfs/pkg/vfs"
)

const kymaFolder = ".kyma"
Expand All @@ -33,7 +29,7 @@ func KymaHome() (string, error) {
return p, nil
}

// isDir determines if a file represented by `path` is a directory or not
// IsDir determines if a file represented by `path` is a directory or not
func IsDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
Expand All @@ -57,25 +53,6 @@ func IsDirEmpty(path string) (bool, error) {
return false, err
}

// FileType returns the mimetype of a file.
func FileType(fs vfs.FileSystem, path string) (string, error) {
file, err := fs.Open(path)
if err != nil {
return "", err
}
defer file.Close()
// Only the first 512 bytes are used to sniff the content type.
buf := make([]byte, 512)
_, err = file.Read(buf)
if err != nil {
return "", err
}

// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
return http.DetectContentType(buf), nil
}

// SearchForTargetDirByName walks the given root path and searches for a directory with the given name.
// If the directory is found the function returns the path to the directory and nil as error.
// If the directory is not found the function returns an empty string and an error.
Expand All @@ -91,7 +68,7 @@ func SearchForTargetDirByName(root string, targetFolderName string) (gitFolderPa
return nil
})

if walkErr == errCustomSkipSearch {
if errors.Is(walkErr, errCustomSkipSearch) {
return gitFolderPath, nil
}
return
Expand Down
28 changes: 11 additions & 17 deletions pkg/module/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package module

import (
"fmt"
"os"
"strings"

"github.com/kyma-project/cli/pkg/module/gitsource"
"github.com/mandelsoft/vfs/pkg/projectionfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/common/accessobj"
ocm "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
"os"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -61,21 +59,17 @@ func CreateArchive(fs vfs.FileSystem, path string, cd *ocm.ComponentDescriptor)
)
}

// AddSources adds the sources to the component descriptor. If the def.Source is a git repository
func AddSources(cd *ocm.ComponentDescriptor, def *Definition, gitRemote string) error {
if strings.HasSuffix(def.Source, ".git") {

Choose a reason for hiding this comment

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

This condition was added as part of #1751. Is it still working?

Copy link
Member Author

@lindnerby lindnerby Oct 30, 2023

Choose a reason for hiding this comment

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

The overall flow of this adding of git source meta information is very illogical and needed some changes. One of these is I made this function explicit, see addGitSources

gitSource := gitsource.NewGitSource()
var err error
if def.Repo, err = gitSource.DetermineRepositoryURL(gitRemote, def.Repo, def.Source); err != nil {
return err
}
src, err := gitSource.FetchSource(def.Source, def.Repo, def.Version)

if err != nil {
return err
}
appendSourcesForCd(cd, src)
// AddGitSources adds the git sources to the component descriptor
func AddGitSources(cd *ocm.ComponentDescriptor, def *Definition, gitRemote string) error {
var err error
if def.Repo, err = gitsource.DetermineRepositoryURL(gitRemote, def.Repo, def.Source); err != nil {
return err
}
src, err := gitsource.FetchSource(def.Source, def.Repo, def.Version)
if err != nil {
return err
}
appendSourcesForCd(cd, src)

return nil
}
Expand Down
47 changes: 29 additions & 18 deletions pkg/module/gitsource/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/github"
ocm "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
ocmv1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/pkg/errors"
)

func (g GitSource) FetchSource(path, repo, version string) (*ocm.Source, error) {
ref, commit, err := g.getGitInfo(path)
const (
ocmIdentityName = "module-sources"
ocmVersion = "v1"
refLabel = "git.kyma-project.io/ref"
)

var (
errNotGit = errors.New("not a git repository")
)

func FetchSource(path, repo, version string) (*ocm.Source, error) {
ref, commit, err := getGitInfo(path)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +52,7 @@ func (g GitSource) FetchSource(path, repo, version string) (*ocm.Source, error)
}, nil
}

func (g GitSource) DetermineRepositoryURL(gitRemote, repo, repoPath string) (string, error) {
func DetermineRepositoryURL(gitRemote, repo, repoPath string) (string, error) {
if repo != "" {
return repo, nil
}
Expand All @@ -64,10 +75,9 @@ func (g GitSource) DetermineRepositoryURL(gitRemote, repo, repoPath string) (str
return repo, nil
}

func (g GitSource) getGitInfo(gitPath string) (string, string, error) {
func getGitInfo(gitPath string) (string, string, error) {
if gitPath == "" {
return "", "", fmt.Errorf("could not get git information, the path is empty")

}

if gitPath == string(filepath.Separator) {
Expand Down Expand Up @@ -97,18 +107,19 @@ func fetchRepoURLFromRemotes(gitRemotes []*git.Remote, remoteName string) (strin
}
}

if remote.Config() != nil {
// get remote URL and convert to HTTPS in case it is an SSH URL
httpURL := remote.Config().URLs[0]
if strings.HasPrefix(httpURL, "git@") {
httpURL = strings.Replace(httpURL, ":", "/", 1)
httpURL = strings.Replace(httpURL, "git@", "https://", 1)
}
repoURL, err := url.Parse(httpURL)
if err != nil {
return "", fmt.Errorf("could not parse repository URL %q: %w", httpURL, err)
}
return repoURL.String(), nil
if remote.Config() == nil {
return "", fmt.Errorf("could not find git remote in: %s", remoteName)
}

// get remote URL and convert to HTTPS in case it is an SSH URL
httpURL := remote.Config().URLs[0]
if strings.HasPrefix(httpURL, "git@") {
httpURL = strings.Replace(httpURL, ":", "/", 1)
httpURL = strings.Replace(httpURL, "git@", "https://", 1)
}
repoURL, err := url.Parse(httpURL)
if err != nil {
return "", fmt.Errorf("could not parse repository URL %q: %w", httpURL, err)
}
return "", fmt.Errorf("could not find git remote in: %s", remoteName)
return repoURL.String(), nil
}
21 changes: 0 additions & 21 deletions pkg/module/gitsource/source.go

This file was deleted.

Loading