Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

ocm command and some ctf cmd extension #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/component-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"os"

"github.com/gardener/component-cli/cmd/component-cli/app"
"github.com/gardener/component-cli/cmd/ocm/app"
)

func main() {
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions cmd/ocm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0

package main

import (
"context"
"fmt"
"os"

"github.com/gardener/component-cli/cmd/ocm/app"
)

func main() {
ctx := context.Background()
defer ctx.Done()
cmd := app.NewComponentsCliCommand(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also rename the function from NewComponentsCliCommand() to NewOCMCliCommand() ?


if err := cmd.Execute(); err != nil {
fmt.Print(err)
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion docs/reference/component-cli_ctf_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Adds component archives to a ctf

```
component-cli ctf add CTF_PATH [-f component-archive]... [flags]
component-cli ctf add <CTF_PATH> <component-archive> ... [flags]
```

### Options
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/component-cli_ctf_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Note: Currently only component archives are supoprted. Generic OCI Artifacts wil


```
component-cli ctf push CTF_PATH [flags]
component-cli ctf push <CTF_PATH> ... [flags]
```

### Options
Expand Down
4 changes: 2 additions & 2 deletions hack/generate-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

"github.com/spf13/cobra/doc"

"github.com/gardener/component-cli/cmd/component-cli/app"
"github.com/gardener/component-cli/cmd/ocm/app"
)

func main() {
fmt.Println("> Generate Docs for ComponentCli")

if len(os.Args) != 2 { // expect 2 as the first one is the programm name
fmt.Printf("Expected exactly one argument, but got %d", len(os.Args) - 1)
fmt.Printf("Expected exactly one argument, but got %d", len(os.Args)-1)
os.Exit(1)
}
outputDir := os.Args[1]
Expand Down
5 changes: 5 additions & 0 deletions pkg/commands/componentarchive/componentarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
pflag "github.com/spf13/pflag"

"github.com/gardener/component-cli/pkg/commands/componentarchive/componentreferences"
"github.com/gardener/component-cli/pkg/commands/componentarchive/get"
"github.com/gardener/component-cli/pkg/commands/componentarchive/remote"
"github.com/gardener/component-cli/pkg/commands/componentarchive/resources"
"github.com/gardener/component-cli/pkg/commands/componentarchive/set"
"github.com/gardener/component-cli/pkg/commands/componentarchive/sources"
ctfcmd "github.com/gardener/component-cli/pkg/commands/ctf"
"github.com/gardener/component-cli/pkg/componentarchive"
Expand Down Expand Up @@ -74,6 +76,8 @@ func NewComponentArchiveCommand(ctx context.Context) *cobra.Command {
cmd.AddCommand(resources.NewResourcesCommand(ctx))
cmd.AddCommand(componentreferences.NewCompRefCommand(ctx))
cmd.AddCommand(sources.NewSourcesCommand(ctx))
cmd.AddCommand(set.NewSetCommand(ctx))
cmd.AddCommand(get.NewGetCommand(ctx))
return cmd
}

Expand Down Expand Up @@ -110,6 +114,7 @@ func (o *ComponentArchiveOptions) Run(ctx context.Context, log logr.Logger, fs v
return fmt.Errorf("unable to add component archive to ctf: %w", err)
}
log.Info("Successfully added ctf\n")
return nil
}

// only copy essential files to the temp dir
Expand Down
6 changes: 6 additions & 0 deletions pkg/commands/componentarchive/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package componentarchive

import (
"context"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -70,6 +71,11 @@ func (o *CreateOptions) Complete(args []string) error {
}

func (o *CreateOptions) validate() error {
if o.Overwrite && len(o.Name) != 0 {
if len(o.Version) == 0 {
return errors.New("a version has to be provided for a minimal component descriptor")
}
}
return o.BuilderOptions.Validate()
}

Expand Down
113 changes: 113 additions & 0 deletions pkg/commands/componentarchive/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0

package get

import (
"context"
"errors"
"fmt"
"os"

cdvalidation "github.com/gardener/component-spec/bindings-go/apis/v2/validation"
"github.com/go-logr/logr"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/gardener/component-cli/pkg/componentarchive"
"github.com/gardener/component-cli/pkg/logger"
)

// Options defines the options that are used to add resources to a component descriptor
Copy link
Contributor

Choose a reason for hiding this comment

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

Docstring seems misleading to me

type Options struct {
componentarchive.BuilderOptions
Property string
}

// NewGetCommand creates a command to add additional resources to a component descriptor.
Copy link
Contributor

Choose a reason for hiding this comment

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

Docstring seems misleading to me

func NewGetCommand(ctx context.Context) *cobra.Command {
opts := &Options{}
cmd := &cobra.Command{
Use: "get COMPONENT_ARCHIVE_PATH [options...]",
Args: cobra.MinimumNArgs(1),
Short: "set some component descriptor properties",
Copy link
Contributor

Choose a reason for hiding this comment

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

Doc seems misleading to me

Long: `
the set command sets some component descriptor properies like the component name and/or version.
Copy link
Contributor

Choose a reason for hiding this comment

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

Doc seems misleading to me


The component archive can be specified by the first argument, the flag "--archive" or as env var "COMPONENT_ARCHIVE_PATH".
The component archive is expected to be a filesystem archive.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := opts.Complete(args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

if err := opts.Run(ctx, logger.Log, osfs.New()); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
},
}

opts.AddFlags(cmd.Flags())

return cmd
}

func (o *Options) Run(ctx context.Context, log logr.Logger, fs vfs.FileSystem) error {
result, err := o.Get(ctx, log, fs)
if err != nil {
return err
}
fmt.Println(result)
return nil
}

func (o *Options) Get(ctx context.Context, log logr.Logger, fs vfs.FileSystem) (string, error) {
o.Modify = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you set the Modify flag to true if the command is only a "get"?

archive, err := o.BuilderOptions.Build(fs)
if err != nil {
return "", err
}

if err := cdvalidation.Validate(archive.ComponentDescriptor); err != nil {
return "", fmt.Errorf("invalid component descriptor: %w", err)
}

switch o.Property {
case "name":
return archive.ComponentDescriptor.Name, nil
case "version":
return archive.ComponentDescriptor.Version, nil
}

return "", nil
}

func (o *Options) Complete(args []string) error {

if len(args) == 0 {
return errors.New("at least a component archive path argument has to be defined")
}
o.BuilderOptions.ComponentArchivePath = args[0]
o.BuilderOptions.Default()

return o.validate()
}

func (o *Options) validate() error {
if len(o.Property) == 0 {
return errors.New("a property must be specified")
}
return o.BuilderOptions.Validate()
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Property, "property", "", "name of the property (name or version)")

o.BuilderOptions.AddFlags(fs)
}
60 changes: 60 additions & 0 deletions pkg/commands/componentarchive/get/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0

package get_test

import (
"context"
"testing"

"github.com/go-logr/logr"
"github.com/mandelsoft/vfs/pkg/layerfs"
"github.com/mandelsoft/vfs/pkg/memoryfs"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/projectionfs"
"github.com/mandelsoft/vfs/pkg/vfs"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/gardener/component-cli/pkg/commands/componentarchive/get"
"github.com/gardener/component-cli/pkg/componentarchive"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Resources Test Suite")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
RunSpecs(t, "Resources Test Suite")
RunSpecs(t, "Component Archive Get Test Suite")

}

var _ = Describe("Set", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var _ = Describe("Set", func() {
var _ = Describe("Get", func() {


var testdataFs vfs.FileSystem

BeforeEach(func() {
fs, err := projectionfs.New(osfs.New(), "./testdata")
Expect(err).ToNot(HaveOccurred())
testdataFs = layerfs.New(memoryfs.New(), fs)
})

It("should get name", func() {
opts := &get.Options{
BuilderOptions: componentarchive.BuilderOptions{
ComponentArchivePath: "./00-component",
},
Property: "name",
}

Expect(opts.Get(context.TODO(), logr.Discard(), testdataFs)).To(Equal("example.com/component"))
})

It("should get version", func() {
opts := &get.Options{
BuilderOptions: componentarchive.BuilderOptions{
ComponentArchivePath: "./00-component",
},
Property: "version",
}

Expect(opts.Get(context.TODO(), logr.Discard(), testdataFs)).To(Equal("v0.0.0"))
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
component:
componentReferences: []
name: example.com/component
provider: internal
repositoryContexts:
- baseUrl: eu.gcr.io/gardener-project/components/dev
type: ociRegistry
resources:
- name: 'ubuntu'
version: 'v0.0.1'
type: 'ociImage'
relation: 'external'
access:
type: 'ociRegistry'
imageReference: 'ubuntu:18.0'
sources: []
version: v0.0.0
meta:
schemaVersion: v2
11 changes: 9 additions & 2 deletions pkg/commands/componentarchive/remote/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (o *PushOptions) Run(ctx context.Context, log logr.Logger, fs vfs.FileSyste
}
// update repository context
if len(o.BaseUrl) != 0 {
log.Info(fmt.Sprintf("Update repository context in component descriptor %q", o.BaseUrl))
if err := cdv2.InjectRepositoryContext(archive.ComponentDescriptor, cdv2.NewOCIRegistryRepository(o.BaseUrl, "")); err != nil {
return fmt.Errorf("unable to add repository context to component descriptor: %w", err)
}
Expand All @@ -95,7 +96,12 @@ func (o *PushOptions) Run(ctx context.Context, log logr.Logger, fs vfs.FileSyste
return fmt.Errorf("unable to build oci artifact for component acrchive: %w", err)
}

ref, err := components.OCIRef(archive.ComponentDescriptor.GetEffectiveRepositoryContext(), archive.ComponentDescriptor.Name, archive.ComponentDescriptor.Version)
rctx := archive.ComponentDescriptor.GetEffectiveRepositoryContext()
if rctx == nil {
return fmt.Errorf("no repository context given")
}
// attention nil struct pointer passed to interface parameter (non-nil value)
ref, err := components.OCIRef(rctx, archive.ComponentDescriptor.Name, archive.ComponentDescriptor.Version)
if err != nil {
return fmt.Errorf("invalid component reference: %w", err)
}
Expand All @@ -105,7 +111,8 @@ func (o *PushOptions) Run(ctx context.Context, log logr.Logger, fs vfs.FileSyste
log.Info(fmt.Sprintf("Successfully uploaded component descriptor at %q", ref))

for _, tag := range o.AdditionalTags {
ref, err := components.OCIRef(archive.ComponentDescriptor.GetEffectiveRepositoryContext(), archive.ComponentDescriptor.Name, tag)
log.Info(fmt.Sprintf("Push for additional tag %q", tag))
ref, err := components.OCIRef(rctx, archive.ComponentDescriptor.Name, tag)
if err != nil {
return fmt.Errorf("invalid component reference: %w", err)
}
Expand Down
Loading