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

Commit

Permalink
add an exclude command for images that should not be added as compone…
Browse files Browse the repository at this point in the history
…nt reference

```feature user
Added an exclude flag to the image-vector generation command for images that should not be added as component reference

```
  • Loading branch information
Tim Schrodi committed Feb 16, 2021
1 parent 29b3806 commit 329e1c9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
13 changes: 7 additions & 6 deletions docs/reference/components-cli_image-vector_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ components-cli image-vector add --comp-desc component-descriptor-file --image-ve
### Options

```
--comp-desc string path to the component descriptor directory
--component-prefixes stringArray Specify all prefixes that define a image from another component
--generic-dependencies string Specify all prefixes that define a image from another component
--generic-dependency stringArray Specify all image source names that are a generic dependency.
-h, --help help for add
--image-vector string The path to the resources defined as yaml or json
--comp-desc string path to the component descriptor directory
--component-prefixes stringArray Specify all prefixes that define a image from another component
--exclude-component-reference stringArray Specify all image name that should not be added as component reference
--generic-dependencies string Specify all prefixes that define a image from another component
--generic-dependency stringArray Specify all image source names that are a generic dependency.
-h, --help help for add
--image-vector string The path to the resources defined as yaml or json
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/imagevector/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (o *AddOptions) AddFlags(set *pflag.FlagSet) {
set.StringVar(&o.ComponentDescriptorPath, "comp-desc", "", "path to the component descriptor directory")
set.StringVar(&o.ImageVectorPath, "image-vector", "", "The path to the resources defined as yaml or json")
set.StringArrayVar(&o.ParseImageOptions.ComponentReferencePrefixes, "component-prefixes", []string{}, "Specify all prefixes that define a image from another component")
set.StringArrayVar(&o.ParseImageOptions.ExcludeComponentReference, "exclude-component-reference", []string{}, "Specify all image name that should not be added as component reference")
set.StringArrayVar(&o.ParseImageOptions.GenericDependencies, "generic-dependency", []string{}, "Specify all image source names that are a generic dependency.")
set.StringVar(&o.GenericDependencies, "generic-dependencies", "", "Specify all prefixes that define a image from another component")
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/commands/imagevector/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,37 @@ var _ = Describe("Add", func() {
})))
})

It("should not add a image sources that match a given pattern as component reference but is excluded", func() {

opts := &ivcmd.AddOptions{
ComponentDescriptorPath: "./00-component/component-descriptor.yaml",
ImageVectorPath: "./resources/20-comp-ref.yaml",
ParseImageOptions: imagevector.ParseImageOptions{
ComponentReferencePrefixes: []string{"eu.gcr.io/gardener-project/gardener"},
ExcludeComponentReference: []string{"cluster-autoscaler"},
},
}

Expect(opts.Run(context.TODO(), testlog.NullLogger{}, testdataFs)).To(Succeed())

data, err := vfs.ReadFile(testdataFs, opts.ComponentDescriptorPath)
Expect(err).ToNot(HaveOccurred())

cd := &cdv2.ComponentDescriptor{}
Expect(codec.Decode(data, cd)).To(Succeed())

Expect(cd.ComponentReferences).To(HaveLen(0))
Expect(cd.Resources).To(HaveLen(1))
Expect(cd.Resources[0]).To(MatchFields(IgnoreExtras, Fields{
"Relation": Equal(cdv2.ExternalRelation),
}))
Expect(cd.Resources[0].IdentityObjectMeta).To(MatchFields(IgnoreExtras, Fields{
"Name": Equal("cluster-autoscaler"),
"Version": Equal("v0.10.0"),
"ExtraIdentity": HaveKeyWithValue(imagevector.TagExtraIdentity, "v0.10.0"),
}))
})

It("should add two image sources that match a given pattern as component reference", func() {

opts := &ivcmd.AddOptions{
Expand Down
14 changes: 13 additions & 1 deletion pkg/imagevector/imagevector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type ParseImageOptions struct {
// These images are then not added as direct resources but the source repository is used as the component reference.
ComponentReferencePrefixes []string

// ExcludeComponentReference defines a list of image names that should be added as component reference
ExcludeComponentReference []string

// GenericDependencies define images that should be untouched and not added as real dependency to the component descriptors.
// These dependencies are added a specific label to the component descriptor.
GenericDependencies []string
Expand Down Expand Up @@ -48,7 +51,7 @@ func ParseImageVector(cd *cdv2.ComponentDescriptor, reader io.Reader, opts *Pars
continue
}

if entryMatchesPrefix(opts.ComponentReferencePrefixes, image.Repository) {
if entryMatchesPrefix(opts.ComponentReferencePrefixes, image.Repository) && !isOneOf(opts.ExcludeComponentReference, image.Name) {
// add image as component reference
ref := cdv2.ComponentReference{
Name: image.Name,
Expand Down Expand Up @@ -283,3 +286,12 @@ func entryMatchesPrefix(prefixes []string, val string) bool {
}
return false
}

func isOneOf(keys []string, key string) bool {
for _, k := range keys {
if k == key {
return true
}
}
return false
}

0 comments on commit 329e1c9

Please sign in to comment.