diff --git a/Makefile b/Makefile index 6deb4b1..00e0ce0 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ PLATFORMS ?= linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 # Setup Go GO_REQUIRED_VERSION = 1.19 GOLANGCILINT_VERSION ?= 1.50.0 -GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/uptest $(GO_PROJECT)/cmd/updoc $(GO_PROJECT)/cmd/ttr $(GO_PROJECT)/cmd/perf $(GO_PROJECT)/cmd/provider-list +GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/uptest $(GO_PROJECT)/cmd/updoc $(GO_PROJECT)/cmd/ttr $(GO_PROJECT)/cmd/perf GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.Version=$(VERSION) GO_SUBDIRS += cmd internal GO111MODULE = on diff --git a/cmd/provider-list/converter.go b/cmd/provider-list/converter.go deleted file mode 100644 index 3259fcd..0000000 --- a/cmd/provider-list/converter.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2023 Upbound Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package main has generic functions to get the new provider names -// from compositions and managed resources. -package main - -import ( - "fmt" - "strings" - - "github.com/pkg/errors" - "github.com/upbound/upjet/pkg/migration" -) - -// SSOPNames is a global map for collecting the new provider names -var SSOPNames = map[string]struct{}{} - -// GetSSOPNameFromManagedResource collects the new provider name from MR -func GetSSOPNameFromManagedResource(u migration.UnstructuredWithMetadata) error { - newProviderName := getProviderAndServiceName(u.Object.GroupVersionKind().Group) - if newProviderName != "" { - SSOPNames[newProviderName] = struct{}{} - } - return nil -} - -// GetSSOPNameFromComposition collects the new provider name from Composition -func GetSSOPNameFromComposition(u migration.UnstructuredWithMetadata) error { - composition, err := migration.ToComposition(u.Object) - if err != nil { - return errors.Wrap(err, "unstructured object cannot be converted to composition") - } - for _, composedTemplate := range composition.Spec.Resources { - composedUnstructured, err := migration.FromRawExtension(composedTemplate.Base) - if err != nil { - return errors.Wrap(err, "resource raw cannot convert to unstructured") - } - newProviderName := getProviderAndServiceName(composedUnstructured.GroupVersionKind().Group) - if newProviderName != "" { - SSOPNames[newProviderName] = struct{}{} - } - } - return nil -} - -func getProviderAndServiceName(name string) string { - parts := strings.Split(name, ".") - if len(parts) > 3 { - provider := "" - switch parts[1] { - case "aws": - provider = "provider-aws" - case "gcp": - provider = "provider-gcp" - case "azure": - provider = "provider-azure" - default: - return "" - } - service := parts[0] - return fmt.Sprintf("%s-%s", provider, service) - } - return "" -} diff --git a/cmd/provider-list/converter_test.go b/cmd/provider-list/converter_test.go deleted file mode 100644 index e68f727..0000000 --- a/cmd/provider-list/converter_test.go +++ /dev/null @@ -1,201 +0,0 @@ -// Copyright 2023 Upbound Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/upbound/upjet/pkg/migration" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" -) - -var ( - unstructuredAwsVpc = map[string]interface{}{ - "apiVersion": "ec2.aws.upbound.io/v1beta1", - "kind": "VPC", - "metadata": map[string]interface{}{ - "name": "sample-vpc", - }, - } - unstructuredAwsProviderConfig = map[string]interface{}{ - "apiVersion": "aws.upbound.io/v1beta1", - "kind": "ProviderConfig", - "metadata": map[string]interface{}{ - "name": "sample-pc", - }, - } - unstructuredAzureZone = map[string]interface{}{ - "apiVersion": "network.azure.upbound.io/v1beta1", - "kind": "Zone", - "metadata": map[string]interface{}{ - "name": "sample-zone", - }, - } - unstructuredAzureResourceGroup = map[string]interface{}{ - "apiVersion": "azure.upbound.io/v1beta1", - "kind": "ResourceGroup", - "metadata": map[string]interface{}{ - "name": "example-resources", - }, - } - unstructuredGcpZone = map[string]interface{}{ - "apiVersion": "network.gcp.upbound.io/v1beta1", - "kind": "Zone", - "metadata": map[string]interface{}{ - "name": "sample-zone", - }, - } - unstructuredGcpProviderConfig = map[string]interface{}{ - "apiVersion": "gcp.upbound.io/v1beta1", - "kind": "ProviderConfig", - "metadata": map[string]interface{}{ - "name": "sample-pc", - }, - } - unstructuredInvalidProviderConfig = map[string]interface{}{ - "apiVersion": "xyz.invalid.upbound.io/v1beta1", - "kind": "Kind", - "metadata": map[string]interface{}{ - "name": "sample-pc", - }, - } -) - -func TestGetSSOPNameFromManagedResource(t *testing.T) { - type args struct { - u migration.UnstructuredWithMetadata - } - type want struct { - ssopMap map[string]struct{} - err error - } - - cases := map[string]struct { - args - want - }{ - "Aws": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredAwsVpc, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{ - "provider-aws-ec2": {}, - }, - err: nil, - }, - }, - "Family-Aws": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredAwsProviderConfig, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{}, - err: nil, - }, - }, - "Azure": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredAzureZone, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{ - "provider-azure-network": {}, - }, - err: nil, - }, - }, - "Family-Azure": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredAzureResourceGroup, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{}, - err: nil, - }, - }, - "Gcp": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredGcpZone, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{ - "provider-gcp-network": {}, - }, - err: nil, - }, - }, - "Family-Gcp": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredGcpProviderConfig, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{}, - err: nil, - }, - }, - "InvalidProvider": { - args: args{ - u: migration.UnstructuredWithMetadata{ - Object: unstructured.Unstructured{ - Object: unstructuredInvalidProviderConfig, - }, - }, - }, - want: want{ - ssopMap: map[string]struct{}{}, - err: nil, - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - SSOPNames = map[string]struct{}{} - err := GetSSOPNameFromManagedResource(tc.u) - if diff := cmp.Diff(tc.want.err, err); diff != "" { - t.Errorf("\nNext(...): -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.ssopMap, SSOPNames); diff != "" { - t.Errorf("\nNext(...): -want, +got:\n%s", diff) - } - }) - } -} diff --git a/cmd/provider-list/main.go b/cmd/provider-list/main.go deleted file mode 100644 index e90371a..0000000 --- a/cmd/provider-list/main.go +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2023 Upbound Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// main package for the provider-list tool that can be used to dump the names -// of the required provider family (service-scoped provider) packages -// that satisfy: -// - All managed resources and Crossplane Compositions observed in a cluster. -// - All Crossplane Compositions observed in the source tree of -// a Crossplane Configuration package. -package main - -import ( - "fmt" - "log" - "os" - "path/filepath" - "sort" - - "github.com/alecthomas/kong" - "github.com/pkg/errors" - "github.com/upbound/upjet/pkg/migration" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -const ( - defaultKubeConfig = ".kube/config" -) - -// Options represents the available subcommands of provider-list: -// "generate" and "upload". -type Options struct { - RegistryOrg string `name:"regorg" required:"" default:"xpkg.upbound.io/upbound" help:"/ for the provider family packages."` - Version string `name:"family-version" required:"" help:"Version of the provider family packages."` - // sub-commands - Local struct { - Path string `name:"path" required:"" help:"Source directory for the Crossplane Configuration package."` - } `kong:"cmd"` - Cluster struct { - KubeConfig string `name:"kubeconfig" optional:"" help:"Path to the kubeconfig to use."` - } `kong:"cmd"` -} - -func main() { - opts := &Options{} - kongCtx := kong.Parse(opts, kong.Name("provider-list"), - kong.Description("Upbound provider families package dependency listing tool"), - kong.UsageOnError(), - kong.ConfigureHelp(kong.HelpOptions{ - Compact: true, - FlagsLast: true, - Summary: true, - })) - - r := migration.NewRegistry(runtime.NewScheme()) - r.RegisterPreProcessor(migration.CategoryManaged, migration.PreProcessor(GetSSOPNameFromManagedResource)) - r.RegisterPreProcessor(migration.CategoryComposition, migration.PreProcessor(GetSSOPNameFromComposition)) - kongCtx.FatalIfErrorf(r.AddCompositionTypes(), "Failed to register the Crossplane Composition types with the migration registry") - - var source migration.Source - var err error - switch kongCtx.Command() { - case "local": - source, err = localListing(opts) - case "cluster": - source, err = clusterListing(opts, r) - } - kongCtx.FatalIfErrorf(err, "Failed to initialize the migration source") - - pg := migration.NewPlanGenerator(r, source, nil, migration.WithSkipGVKs(schema.GroupVersionKind{})) - kongCtx.FatalIfErrorf(pg.GeneratePlan(), "Failed to list the required provider family packages") - providers := make([]string, 0, len(SSOPNames)) - for p := range SSOPNames { - providers = append(providers, p) - } - sort.Strings(providers) - logger := log.New(os.Stdout, "", 0) - for _, p := range providers { - logger.Printf("%s", fmt.Sprintf("%s/%s:%s", opts.RegistryOrg, p, opts.Version)) - } -} - -func clusterListing(opts *Options, r *migration.Registry) (migration.Source, error) { - if len(opts.Cluster.KubeConfig) == 0 { - homeDir, err := os.UserHomeDir() - if err != nil { - return nil, errors.Wrapf(err, "failed to get user's home") - } - opts.Cluster.KubeConfig = filepath.Join(homeDir, defaultKubeConfig) - } - source, err := migration.NewKubernetesSourceFromKubeConfig(opts.Cluster.KubeConfig, - migration.WithCategories([]migration.Category{"managed"}), migration.WithRegistry(r)) - return source, errors.Wrap(err, "failed to initialize the migration Kubernetes source") -} - -func localListing(opts *Options) (migration.Source, error) { - source, err := migration.NewFileSystemSource(opts.Local.Path) - return source, errors.Wrap(err, "failed to initialize the migration FileSystem source") -} diff --git a/go.mod b/go.mod index ecdcbd8..00d57c4 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,6 @@ require ( github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 github.com/tufin/oasdiff v1.2.6 - github.com/upbound/upjet v0.9.0-rc.0.0.20230609110242-aaafdd4057c5 golang.org/x/mod v0.7.0 google.golang.org/api v0.103.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 @@ -41,7 +40,6 @@ require ( github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect - github.com/crossplane/crossplane v1.10.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/evanphx/json-patch v4.12.0+incompatible // indirect @@ -95,6 +93,7 @@ require ( google.golang.org/genproto v0.0.0-20221202195650-67e5cbc046fd // indirect google.golang.org/grpc v1.50.1 // indirect google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/component-base v0.26.3 // indirect diff --git a/go.sum b/go.sum index 7d701ee..7e2c81b 100644 --- a/go.sum +++ b/go.sum @@ -86,8 +86,6 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/crossplane/crossplane v1.10.0 h1:JP6TdhoZuRS27rYd+HCZNEBdf/1/w+rqIShW2qz/Qhk= -github.com/crossplane/crossplane v1.10.0/go.mod h1:a90wo/wkTDHhh8artgsUiLtQu5DIYwA7biHPDoMssms= github.com/crossplane/crossplane-runtime v0.20.0-rc.0.0.20230406155702-4e1673b7141f h1:wDRr6gaoiQstEdddrn0B5SSSgzdXreOQAbdmRH+9JeI= github.com/crossplane/crossplane-runtime v0.20.0-rc.0.0.20230406155702-4e1673b7141f/go.mod h1:ebtUpmconMy8RKUEhrCXTUFSOpfGQqbKM2E+rjCCYJo= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -156,7 +154,6 @@ github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= -github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -260,8 +257,9 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxv github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -364,8 +362,6 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tufin/oasdiff v1.2.6 h1:npdeFfs1lvTTcsT5HB/zDJZntl6tG2oMMK0dfFSF+yM= github.com/tufin/oasdiff v1.2.6/go.mod h1:7Ukdw7vE8EFNl8mf7e9rNX/aXptYn0RGtXoPCjjjlBU= -github.com/upbound/upjet v0.9.0-rc.0.0.20230609110242-aaafdd4057c5 h1:SNIJxmSZk4YAfoxoT+hAGqZLOEkxGY0umVDJLVO8iso= -github.com/upbound/upjet v0.9.0-rc.0.0.20230609110242-aaafdd4057c5/go.mod h1:BPd4e7OHn7pszWQNTgtJy14MTWhJLBQUZIs5Jr6rqiw= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= @@ -740,6 +736,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=