Skip to content

Commit

Permalink
Provide SecurityGroups attachment to machines
Browse files Browse the repository at this point in the history
Automatically attach created security groups
to machines if managedSecurityGroups is true
in clusterSpec.providerSpec.

Change-Id: I634c0d7a461874c5cb164cd19792885ccfc95770
  • Loading branch information
eromanova committed Mar 13, 2019
1 parent db79633 commit 6c68d05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
46 changes: 37 additions & 9 deletions pkg/cloud/openstack/clients/machineservice.go
Expand Up @@ -314,7 +314,35 @@ func isDuplicate(list []string, name string) bool {
return false
}

func GetSecurityGroups(is *InstanceService, sg_param []openstackconfigv1.SecurityGroupParam) ([]string, error) {
func GetSecurityGroups(is *InstanceService, sg_param []openstackconfigv1.SecurityGroupParam, cluster *clusterv1.Cluster, machine *clusterv1.Machine) ([]string, error) {
clusterName := fmt.Sprintf("%s/%s", cluster.Namespace, cluster.Name)
secGroupService, err := NewSecGroupService(is.networkClient)
if err != nil {
return nil, fmt.Errorf("failed to create security group service: %v", err)
}

clusterStatus, err := openstackconfigv1.ClusterStatusFromProviderStatus(cluster.Status.ProviderStatus)
if err != nil {
return nil, fmt.Errorf("failed to load cluster provider status: %v", err)
}

clusterSpec, err := openstackconfigv1.ClusterSpecFromProviderSpec(cluster.Spec.ProviderSpec)
if err != nil {
return nil, fmt.Errorf("failed to load cluster provider spec: %v", err)
}

err = secGroupService.Reconcile(clusterName, *clusterSpec, clusterStatus)
if err != nil {
return nil, fmt.Errorf("failed to reconcile security groups: %v", err)
}

if clusterSpec.ManagedSecurityGroups {
if util.IsControlPlaneMachine(machine) {
return []string{clusterStatus.ControlPlaneSecurityGroup.ID, clusterStatus.GlobalSecurityGroup.ID}, nil
}
return []string{clusterStatus.GlobalSecurityGroup.ID}, nil
}

var sgIDs []string
for _, sg := range sg_param {
listOpts := groups.ListOpts(sg.Filter)
Expand All @@ -340,7 +368,7 @@ func GetSecurityGroups(is *InstanceService, sg_param []openstackconfigv1.Securit
return sgIDs, nil
}

func (is *InstanceService) InstanceCreate(clusterName string, name string, config *openstackconfigv1.OpenstackProviderSpec, cmd string, keyName string) (instance *Instance, err error) {
func (is *InstanceService) InstanceCreate(cluster *clusterv1.Cluster, machine *clusterv1.Machine, config *openstackconfigv1.OpenstackProviderSpec, cmd string, keyName string) (instance *Instance, err error) {
if config == nil {
return nil, fmt.Errorf("create Options need be specified to create instace")
}
Expand All @@ -355,10 +383,10 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi
}
clusterTags := []string{
"cluster-api-provider-openstack",
clusterName,
fmt.Sprintf("%s/%s", cluster.Namespace, cluster.Name),
}
// Get security groups
securityGroups, err := GetSecurityGroups(is, config.SecurityGroups)
securityGroups, err := GetSecurityGroups(is, config.SecurityGroups, cluster, machine)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -406,7 +434,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi
return nil, fmt.Errorf("No network was found or provided. Please check your machine configuration and try again")
}
allPages, err := ports.List(is.networkClient, ports.ListOpts{
Name: name,
Name: machine.Name,
NetworkID: net.networkID,
}).AllPages()
if err != nil {
Expand All @@ -419,7 +447,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi
var port ports.Port
if len(portList) == 0 {
// create server port
port, err = CreatePort(is, name, net, &securityGroups)
port, err = CreatePort(is, machine.Name, net, &securityGroups)
if err != nil {
return nil, fmt.Errorf("Failed to create port err: %v", err)
}
Expand All @@ -438,7 +466,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi

if config.Trunk == true {
allPages, err := trunks.List(is.networkClient, trunks.ListOpts{
Name: name,
Name: machine.Name,
PortID: port.ID,
}).AllPages()
if err != nil {
Expand All @@ -452,7 +480,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi
if len(trunkList) == 0 {
// create trunk with the previous port as parent
trunkCreateOpts := trunks.CreateOpts{
Name: name,
Name: machine.Name,
PortID: port.ID,
}
newTrunk, err := trunks.Create(is.networkClient, trunkCreateOpts).Extract()
Expand All @@ -473,7 +501,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi
}

serverCreateOpts := servers.CreateOpts{
Name: name,
Name: machine.Name,
ImageName: config.Image,
FlavorName: config.Flavor,
AvailabilityZone: config.AvailabilityZone,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/openstack/machine/actuator.go
Expand Up @@ -145,7 +145,7 @@ func (oc *OpenstackClient) Create(ctx context.Context, cluster *clusterv1.Cluste
userDataRendered = string(userData)
}

instance, err = machineService.InstanceCreate(fmt.Sprintf("%s/%s", cluster.ObjectMeta.Namespace, cluster.Name), machine.Name, providerSpec, userDataRendered, providerSpec.KeyName)
instance, err = machineService.InstanceCreate(cluster, machine, providerSpec, userDataRendered, providerSpec.KeyName)
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"error creating Openstack instance: %v", err))
Expand Down

0 comments on commit 6c68d05

Please sign in to comment.