Skip to content

Commit

Permalink
Follow Up To Maven Access (#781)
Browse files Browse the repository at this point in the history
## Description
This Pull Request is a follow up to the [Pull
Request](#731) adding a
Maven Access Method. This Pull Request adds a corresponding input type,
a blob access and corresponding helper methods in the elements package
(and therefore makes several adjustments).
  • Loading branch information
fabianburth authored May 28, 2024
1 parent ab463b2 commit 509ba89
Show file tree
Hide file tree
Showing 163 changed files with 4,145 additions and 1,458 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ linters-settings:
line-length: 120
gosec:
exclude-generated: true
gocritic:
disabled-checks:
- elseif

issues:
exclude:
Expand Down
3 changes: 2 additions & 1 deletion cmds/ocm/commands/controllercmds/common/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"path/filepath"

"github.com/fluxcd/pkg/ssa"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/out"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func Install(ctx context.Context, octx clictx.Context, sm *ssa.ResourceManager, releaseURL, baseURL, manifest, filename, version string, dryRun bool) error {
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/controllercmds/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/fluxcd/pkg/ssa"
"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/common"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
Expand All @@ -15,6 +14,7 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/names"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
Expand Down
7 changes: 4 additions & 3 deletions cmds/ocm/commands/controllercmds/uninstall/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"time"

"github.com/fluxcd/pkg/ssa"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/names"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/out"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package uninstall

import (
"context"
_ "embed"
"fmt"
"os"

_ "embed"

"github.com/fluxcd/pkg/ssa"
"github.com/mandelsoft/filepath/pkg/filepath"

"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/common"
"github.com/open-component-model/ocm/pkg/out"
)
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/misccmds/credentials/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/mandelsoft/goutils/errors"
"github.com/open-component-model/ocm/pkg/listformat"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand All @@ -15,6 +14,7 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/listformat"
"github.com/open-component-model/ocm/pkg/out"
)

Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/common/addhdlrs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/errkind"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/util/validation/field"

Expand All @@ -18,6 +17,7 @@ import (
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/errkind"
"github.com/open-component-model/ocm/pkg/runtime"
utils2 "github.com/open-component-model/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/utils/template"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/mandelsoft/goutils/errors"

"github.com/open-component-model/ocm/cmds/ocm/pkg/output"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sort"

"github.com/Masterminds/semver/v3"

"github.com/mandelsoft/goutils/errors"

"github.com/open-component-model/ocm/cmds/ocm/pkg/output"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
Expand Down
11 changes: 9 additions & 2 deletions cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
)

var (
HintOption = options.HintOption
MediaTypeOption = options.MediatypeOption
HintOption = options.HintOption
MediaTypeOption = options.MediatypeOption

URLOption = options.URLOption
HTTPHeaderOption = options.HTTPHeaderOption
HTTPVerbOption = options.HTTPVerbOption
HTTPBodyOption = options.HTTPBodyOption
HTTPRedirectOption = options.HTTPRedirectOption

RepositoryOption = options.RepositoryOption
GroupOption = options.GroupOption
ArtifactOption = options.ArtifactOption
ClassifierOption = options.ClassifierOption
ExtensionOption = options.ExtensionOption
)

// string options
Expand Down
1 change: 1 addition & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/dockermulti"
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/file"
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/helm"
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/maven"
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/ociartifact"
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/spiff"
_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/utf8"
Expand Down
32 changes: 32 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/maven/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package maven

import (
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/options"
"github.com/open-component-model/ocm/pkg/cobrautils/flagsets"
)

func ConfigHandler() flagsets.ConfigOptionTypeSetHandler {
return flagsets.NewConfigOptionTypeSetHandler(
TYPE, AddConfig,
options.URLOption,
options.PathOption,
options.GroupOption,
options.ArtifactOption,
options.VersionOption,
// optional
options.ClassifierOption,
options.ExtensionOption,
)
}

func AddConfig(opts flagsets.ConfigOptions, config flagsets.Config) error {
flagsets.AddFieldByOptionP(opts, options.URLOption, config, "repoUrl")
flagsets.AddFieldByOptionP(opts, options.PathOption, config, "path")
flagsets.AddFieldByOptionP(opts, options.GroupOption, config, "groupId")
flagsets.AddFieldByOptionP(opts, options.ArtifactOption, config, "artifactId")
flagsets.AddFieldByOptionP(opts, options.VersionOption, config, "version")
// optional
flagsets.AddFieldByOptionP(opts, options.ClassifierOption, config, "classifier")
flagsets.AddFieldByOptionP(opts, options.ExtensionOption, config, "extension")
return nil
}
96 changes: 96 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/maven/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package maven_test

import (
"crypto"
"github.com/open-component-model/ocm/pkg/maven/maventest"

. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/cmds/ocm/testhelper"

"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/localblob"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
"github.com/open-component-model/ocm/pkg/mime"
"github.com/open-component-model/ocm/pkg/utils/tarutils"
)

const (
ARCH = "test.ca"
VERSION = "v1"
)

var _ = Describe("Test Environment", func() {
var env *TestEnv

BeforeEach(func() {
env = NewTestEnv(TestData(), maventest.TestData("/maven/testdata"))

Expect(env.Execute("create", "ca", "-ft", "directory", "test.de/x", VERSION, "--provider", "mandelsoft", "--file", ARCH)).To(Succeed())
})

AfterEach(func() {
env.Cleanup()
})

It("add maven from file system described by resources.yaml", func() {
Expect(env.Execute("add", "resources", "--file", ARCH, "/testdata/resources1.yaml")).To(Succeed())
data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName))
Expect(err).To(Succeed())
cd, err := compdesc.Decode(data)
Expect(err).To(Succeed())
Expect(len(cd.Resources)).To(Equal(1))
access := Must(env.Context.OCMContext().AccessSpecForSpec(cd.Resources[0].Access)).(*localblob.AccessSpec)
Expect(access.MediaType).To(Equal(mime.MIME_TGZ))
fi := Must(env.FileSystem().Stat(env.Join(ARCH, "blobs", access.LocalReference)))
Expect(fi.Size()).To(Equal(int64(1570)))
li := Must(tarutils.ListArchiveContent(env.Join(ARCH, "blobs", access.LocalReference), env.FileSystem()))
Expect(li).To(ConsistOf(
"sdk-modules-bom-5.7.0-random-content.json",
"sdk-modules-bom-5.7.0-random-content.txt",
"sdk-modules-bom-5.7.0-sources.jar",
"sdk-modules-bom-5.7.0.jar",
"sdk-modules-bom-5.7.0.pom"))
Expect(cd.Resources[0].Digest.HashAlgorithm).To(Equal(crypto.SHA256.String()))
Expect(cd.Resources[0].Digest.Value).To(Equal("16cfb5ced0ea7688dba14aeb0d3aa76ad46e4661bfcc556ffd7287de3b2f7152"))
})

It("add maven from file system described by cli options", func() {
meta := `
name: testdata
type: mavenArtifact
`
Expect(env.Execute("add", "resources", "--file", ARCH, "--resource", meta, "--inputType", "maven",
"--inputPath", "/maven/testdata/.m2/repository", "--groupId", "com.sap.cloud.sdk", "--artifactId", "sdk-modules-bom",
"--inputVersion", "5.7.0", "--classifier", "", "--extension", "pom")).To(Succeed())
data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName))
Expect(err).To(Succeed())
cd, err := compdesc.Decode(data)
Expect(err).To(Succeed())
Expect(len(cd.Resources)).To(Equal(1))
access := Must(env.Context.OCMContext().AccessSpecForSpec(cd.Resources[0].Access)).(*localblob.AccessSpec)
Expect(access.MediaType).To(Equal(mime.MIME_XML))
fi := Must(env.FileSystem().Stat(env.Join(ARCH, "blobs", access.LocalReference)))
Expect(fi.Size()).To(Equal(int64(7153)))
})

It("add maven file from file system described by cli options", func() {
meta := `
name: testdata
type: mavenArtifact
`
Expect(env.Execute("add", "resources", "--file", ARCH, "--resource", meta, "--inputType", "maven",
"--inputPath", "/maven/testdata/.m2/repository", "--groupId", "com.sap.cloud.sdk", "--artifactId", "sdk-modules-bom",
"--inputVersion", "5.7.0", "--extension", "pom")).To(Succeed())
data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName))
Expect(err).To(Succeed())
cd, err := compdesc.Decode(data)
Expect(err).To(Succeed())
Expect(len(cd.Resources)).To(Equal(1))
access := Must(env.Context.OCMContext().AccessSpecForSpec(cd.Resources[0].Access)).(*localblob.AccessSpec)
Expect(access.MediaType).To(Equal(mime.MIME_TGZ))
fi := Must(env.FileSystem().Stat(env.Join(ARCH, "blobs", access.LocalReference)))
Expect(fi.Size()).To(Equal(int64(1109)))
})
})
99 changes: 99 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/maven/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package maven

import (
"fmt"

"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/cpi"
"github.com/open-component-model/ocm/pkg/blobaccess"
mavenblob "github.com/open-component-model/ocm/pkg/blobaccess/maven"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr"
"github.com/open-component-model/ocm/pkg/maven"
)

type Spec struct {
cpi.PathSpec `json:",inline"`
// RepoUrl defines the url from which the artifact is downloaded.
RepoUrl string `json:"repoUrl,omitempty"`

maven.Coordinates `json:",inline"`
}

var _ inputs.InputSpec = (*Spec)(nil)

func New(repoUrl, groupId, artifactId, version string, classifier, extension *string) *Spec {
return &Spec{
PathSpec: cpi.NewPathSpec(TYPE, ""),
RepoUrl: repoUrl,
Coordinates: *maven.NewCoordinates(groupId, artifactId, version,
maven.WithOptionalClassifier(classifier),
maven.WithOptionalExtension(extension)),
}
}

func NewForFilePath(filePath, groupId, artifactId, version string, classifier, extension *string) *Spec {
return &Spec{
PathSpec: cpi.NewPathSpec(TYPE, filePath),
RepoUrl: "",
Coordinates: *maven.NewCoordinates(groupId, artifactId, version,
maven.WithOptionalClassifier(classifier),
maven.WithOptionalExtension(extension)),
}
}

func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath string) field.ErrorList {
var allErrs field.ErrorList
if s.RepoUrl == "" {
allErrs = s.PathSpec.Validate(fldPath, ctx, inputFilePath)
} else {
if s.Path != "" {
pathField := fldPath.Child("path")
allErrs = append(allErrs, field.Forbidden(pathField, "only path or repoUrl can be specified, not both"))
}
}
if s.ArtifactId == "" {
pathField := fldPath.Child("artifactId")
allErrs = append(allErrs, field.Invalid(pathField, s.ArtifactId, "no artifact id"))
}
if s.GroupId == "" {
pathField := fldPath.Child("groupId")
allErrs = append(allErrs, field.Invalid(pathField, s.GroupId, "no group id"))
}
if s.Version == "" {
pathField := fldPath.Child("version")
allErrs = append(allErrs, field.Invalid(pathField, s.GroupId, "no group id"))
}

return allErrs
}

func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) {
var repo *maven.Repository
var err error

fs := ctx.FileSystem()
if s.Path != "" {
inputInfo, inputPath, err := inputs.FileInfo(ctx, s.Path, info.InputFilePath)
if err != nil {
return nil, "", err
}
if !inputInfo.IsDir() {
return nil, "", fmt.Errorf("maven file repository must be a directory")
}
repo = maven.NewFileRepository(inputPath, fs)
} else {
repo, err = maven.NewUrlRepository(s.RepoUrl, fs)
if err != nil {
return nil, "", err
}
}
access, err := mavenblob.BlobAccessForMavenCoords(repo, &s.Coordinates,
mavenblob.WithCredentialContext(ctx),
mavenblob.WithLoggingContext(ctx),
mavenblob.WithCachingFileSystem(vfsattr.Get(ctx)),
)

return access, "", err
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mvn_test
package maven_test

import (
"testing"
Expand All @@ -9,5 +9,5 @@ import (

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Maven (mvn) Test Suite")
RunSpecs(t, "Input Type maven")
}
Loading

0 comments on commit 509ba89

Please sign in to comment.