github.com/openshift/installer@v1.4.17/pkg/infrastructure/openstack/preprovision/servergroups.go (about) 1 package preprovision 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servergroups" 8 "github.com/gophercloud/gophercloud/v2/pagination" 9 "github.com/sirupsen/logrus" 10 capo "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" 11 12 mapov1alpha1 "github.com/openshift/api/machine/v1alpha1" 13 "github.com/openshift/installer/pkg/asset/installconfig" 14 tfvars "github.com/openshift/installer/pkg/tfvars/openstack" 15 "github.com/openshift/installer/pkg/types/openstack" 16 "github.com/openshift/installer/pkg/types/openstack/defaults" 17 ) 18 19 // ServerGroups creates server groups referenced by name in the Machine 20 // manifests if they don't exist already. The newly created server groups have 21 // the policy defined in the install-config's machine-pools. 22 func ServerGroups(ctx context.Context, installConfig *installconfig.InstallConfig, capiMachines []capo.OpenStackMachine, mapoWorkerProviderSpecs []mapov1alpha1.OpenstackProviderSpec) error { 23 logrus.Debugf("Creating the server groups") 24 computeClient, err := defaults.NewServiceClient(ctx, "compute", defaults.DefaultClientOpts(installConfig.Config.Platform.OpenStack.Cloud)) 25 if err != nil { 26 return fmt.Errorf("failed to build an OpenStack client: %w", err) 27 } 28 computeClient.Microversion = "2.64" 29 30 var masterPolicy, workerPolicy openstack.ServerGroupPolicy 31 { 32 if masterMP := installConfig.Config.ControlPlane; masterMP != nil { 33 masterPolicy = tfvars.GetServerGroupPolicy(masterMP.Platform.OpenStack, installConfig.Config.OpenStack.DefaultMachinePlatform) 34 } else { 35 masterPolicy = tfvars.GetServerGroupPolicy(nil, installConfig.Config.OpenStack.DefaultMachinePlatform) 36 } 37 38 if workerMP := installConfig.Config.WorkerMachinePool(); workerMP != nil { 39 workerPolicy = tfvars.GetServerGroupPolicy(workerMP.Platform.OpenStack, installConfig.Config.OpenStack.DefaultMachinePlatform) 40 } else { 41 workerPolicy = tfvars.GetServerGroupPolicy(nil, installConfig.Config.OpenStack.DefaultMachinePlatform) 42 } 43 } 44 45 // serverGroups is the set of server groups to be created. 46 serverGroups := make(map[string]openstack.ServerGroupPolicy, len(capiMachines)+len(mapoWorkerProviderSpecs)) 47 for _, machine := range capiMachines { 48 if _, ok := machine.Labels["cluster.x-k8s.io/control-plane"]; !ok { 49 logrus.Debugf("Found unexpected machine %q among the CAPI Machine manifests", machine.GetName()) 50 continue 51 } 52 if sgParam := machine.Spec.ServerGroup; sgParam != nil && sgParam.Filter != nil && sgParam.Filter.Name != nil { 53 serverGroupName := *sgParam.Filter.Name 54 if visitedPolicy, ok := serverGroups[serverGroupName]; ok { 55 if masterPolicy != visitedPolicy { 56 return fmt.Errorf("server group %q is referenced with different policies in the install-config machine-pools", serverGroupName) 57 } 58 continue 59 } 60 serverGroups[serverGroupName] = masterPolicy 61 } 62 } 63 for _, providerSpec := range mapoWorkerProviderSpecs { 64 if serverGroupName := providerSpec.ServerGroupName; serverGroupName != "" { 65 if visitedPolicy, ok := serverGroups[serverGroupName]; ok { 66 if workerPolicy != visitedPolicy { 67 return fmt.Errorf("server group %q is referenced with different policies in the install-config machine-pools", serverGroupName) 68 } 69 continue 70 } 71 serverGroups[serverGroupName] = workerPolicy 72 } 73 } 74 75 // Remove existing server groups from the list of resources to be created. 76 if err = servergroups.List(computeClient, nil).EachPage(ctx, func(_ context.Context, p pagination.Page) (bool, error) { 77 sgs, err := servergroups.ExtractServerGroups(p) 78 if err != nil { 79 return false, err 80 } 81 82 for i := range sgs { 83 delete(serverGroups, sgs[i].Name) 84 } 85 return true, nil 86 }); err != nil { 87 return fmt.Errorf("failed to list server groups: %w", err) 88 } 89 90 // Create the server groups referenced by name in the Machine manifests, that don't exist already. 91 for name, policy := range serverGroups { 92 logrus.Debugf("Creating server group %q with policy %q", name, policy) 93 if _, err := servergroups.Create(ctx, computeClient, servergroups.CreateOpts{ 94 Name: name, 95 Policy: string(policy), 96 }).Extract(); err != nil { 97 return fmt.Errorf("failed to create server group %q: %w", name, err) 98 } 99 } 100 return nil 101 }