Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions config/master-machine-with-tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ spec:
regionId: FILLIN
zoneId: FILLIN
securityGroups:
- tags:
- key: Name
value: ocp-test-sg
- key: "kubernetes.io/cluster/abc-def"
value: owned
- key: OCP
value: ISVAlibaba
- type: Tags
tags:
- key: Name
value: ocp-test-sg
- key: "kubernetes.io/cluster/abc-def"
value: owned
- key: OCP
value: ISVAlibaba
vpcId: FILLIN
vSwitch:
type: Tags
tags:
- key: Name
value: ocp-test-vswitch
- key: "kubernetes.io/cluster/abc-def"
value: owned
- key: OCP
value: ISVAlibaba
- key: Name
value: ocp-test-vswitch
- key: "kubernetes.io/cluster/abc-def"
value: owned
- key: OCP
value: ISVAlibaba
systemDiskCategory: FILLIN
systemDiskSize: FILLIN
internetMaxBandwidthOut: FILLIN
Expand Down
4 changes: 3 additions & 1 deletion config/worker-machine-with-tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ spec:
regionId: FILLIN
zoneId: FILLIN
securityGroups:
- tags:
- type: Tags
tags:
- key: Name
value: ocp-test-sg
- key: "kubernetes.io/cluster/abc-def"
Expand All @@ -28,6 +29,7 @@ spec:
value: ISVAlibaba
vpcId: FILLIN
vSwitch:
type: Tags
tags:
- key: Name
value: ocp-test-vswitch
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-logr/logr v1.2.2
github.com/golang/mock v1.6.0
github.com/onsi/gomega v1.17.0
github.com/openshift/api v0.0.0-20211222145011-3bf13cf5081a
github.com/openshift/api v0.0.0-20220124143425-d74727069f6f
github.com/openshift/machine-api-operator v0.2.1-0.20220119165902-b88b83bb15f9
github.com/stretchr/testify v1.7.0
k8s.io/api v0.23.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,9 @@ github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQ
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/openshift/api v0.0.0-20211209135129-c58d9f695577/go.mod h1:DoslCwtqUpr3d/gsbq4ZlkaMEdYqKxuypsDjorcHhME=
github.com/openshift/api v0.0.0-20211222145011-3bf13cf5081a h1:lJQ6mKlpoUn19dmaLY/ldRm2Mwi+WtTJ2MD6bteLa4o=
github.com/openshift/api v0.0.0-20211222145011-3bf13cf5081a/go.mod h1:F/eU6jgr6Q2VhMu1mSpMmygxAELd7+BUxs3NHZ25jV4=
github.com/openshift/api v0.0.0-20220124143425-d74727069f6f h1:iOTv1WudhVm2UsoST+L+ZrA5A9w57h9vmQsdlBuqG6g=
github.com/openshift/api v0.0.0-20220124143425-d74727069f6f/go.mod h1:F/eU6jgr6Q2VhMu1mSpMmygxAELd7+BUxs3NHZ25jV4=
github.com/openshift/build-machinery-go v0.0.0-20210712174854-1bb7fd1518d3/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE=
github.com/openshift/build-machinery-go v0.0.0-20211213093930-7e33a7eb4ce3/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE=
github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3 h1:SG1aqwleU6bGD0X4mhkTNupjVnByMYYuW4XbnCPavQU=
Expand Down
88 changes: 62 additions & 26 deletions pkg/actuators/machine/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ func runInstances(machine *machinev1beta1.Machine, machineProviderConfig *machin
runInstancesRequest.RegionId = machineProviderConfig.RegionID

// ResourceGroupID
if machineProviderConfig.ResourceGroupID != "" {
runInstancesRequest.ResourceGroupId = machineProviderConfig.ResourceGroupID
if groupId, err := getResourceGroupId(machineProviderConfig); err != nil {
klog.Errorf("Unable to determine resource group ID for machine %q, err %q", machine.Name, err)
return nil, mapierrors.InvalidMachineConfiguration("Unable to determine resource group ID for machine: %q", machine.Name)
} else {
runInstancesRequest.ResourceGroupId = groupId
}

// SecurityGroupIDs
Expand Down Expand Up @@ -367,16 +370,21 @@ func getSecurityGroupIDs(machine runtimeclient.ObjectKey, machineProviderConfig
}

for _, sg := range machineProviderConfig.SecurityGroups {
if sg.ID != "" {
securityGroupIDs = append(securityGroupIDs, sg.ID)
} else {
if sg.Tags != nil {
ids, err := getSecurityGroupIDByTags(machine, machineProviderConfig, sg.Tags, client)
if err != nil {
return nil, err
}
securityGroupIDs = append(securityGroupIDs, ids...)
switch sg.Type {
case machinev1.AlibabaResourceReferenceTypeID:
if sg.ID != nil && *sg.ID != "" {
securityGroupIDs = append(securityGroupIDs, *sg.ID)
} else {
return nil, mapierrors.InvalidMachineConfiguration("No security group ID provided")
}
case machinev1.AlibabaResourceReferenceTypeTags:
ids, err := getSecurityGroupIDByTags(machine, machineProviderConfig, sg.Tags, client)
if err != nil {
return nil, err
}
securityGroupIDs = append(securityGroupIDs, ids...)
default:
return nil, mapierrors.InvalidMachineConfiguration("Unknown security group resource reference type: %s", sg.Type)
}
}
if len(securityGroupIDs) == 0 {
Expand All @@ -385,12 +393,20 @@ func getSecurityGroupIDs(machine runtimeclient.ObjectKey, machineProviderConfig
return &securityGroupIDs, nil
}

func getSecurityGroupIDByTags(machine runtimeclient.ObjectKey, machineProviderConfig *machinev1.AlibabaCloudMachineProviderConfig, tags []machinev1.Tag, client alibabacloudClient.Client) ([]string, error) {
func getSecurityGroupIDByTags(machine runtimeclient.ObjectKey, machineProviderConfig *machinev1.AlibabaCloudMachineProviderConfig, tags *[]machinev1.Tag, client alibabacloudClient.Client) ([]string, error) {
if tags == nil {
return nil, mapierrors.InvalidMachineConfiguration("No tags provided for security group ID search for machine: %q", machine.Name)
}
request := ecs.CreateDescribeSecurityGroupsRequest()
request.VpcId = machineProviderConfig.VpcID
request.ResourceGroupId = machineProviderConfig.ResourceGroupID
if groupId, err := getResourceGroupId(machineProviderConfig); err != nil {
klog.Errorf("Unable to determine resource group ID for machine %q, err %q", machine.Name, err)
return nil, mapierrors.InvalidMachineConfiguration("Unable to determine resource group ID for machine: %q", machine.Name)
} else {
request.ResourceGroupId = groupId
}
request.RegionId = machineProviderConfig.RegionID
request.Tag = buildDescribeSecurityGroupsTag(tags)
request.Tag = buildDescribeSecurityGroupsTag(*tags)
request.Scheme = "https"

response, err := client.DescribeSecurityGroups(request)
Expand Down Expand Up @@ -440,28 +456,30 @@ func buildDescribeSecurityGroupsTag(tags []machinev1.Tag) *[]ecs.DescribeSecurit

func getVSwitchID(machine runtimeclient.ObjectKey, machineProviderConfig *machinev1.AlibabaCloudMachineProviderConfig, client alibabacloudClient.Client) (string, error) {
klog.Infof("validate vswitch in region %s", machineProviderConfig.RegionID)
if machineProviderConfig.VSwitch.ID == "" && len(machineProviderConfig.VSwitch.Tags) == 0 {
return "", errors.New("no vswitch configuration provided")
}

if machineProviderConfig.VSwitch.ID != "" {
return machineProviderConfig.VSwitch.ID, nil
}

if machineProviderConfig.VSwitch.Tags != nil {
switch machineProviderConfig.VSwitch.Type {
case machinev1.AlibabaResourceReferenceTypeID:
if machineProviderConfig.VSwitch.ID != nil && *machineProviderConfig.VSwitch.ID != "" {
return *machineProviderConfig.VSwitch.ID, nil
} else {
return "", mapierrors.InvalidMachineConfiguration("No vswitch resource id provided")
}
case machinev1.AlibabaResourceReferenceTypeTags:
return getVSwitchIDFromTags(machine, machineProviderConfig, client)
default:
return "", mapierrors.InvalidMachineConfiguration("Unknown vswitch resource reference type: %s", machineProviderConfig.VSwitch.Type)
}

return "", fmt.Errorf("no vSwitch found from configuration")
}

func getVSwitchIDFromTags(machine runtimeclient.ObjectKey, mpc *machinev1.AlibabaCloudMachineProviderConfig, client alibabacloudClient.Client) (string, error) {
if mpc.VSwitch.Tags == nil {
return "", mapierrors.InvalidMachineConfiguration("No tags provided for VSwitch ID search for machine: %q", machine.Name)
}
// Build a request to fetch the vSwitchID from the tags provided
describeVSwitchesRequest := vpc.CreateDescribeVSwitchesRequest()
describeVSwitchesRequest.Scheme = "https"
describeVSwitchesRequest.RegionId = mpc.RegionID
describeVSwitchesRequest.VpcId = mpc.VpcID
describeVSwitchesRequest.Tag = buildDescribeVSwitchesTag(mpc.VSwitch.Tags)
describeVSwitchesRequest.Tag = buildDescribeVSwitchesTag(*mpc.VSwitch.Tags)
describeVSwitchesResponse, err := client.DescribeVSwitches(describeVSwitchesRequest)
if err != nil {
metrics.RegisterFailedInstanceCreate(&metrics.MachineLabels{
Expand Down Expand Up @@ -801,3 +819,21 @@ func correctExistingTags(machine *machinev1beta1.Machine, regionID string, insta

return nil
}

// getResourceGroupId takes an AlibabaCloudMachineProviderConfig and will return the
// resource group id if available, or determine the group id by using the search tags.
// An error will be returned if no group id can be found, or if multiple groups are
// found from the search tags.
func getResourceGroupId(machineProviderConfig *machinev1.AlibabaCloudMachineProviderConfig) (string, error) {
switch machineProviderConfig.ResourceGroup.Type {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when resource group is nil? Is this allowed? IIRC someone said resource group was an optional field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like we fail if the resource group is nil currently, the way the code is structured i'm not sure how it's supposed to find the security groups without the group id. but maybe that's ok?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i asked around a little bit, and we should not allow creation of resources in the default group. the user should always specify the resource group.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a comment about most customers using the default group, are we sure this is correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i worded that poorly. we definitely should allow creation in the default group, but we should force the user to always specify the resource group so that there are no surprises when creating new machines.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me that resource group will never be nil in a provider config https://github.com/openshift/api/blob/master/machine/v1/types_alibabaprovider.go#L140 it's not a pointer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, we agreed that the user should always provide this information, even in the case where they want to use the default resource group. this will help ensure that the user isn't creating resources in the wrong group by accident.

case machinev1.AlibabaResourceReferenceTypeID:
if machineProviderConfig.ResourceGroup.ID != nil && *machineProviderConfig.ResourceGroup.ID != "" {
return *machineProviderConfig.ResourceGroup.ID, nil
} else {
return "", mapierrors.InvalidMachineConfiguration("No resource group ID provided")
}
// TODO add name search lookup case here
default:
return "", mapierrors.InvalidMachineConfiguration("unknown resource group reference type: %s", machineProviderConfig.ResourceGroup.Type)
}
}
13 changes: 10 additions & 3 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ func stubAlibabaCloudCredentialsSecret() *corev1.Secret {
}

func stubProviderConfig() *machinev1.AlibabaCloudMachineProviderConfig {
vSwitchID := stubVSwitchID
return &machinev1.AlibabaCloudMachineProviderConfig{
InstanceType: stubInstanceType,
ImageID: stubImageID,
RegionID: stubRegionID,
ZoneID: stubZoneID,
SecurityGroups: []machinev1.AlibabaResourceReference{
{ID: stubVSwitchID},
{
Type: machinev1.AlibabaResourceReferenceTypeID,
ID: &vSwitchID,
},
},
VpcID: stubVpcID,
VSwitch: machinev1.AlibabaResourceReference{
Type: machinev1.AlibabaResourceReferenceTypeID,
ID: &vSwitchID,
},
VpcID: stubVpcID,
VSwitch: machinev1.AlibabaResourceReference{ID: stubVSwitchID},
SystemDisk: machinev1.SystemDiskProperties{
Category: stubSystemDiskCategory,
Size: stubSystemDiskSize,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading