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

Commit

Permalink
add automatic minimal component descriptor generation if it does not …
Browse files Browse the repository at this point in the history
…exist
  • Loading branch information
Tim Schrodi committed Dec 18, 2020
1 parent 91a85b0 commit 71f1af5
Show file tree
Hide file tree
Showing 20 changed files with 339 additions and 200 deletions.
20 changes: 10 additions & 10 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ The subcommand adds all resources defined in by a file.

```shell script
# define by file
$ cat <<EOF
$ cat <<EOF > ./resource.yaml
name: 'ubuntu'
version: 'v0.0.1'
type: 'ociImage'
relation: 'external'
access:
type: 'ociRegistry'
imageReference: 'ubuntu:18.0'
EOF > ./resource.yaml
EOF
$ component-cli ca resources add -r ./resource.yaml
```

The resources can also be added using stdin.
```shell script
# define by file
$ cat <<EOF
$ cat <<EOF | component-cli ca resources add
name: 'ubuntu'
version: 'v0.0.1'
type: 'ociImage'
relation: 'external'
access:
type: 'ociRegistry'
imageReference: 'ubuntu:18.0'
EOF | component-cli ca resources add
EOF
```

The file is expected to be a yaml, and multiple resources can be added by using the yaml multi doc syntax:
Expand Down Expand Up @@ -71,20 +71,20 @@ This will automatically add the file as local artifact to the component descript
:warning: When the given input path is a directory, a tar archive is automatically created.

```
$ cat <<EOF
$ cat <<EOF > ./resource.yaml
name: 'myconfig'
type: 'json'
relation: 'local'
input:
type: "file"
path: "./blob.raw" # path is realtive to the current resource file
EOF > ./resource.yaml
EOF
$ cat <<EOF
$ cat <<EOF > ./blob.raw
{
"key": "value"
}
EOF > ./blob.raw
EOF
$ component-cli ca resources add -r ./resource.yaml
```
Expand Down Expand Up @@ -123,7 +123,7 @@ The subcommand offers the possibility to add a component-reference either via co

```shell script
# define by file
$ cat <<EOF
$ cat <<EOF > ./comp-ref.yaml
name: 'cli'
componentName: 'github.com/gardener/component-spec'
version: 'v0.0.1'
Expand All @@ -134,7 +134,7 @@ labels:
mylabel: efg
mysecondlabel:
key: true
EOF > ./comp-ref.yaml
EOF
$ component-cli component-reference add -r ./comp-ref.yaml
```

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/components-cli_component-archive.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
* [components-cli component-archive component-references](components-cli_component-archive_component-references.md) - command to modify component references of a component descriptor
* [components-cli component-archive export](components-cli_component-archive_export.md) - Exports a component archive as defined by CTF
* [components-cli component-archive resources](components-cli_component-archive_resources.md) - command to modify resources of a component descriptor
* [components-cli component-archive source](components-cli_component-archive_source.md) - command to modify sources of a component descriptor
* [components-cli component-archive sources](components-cli_component-archive_sources.md) - command to modify sources of a component descriptor

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ components-cli component-archive component-references add [flags]
### Options

```
--archive string path to the component descriptor archive directory
-h, --help help for add
-r, --resource string The path to the resources defined as yaml or json
-a, --archive string path to the component archive directory
--component-name string name of the component
--component-version string version of the component
-h, --help help for add
--repo-ctx string repository context url for component to upload. The repository url will be automatically added to the repository contexts.
-r, --resource string The path to the resources defined as yaml or json
```

### Options inherited from parent commands
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/components-cli_component-archive_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ components-cli component-archive export [component-archive-path] [-o output-dir/
### Options

```
-f, --format string output format of the component archive. Can be 'fs', 'tar' or 'tgz'
-h, --help help for export
-o, --out string writes the resulting archive to the given path
-f, --format CAOutputFormat output format of the component archive. Can be "fs"', "tar"' or "tgz"'
-h, --help help for export
-o, --out string writes the resulting archive to the given path
```

### Options inherited from parent commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ components-cli component-archive resources add [component archive path] [-r reso
### Options

```
-a, --archive string path to the component archive directory
-h, --help help for add
-r, --resource string The path to the resources defined as yaml or json
-a, --archive string path to the component archive directory
--component-name string name of the component
--component-version string version of the component
-h, --help help for add
--repo-ctx string repository context url for component to upload. The repository url will be automatically added to the repository contexts.
-r, --resource string The path to the resources defined as yaml or json
```

### Options inherited from parent commands
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## components-cli component-archive source
## components-cli component-archive sources

command to modify sources of a component descriptor

### Options

```
-h, --help help for source
-h, --help help for sources
```

### Options inherited from parent commands
Expand All @@ -22,5 +22,5 @@ command to modify sources of a component descriptor
### SEE ALSO

* [components-cli component-archive](components-cli_component-archive.md) -
* [components-cli component-archive source add](components-cli_component-archive_source_add.md) - Adds a source to a component descriptor
* [components-cli component-archive sources add](components-cli_component-archive_sources_add.md) - Adds a source to a component descriptor

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## components-cli component-archive source add
## components-cli component-archive sources add

Adds a source to a component descriptor

Expand Down Expand Up @@ -40,15 +40,18 @@ input:


```
components-cli component-archive source add [flags]
components-cli component-archive sources add [flags]
```

### Options

```
--archive string path to the component archive directory
-h, --help help for add
-r, --resource string The path to the resources defined as yaml or json
-a, --archive string path to the component archive directory
--component-name string name of the component
--component-version string version of the component
-h, --help help for add
--repo-ctx string repository context url for component to upload. The repository url will be automatically added to the repository contexts.
-r, --resource string The path to the resources defined as yaml or json
```

### Options inherited from parent commands
Expand All @@ -64,5 +67,5 @@ components-cli component-archive source add [flags]

### SEE ALSO

* [components-cli component-archive source](components-cli_component-archive_source.md) - command to modify sources of a component descriptor
* [components-cli component-archive sources](components-cli_component-archive_sources.md) - command to modify sources of a component descriptor

2 changes: 1 addition & 1 deletion pkg/commands/componentarchive/componentarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func NewComponentArchiveCommand(ctx context.Context) *cobra.Command {
cmd.AddCommand(NewExportCommand(ctx))
cmd.AddCommand(resources.NewResourcesCommand(ctx))
cmd.AddCommand(componentreferences.NewCompRefCommand(ctx))
cmd.AddCommand(sources.NewSourceCommand(ctx))
cmd.AddCommand(sources.NewSourcesCommand(ctx))
return cmd
}
37 changes: 10 additions & 27 deletions pkg/commands/componentarchive/componentreferences/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package componentreferences

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -18,21 +17,19 @@ import (
"github.com/ghodss/yaml"
"github.com/go-logr/logr"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/projectionfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/validation/field"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"

"github.com/gardener/component-cli/pkg/commands/constants"
"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
type Options struct {
// ComponentArchivePath is the path to the component archive
ComponentArchivePath string
componentarchive.BuilderOptions

// either components can be added by a yaml resource template or by input flags

Expand Down Expand Up @@ -88,14 +85,9 @@ version: 'v0.0.2'
func (o *Options) Run(ctx context.Context, log logr.Logger, fs vfs.FileSystem) error {
compDescFilePath := filepath.Join(o.ComponentArchivePath, ctf.ComponentDescriptorFileName)

// add the input to the ctf format
archiveFs, err := projectionfs.New(fs, o.ComponentArchivePath)
archive, err := o.BuilderOptions.Build(fs)
if err != nil {
return fmt.Errorf("unable to create projectionfilesystem: %w", err)
}
archive, err := ctf.NewComponentArchiveFromFilesystem(archiveFs)
if err != nil {
return fmt.Errorf("unable to parse component archive from %s: %w", compDescFilePath, err)
return err
}

refs, err := o.generateComponentReferences(fs, archive.ComponentDescriptor)
Expand Down Expand Up @@ -124,35 +116,26 @@ func (o *Options) Run(ctx context.Context, log logr.Logger, fs vfs.FileSystem) e
if err != nil {
return fmt.Errorf("unable to encode component descriptor: %w", err)
}
if err := vfs.WriteFile(fs, compDescFilePath, data, 06444); err != nil {
if err := vfs.WriteFile(fs, compDescFilePath, data, 0664); err != nil {
return fmt.Errorf("unable to write modified comonent descriptor: %w", err)
}
log.V(1).Info("Successfully added all component references to component descriptor")
return nil
}

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

// default component path to env var
if len(o.ComponentArchivePath) == 0 {
o.ComponentArchivePath = filepath.Dir(os.Getenv(constants.ComponentDescriptorPathEnvName))
}

o.BuilderOptions.Default()
return o.validate()
}

func (o *Options) validate() error {
if len(o.ComponentArchivePath) == 0 {
return errors.New("component descriptor path must be provided")
}
return nil
return o.BuilderOptions.Validate()
}

func (o *Options) AddFlags(set *pflag.FlagSet) {
set.StringVar(&o.ComponentArchivePath, "archive", "", "path to the component descriptor archive directory")

func (o *Options) AddFlags(fs *pflag.FlagSet) {
o.BuilderOptions.AddFlags(fs)
// specify the resource
set.StringVarP(&o.ComponentReferenceObjectPath, "resource", "r", "", "The path to the resources defined as yaml or json")
fs.StringVarP(&o.ComponentReferenceObjectPath, "resource", "r", "", "The path to the resources defined as yaml or json")
}

// generateComponentReferences parses component references from the given path and stdin.
Expand Down
9 changes: 5 additions & 4 deletions pkg/commands/componentarchive/componentreferences/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
. "github.com/onsi/gomega/gstruct"

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

func TestConfig(t *testing.T) {
Expand All @@ -44,7 +45,7 @@ var _ = Describe("Add", func() {
It("should add a reference defined by a file", func() {

opts := &componentreferences.Options{
ComponentArchivePath: "./00-component",
BuilderOptions: componentarchive.BuilderOptions{ComponentArchivePath: "./00-component"},
ComponentReferenceObjectPath: "./resources/00-ref.yaml",
}

Expand Down Expand Up @@ -76,7 +77,7 @@ var _ = Describe("Add", func() {
os.Stdin = input

opts := &componentreferences.Options{
ComponentArchivePath: "./00-component",
BuilderOptions: componentarchive.BuilderOptions{ComponentArchivePath: "./00-component"},
}

Expect(opts.Run(context.TODO(), testlog.NullLogger{}, testdataFs)).To(Succeed())
Expand All @@ -98,7 +99,7 @@ var _ = Describe("Add", func() {
It("should add multiple reference defined by a multi doc file", func() {

opts := &componentreferences.Options{
ComponentArchivePath: "./00-component",
BuilderOptions: componentarchive.BuilderOptions{ComponentArchivePath: "./00-component"},
ComponentReferenceObjectPath: "./resources/01-multi-doc.yaml",
}

Expand All @@ -125,7 +126,7 @@ var _ = Describe("Add", func() {

It("should throw an error if an invalid resource is defined", func() {
opts := &componentreferences.Options{
ComponentArchivePath: "./00-component",
BuilderOptions: componentarchive.BuilderOptions{ComponentArchivePath: "./00-component"},
ComponentReferenceObjectPath: "./resources/10-invalid.yaml",
}

Expand Down
Loading

0 comments on commit 71f1af5

Please sign in to comment.