From acf692abb50d648bb97b8cfcb4cbc3b5d6935b77 Mon Sep 17 00:00:00 2001 From: Alexander Demichev Date: Fri, 29 Oct 2021 16:59:38 +0200 Subject: [PATCH] Migrate to openshift/api --- cmd/manager/main.go | 4 +- pkg/actuators/machine/actuator.go | 2 +- pkg/actuators/machine/actuator_test.go | 2 +- pkg/actuators/machine/instances.go | 45 ++++--- pkg/actuators/machine/instances_test.go | 95 +++++++------- pkg/actuators/machine/machine_scope.go | 15 ++- pkg/actuators/machine/machine_scope_test.go | 17 ++- pkg/actuators/machine/reconciler.go | 11 +- pkg/actuators/machine/reconciler_test.go | 119 +++++++++--------- pkg/actuators/machine/stubs.go | 43 ++++--- pkg/actuators/machine/utils.go | 98 ++++++++++++--- pkg/actuators/machine/utils_test.go | 2 +- pkg/actuators/machineset/controller.go | 6 +- .../machineset/controller_suite_test.go | 2 +- pkg/actuators/machineset/controller_test.go | 7 +- pkg/termination/termination_suite_test.go | 2 +- 16 files changed, 262 insertions(+), 208 deletions(-) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index e3de7cc81b..da1940215b 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -20,7 +20,7 @@ import ( "time" configv1 "github.com/openshift/api/config/v1" - mapiv1beta1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" "github.com/openshift/machine-api-operator/pkg/controller/machine" "github.com/openshift/machine-api-operator/pkg/metrics" corev1 "k8s.io/api/core/v1" @@ -132,7 +132,7 @@ func main() { } // Setup Scheme for all resources - if err := mapiv1beta1.AddToScheme(mgr.GetScheme()); err != nil { + if err := machinev1.AddToScheme(mgr.GetScheme()); err != nil { klog.Fatalf("Error setting up scheme: %v", err) } diff --git a/pkg/actuators/machine/actuator.go b/pkg/actuators/machine/actuator.go index c50f638b28..96f34299ba 100644 --- a/pkg/actuators/machine/actuator.go +++ b/pkg/actuators/machine/actuator.go @@ -20,7 +20,7 @@ import ( "context" "fmt" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/record" "k8s.io/klog/v2" diff --git a/pkg/actuators/machine/actuator_test.go b/pkg/actuators/machine/actuator_test.go index ee32aa910e..ea312e38b7 100644 --- a/pkg/actuators/machine/actuator_test.go +++ b/pkg/actuators/machine/actuator_test.go @@ -10,7 +10,7 @@ import ( "github.com/golang/mock/gomock" . "github.com/onsi/gomega" configv1 "github.com/openshift/api/config/v1" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" diff --git a/pkg/actuators/machine/instances.go b/pkg/actuators/machine/instances.go index b070855968..4bbceae5b7 100644 --- a/pkg/actuators/machine/instances.go +++ b/pkg/actuators/machine/instances.go @@ -9,19 +9,16 @@ import ( "strings" "time" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/ec2" configv1 "github.com/openshift/api/config/v1" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" mapierrors "github.com/openshift/machine-api-operator/pkg/controller/machine" "github.com/openshift/machine-api-operator/pkg/metrics" "k8s.io/klog/v2" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" - runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" - awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" + runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) // Scan machine tags, and return a deduped tags list. The first found value gets precedence. @@ -56,7 +53,7 @@ func removeStoppedMachine(machine *machinev1.Machine, client awsclient.Client) e return err } -func buildEC2Filters(inputFilters []awsproviderv1.Filter) []*ec2.Filter { +func buildEC2Filters(inputFilters []machinev1.Filter) []*ec2.Filter { filters := make([]*ec2.Filter, len(inputFilters)) for i, f := range inputFilters { values := make([]*string, len(f.Values)) @@ -71,7 +68,7 @@ func buildEC2Filters(inputFilters []awsproviderv1.Filter) []*ec2.Filter { return filters } -func getSecurityGroupsIDs(securityGroups []awsproviderv1.AWSResourceReference, client awsclient.Client) ([]*string, error) { +func getSecurityGroupsIDs(securityGroups []machinev1.AWSResourceReference, client awsclient.Client) ([]*string, error) { var securityGroupIDs []*string for _, g := range securityGroups { // ID has priority @@ -102,13 +99,13 @@ func getSecurityGroupsIDs(securityGroups []awsproviderv1.AWSResourceReference, c return securityGroupIDs, nil } -func getSubnetIDs(machine runtimeclient.ObjectKey, subnet awsproviderv1.AWSResourceReference, availabilityZone string, client awsclient.Client) ([]*string, error) { +func getSubnetIDs(machine runtimeclient.ObjectKey, subnet machinev1.AWSResourceReference, availabilityZone string, client awsclient.Client) ([]*string, error) { var subnetIDs []*string // ID has priority if subnet.ID != nil { subnetIDs = append(subnetIDs, subnet.ID) } else { - var filters []awsproviderv1.Filter + var filters []machinev1.Filter if availabilityZone != "" { // Improve error logging for better user experience. // Otherwise, during the process of minimizing API calls, this is a good @@ -125,7 +122,7 @@ func getSubnetIDs(machine runtimeclient.ObjectKey, subnet awsproviderv1.AWSResou klog.Errorf("error describing availability zones: %v", err) return nil, fmt.Errorf("error describing availability zones: %v", err) } - filters = append(filters, awsproviderv1.Filter{Name: "availabilityZone", Values: []string{availabilityZone}}) + filters = append(filters, machinev1.Filter{Name: "availabilityZone", Values: []string{availabilityZone}}) } filters = append(filters, subnet.Filters...) klog.Info("Describing subnets based on filters") @@ -153,7 +150,7 @@ func getSubnetIDs(machine runtimeclient.ObjectKey, subnet awsproviderv1.AWSResou return subnetIDs, nil } -func getAMI(machine runtimeclient.ObjectKey, AMI awsproviderv1.AWSResourceReference, client awsclient.Client) (*string, error) { +func getAMI(machine runtimeclient.ObjectKey, AMI machinev1.AWSResourceReference, client awsclient.Client) (*string, error) { if AMI.ID != nil { amiID := AMI.ID klog.Infof("Using AMI %s", *amiID) @@ -200,7 +197,7 @@ func getAMI(machine runtimeclient.ObjectKey, AMI awsproviderv1.AWSResourceRefere return nil, fmt.Errorf("AMI ID or AMI filters need to be specified") } -func getBlockDeviceMappings(machine runtimeclient.ObjectKey, blockDeviceMappingSpecs []awsproviderv1.BlockDeviceMappingSpec, AMI string, client awsclient.Client) ([]*ec2.BlockDeviceMapping, error) { +func getBlockDeviceMappings(machine runtimeclient.ObjectKey, blockDeviceMappingSpecs []machinev1.BlockDeviceMappingSpec, AMI string, client awsclient.Client) ([]*ec2.BlockDeviceMapping, error) { blockDeviceMappings := make([]*ec2.BlockDeviceMapping, 0) if len(blockDeviceMappingSpecs) == 0 { @@ -273,7 +270,7 @@ func getBlockDeviceMappings(machine runtimeclient.ObjectKey, blockDeviceMappingS return blockDeviceMappings, nil } -func launchInstance(machine *machinev1.Machine, machineProviderConfig *awsproviderv1.AWSMachineProviderConfig, userData []byte, client awsclient.Client, infra *configv1.Infrastructure) (*ec2.Instance, error) { +func launchInstance(machine *machinev1.Machine, machineProviderConfig *machinev1.AWSMachineProviderConfig, userData []byte, client awsclient.Client, infra *configv1.Infrastructure) (*ec2.Instance, error) { machineKey := runtimeclient.ObjectKey{ Name: machine.Name, Namespace: machine.Namespace, @@ -348,7 +345,7 @@ func launchInstance(machine *machinev1.Machine, machineProviderConfig *awsprovid switch instanceTenancy { case "": // Do nothing when not set - case awsproviderv1.DefaultTenancy, awsproviderv1.DedicatedTenancy, awsproviderv1.HostTenancy: + case machinev1.DefaultTenancy, machinev1.DedicatedTenancy, machinev1.HostTenancy: if placement == nil { placement = &ec2.Placement{} } @@ -357,9 +354,9 @@ func launchInstance(machine *machinev1.Machine, machineProviderConfig *awsprovid default: return nil, mapierrors.CreateMachine("invalid instance tenancy: %s. Allowed options are: %s,%s,%s", instanceTenancy, - awsproviderv1.DefaultTenancy, - awsproviderv1.DedicatedTenancy, - awsproviderv1.HostTenancy) + machinev1.DefaultTenancy, + machinev1.DedicatedTenancy, + machinev1.HostTenancy) } inputConfig := ec2.RunInstancesInput{ @@ -412,7 +409,7 @@ func launchInstance(machine *machinev1.Machine, machineProviderConfig *awsprovid } // buildTagList compile a list of ec2 tags from machine provider spec and infrastructure object platform spec -func buildTagList(machineName string, clusterID string, machineTags []awsproviderv1.TagSpecification, infra *configv1.Infrastructure) []*ec2.Tag { +func buildTagList(machineName string, clusterID string, machineTags []machinev1.TagSpecification, infra *configv1.Infrastructure) []*ec2.Tag { rawTagList := []*ec2.Tag{} mergedTags := mergeInfrastructureAndMachineSpecTags(machineTags, infra) @@ -433,16 +430,16 @@ func buildTagList(machineName string, clusterID string, machineTags []awsprovide // mergeInfrastructureAndMachineSpecTags merge list of tags from machine provider spec and Infrastructure object platform spec. // Machine tags have precedence over Infrastructure -func mergeInfrastructureAndMachineSpecTags(machineSpecTags []awsproviderv1.TagSpecification, infra *configv1.Infrastructure) []awsproviderv1.TagSpecification { +func mergeInfrastructureAndMachineSpecTags(machineSpecTags []machinev1.TagSpecification, infra *configv1.Infrastructure) []machinev1.TagSpecification { if infra == nil || infra.Status.PlatformStatus == nil || infra.Status.PlatformStatus.AWS == nil || infra.Status.PlatformStatus.AWS.ResourceTags == nil { return machineSpecTags } - mergedList := []awsproviderv1.TagSpecification{} + mergedList := []machinev1.TagSpecification{} mergedList = append(mergedList, machineSpecTags...) for _, tag := range infra.Status.PlatformStatus.AWS.ResourceTags { - mergedList = append(mergedList, awsproviderv1.TagSpecification{Name: tag.Key, Value: tag.Value}) + mergedList = append(mergedList, machinev1.TagSpecification{Name: tag.Key, Value: tag.Value}) } return mergedList @@ -479,7 +476,7 @@ func sortInstances(instances []*ec2.Instance) { sort.Sort(instanceList(instances)) } -func getInstanceMarketOptionsRequest(providerConfig *awsproviderv1.AWSMachineProviderConfig) *ec2.InstanceMarketOptionsRequest { +func getInstanceMarketOptionsRequest(providerConfig *machinev1.AWSMachineProviderConfig) *ec2.InstanceMarketOptionsRequest { if providerConfig.SpotMarketOptions == nil { // Instance is not a Spot instance return nil diff --git a/pkg/actuators/machine/instances_test.go b/pkg/actuators/machine/instances_test.go index daac71624b..ef883f2d4c 100644 --- a/pkg/actuators/machine/instances_test.go +++ b/pkg/actuators/machine/instances_test.go @@ -8,12 +8,11 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - configv1 "github.com/openshift/api/config/v1" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" - "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/golang/mock/gomock" + configv1 "github.com/openshift/api/config/v1" + machinev1 "github.com/openshift/api/machine/v1beta1" mockaws "sigs.k8s.io/cluster-api-provider-aws/pkg/client/mock" + "sigs.k8s.io/controller-runtime/pkg/client" ) func TestRemoveDuplicatedTags(t *testing.T) { @@ -60,13 +59,13 @@ func TestRemoveDuplicatedTags(t *testing.T) { func TestBuildTagList(t *testing.T) { cases := []struct { name string - machineSpecTags []awsproviderv1.TagSpecification + machineSpecTags []machinev1.TagSpecification infra *configv1.Infrastructure expected []*ec2.Tag }{ { name: "with empty infra and provider spec should return default tags", - machineSpecTags: []awsproviderv1.TagSpecification{}, + machineSpecTags: []machinev1.TagSpecification{}, infra: &configv1.Infrastructure{ Status: configv1.InfrastructureStatus{ PlatformStatus: &configv1.PlatformStatus{ @@ -83,7 +82,7 @@ func TestBuildTagList(t *testing.T) { }, { name: "with empty infra should return default tags", - machineSpecTags: []awsproviderv1.TagSpecification{}, + machineSpecTags: []machinev1.TagSpecification{}, infra: &configv1.Infrastructure{}, // should work with empty infra object expected: []*ec2.Tag{ {Key: aws.String("kubernetes.io/cluster/clusterID"), Value: aws.String("owned")}, @@ -92,7 +91,7 @@ func TestBuildTagList(t *testing.T) { }, { name: "with nil infra should return default tags", - machineSpecTags: []awsproviderv1.TagSpecification{}, + machineSpecTags: []machinev1.TagSpecification{}, infra: nil, // should work with nil infra object expected: []*ec2.Tag{ {Key: aws.String("kubernetes.io/cluster/clusterID"), Value: aws.String("owned")}, @@ -101,7 +100,7 @@ func TestBuildTagList(t *testing.T) { }, { name: "should filter out bad tags from provider spec", - machineSpecTags: []awsproviderv1.TagSpecification{ + machineSpecTags: []machinev1.TagSpecification{ {Name: "Name", Value: "badname"}, {Name: "kubernetes.io/cluster/badid", Value: "badvalue"}, {Name: "good", Value: "goodvalue"}, @@ -116,7 +115,7 @@ func TestBuildTagList(t *testing.T) { }, { name: "should filter out bad tags from infra object", - machineSpecTags: []awsproviderv1.TagSpecification{}, + machineSpecTags: []machinev1.TagSpecification{}, infra: &configv1.Infrastructure{ Status: configv1.InfrastructureStatus{ PlatformStatus: &configv1.PlatformStatus{ @@ -148,7 +147,7 @@ func TestBuildTagList(t *testing.T) { }, { name: "tags from machine object should have precedence", - machineSpecTags: []awsproviderv1.TagSpecification{ + machineSpecTags: []machinev1.TagSpecification{ {Name: "Name", Value: "badname"}, {Name: "kubernetes.io/cluster/badid", Value: "badvalue"}, {Name: "good", Value: "goodvalue"}, @@ -191,7 +190,7 @@ func TestBuildEC2Filters(t *testing.T) { value2 := "B" value3 := "C" - inputFilters := []awsproviderv1.Filter{ + inputFilters := []machinev1.Filter{ { Name: filter1, Values: []string{value1, value2}, @@ -239,10 +238,10 @@ func TestGetBlockDeviceMappings(t *testing.T) { }, }, nil).AnyTimes() - oneBlockDevice := []awsproviderv1.BlockDeviceMappingSpec{ + oneBlockDevice := []machinev1.BlockDeviceMappingSpec{ { DeviceName: &rootDeviceName, - EBS: &awsproviderv1.EBSBlockDeviceSpec{ + EBS: &machinev1.EBSBlockDeviceSpec{ VolumeSize: &volumeSize, VolumeType: &volumeType, }, @@ -264,10 +263,10 @@ func TestGetBlockDeviceMappings(t *testing.T) { }, } - blockDevices := []awsproviderv1.BlockDeviceMappingSpec{ + blockDevices := []machinev1.BlockDeviceMappingSpec{ { DeviceName: &rootDeviceName, - EBS: &awsproviderv1.EBSBlockDeviceSpec{ + EBS: &machinev1.EBSBlockDeviceSpec{ VolumeSize: &volumeSize, VolumeType: &volumeType, }, @@ -276,7 +275,7 @@ func TestGetBlockDeviceMappings(t *testing.T) { }, { DeviceName: &deviceName2, - EBS: &awsproviderv1.EBSBlockDeviceSpec{ + EBS: &machinev1.EBSBlockDeviceSpec{ VolumeSize: &volumeSize2, VolumeType: &volumeType, }, @@ -308,23 +307,23 @@ func TestGetBlockDeviceMappings(t *testing.T) { }, } - blockDevicesOneEmptyName := make([]awsproviderv1.BlockDeviceMappingSpec, len(blockDevices)) + blockDevicesOneEmptyName := make([]machinev1.BlockDeviceMappingSpec, len(blockDevices)) copy(blockDevicesOneEmptyName, blockDevices) blockDevicesOneEmptyName[0].DeviceName = nil - blockDevicesTwoEmptyNames := make([]awsproviderv1.BlockDeviceMappingSpec, len(blockDevicesOneEmptyName)) + blockDevicesTwoEmptyNames := make([]machinev1.BlockDeviceMappingSpec, len(blockDevicesOneEmptyName)) copy(blockDevicesTwoEmptyNames, blockDevicesOneEmptyName) blockDevicesTwoEmptyNames[1].DeviceName = nil testCases := []struct { description string - blockDevices []awsproviderv1.BlockDeviceMappingSpec + blockDevices []machinev1.BlockDeviceMappingSpec expected []*ec2.BlockDeviceMapping expectedErr bool }{ { description: "When it gets an empty blockDevices list", - blockDevices: []awsproviderv1.BlockDeviceMappingSpec{}, + blockDevices: []machinev1.BlockDeviceMappingSpec{}, expected: []*ec2.BlockDeviceMapping{}, }, { @@ -465,7 +464,7 @@ func TestLaunchInstance(t *testing.T) { cases := []struct { name string - providerConfig *awsproviderv1.AWSMachineProviderConfig + providerConfig *machinev1.AWSMachineProviderConfig securityGroupOutput *ec2.DescribeSecurityGroupsOutput securityGroupErr error subnetOutput *ec2.DescribeSubnetsOutput @@ -482,9 +481,9 @@ func TestLaunchInstance(t *testing.T) { { name: "Security groups with filters", providerConfig: stubPCSecurityGroups( - []awsproviderv1.AWSResourceReference{ + []machinev1.AWSResourceReference{ { - Filters: []awsproviderv1.Filter{}, + Filters: []machinev1.Filter{}, }, }, ), @@ -527,9 +526,9 @@ func TestLaunchInstance(t *testing.T) { { name: "Security groups with filters with error", providerConfig: stubPCSecurityGroups( - []awsproviderv1.AWSResourceReference{ + []machinev1.AWSResourceReference{ { - Filters: []awsproviderv1.Filter{}, + Filters: []machinev1.Filter{}, }, }, ), @@ -538,9 +537,9 @@ func TestLaunchInstance(t *testing.T) { { name: "No security group", providerConfig: stubPCSecurityGroups( - []awsproviderv1.AWSResourceReference{ + []machinev1.AWSResourceReference{ { - Filters: []awsproviderv1.Filter{}, + Filters: []machinev1.Filter{}, }, }, ), @@ -575,8 +574,8 @@ func TestLaunchInstance(t *testing.T) { }, { name: "Subnet with filters", - providerConfig: stubPCSubnet(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{}, + providerConfig: stubPCSubnet(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{}, }), subnetOutput: &ec2.DescribeSubnetsOutput{ Subnets: []*ec2.Subnet{ @@ -625,22 +624,22 @@ func TestLaunchInstance(t *testing.T) { }, { name: "Subnet with filters with error", - providerConfig: stubPCSubnet(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{}, + providerConfig: stubPCSubnet(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{}, }), subnetErr: fmt.Errorf("error"), }, { name: "Subnet with availability zone with error", - providerConfig: stubPCSubnet(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{}, + providerConfig: stubPCSubnet(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{}, }), azErr: fmt.Errorf("error"), }, { name: "AMI with filters", - providerConfig: stubPCAMI(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{ + providerConfig: stubPCAMI(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{ { Name: "foo", Values: []string{"bar"}, @@ -692,15 +691,15 @@ func TestLaunchInstance(t *testing.T) { }, { name: "AMI with filters with error", - providerConfig: stubPCAMI(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{}, + providerConfig: stubPCAMI(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{}, }), imageErr: fmt.Errorf("error"), }, { name: "AMI with filters with no image", - providerConfig: stubPCAMI(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{ + providerConfig: stubPCAMI(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{ { Name: "image_stage", Values: []string{"base"}, @@ -748,8 +747,8 @@ func TestLaunchInstance(t *testing.T) { }, { name: "AMI with filters with two images", - providerConfig: stubPCAMI(awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{ + providerConfig: stubPCAMI(machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{ { Name: "image_stage", Values: []string{"base"}, @@ -805,7 +804,7 @@ func TestLaunchInstance(t *testing.T) { }, { name: "AMI not specified", - providerConfig: stubPCAMI(awsproviderv1.AWSResourceReference{}), + providerConfig: stubPCAMI(machinev1.AWSResourceReference{}), }, { name: "Dedicated instance tenancy", @@ -935,7 +934,7 @@ func TestSortInstances(t *testing.T) { func TestGetInstanceMarketOptionsRequest(t *testing.T) { testCases := []struct { name string - spotMarketOptions *awsproviderv1.SpotMarketOptions + spotMarketOptions *machinev1.SpotMarketOptions expectedRequest *ec2.InstanceMarketOptionsRequest }{ { @@ -945,7 +944,7 @@ func TestGetInstanceMarketOptionsRequest(t *testing.T) { }, { name: "with an empty Spot options specified", - spotMarketOptions: &awsproviderv1.SpotMarketOptions{}, + spotMarketOptions: &machinev1.SpotMarketOptions{}, expectedRequest: &ec2.InstanceMarketOptionsRequest{ MarketType: aws.String(ec2.MarketTypeSpot), SpotOptions: &ec2.SpotMarketOptions{ @@ -956,7 +955,7 @@ func TestGetInstanceMarketOptionsRequest(t *testing.T) { }, { name: "with an empty MaxPrice specified", - spotMarketOptions: &awsproviderv1.SpotMarketOptions{ + spotMarketOptions: &machinev1.SpotMarketOptions{ MaxPrice: aws.String(""), }, expectedRequest: &ec2.InstanceMarketOptionsRequest{ @@ -969,7 +968,7 @@ func TestGetInstanceMarketOptionsRequest(t *testing.T) { }, { name: "with a valid MaxPrice specified", - spotMarketOptions: &awsproviderv1.SpotMarketOptions{ + spotMarketOptions: &machinev1.SpotMarketOptions{ MaxPrice: aws.String("0.01"), }, expectedRequest: &ec2.InstanceMarketOptionsRequest{ @@ -985,7 +984,7 @@ func TestGetInstanceMarketOptionsRequest(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - providerConfig := &awsproviderv1.AWSMachineProviderConfig{ + providerConfig := &machinev1.AWSMachineProviderConfig{ SpotMarketOptions: tc.spotMarketOptions, } diff --git a/pkg/actuators/machine/machine_scope.go b/pkg/actuators/machine/machine_scope.go index ce96f0a39c..5a03dfc425 100644 --- a/pkg/actuators/machine/machine_scope.go +++ b/pkg/actuators/machine/machine_scope.go @@ -6,11 +6,10 @@ import ( "strings" "github.com/aws/aws-sdk-go/service/ec2" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" machineapierros "github.com/openshift/machine-api-operator/pkg/controller/machine" corev1 "k8s.io/api/core/v1" "k8s.io/klog/v2" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -45,17 +44,17 @@ type machineScope struct { // machine resource machine *machinev1.Machine machineToBePatched runtimeclient.Patch - providerSpec *awsproviderv1.AWSMachineProviderConfig - providerStatus *awsproviderv1.AWSMachineProviderStatus + providerSpec *machinev1.AWSMachineProviderConfig + providerStatus *machinev1.AWSMachineProviderStatus } func newMachineScope(params machineScopeParams) (*machineScope, error) { - providerSpec, err := awsproviderv1.ProviderSpecFromRawExtension(params.machine.Spec.ProviderSpec.Value) + providerSpec, err := ProviderSpecFromRawExtension(params.machine.Spec.ProviderSpec.Value) if err != nil { return nil, machineapierros.InvalidMachineConfiguration("failed to get machine config: %v", err) } - providerStatus, err := awsproviderv1.ProviderStatusFromRawExtension(params.machine.Status.ProviderStatus) + providerStatus, err := ProviderStatusFromRawExtension(params.machine.Status.ProviderStatus) if err != nil { return nil, machineapierros.InvalidMachineConfiguration("failed to get machine provider status: %v", err.Error()) } @@ -85,7 +84,7 @@ func newMachineScope(params machineScopeParams) (*machineScope, error) { func (s *machineScope) patchMachine() error { klog.V(3).Infof("%v: patching machine", s.machine.GetName()) - providerStatus, err := awsproviderv1.RawExtensionFromProviderStatus(s.providerStatus) + providerStatus, err := RawExtensionFromProviderStatus(s.providerStatus) if err != nil { return machineapierros.InvalidMachineConfiguration("failed to get machine provider status: %v", err.Error()) } @@ -136,7 +135,7 @@ func (s *machineScope) getUserData() ([]byte, error) { return userData, nil } -func (s *machineScope) setProviderStatus(instance *ec2.Instance, condition awsproviderv1.AWSMachineProviderCondition) error { +func (s *machineScope) setProviderStatus(instance *ec2.Instance, condition machinev1.AWSMachineProviderCondition) error { klog.Infof("%s: Updating status", s.machine.Name) networkAddresses := []corev1.NodeAddress{} diff --git a/pkg/actuators/machine/machine_scope_test.go b/pkg/actuators/machine/machine_scope_test.go index d0450b12c8..b389b4d986 100644 --- a/pkg/actuators/machine/machine_scope_test.go +++ b/pkg/actuators/machine/machine_scope_test.go @@ -11,12 +11,11 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/golang/mock/gomock" . "github.com/onsi/gomega" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" mockaws "sigs.k8s.io/cluster-api-provider-aws/pkg/client/mock" "sigs.k8s.io/controller-runtime/pkg/client" @@ -27,8 +26,8 @@ import ( const testNamespace = "aws-test" -func machineWithSpec(spec *awsproviderv1.AWSMachineProviderConfig) *machinev1.Machine { - rawSpec, err := awsproviderv1.RawExtensionFromProviderSpec(spec) +func machineWithSpec(spec *machinev1.AWSMachineProviderConfig) *machinev1.Machine { + rawSpec, err := RawExtensionFromProviderSpec(spec) if err != nil { panic("Failed to encode raw extension from provider spec") } @@ -49,7 +48,7 @@ func machineWithSpec(spec *awsproviderv1.AWSMachineProviderConfig) *machinev1.Ma func TestGetUserData(t *testing.T) { userDataSecretName := "aws-ignition" - defaultProviderSpec := &awsproviderv1.AWSMachineProviderConfig{ + defaultProviderSpec := &machinev1.AWSMachineProviderConfig{ UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, @@ -58,7 +57,7 @@ func TestGetUserData(t *testing.T) { testCases := []struct { testCase string userDataSecret *corev1.Secret - providerSpec *awsproviderv1.AWSMachineProviderConfig + providerSpec *machinev1.AWSMachineProviderConfig expectedUserdata []byte expectError bool }{ @@ -107,7 +106,7 @@ func TestGetUserData(t *testing.T) { { testCase: "no user-data in provider spec", userDataSecret: nil, - providerSpec: &awsproviderv1.AWSMachineProviderConfig{}, + providerSpec: &machinev1.AWSMachineProviderConfig{}, expectError: false, expectedUserdata: nil, }, @@ -177,7 +176,7 @@ func TestPatchMachine(t *testing.T) { failedPhase := "Failed" - providerStatus := &awsproviderv1.AWSMachineProviderStatus{} + providerStatus := &machinev1.AWSMachineProviderStatus{} testCases := []struct { name string @@ -217,7 +216,7 @@ func TestPatchMachine(t *testing.T) { providerStatus.InstanceState = &instanceState }, expect: func(m *machinev1.Machine) error { - providerStatus, err := awsproviderv1.ProviderStatusFromRawExtension(m.Status.ProviderStatus) + providerStatus, err := ProviderStatusFromRawExtension(m.Status.ProviderStatus) if err != nil { return fmt.Errorf("unable to get provider status: %v", err) } diff --git a/pkg/actuators/machine/reconciler.go b/pkg/actuators/machine/reconciler.go index dc95fda0bf..8e52750169 100644 --- a/pkg/actuators/machine/reconciler.go +++ b/pkg/actuators/machine/reconciler.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" configv1 "github.com/openshift/api/config/v1" + machinev1 "github.com/openshift/api/machine/v1beta1" machinecontroller "github.com/openshift/machine-api-operator/pkg/controller/machine" "github.com/openshift/machine-api-operator/pkg/metrics" corev1 "k8s.io/api/core/v1" @@ -15,8 +16,6 @@ import ( "k8s.io/klog/v2" awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client" - - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" ) const ( @@ -303,9 +302,9 @@ func (r *Reconciler) updateLoadBalancers(instance *ec2.Instance) error { networkLoadBalancerNames := []string{} for _, loadBalancerRef := range r.providerSpec.LoadBalancers { switch loadBalancerRef.Type { - case awsproviderv1.NetworkLoadBalancerType: + case machinev1.NetworkLoadBalancerType: networkLoadBalancerNames = append(networkLoadBalancerNames, loadBalancerRef.Name) - case awsproviderv1.ClassicLoadBalancerType: + case machinev1.ClassicLoadBalancerType: classicLoadBalancerNames = append(classicLoadBalancerNames, loadBalancerRef.Name) } } @@ -339,7 +338,7 @@ func (r *Reconciler) removeFromLoadBalancers(instances []*ec2.Instance) error { } networkLoadBalancerNames := []string{} for _, loadBalancerRef := range r.providerSpec.LoadBalancers { - if loadBalancerRef.Type == awsproviderv1.NetworkLoadBalancerType { + if loadBalancerRef.Type == machinev1.NetworkLoadBalancerType { networkLoadBalancerNames = append(networkLoadBalancerNames, loadBalancerRef.Name) } } @@ -400,7 +399,7 @@ func (r *Reconciler) setMachineCloudProviderSpecifics(instance *ec2.Instance) er // Reaching to machine provider config since the region is not directly // providing by *ec2.Instance object - machineProviderConfig, err := awsproviderv1.ProviderSpecFromRawExtension(r.machine.Spec.ProviderSpec.Value) + machineProviderConfig, err := ProviderSpecFromRawExtension(r.machine.Spec.ProviderSpec.Value) if err != nil { return fmt.Errorf("error decoding MachineProviderConfig: %w", err) } diff --git a/pkg/actuators/machine/reconciler_test.go b/pkg/actuators/machine/reconciler_test.go index 9bf9d20803..fe75932329 100644 --- a/pkg/actuators/machine/reconciler_test.go +++ b/pkg/actuators/machine/reconciler_test.go @@ -11,13 +11,12 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/golang/mock/gomock" configv1 "github.com/openshift/api/config/v1" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" machinecontroller "github.com/openshift/machine-api-operator/pkg/controller/machine" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/utils/pointer" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" mockaws "sigs.k8s.io/cluster-api-provider-aws/pkg/client/mock" runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -63,7 +62,7 @@ func TestAvailabilityZone(t *testing.T) { t.Fatal(err) } - machinePc, err := awsproviderv1.ProviderSpecFromRawExtension(machine.Spec.ProviderSpec.Value) + machinePc, err := ProviderSpecFromRawExtension(machine.Spec.ProviderSpec.Value) if err != nil { t.Fatal(err) } @@ -78,7 +77,7 @@ func TestAvailabilityZone(t *testing.T) { machinePc.Subnet.ID = aws.String(tc.subnet) } - config, err := awsproviderv1.RawExtensionFromProviderSpec(machinePc) + config, err := RawExtensionFromProviderSpec(machinePc) if err != nil { t.Fatal(err) } @@ -212,41 +211,41 @@ func TestCreate(t *testing.T) { testCases := []struct { testcase string - providerConfig *awsproviderv1.AWSMachineProviderConfig + providerConfig *machinev1.AWSMachineProviderConfig userDataSecret *corev1.Secret awsCredentialsSecret *corev1.Secret expectedError error }{ { testcase: "Create succeed", - providerConfig: &awsproviderv1.AWSMachineProviderConfig{ - AMI: awsproviderv1.AWSResourceReference{ + providerConfig: &machinev1.AWSMachineProviderConfig{ + AMI: machinev1.AWSResourceReference{ ID: aws.String("ami-a9acbbd6"), }, CredentialsSecret: &corev1.LocalObjectReference{ Name: awsCredentialsSecretName, }, InstanceType: "m4.xlarge", - Placement: awsproviderv1.Placement{ + Placement: machinev1.Placement{ Region: region, AvailabilityZone: defaultAvailabilityZone, }, - Subnet: awsproviderv1.AWSResourceReference{ + Subnet: machinev1.AWSResourceReference{ ID: aws.String("subnet-0e56b13a64ff8a941"), }, - IAMInstanceProfile: &awsproviderv1.AWSResourceReference{ + IAMInstanceProfile: &machinev1.AWSResourceReference{ ID: aws.String("openshift_master_launch_instances"), }, KeyName: aws.String(keyName), UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, - Tags: []awsproviderv1.TagSpecification{ + Tags: []machinev1.TagSpecification{ {Name: "openshift-node-group-config", Value: "node-config-master"}, {Name: "host-type", Value: "master"}, {Name: "sub-host-type", Value: "default"}, }, - SecurityGroups: []awsproviderv1.AWSResourceReference{ + SecurityGroups: []machinev1.AWSResourceReference{ {ID: aws.String("sg-00868b02fbe29de17")}, {ID: aws.String("sg-0a4658991dc5eb40a")}, {ID: aws.String("sg-009a70e28fa4ba84e")}, @@ -254,22 +253,22 @@ func TestCreate(t *testing.T) { {ID: aws.String("sg-08b1ffd32874d59a2")}, }, PublicIP: aws.Bool(true), - LoadBalancers: []awsproviderv1.LoadBalancerReference{ + LoadBalancers: []machinev1.LoadBalancerReference{ { Name: "cluster-con", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-ext", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-int", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-net-lb", - Type: awsproviderv1.NetworkLoadBalancerType, + Type: machinev1.NetworkLoadBalancerType, }, }, }, @@ -279,34 +278,34 @@ func TestCreate(t *testing.T) { }, { testcase: "Bad userData", - providerConfig: &awsproviderv1.AWSMachineProviderConfig{ - AMI: awsproviderv1.AWSResourceReference{ + providerConfig: &machinev1.AWSMachineProviderConfig{ + AMI: machinev1.AWSResourceReference{ ID: aws.String("ami-a9acbbd6"), }, CredentialsSecret: &corev1.LocalObjectReference{ Name: awsCredentialsSecretName, }, InstanceType: "m4.xlarge", - Placement: awsproviderv1.Placement{ + Placement: machinev1.Placement{ Region: region, AvailabilityZone: defaultAvailabilityZone, }, - Subnet: awsproviderv1.AWSResourceReference{ + Subnet: machinev1.AWSResourceReference{ ID: aws.String("subnet-0e56b13a64ff8a941"), }, - IAMInstanceProfile: &awsproviderv1.AWSResourceReference{ + IAMInstanceProfile: &machinev1.AWSResourceReference{ ID: aws.String("openshift_master_launch_instances"), }, KeyName: aws.String(keyName), UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, - Tags: []awsproviderv1.TagSpecification{ + Tags: []machinev1.TagSpecification{ {Name: "openshift-node-group-config", Value: "node-config-master"}, {Name: "host-type", Value: "master"}, {Name: "sub-host-type", Value: "default"}, }, - SecurityGroups: []awsproviderv1.AWSResourceReference{ + SecurityGroups: []machinev1.AWSResourceReference{ {ID: aws.String("sg-00868b02fbe29de17")}, {ID: aws.String("sg-0a4658991dc5eb40a")}, {ID: aws.String("sg-009a70e28fa4ba84e")}, @@ -314,22 +313,22 @@ func TestCreate(t *testing.T) { {ID: aws.String("sg-08b1ffd32874d59a2")}, }, PublicIP: aws.Bool(true), - LoadBalancers: []awsproviderv1.LoadBalancerReference{ + LoadBalancers: []machinev1.LoadBalancerReference{ { Name: "cluster-con", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-ext", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-int", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-net-lb", - Type: awsproviderv1.NetworkLoadBalancerType, + Type: machinev1.NetworkLoadBalancerType, }, }, }, @@ -347,35 +346,35 @@ func TestCreate(t *testing.T) { }, { testcase: "Failed security groups return invalid config machine error", - providerConfig: &awsproviderv1.AWSMachineProviderConfig{ - AMI: awsproviderv1.AWSResourceReference{ + providerConfig: &machinev1.AWSMachineProviderConfig{ + AMI: machinev1.AWSResourceReference{ ID: aws.String("ami-a9acbbd6"), }, CredentialsSecret: &corev1.LocalObjectReference{ Name: awsCredentialsSecretName, }, InstanceType: "m4.xlarge", - Placement: awsproviderv1.Placement{ + Placement: machinev1.Placement{ Region: region, AvailabilityZone: defaultAvailabilityZone, }, - Subnet: awsproviderv1.AWSResourceReference{ + Subnet: machinev1.AWSResourceReference{ ID: aws.String("subnet-0e56b13a64ff8a941"), }, - IAMInstanceProfile: &awsproviderv1.AWSResourceReference{ + IAMInstanceProfile: &machinev1.AWSResourceReference{ ID: aws.String("openshift_master_launch_instances"), }, KeyName: aws.String(keyName), UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, - Tags: []awsproviderv1.TagSpecification{ + Tags: []machinev1.TagSpecification{ {Name: "openshift-node-group-config", Value: "node-config-master"}, {Name: "host-type", Value: "master"}, {Name: "sub-host-type", Value: "default"}, }, - SecurityGroups: []awsproviderv1.AWSResourceReference{{ - Filters: []awsproviderv1.Filter{{ + SecurityGroups: []machinev1.AWSResourceReference{{ + Filters: []machinev1.Filter{{ Name: "tag:Name", Values: []string{fmt.Sprintf("%s-%s-sg", stubClusterID, "role")}, }}, @@ -388,37 +387,37 @@ func TestCreate(t *testing.T) { }, { testcase: "Failed Availability zones return invalid config machine error", - providerConfig: &awsproviderv1.AWSMachineProviderConfig{ - AMI: awsproviderv1.AWSResourceReference{ + providerConfig: &machinev1.AWSMachineProviderConfig{ + AMI: machinev1.AWSResourceReference{ ID: aws.String("ami-a9acbbd6"), }, CredentialsSecret: &corev1.LocalObjectReference{ Name: awsCredentialsSecretName, }, InstanceType: "m4.xlarge", - Placement: awsproviderv1.Placement{ + Placement: machinev1.Placement{ Region: region, AvailabilityZone: defaultAvailabilityZone, }, - Subnet: awsproviderv1.AWSResourceReference{ - Filters: []awsproviderv1.Filter{{ + Subnet: machinev1.AWSResourceReference{ + Filters: []machinev1.Filter{{ Name: "tag:Name", Values: []string{fmt.Sprintf("%s-private-%s", stubClusterID, "az")}, }}, }, - IAMInstanceProfile: &awsproviderv1.AWSResourceReference{ + IAMInstanceProfile: &machinev1.AWSResourceReference{ ID: aws.String("openshift_master_launch_instances"), }, KeyName: aws.String(keyName), UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, - Tags: []awsproviderv1.TagSpecification{ + Tags: []machinev1.TagSpecification{ {Name: "openshift-node-group-config", Value: "node-config-master"}, {Name: "host-type", Value: "master"}, {Name: "sub-host-type", Value: "default"}, }, - SecurityGroups: []awsproviderv1.AWSResourceReference{ + SecurityGroups: []machinev1.AWSResourceReference{ {ID: aws.String("sg-00868b02fbe29de17")}, {ID: aws.String("sg-0a4658991dc5eb40a")}, {ID: aws.String("sg-009a70e28fa4ba84e")}, @@ -433,43 +432,43 @@ func TestCreate(t *testing.T) { }, { testcase: "Failed BlockDevices return invalid config machine error", - providerConfig: &awsproviderv1.AWSMachineProviderConfig{ - AMI: awsproviderv1.AWSResourceReference{ + providerConfig: &machinev1.AWSMachineProviderConfig{ + AMI: machinev1.AWSResourceReference{ ID: aws.String("ami-a9acbbd6"), }, CredentialsSecret: &corev1.LocalObjectReference{ Name: awsCredentialsSecretName, }, InstanceType: "m4.xlarge", - Placement: awsproviderv1.Placement{ + Placement: machinev1.Placement{ Region: region, AvailabilityZone: defaultAvailabilityZone, }, - BlockDevices: []awsproviderv1.BlockDeviceMappingSpec{ + BlockDevices: []machinev1.BlockDeviceMappingSpec{ { - EBS: &awsproviderv1.EBSBlockDeviceSpec{ + EBS: &machinev1.EBSBlockDeviceSpec{ VolumeType: pointer.StringPtr("type"), VolumeSize: pointer.Int64Ptr(int64(1)), Iops: pointer.Int64Ptr(int64(1)), }, }, }, - Subnet: awsproviderv1.AWSResourceReference{ + Subnet: machinev1.AWSResourceReference{ ID: aws.String("subnet-0e56b13a64ff8a941"), }, - IAMInstanceProfile: &awsproviderv1.AWSResourceReference{ + IAMInstanceProfile: &machinev1.AWSResourceReference{ ID: aws.String("openshift_master_launch_instances"), }, KeyName: aws.String(keyName), UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, - Tags: []awsproviderv1.TagSpecification{ + Tags: []machinev1.TagSpecification{ {Name: "openshift-node-group-config", Value: "node-config-master"}, {Name: "host-type", Value: "master"}, {Name: "sub-host-type", Value: "default"}, }, - SecurityGroups: []awsproviderv1.AWSResourceReference{ + SecurityGroups: []machinev1.AWSResourceReference{ {ID: aws.String("sg-00868b02fbe29de17")}, {ID: aws.String("sg-0a4658991dc5eb40a")}, {ID: aws.String("sg-009a70e28fa4ba84e")}, @@ -487,7 +486,7 @@ func TestCreate(t *testing.T) { // create fake resources t.Logf("testCase: %v", tc.testcase) - encodedProviderConfig, err := awsproviderv1.RawExtensionFromProviderSpec(tc.providerConfig) + encodedProviderConfig, err := RawExtensionFromProviderSpec(tc.providerConfig) if err != nil { t.Fatalf("Unexpected error") } @@ -769,13 +768,13 @@ func TestGetMachineInstances(t *testing.T) { testCases := []struct { testcase string - providerStatus awsproviderv1.AWSMachineProviderStatus + providerStatus machinev1.AWSMachineProviderStatus awsClientFunc func(*gomock.Controller) awsclient.Client exists bool }{ { testcase: "empty-status-search-by-tag", - providerStatus: awsproviderv1.AWSMachineProviderStatus{}, + providerStatus: machinev1.AWSMachineProviderStatus{}, awsClientFunc: func(ctrl *gomock.Controller) awsclient.Client { mockAWSClient := mockaws.NewMockClient(ctrl) @@ -801,7 +800,7 @@ func TestGetMachineInstances(t *testing.T) { }, { testcase: "has-status-search-by-id-running", - providerStatus: awsproviderv1.AWSMachineProviderStatus{ + providerStatus: machinev1.AWSMachineProviderStatus{ InstanceID: aws.String(instanceID), }, awsClientFunc: func(ctrl *gomock.Controller) awsclient.Client { @@ -822,7 +821,7 @@ func TestGetMachineInstances(t *testing.T) { }, { testcase: "has-status-search-by-id-terminated", - providerStatus: awsproviderv1.AWSMachineProviderStatus{ + providerStatus: machinev1.AWSMachineProviderStatus{ InstanceID: aws.String(instanceID), }, awsClientFunc: func(ctrl *gomock.Controller) awsclient.Client { @@ -859,7 +858,7 @@ func TestGetMachineInstances(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - awsStatusRaw, err := awsproviderv1.RawExtensionFromProviderStatus(&tc.providerStatus) + awsStatusRaw, err := RawExtensionFromProviderStatus(&tc.providerStatus) if err != nil { t.Fatal(err) } diff --git a/pkg/actuators/machine/stubs.go b/pkg/actuators/machine/stubs.go index 7f534cf04a..4b1ecfd8fb 100644 --- a/pkg/actuators/machine/stubs.go +++ b/pkg/actuators/machine/stubs.go @@ -9,11 +9,10 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/elbv2" configv1 "github.com/openshift/api/config/v1" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" machinecontroller "github.com/openshift/machine-api-operator/pkg/controller/machine" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" ) @@ -41,35 +40,35 @@ runcmd: - [ cat, /root/node_bootstrap/node_settings.yaml] ` -func stubProviderConfig() *awsproviderv1.AWSMachineProviderConfig { - return &awsproviderv1.AWSMachineProviderConfig{ - AMI: awsproviderv1.AWSResourceReference{ +func stubProviderConfig() *machinev1.AWSMachineProviderConfig { + return &machinev1.AWSMachineProviderConfig{ + AMI: machinev1.AWSResourceReference{ ID: aws.String(stubAMIID), }, CredentialsSecret: &corev1.LocalObjectReference{ Name: awsCredentialsSecretName, }, InstanceType: "m4.xlarge", - Placement: awsproviderv1.Placement{ + Placement: machinev1.Placement{ Region: region, AvailabilityZone: defaultAvailabilityZone, }, - Subnet: awsproviderv1.AWSResourceReference{ + Subnet: machinev1.AWSResourceReference{ ID: aws.String("subnet-0e56b13a64ff8a941"), }, - IAMInstanceProfile: &awsproviderv1.AWSResourceReference{ + IAMInstanceProfile: &machinev1.AWSResourceReference{ ID: aws.String("openshift_master_launch_instances"), }, KeyName: aws.String(keyName), UserDataSecret: &corev1.LocalObjectReference{ Name: userDataSecretName, }, - Tags: []awsproviderv1.TagSpecification{ + Tags: []machinev1.TagSpecification{ {Name: "openshift-node-group-config", Value: "node-config-master"}, {Name: "host-type", Value: "master"}, {Name: "sub-host-type", Value: "default"}, }, - SecurityGroups: []awsproviderv1.AWSResourceReference{ + SecurityGroups: []machinev1.AWSResourceReference{ {ID: aws.String("sg-00868b02fbe29de17")}, {ID: aws.String("sg-0a4658991dc5eb40a")}, {ID: aws.String("sg-009a70e28fa4ba84e")}, @@ -77,22 +76,22 @@ func stubProviderConfig() *awsproviderv1.AWSMachineProviderConfig { {ID: aws.String("sg-08b1ffd32874d59a2")}, }, PublicIP: aws.Bool(true), - LoadBalancers: []awsproviderv1.LoadBalancerReference{ + LoadBalancers: []machinev1.LoadBalancerReference{ { Name: "cluster-con", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-ext", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-int", - Type: awsproviderv1.ClassicLoadBalancerType, + Type: machinev1.ClassicLoadBalancerType, }, { Name: "cluster-net-lb", - Type: awsproviderv1.NetworkLoadBalancerType, + Type: machinev1.NetworkLoadBalancerType, }, }, } @@ -101,7 +100,7 @@ func stubProviderConfig() *awsproviderv1.AWSMachineProviderConfig { func stubMachine() (*machinev1.Machine, error) { machinePc := stubProviderConfig() - providerSpec, err := awsproviderv1.RawExtensionFromProviderSpec(machinePc) + providerSpec, err := RawExtensionFromProviderSpec(machinePc) if err != nil { return nil, fmt.Errorf("codec.EncodeProviderSpec failed: %v", err) } @@ -203,31 +202,31 @@ func stubInstance(imageID, instanceID string, setIP bool) *ec2.Instance { } } -func stubPCSecurityGroups(groups []awsproviderv1.AWSResourceReference) *awsproviderv1.AWSMachineProviderConfig { +func stubPCSecurityGroups(groups []machinev1.AWSResourceReference) *machinev1.AWSMachineProviderConfig { pc := stubProviderConfig() pc.SecurityGroups = groups return pc } -func stubPCSubnet(subnet awsproviderv1.AWSResourceReference) *awsproviderv1.AWSMachineProviderConfig { +func stubPCSubnet(subnet machinev1.AWSResourceReference) *machinev1.AWSMachineProviderConfig { pc := stubProviderConfig() pc.Subnet = subnet return pc } -func stubPCAMI(ami awsproviderv1.AWSResourceReference) *awsproviderv1.AWSMachineProviderConfig { +func stubPCAMI(ami machinev1.AWSResourceReference) *machinev1.AWSMachineProviderConfig { pc := stubProviderConfig() pc.AMI = ami return pc } -func stubDedicatedInstanceTenancy() *awsproviderv1.AWSMachineProviderConfig { +func stubDedicatedInstanceTenancy() *machinev1.AWSMachineProviderConfig { pc := stubProviderConfig() - pc.Placement.Tenancy = awsproviderv1.DedicatedTenancy + pc.Placement.Tenancy = machinev1.DedicatedTenancy return pc } -func stubInvalidInstanceTenancy() *awsproviderv1.AWSMachineProviderConfig { +func stubInvalidInstanceTenancy() *machinev1.AWSMachineProviderConfig { pc := stubProviderConfig() pc.Placement.Tenancy = "invalid" return pc diff --git a/pkg/actuators/machine/utils.go b/pkg/actuators/machine/utils.go index 6bf13a0d70..5b374cbe0f 100644 --- a/pkg/actuators/machine/utils.go +++ b/pkg/actuators/machine/utils.go @@ -17,19 +17,19 @@ limitations under the License. package machine import ( + "encoding/json" "fmt" "net" "strings" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" machinecontroller "github.com/openshift/machine-api-operator/pkg/controller/machine" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" ) @@ -257,7 +257,7 @@ func terminateInstances(client awsclient.Client, instances []*ec2.Instance) ([]* // a condition will be added to the slice // If the machine does already have a condition with the specified type, // the condition will be updated if either of the following are true. -func setAWSMachineProviderCondition(condition awsproviderv1.AWSMachineProviderCondition, conditions []awsproviderv1.AWSMachineProviderCondition) []awsproviderv1.AWSMachineProviderCondition { +func setAWSMachineProviderCondition(condition machinev1.AWSMachineProviderCondition, conditions []machinev1.AWSMachineProviderCondition) []machinev1.AWSMachineProviderCondition { now := metav1.Now() if existingCondition := findProviderCondition(conditions, condition.Type); existingCondition == nil { @@ -271,7 +271,7 @@ func setAWSMachineProviderCondition(condition awsproviderv1.AWSMachineProviderCo return conditions } -func findProviderCondition(conditions []awsproviderv1.AWSMachineProviderCondition, conditionType awsproviderv1.AWSMachineProviderConditionType) *awsproviderv1.AWSMachineProviderCondition { +func findProviderCondition(conditions []machinev1.AWSMachineProviderCondition, conditionType machinev1.ConditionType) *machinev1.AWSMachineProviderCondition { for i := range conditions { if conditions[i].Type == conditionType { return &conditions[i] @@ -280,7 +280,7 @@ func findProviderCondition(conditions []awsproviderv1.AWSMachineProviderConditio return nil } -func updateExistingCondition(newCondition, existingCondition *awsproviderv1.AWSMachineProviderCondition) { +func updateExistingCondition(newCondition, existingCondition *machinev1.AWSMachineProviderCondition) { if !shouldUpdateCondition(newCondition, existingCondition) { return } @@ -294,7 +294,7 @@ func updateExistingCondition(newCondition, existingCondition *awsproviderv1.AWSM existingCondition.LastProbeTime = newCondition.LastProbeTime } -func shouldUpdateCondition(newCondition, existingCondition *awsproviderv1.AWSMachineProviderCondition) bool { +func shouldUpdateCondition(newCondition, existingCondition *machinev1.AWSMachineProviderCondition) bool { return newCondition.Reason != existingCondition.Reason || newCondition.Message != existingCondition.Message } @@ -370,20 +370,20 @@ func extractNodeAddresses(instance *ec2.Instance, domainNames []string) ([]corev return addresses, nil } -func conditionSuccess() awsproviderv1.AWSMachineProviderCondition { - return awsproviderv1.AWSMachineProviderCondition{ - Type: awsproviderv1.MachineCreation, +func conditionSuccess() machinev1.AWSMachineProviderCondition { + return machinev1.AWSMachineProviderCondition{ + Type: machinev1.MachineCreation, Status: corev1.ConditionTrue, - Reason: awsproviderv1.MachineCreationSucceeded, + Reason: machinev1.MachineCreationSucceededConditionReason, Message: "Machine successfully created", } } -func conditionFailed() awsproviderv1.AWSMachineProviderCondition { - return awsproviderv1.AWSMachineProviderCondition{ - Type: awsproviderv1.MachineCreation, +func conditionFailed() machinev1.AWSMachineProviderCondition { + return machinev1.AWSMachineProviderCondition{ + Type: machinev1.MachineCreation, Status: corev1.ConditionFalse, - Reason: awsproviderv1.MachineCreationFailed, + Reason: machinev1.MachineCreationFailedConditionReason, } } @@ -406,3 +406,67 @@ func getClusterID(machine *machinev1.Machine) (string, bool) { } return clusterID, ok } + +// RawExtensionFromProviderSpec marshals the machine provider spec. +func RawExtensionFromProviderSpec(spec *machinev1.AWSMachineProviderConfig) (*runtime.RawExtension, error) { + if spec == nil { + return &runtime.RawExtension{}, nil + } + + var rawBytes []byte + var err error + if rawBytes, err = json.Marshal(spec); err != nil { + return nil, fmt.Errorf("error marshalling providerSpec: %v", err) + } + + return &runtime.RawExtension{ + Raw: rawBytes, + }, nil +} + +// RawExtensionFromProviderStatus marshals the machine provider status +func RawExtensionFromProviderStatus(status *machinev1.AWSMachineProviderStatus) (*runtime.RawExtension, error) { + if status == nil { + return &runtime.RawExtension{}, nil + } + + var rawBytes []byte + var err error + if rawBytes, err = json.Marshal(status); err != nil { + return nil, fmt.Errorf("error marshalling providerStatus: %v", err) + } + + return &runtime.RawExtension{ + Raw: rawBytes, + }, nil +} + +// ProviderSpecFromRawExtension unmarshals a raw extension into an AWSMachineProviderSpec type +func ProviderSpecFromRawExtension(rawExtension *runtime.RawExtension) (*machinev1.AWSMachineProviderConfig, error) { + if rawExtension == nil { + return &machinev1.AWSMachineProviderConfig{}, nil + } + + spec := new(machinev1.AWSMachineProviderConfig) + if err := json.Unmarshal(rawExtension.Raw, &spec); err != nil { + return nil, fmt.Errorf("error unmarshalling providerSpec: %v", err) + } + + klog.V(5).Infof("Got provider Spec from raw extension: %+v", spec) + return spec, nil +} + +// ProviderStatusFromRawExtension unmarshals a raw extension into an AWSMachineProviderStatus type +func ProviderStatusFromRawExtension(rawExtension *runtime.RawExtension) (*machinev1.AWSMachineProviderStatus, error) { + if rawExtension == nil { + return &machinev1.AWSMachineProviderStatus{}, nil + } + + providerStatus := new(machinev1.AWSMachineProviderStatus) + if err := json.Unmarshal(rawExtension.Raw, providerStatus); err != nil { + return nil, fmt.Errorf("error unmarshalling providerStatus: %v", err) + } + + klog.V(5).Infof("Got provider Status from raw extension: %+v", providerStatus) + return providerStatus, nil +} diff --git a/pkg/actuators/machine/utils_test.go b/pkg/actuators/machine/utils_test.go index 137b1c1a0b..892dc5f83b 100644 --- a/pkg/actuators/machine/utils_test.go +++ b/pkg/actuators/machine/utils_test.go @@ -5,7 +5,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/equality" "k8s.io/client-go/kubernetes/scheme" diff --git a/pkg/actuators/machineset/controller.go b/pkg/actuators/machineset/controller.go index c7b973cc09..a483106eee 100644 --- a/pkg/actuators/machineset/controller.go +++ b/pkg/actuators/machineset/controller.go @@ -6,14 +6,14 @@ import ( "strconv" "github.com/go-logr/logr" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" mapierrors "github.com/openshift/machine-api-operator/pkg/controller/machine" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/record" "k8s.io/klog/v2" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" + utils "sigs.k8s.io/cluster-api-provider-aws/pkg/actuators/machine" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -106,7 +106,7 @@ func isInvalidConfigurationError(err error) bool { } func (r *Reconciler) reconcile(machineSet *machinev1.MachineSet) (ctrl.Result, error) { - providerConfig, err := awsproviderv1.ProviderSpecFromRawExtension(machineSet.Spec.Template.Spec.ProviderSpec.Value) + providerConfig, err := utils.ProviderSpecFromRawExtension(machineSet.Spec.Template.Spec.ProviderSpec.Value) if err != nil { return ctrl.Result{}, mapierrors.InvalidMachineConfiguration("failed to get providerConfig: %v", err) } diff --git a/pkg/actuators/machineset/controller_suite_test.go b/pkg/actuators/machineset/controller_suite_test.go index 709b427b20..985d1246e4 100644 --- a/pkg/actuators/machineset/controller_suite_test.go +++ b/pkg/actuators/machineset/controller_suite_test.go @@ -21,7 +21,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/envtest" diff --git a/pkg/actuators/machineset/controller_test.go b/pkg/actuators/machineset/controller_test.go index 715b11b48f..9f926840c3 100644 --- a/pkg/actuators/machineset/controller_test.go +++ b/pkg/actuators/machineset/controller_test.go @@ -23,12 +23,11 @@ import ( . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" gtypes "github.com/onsi/gomega/types" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/record" - awsproviderv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/log" @@ -284,7 +283,7 @@ func newTestMachineSet(namespace string, instanceType string, existingAnnotation annotations[k] = v } - machineProviderSpec := &awsproviderv1.AWSMachineProviderConfig{ + machineProviderSpec := &machinev1.AWSMachineProviderConfig{ InstanceType: instanceType, } providerSpec, err := providerSpecFromMachine(machineProviderSpec) @@ -308,7 +307,7 @@ func newTestMachineSet(namespace string, instanceType string, existingAnnotation }, nil } -func providerSpecFromMachine(in *awsproviderv1.AWSMachineProviderConfig) (machinev1.ProviderSpec, error) { +func providerSpecFromMachine(in *machinev1.AWSMachineProviderConfig) (machinev1.ProviderSpec, error) { bytes, err := json.Marshal(in) if err != nil { return machinev1.ProviderSpec{}, err diff --git a/pkg/termination/termination_suite_test.go b/pkg/termination/termination_suite_test.go index 12d16e4ccd..c8817a7512 100644 --- a/pkg/termination/termination_suite_test.go +++ b/pkg/termination/termination_suite_test.go @@ -21,7 +21,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" + machinev1 "github.com/openshift/api/machine/v1beta1" "k8s.io/apimachinery/pkg/runtime" kubernetesscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest"