Skip to content

Commit

Permalink
Add two pre-processor functions for new family provider migraiton
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Jun 9, 2023
1 parent 306a7e7 commit da98db7
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 76 deletions.
76 changes: 76 additions & 0 deletions cmd/provider-list/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 { //nolint:unparam // Because of the signature of preprocessor interface
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 { //nolint:deadcode // It will be used in the main of this tool
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 ""
}
201 changes: 201 additions & 0 deletions cmd/provider-list/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// 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)
}
})
}
}
48 changes: 25 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@ require (
cloud.google.com/go/storage v1.27.0
github.com/adrg/frontmatter v0.2.0
github.com/alecthomas/kong v0.7.1
github.com/crossplane/crossplane-runtime v0.19.1
github.com/crossplane/crossplane-runtime v0.20.0-rc.0.0.20230406155702-4e1673b7141f
github.com/getkin/kin-openapi v0.108.0
github.com/google/go-cmp v0.5.9
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/common v0.37.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.8.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/afero v1.9.2
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.102.0
google.golang.org/api v0.103.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.26.1
k8s.io/apiextensions-apiserver v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/api v0.26.3
k8s.io/apiextensions-apiserver v0.26.3
k8s.io/apimachinery v0.26.3
k8s.io/cli-runtime v0.26.1
k8s.io/client-go v0.26.1
sigs.k8s.io/controller-runtime v0.14.1
k8s.io/client-go v0.26.3
sigs.k8s.io/controller-runtime v0.14.6
sigs.k8s.io/yaml v1.3.0
)

require (
cloud.google.com/go v0.107.0 // indirect
cloud.google.com/go/compute v1.12.1 // indirect
cloud.google.com/go/compute/metadata v0.2.1 // indirect
cloud.google.com/go/iam v0.6.0 // indirect
cloud.google.com/go/compute v1.14.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.7.0 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
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
Expand All @@ -54,8 +56,8 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
Expand All @@ -68,7 +70,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
Expand All @@ -81,21 +83,21 @@ require (
github.com/yuin/goldmark v1.5.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect
google.golang.org/genproto v0.0.0-20221202195650-67e5cbc046fd // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/component-base v0.26.3 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
Expand Down
Loading

0 comments on commit da98db7

Please sign in to comment.