Skip to content

Commit

Permalink
Use discovery sources for plugin discoveries
Browse files Browse the repository at this point in the history
This commit removes the use of TANZU_CLI_PRE_RELEASE_REPO_IMAGE which
was a temporary approach.

Instead, the central discovery is automatically created in the config
file as a "discoverySource" and "tanzu plugin source update" can be used
to override this default value.

This commit also updates the "tanzu plugin source" family of commands.
With the central discovery feature enabled:
1. "tanzu plugin source add" is removed
2. "tanzu plugin source update" no longer has a "--type/-t" flag
3. "tanzu plugin source init" is added

Signed-off-by: Marc Khouzam <kmarc@vmware.com>
  • Loading branch information
marckhouzam committed Apr 26, 2023
1 parent 1281113 commit 675d0af
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 431 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/lithammer/dedent v1.1.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.4
github.com/onsi/gomega v1.27.6
github.com/otiai10/copy v1.4.2
github.com/pkg/errors v0.9.1
github.com/sigstore/cosign v1.13.1
Expand All @@ -32,7 +32,7 @@ require (
github.com/vmware-tanzu/carvel-imgpkg v0.36.1
github.com/vmware-tanzu/carvel-ytt v0.40.0
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-20230415084831-9331f55d2999
github.com/vmware-tanzu/tanzu-plugin-runtime v0.90.0-alpha.0
github.com/vmware-tanzu/tanzu-plugin-runtime v0.90.0-alpha.0.0.20230425191535-014e58e69078
go.pinniped.dev v0.20.0
go.uber.org/multierr v1.8.0
golang.org/x/mod v0.9.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1004,8 +1004,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
github.com/onsi/gomega v1.27.4/go.mod h1:riYq/GJKh8hhoM01HN6Vmuy93AarCXCBGpvFDK3q3fQ=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
Expand Down Expand Up @@ -1287,8 +1287,8 @@ github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-20221207131309-7323ca04b
github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-20221207131309-7323ca04b86c/go.mod h1:ukZpKQ0hf5bjWdJLjn2M6qXP+9giZWQPxt8nOfrCR+o=
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-20230415084831-9331f55d2999 h1:WITDH+wpdl/clw1hwy+2jtq4Pt//i/Mq9lQXGwg3q4c=
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-20230415084831-9331f55d2999/go.mod h1:umFZBUfJ8VI3p0VO/xocuE+4fO9s9QbytEiOqFcH/Tw=
github.com/vmware-tanzu/tanzu-plugin-runtime v0.90.0-alpha.0 h1:7HOxIB70moCj8b39/kezbJbzF2XqoCihGS7bIPhqTAA=
github.com/vmware-tanzu/tanzu-plugin-runtime v0.90.0-alpha.0/go.mod h1:y70TLdev7MX8K6CkAA7h92qVUDyjbX8y9/J5q4UmhRs=
github.com/vmware-tanzu/tanzu-plugin-runtime v0.90.0-alpha.0.0.20230425191535-014e58e69078 h1:FnqG7kCmltbUgnGJiDcokvQT1Bbvs38IrmVIUFj4P34=
github.com/vmware-tanzu/tanzu-plugin-runtime v0.90.0-alpha.0.0.20230425191535-014e58e69078/go.mod h1:FlvOcF26rX4EA+ADjYTJdFh6WVur6O4jh25FDP9Lp7E=
github.com/xanzy/go-gitlab v0.31.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
github.com/xanzy/go-gitlab v0.73.1 h1:UMagqUZLJdjss1SovIC+kJCH4k2AZWXl58gJd38Y/hI=
github.com/xanzy/go-gitlab v0.73.1/go.mod h1:d/a0vswScO7Agg1CZNz15Ic6SSvBG9vfw8egL99t4kA=
Expand Down
6 changes: 3 additions & 3 deletions hack/central-repo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ For the `sandbox2:small` image, the `v22.22.22` of the plugins can be installed.
The steps to follow to use the test central repo are:

1. Start the test repo with `make start-test-central-repo`.
1. Configure the plugin source for the test central repo: `tz config set env.TANZU_CLI_PRE_RELEASE_REPO_IMAGE localhost:9876/tanzu-cli/plugins/central:small`
1. Configure the plugin source for the test central repo: `tz plugin source update default -u localhost:9876/tanzu-cli/plugins/central:small`

Here are the exact commands:

Expand All @@ -33,7 +33,7 @@ cd tanzu-cli
make build
make start-test-central-repo
alias tz=$(pwd)/bin/tanzu
tz config set env.TANZU_CLI_PRE_RELEASE_REPO_IMAGE localhost:9876/tanzu-cli/plugins/central:small
tz plugin source update default -u localhost:9876/tanzu-cli/plugins/central:small

tz plugin search
tz plugin install cluster --target tmc
Expand All @@ -52,7 +52,7 @@ TMC context (`v0.0.1`), but will install them from the test Central Repository.
To use the large test central repo instead:

```bash
tz config set env.TANZU_CLI_PRE_RELEASE_REPO_IMAGE localhost:9876/tanzu-cli/plugins/central:large
tz plugin source update default -u localhost:9876/tanzu-cli/plugins/central:large
```

To stop the central repos: `make stop-test-central-repo`.
Expand Down
115 changes: 85 additions & 30 deletions pkg/command/discovery_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/vmware-tanzu/tanzu-cli/pkg/cli"
"github.com/vmware-tanzu/tanzu-cli/pkg/config"
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"

"github.com/pkg/errors"
Expand All @@ -27,40 +28,47 @@ var (

func newDiscoverySourceCmd() *cobra.Command {
var discoverySourceCmd = &cobra.Command{
Use: "source",
// TODO(khouzam): not to be used for the alpha.0 release, but will be re-added after
Hidden: true,
Short: "Manage plugin discovery sources",
Long: "Manage plugin discovery sources. Discovery source provides metadata about the list of available plugins, their supported versions and how to download them.",
Use: "source",
Short: "Manage plugin discovery sources",
Long: "Manage plugin discovery sources. Discovery source provides metadata about the list of available plugins, their supported versions and how to download them.",
}
discoverySourceCmd.SetUsageFunc(cli.SubCmdUsageFunc)

listDiscoverySourceCmd := newListDiscoverySourceCmd()
addDiscoverySourceCmd := newAddDiscoverySourceCmd()
updateDiscoverySourceCmd := newUpdateDiscoverySourceCmd()
deleteDiscoverySourceCmd := newDeleteDiscoverySourceCmd()

addDiscoverySourceCmd.Flags().StringVarP(&discoverySourceName, "name", "n", "", "name of discovery source")
addDiscoverySourceCmd.Flags().StringVarP(&discoverySourceType, "type", "t", "", "type of discovery source")
addDiscoverySourceCmd.Flags().StringVarP(&uri, "uri", "u", "", "URI for discovery source. URI format might be different based on the type of discovery source")

// Not handling errors below because cobra handles the error when flag user doesn't provide these required flags
_ = cobra.MarkFlagRequired(addDiscoverySourceCmd.Flags(), "name")
_ = cobra.MarkFlagRequired(addDiscoverySourceCmd.Flags(), "type")
_ = cobra.MarkFlagRequired(addDiscoverySourceCmd.Flags(), "uri")

updateDiscoverySourceCmd.Flags().StringVarP(&discoverySourceType, "type", "t", "", "type of discovery source")
updateDiscoverySourceCmd.Flags().StringVarP(&uri, "uri", "u", "", "URI for discovery source. URI format might be different based on the type of discovery source")

listDiscoverySourceCmd.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format (yaml|json|table)")

discoverySourceCmd.AddCommand(
listDiscoverySourceCmd,
addDiscoverySourceCmd,
updateDiscoverySourceCmd,
deleteDiscoverySourceCmd,
)

if !configlib.IsFeatureActivated(constants.FeatureDisableCentralRepositoryForTesting) {
discoverySourceCmd.AddCommand(newInitDiscoverySourceCmd())
updateDiscoverySourceCmd.Flags().StringVarP(&uri, "uri", "u", "", "URI for discovery source. The URI must be of an OCI image")
} else {
updateDiscoverySourceCmd.Flags().StringVarP(&discoverySourceType, "type", "t", "", "type of discovery source")
updateDiscoverySourceCmd.Flags().StringVarP(&uri, "uri", "u", "", "URI for discovery source. The URI format might be different based on the type of discovery source")

// The "add" and "delete" plugin source commands are not needed for the central repo
addDiscoverySourceCmd := newAddDiscoverySourceCmd()

addDiscoverySourceCmd.Flags().StringVarP(&discoverySourceName, "name", "n", "", "name of discovery source")
addDiscoverySourceCmd.Flags().StringVarP(&discoverySourceType, "type", "t", "", "type of discovery source")
addDiscoverySourceCmd.Flags().StringVarP(&uri, "uri", "u", "", "URI for discovery source. The URI format might be different based on the type of discovery source")

// Not handling errors below because cobra handles the error when flag user doesn't provide these required flags
_ = cobra.MarkFlagRequired(addDiscoverySourceCmd.Flags(), "name")
_ = cobra.MarkFlagRequired(addDiscoverySourceCmd.Flags(), "type")
_ = cobra.MarkFlagRequired(addDiscoverySourceCmd.Flags(), "uri")

discoverySourceCmd.AddCommand(
addDiscoverySourceCmd,
)
}
return discoverySourceCmd
}

Expand All @@ -69,6 +77,19 @@ func newListDiscoverySourceCmd() *cobra.Command {
Use: "list",
Short: "List available discovery sources",
RunE: func(cmd *cobra.Command, args []string) error {
if !configlib.IsFeatureActivated(constants.FeatureDisableCentralRepositoryForTesting) {
output := component.NewOutputWriter(cmd.OutOrStdout(), outputFormat, "name", "image")
discoverySources, _ := configlib.GetCLIDiscoverySources()
for _, ds := range discoverySources {
if ds.OCI != nil {
output.AddRow(ds.OCI.Name, ds.OCI.Image)
}
}

output.Render()
return nil
}

output := component.NewOutputWriter(cmd.OutOrStdout(), outputFormat, "name", "type", "scope")

// List standalone scoped discoveries
Expand Down Expand Up @@ -143,23 +164,18 @@ func newUpdateDiscoverySourceCmd() *cobra.Command {
Use: "update [name]",
Short: "Update a discovery source configuration",
Args: cobra.ExactArgs(1),
Example: `
# Update a local discovery source. If URI is relative path,
# $HOME/.config/tanzu-plugins will be considered base path
tanzu plugin source update standalone-local --type local --uri new/path/to/local/discovery
# Update an OCI discovery source. URI should be an OCI image.
tanzu plugin source update standalone-oci --type oci --uri projects.registry.vmware.com/tkg/tanzu-plugins/standalone:v1.0`,

RunE: func(cmd *cobra.Command, args []string) error {
discoveryName := args[0]

discoveryNoExistError := fmt.Errorf("discovery %q does not exist", discoveryName)
discoverySource, _ := configlib.GetCLIDiscoverySource(discoveryName)
if discoverySource == nil {
return discoveryNoExistError
return fmt.Errorf("discovery %q does not exist", discoveryName)
}

if !configlib.IsFeatureActivated(constants.FeatureDisableCentralRepositoryForTesting) {
// With the central discovery, there is no more --type flag
discoverySourceType = common.DiscoveryTypeOCI
}
newDiscoverySource, err := createDiscoverySource(discoverySourceType, discoveryName, uri)
if err != nil {
return err
Expand All @@ -174,6 +190,21 @@ func newUpdateDiscoverySourceCmd() *cobra.Command {
return nil
},
}

if !configlib.IsFeatureActivated(constants.FeatureDisableCentralRepositoryForTesting) {
updateDiscoverySourceCmd.Example = `
# Update the discovery source for an air-gapped scenario. The URI must be an OCI image.
tanzu plugin source update default --uri registry.example.com/tanzu/plugin-inventory:latest`
} else {
updateDiscoverySourceCmd.Example = `
# Update a local discovery source. If URI is relative path,
# $HOME/.config/tanzu-plugins will be considered base path
tanzu plugin source update standalone-local --type local --uri new/path/to/local/discovery
# Update an OCI discovery source. URI should be an OCI image.
tanzu plugin source update standalone-oci --type oci --uri projects.registry.vmware.com/tkg/tanzu-plugins/standalone:v1.0`
}

return updateDiscoverySourceCmd
}

Expand All @@ -184,10 +215,15 @@ func newDeleteDiscoverySourceCmd() *cobra.Command {
Args: cobra.ExactArgs(1),
Example: `
# Delete a discovery source
tanzu plugin discovery delete standalone-oci`,
tanzu plugin discovery delete default`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
discoveryName := args[0]

discoverySource, _ := configlib.GetCLIDiscoverySource(discoveryName)
if discoverySource == nil {
return fmt.Errorf("discovery %q does not exist", discoveryName)
}

err = configlib.DeleteCLIDiscoverySource(discoveryName)
if err != nil {
return err
Expand All @@ -198,6 +234,25 @@ func newDeleteDiscoverySourceCmd() *cobra.Command {
}
return deleteDiscoverySourceCmd
}

func newInitDiscoverySourceCmd() *cobra.Command {
var initDiscoverySourceCmd = &cobra.Command{
Use: "init",
Short: "Initialize the discovery source to its default value",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
err := config.PopulateDefaultCentralDiscovery(true)
if err != nil {
return err
}

log.Successf("successfully initialized discovery source")
return nil
},
}
return initDiscoverySourceCmd
}

func createDiscoverySource(dsType, dsName, uri string) (configtypes.PluginDiscovery, error) {
pluginDiscoverySource := configtypes.PluginDiscovery{}
if dsType == "" {
Expand Down
5 changes: 0 additions & 5 deletions pkg/command/plugin_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
"github.com/vmware-tanzu/tanzu-cli/pkg/pluginmanager"
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
)

Expand Down Expand Up @@ -68,10 +67,6 @@ func TestPluginSearch(t *testing.T) {
os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name())
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")

// Bypass the environment variable for testing
err = os.Setenv(constants.ConfigVariablePreReleasePluginRepoImage, pluginmanager.PreReleasePluginRepoImageBypass)
assert.Nil(err)

featureArray := strings.Split(constants.FeatureContextCommand, ".")
err = config.SetFeature(featureArray[1], featureArray[2], "true")
assert.Nil(err)
Expand Down
5 changes: 0 additions & 5 deletions pkg/command/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/vmware-tanzu/tanzu-cli/pkg/cli"
"github.com/vmware-tanzu/tanzu-cli/pkg/common"
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
"github.com/vmware-tanzu/tanzu-cli/pkg/pluginmanager"
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
Expand Down Expand Up @@ -360,10 +359,6 @@ func TestInstallPlugin(t *testing.T) {
os.Setenv("TANZU_CONFIG_NEXT_GEN", tkgConfigFileNG.Name())
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")

// Bypass the environment variable for testing
err = os.Setenv(constants.ConfigVariablePreReleasePluginRepoImage, pluginmanager.PreReleasePluginRepoImageBypass)
assert.Nil(err)

featureArray := strings.Split(constants.FeatureContextCommand, ".")
err = config.SetFeature(featureArray[1], featureArray[2], "true")
assert.Nil(err)
Expand Down
7 changes: 0 additions & 7 deletions pkg/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,6 @@ func GetTrustedRegistries() []string {
trustedRegistries = append(trustedRegistries, customImageRepo)
}

// If the pre-release plugin repo variable is set, add its host to the list of trusted registries
if preReleaseRepoImage := os.Getenv(constants.ConfigVariablePreReleasePluginRepoImage); preReleaseRepoImage != "" {
if u, err := url.ParseRequestURI("https://" + preReleaseRepoImage); err == nil {
trustedRegistries = append(trustedRegistries, u.Hostname())
}
}

// Add default central plugin discovery image to the trusted registries
if u, err := url.ParseRequestURI("https://" + constants.TanzuCLIDefaultCentralPluginDiscoveryImage); err == nil {
trustedRegistries = append(trustedRegistries, u.Hostname())
Expand Down
15 changes: 1 addition & 14 deletions pkg/config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ var _ = Describe("defaults test cases", func() {
Expect(trustedRegis).NotTo(BeNil())
DefaultAllowedPluginRepositories = ""
})
It("trusted registries should include hostname of env var", func() {
testHost := "example.com"
oldValue := os.Getenv(constants.ConfigVariablePreReleasePluginRepoImage)
err := os.Setenv(constants.ConfigVariablePreReleasePluginRepoImage, testHost+"/test/path")
Expect(err).To(BeNil())

trustedRegis := GetTrustedRegistries()
Expect(trustedRegis).NotTo(BeNil())
Expect(trustedRegis).Should(ContainElement(testHost))

err = os.Setenv(constants.ConfigVariablePreReleasePluginRepoImage, oldValue)
Expect(err).To(BeNil())
})
It("trusted registries should include hostname of additional discoveries", func() {
testHost1 := "registry1.vmware.com"
testHost2 := "registry2.vmware.com"
Expand All @@ -51,7 +38,7 @@ var _ = Describe("defaults test cases", func() {
Expect(trustedRegis).Should(ContainElement(testHost1))
Expect(trustedRegis).Should(ContainElement(testHost2))

err = os.Setenv(constants.ConfigVariablePreReleasePluginRepoImage, oldValue)
err = os.Setenv(constants.ConfigVariableAdditionalDiscoveryForTesting, oldValue)
Expect(err).To(BeNil())
})
It("trusted registries should include hostname of default central discovery", func() {
Expand Down
Loading

0 comments on commit 675d0af

Please sign in to comment.