Skip to content

Commit

Permalink
Selective plugin migration using plugin groups for airgapped environm…
Browse files Browse the repository at this point in the history
…ents

This change adds support for users to do partial migration of plugins
using plugin groups instead of migrating all available plugins.

- Allows users to pass `--group` flag with `tanzu plugin
  download-bundle` to do selective plugin download and generates tar file
- Users can use the generated tar file with the `tanzu plugin
  upload-bundle` command to do partial plugin migration
  • Loading branch information
anujc25 committed Apr 26, 2023
1 parent 3b2b0c1 commit 15b9e64
Show file tree
Hide file tree
Showing 23 changed files with 1,561 additions and 115 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/k14s/kbld v0.32.0
github.com/lithammer/dedent v1.1.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/novln/docker-parser v1.0.0
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.4
github.com/otiai10/copy v1.4.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
github.com/nishanths/predeclared v0.0.0-20200524104333-86fad755b4d3/go.mod h1:nt3d53pc1VYcphSCIaYAJtnPYnr3Zyn8fMq2wvPGPso=
github.com/novln/docker-parser v1.0.0 h1:PjEBd9QnKixcWczNGyEdfUrP6GR0YUilAqG7Wksg3uc=
github.com/novln/docker-parser v1.0.0/go.mod h1:oCeM32fsoUwkwByB5wVjsrsVQySzPWkl3JdlTn1txpE=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand Down
40 changes: 40 additions & 0 deletions pkg/airgapped/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package airgapped

import (
"fmt"
"strings"

dockerparser "github.com/novln/docker-parser"
"github.com/pkg/errors"
)

// GetPluginInventoryMetadataImage returns the plugin inventory metadata
// image based on plugin inventory image.
// E.g. if plugin inventory image is `fake.repo.com/plugin/plugin-inventory:latest`
// it returns metadata image as `fake.repo.com/plugin/plugin-inventory-metadata:latest`
func GetPluginInventoryMetadataImage(pluginInventoryImage string) (string, error) {
ref, err := dockerparser.Parse(pluginInventoryImage)
if err != nil {
return "", errors.Wrapf(err, "invalid image %q", pluginInventoryImage)
}
return fmt.Sprintf("%s-metadata:%s", ref.Repository(), ref.Tag()), nil
}

// GetImageRelativePath returns relative path of the image based on `basePath`
// E.g. If image is `fake.repo.com/plugin/plugin-inventory:latest` with
// basePath as `fake.repo.com/plugin` it should return
// `plugin-inventory:latest` if withTag is true and
// `plugin-inventory` if withTag is false
func GetImageRelativePath(image, basePath string, withTag bool) string {
relativePath := strings.TrimPrefix(image, basePath)
if withTag {
return relativePath
}
if idx := strings.LastIndex(relativePath, ":"); idx != -1 {
return relativePath[:idx]
}
return relativePath
}
102 changes: 102 additions & 0 deletions pkg/airgapped/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package airgapped

import (
"testing"

"github.com/tj/assert"
)

func Test_GetPluginInventoryMetadataImage(t *testing.T) {
assert := assert.New(t)

tests := []struct {
pluginInventoryImage string
expectedMetadataImage string
errString string
}{
{
pluginInventoryImage: "fake.repo.com/plugin/plugin-inventory:latest",
expectedMetadataImage: "fake.repo.com/plugin/plugin-inventory-metadata:latest",
errString: "",
},
{
pluginInventoryImage: "fake.repo.com/plugin/airgapped:v1.0.0",
expectedMetadataImage: "fake.repo.com/plugin/airgapped-metadata:v1.0.0",
errString: "",
},
{
pluginInventoryImage: "fake.repo.com/plugin/metadata",
expectedMetadataImage: "fake.repo.com/plugin/metadata-metadata:latest",
errString: "",
},
{
pluginInventoryImage: "invalid-inventory-image$#",
expectedMetadataImage: "",
errString: "invalid image",
},
}

for _, test := range tests {
t.Run(test.pluginInventoryImage, func(t *testing.T) {
actualMetadataImage, err := GetPluginInventoryMetadataImage(test.pluginInventoryImage)
assert.Equal(actualMetadataImage, test.expectedMetadataImage)
if test.errString == "" {
assert.Nil(err)
} else {
assert.Contains(err.Error(), test.errString)
}
})
}
}

func Test_GetImageRelativePath(t *testing.T) {
assert := assert.New(t)

tests := []struct {
image string
basePath string
withTag bool
expectedRelativePath string
}{
{
image: "fake.repo.com/plugin/plugin-inventory:latest",
basePath: "fake.repo.com/plugin/",
withTag: true,
expectedRelativePath: "plugin-inventory:latest",
},
{
image: "fake.repo.com/plugin/plugin-inventory:latest",
basePath: "fake.repo.com/plugin/",
withTag: false,
expectedRelativePath: "plugin-inventory",
},
{
image: "fake.repo.com/plugin/airgapped:v1.0.0",
basePath: "fake.repo.com/",
withTag: true,
expectedRelativePath: "plugin/airgapped:v1.0.0",
},
{
image: "fake.repo.com/plugin/metadata",
basePath: "fake.repo.com/",
withTag: false,
expectedRelativePath: "plugin/metadata",
},
{
image: "fake.repo.com/plugin/metadata:latest",
basePath: "fake.repo.com/plugin/metadata-metadata",
withTag: true,
expectedRelativePath: "fake.repo.com/plugin/metadata:latest",
},
}

for _, test := range tests {
t.Run(test.image, func(t *testing.T) {
actualImage := GetImageRelativePath(test.image, test.basePath, test.withTag)
assert.Equal(actualImage, test.expectedRelativePath)
})
}
}
Loading

0 comments on commit 15b9e64

Please sign in to comment.