From e0151ed37813d2728e3e34af44cce81a0017e649 Mon Sep 17 00:00:00 2001 From: Richard Vanderpool <49568690+rvanderp3@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:08:43 -0500 Subject: [PATCH] unrevert PR 7418 --- pkg/asset/machines/master.go | 7 +- pkg/asset/machines/vsphere/machines.go | 98 ++++++++++++++++--- pkg/asset/machines/vsphere/machines_test.go | 4 +- pkg/asset/machines/worker.go | 2 +- pkg/asset/manifests/infrastructure.go | 3 +- pkg/asset/manifests/vsphere/infrastructure.go | 10 +- 6 files changed, 106 insertions(+), 18 deletions(-) diff --git a/pkg/asset/machines/master.go b/pkg/asset/machines/master.go index 63b54089e3..dce03fa8c2 100644 --- a/pkg/asset/machines/master.go +++ b/pkg/asset/machines/master.go @@ -473,10 +473,15 @@ func (m *Master) Generate(dependencies asset.Parents) error { pool.Platform.VSphere = &mpool templateName := clusterID.InfraID + "-rhcos" - machines, err = vsphere.Machines(clusterID.InfraID, ic, &pool, templateName, "master", masterUserDataSecretName) + machines, controlPlaneMachineSet, err = vsphere.Machines(clusterID.InfraID, ic, &pool, templateName, "master", masterUserDataSecretName) if err != nil { return errors.Wrap(err, "failed to create master machine objects") } + + if ic.FeatureSet != configv1.TechPreviewNoUpgrade { + controlPlaneMachineSet = nil + } + vsphere.ConfigMasters(machines, clusterID.InfraID) case powervstypes.Name: mpool := defaultPowerVSMachinePoolPlatform() diff --git a/pkg/asset/machines/vsphere/machines.go b/pkg/asset/machines/vsphere/machines.go index 4827944ff0..e968f6329f 100644 --- a/pkg/asset/machines/vsphere/machines.go +++ b/pkg/asset/machines/vsphere/machines.go @@ -10,35 +10,37 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + v1 "github.com/openshift/api/config/v1" + machinev1 "github.com/openshift/api/machine/v1" machineapi "github.com/openshift/api/machine/v1beta1" "github.com/openshift/installer/pkg/types" "github.com/openshift/installer/pkg/types/vsphere" ) // Machines returns a list of machines for a machinepool. -func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]machineapi.Machine, error) { +func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]machineapi.Machine, *machinev1.ControlPlaneMachineSet, error) { if configPlatform := config.Platform.Name(); configPlatform != vsphere.Name { - return nil, fmt.Errorf("non vsphere configuration: %q", configPlatform) + return nil, nil, fmt.Errorf("non vsphere configuration: %q", configPlatform) } if poolPlatform := pool.Platform.Name(); poolPlatform != vsphere.Name { - return nil, fmt.Errorf("non-VSphere machine-pool: %q", poolPlatform) + return nil, nil, fmt.Errorf("non-VSphere machine-pool: %q", poolPlatform) } var failureDomain vsphere.FailureDomain var machines []machineapi.Machine platform := config.Platform.VSphere mpool := pool.Platform.VSphere - replicas := int64(1) + replicas := int32(1) numOfZones := len(mpool.Zones) zones, err := getDefinedZonesFromTopology(platform) if err != nil { - return machines, err + return machines, nil, err } if pool.Replicas != nil { - replicas = *pool.Replicas + replicas = int32(*pool.Replicas) } // Create hosts to populate from. Copying so we can remove without changing original @@ -53,7 +55,11 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine } } - for idx := int64(0); idx < replicas; idx++ { + failureDomains := []machinev1.VSphereFailureDomain{} + + vsphereMachineProvider := &machineapi.VSphereMachineProviderSpec{} + + for idx := int32(0); idx < replicas; idx++ { logrus.Debugf("Creating %v machine %v", role, idx) var host *vsphere.Host desiredZone := mpool.Zones[int(idx)%numOfZones] @@ -66,7 +72,7 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine logrus.Debugf("Desired zone: %v", desiredZone) if _, exists := zones[desiredZone]; !exists { - return nil, errors.Errorf("zone [%s] specified by machinepool is not defined", desiredZone) + return nil, nil, errors.Errorf("zone [%s] specified by machinepool is not defined", desiredZone) } failureDomain = zones[desiredZone] @@ -77,6 +83,10 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine "machine.openshift.io/cluster-api-machine-type": role, } + failureDomains = append(failureDomains, machinev1.VSphereFailureDomain{ + Name: failureDomain.Name, + }) + osImageForZone := failureDomain.Topology.Template if failureDomain.Topology.Template == "" { osImageForZone = fmt.Sprintf("%s-%s-%s", osImage, failureDomain.Region, failureDomain.Zone) @@ -84,11 +94,11 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine vcenter, err := getVCenterFromServerName(failureDomain.Server, platform) if err != nil { - return nil, errors.Wrap(err, "unable to find vCenter in failure domains") + return nil, nil, errors.Wrap(err, "unable to find vCenter in failure domains") } provider, err := provider(clusterID, vcenter, failureDomain, mpool, osImageForZone, userDataSecret) if err != nil { - return nil, errors.Wrap(err, "failed to create provider") + return nil, nil, errors.Wrap(err, "failed to create provider") } // Apply static IP if configured @@ -112,8 +122,74 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine }, } machines = append(machines, machine) + + vsphereMachineProvider = provider.DeepCopy() } - return machines, nil + + // when multiple zones are defined, network and workspace are derived from the topology + origProv := vsphereMachineProvider.DeepCopy() + if len(failureDomains) > 1 { + vsphereMachineProvider.Network = machineapi.NetworkSpec{} + vsphereMachineProvider.Workspace = &machineapi.Workspace{} + vsphereMachineProvider.Template = "" + } + + if len(hosts) > 0 { + vsphereMachineProvider.Network.Devices = []machineapi.NetworkDeviceSpec{ + { + AddressesFromPools: origProv.Network.Devices[0].AddressesFromPools, + Nameservers: origProv.Network.Devices[0].Nameservers, + }, + } + } + + controlPlaneMachineSet := &machinev1.ControlPlaneMachineSet{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "machine.openshift.io/v1", + Kind: "ControlPlaneMachineSet", + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: "openshift-machine-api", + Name: "cluster", + Labels: map[string]string{ + "machine.openshift.io/cluster-api-cluster": clusterID, + }, + }, + Spec: machinev1.ControlPlaneMachineSetSpec{ + Replicas: &replicas, + State: machinev1.ControlPlaneMachineSetStateActive, + Selector: metav1.LabelSelector{ + MatchLabels: map[string]string{ + "machine.openshift.io/cluster-api-machine-role": role, + "machine.openshift.io/cluster-api-machine-type": role, + "machine.openshift.io/cluster-api-cluster": clusterID, + }, + }, + Template: machinev1.ControlPlaneMachineSetTemplate{ + MachineType: machinev1.OpenShiftMachineV1Beta1MachineType, + OpenShiftMachineV1Beta1Machine: &machinev1.OpenShiftMachineV1Beta1MachineTemplate{ + FailureDomains: &machinev1.FailureDomains{ + Platform: v1.VSpherePlatformType, + VSphere: failureDomains, + }, + ObjectMeta: machinev1.ControlPlaneMachineSetTemplateObjectMeta{ + Labels: map[string]string{ + "machine.openshift.io/cluster-api-cluster": clusterID, + "machine.openshift.io/cluster-api-machine-role": role, + "machine.openshift.io/cluster-api-machine-type": role, + }, + }, + Spec: machineapi.MachineSpec{ + ProviderSpec: machineapi.ProviderSpec{ + Value: &runtime.RawExtension{Object: vsphereMachineProvider}, + }, + }, + }, + }, + }, + } + + return machines, controlPlaneMachineSet, nil } // applyNetworkConfig this function will apply the static ip configuration to the networkDevice diff --git a/pkg/asset/machines/vsphere/machines_test.go b/pkg/asset/machines/vsphere/machines_test.go index ca84c880f3..e9c279fb91 100644 --- a/pkg/asset/machines/vsphere/machines_test.go +++ b/pkg/asset/machines/vsphere/machines_test.go @@ -378,7 +378,7 @@ func TestConfigMasters(t *testing.T) { for _, tc := range testCases { t.Run(tc.testCase, func(t *testing.T) { - machines, err := Machines(clusterID, tc.installConfig, tc.machinePool, "", "", "") + machines, _, err := Machines(clusterID, tc.installConfig, tc.machinePool, "", "", "") assertOnUnexpectedErrorState(t, tc.expectedError, err) if len(tc.workspaces) > 0 { @@ -449,7 +449,7 @@ func TestHostsToMachines(t *testing.T) { for _, tc := range testCases { t.Run(tc.testCase, func(t *testing.T) { - machines, err := Machines(clusterID, tc.installConfig, tc.machinePool, "", tc.role, "") + machines, _, err := Machines(clusterID, tc.installConfig, tc.machinePool, "", tc.role, "") assertOnUnexpectedErrorState(t, tc.expectedError, err) // Check machine counts diff --git a/pkg/asset/machines/worker.go b/pkg/asset/machines/worker.go index ce63807a60..6ccaa86573 100644 --- a/pkg/asset/machines/worker.go +++ b/pkg/asset/machines/worker.go @@ -636,7 +636,7 @@ func (w *Worker) Generate(dependencies asset.Parents) error { logrus.Debug("Generating worker machines with static IPs.") templateName := clusterID.InfraID + "-rhcos" - machines, err = vsphere.Machines(clusterID.InfraID, ic, &pool, templateName, "worker", workerUserDataSecretName) + machines, _, err = vsphere.Machines(clusterID.InfraID, ic, &pool, templateName, "worker", workerUserDataSecretName) if err != nil { return errors.Wrap(err, "failed to create worker machine objects") } diff --git a/pkg/asset/manifests/infrastructure.go b/pkg/asset/manifests/infrastructure.go index 5478bf303d..274dd2b42f 100644 --- a/pkg/asset/manifests/infrastructure.go +++ b/pkg/asset/manifests/infrastructure.go @@ -247,8 +247,7 @@ func (i *Infrastructure) Generate(dependencies asset.Parents) error { } } - config.Spec.PlatformSpec.VSphere = vsphereinfra.GetInfraPlatformSpec(installConfig) - + config.Spec.PlatformSpec.VSphere = vsphereinfra.GetInfraPlatformSpec(installConfig, clusterID.InfraID) if _, exists := cloudproviderconfig.ConfigMap.Data["vsphere.conf"]; exists { cloudProviderConfigMapKey = "vsphere.conf" } diff --git a/pkg/asset/manifests/vsphere/infrastructure.go b/pkg/asset/manifests/vsphere/infrastructure.go index cee6e970b2..f28b8936ce 100644 --- a/pkg/asset/manifests/vsphere/infrastructure.go +++ b/pkg/asset/manifests/vsphere/infrastructure.go @@ -1,12 +1,14 @@ package vsphere import ( + "fmt" + configv1 "github.com/openshift/api/config/v1" "github.com/openshift/installer/pkg/asset/installconfig" ) // GetInfraPlatformSpec constructs VSpherePlatformSpec for the infrastructure spec -func GetInfraPlatformSpec(ic *installconfig.InstallConfig) *configv1.VSpherePlatformSpec { +func GetInfraPlatformSpec(ic *installconfig.InstallConfig, clusterID string) *configv1.VSpherePlatformSpec { var platformSpec configv1.VSpherePlatformSpec icPlatformSpec := ic.Config.VSphere @@ -21,6 +23,11 @@ func GetInfraPlatformSpec(ic *installconfig.InstallConfig) *configv1.VSpherePlat for _, failureDomain := range icPlatformSpec.FailureDomains { topology := failureDomain.Topology if topology.ComputeCluster != "" && topology.Networks[0] != "" { + template := topology.Template + if len(template) == 0 { + template = fmt.Sprintf("/%s/vm/%s-rhcos-%s-%s", topology.Datacenter, clusterID, failureDomain.Region, failureDomain.Zone) + } + platformSpec.FailureDomains = append(platformSpec.FailureDomains, configv1.VSpherePlatformFailureDomainSpec{ Name: failureDomain.Name, Region: failureDomain.Region, @@ -33,6 +40,7 @@ func GetInfraPlatformSpec(ic *installconfig.InstallConfig) *configv1.VSpherePlat Datastore: topology.Datastore, ResourcePool: topology.ResourcePool, Folder: topology.Folder, + Template: template, }, }) }