-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add two pre-processor functions for new family provider migraiton
Signed-off-by: Sergen Yalçın <[email protected]>
- Loading branch information
1 parent
306a7e7
commit da98db7
Showing
4 changed files
with
386 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.