mirror of
https://github.com/openshift/installer.git
synced 2026-02-06 00:48:45 +01:00
Added static ip support to CAPI installer for vSphere.
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/openshift/installer/pkg/asset/installconfig/vsphere"
|
||||
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
|
||||
"github.com/openshift/installer/pkg/types"
|
||||
"github.com/openshift/installer/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -69,8 +70,9 @@ func GenerateMachines(ctx context.Context, clusterID string, config *types.Insta
|
||||
|
||||
capvMachines := make([]*capv.VSphereMachine, 0, len(machines))
|
||||
result := make([]*asset.RuntimeFile, 0, len(machines))
|
||||
staticIP := false
|
||||
|
||||
for _, machine := range machines {
|
||||
for mIndex, machine := range machines {
|
||||
providerSpec, ok := machine.Spec.ProviderSpec.Value.Object.(*machinev1.VSphereMachineProviderSpec)
|
||||
if !ok {
|
||||
return nil, errors.New("unable to convert ProviderSpec to VSphereMachineProviderSpec")
|
||||
@@ -79,22 +81,33 @@ func GenerateMachines(ctx context.Context, clusterID string, config *types.Insta
|
||||
vcenterContext := metadata.VCenterContexts[providerSpec.Workspace.Server]
|
||||
resourcePool := providerSpec.Workspace.ResourcePool
|
||||
|
||||
customVMXKeys := map[string]string{
|
||||
"guestinfo.hostname": machine.Name,
|
||||
"guestinfo.domain": strings.TrimSuffix(config.ClusterDomain(), "."),
|
||||
"stealclock.enable": "TRUE",
|
||||
}
|
||||
|
||||
capvNetworkDevices := []capv.NetworkDeviceSpec{}
|
||||
for _, networkDevice := range providerSpec.Network.Devices {
|
||||
networkName, err := getNetworkInventoryPath(vcenterContext, networkDevice.NetworkName, providerSpec)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get network inventory path: %w", err)
|
||||
}
|
||||
capvNetworkDevices = append(capvNetworkDevices, capv.NetworkDeviceSpec{
|
||||
deviceSpec := capv.NetworkDeviceSpec{
|
||||
NetworkName: networkName,
|
||||
DHCP4: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
customVMXKeys := map[string]string{
|
||||
"guestinfo.hostname": machine.Name,
|
||||
"guestinfo.domain": strings.TrimSuffix(config.ClusterDomain(), "."),
|
||||
"stealclock.enable": "TRUE",
|
||||
// Static IP configured. Add kargs.
|
||||
if len(networkDevice.AddressesFromPools) > 0 {
|
||||
staticIP = true
|
||||
kargs, err := utils.ConstructNetworkKargsFromMachine(data.IPClaims, data.IPAddresses, &machines[mIndex], networkDevice)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get static ip config for machine %v: %w", machine.Name, err)
|
||||
}
|
||||
customVMXKeys["guestinfo.afterburn.initrd.network-kargs"] = kargs
|
||||
}
|
||||
capvNetworkDevices = append(capvNetworkDevices, deviceSpec)
|
||||
}
|
||||
|
||||
vsphereMachine := &capv.VSphereMachine{
|
||||
@@ -166,6 +179,15 @@ func GenerateMachines(ctx context.Context, clusterID string, config *types.Insta
|
||||
if role == masterRole {
|
||||
customVMXKeys := map[string]string{}
|
||||
|
||||
// If we detected static IP for masters, lets apply to bootstrap as well.
|
||||
if staticIP {
|
||||
kargs, err := utils.ConstructKargsForBootstrap(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get static ip config for bootstrap: %w", err)
|
||||
}
|
||||
customVMXKeys["guestinfo.afterburn.initrd.network-kargs"] = kargs
|
||||
}
|
||||
|
||||
bootstrapSpec := capvMachines[0].Spec
|
||||
bootstrapSpec.CustomVMXKeys = customVMXKeys
|
||||
bootstrapVSphereMachine := &capv.VSphereMachine{
|
||||
|
||||
@@ -9,48 +9,27 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"sigs.k8s.io/cluster-api/exp/ipam/api/v1alpha1"
|
||||
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
|
||||
|
||||
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
|
||||
"github.com/openshift/installer/pkg/types"
|
||||
)
|
||||
|
||||
func getIPAddressForClaim(claim v1alpha1.IPAddressClaim, addresses []v1alpha1.IPAddress) (*v1alpha1.IPAddress, error) {
|
||||
for _, address := range addresses {
|
||||
if address.Name == claim.Status.AddressRef.Name {
|
||||
return &address, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to find address for claim %s", claim.Name)
|
||||
}
|
||||
|
||||
// ConstructNetworkKargsFromMachine does something.
|
||||
func ConstructNetworkKargsFromMachine(claims []v1alpha1.IPAddressClaim, addresses []v1alpha1.IPAddress, machine *machinev1beta1.Machine) (string, error) {
|
||||
var addressList []string
|
||||
var gatewayList []string
|
||||
var nameserverList []string
|
||||
|
||||
for _, claim := range claims {
|
||||
for _, ownerReference := range claim.OwnerReferences {
|
||||
if ownerReference.Name != machine.Name {
|
||||
continue
|
||||
}
|
||||
address, err := getIPAddressForClaim(claim, addresses)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get address for claim %s: %w", claim.Name, err)
|
||||
}
|
||||
|
||||
addressList = append(addressList, fmt.Sprintf("%s/%d", address.Spec.Address, address.Spec.Prefix))
|
||||
gatewayList = append(gatewayList, address.Spec.Gateway)
|
||||
for _, networkDevices := range machine.Spec.ProviderSpec.Value.Object.(*machinev1beta1.VSphereMachineProviderSpec).Network.Devices {
|
||||
if networkDevices.Nameservers == nil {
|
||||
continue
|
||||
}
|
||||
nameserverList = append(nameserverList, networkDevices.Nameservers...)
|
||||
func ConstructNetworkKargsFromMachine(claims []ipamv1.IPAddressClaim, addresses []ipamv1.IPAddress, machine *machinev1beta1.Machine, network machinev1beta1.NetworkDeviceSpec) (string, error) {
|
||||
var ipAddresses []string
|
||||
var gateways []string
|
||||
for idx := range network.AddressesFromPools {
|
||||
for _, address := range addresses {
|
||||
logrus.Debugf("Checking IPAdress %v. Does it match? %v", address.Name, fmt.Sprintf("%s-claim-%d-%d", machine.Name, 0, idx))
|
||||
if address.Name == fmt.Sprintf("%s-claim-%d-%d", machine.Name, 0, idx) {
|
||||
ipAddresses = append(ipAddresses, fmt.Sprintf("%v/%v", address.Spec.Address, address.Spec.Prefix))
|
||||
gateways = append(gateways, address.Spec.Gateway)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return ConstructKargsFromNetworkConfig(addressList, nameserverList, gatewayList)
|
||||
return ConstructKargsFromNetworkConfig(ipAddresses, network.Nameservers, gateways)
|
||||
}
|
||||
|
||||
func getSubnetMask(prefix netip.Prefix) (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user