Skip to content

Commit b853830

Browse files
committed
CORS-3215: capi: implement bootstrap gather
By extracting only the "status" stanza from the machine manifests, we are able to parse IP addresses in a provider-agnostic way.
1 parent 0481a93 commit b853830

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

pkg/infrastructure/clusterapi/clusterapi.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package clusterapi
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"time"
79

810
"github.com/sirupsen/logrus"
@@ -337,8 +339,83 @@ func (i *InfraProvider) DestroyBootstrap(dir string) error {
337339
return nil
338340
}
339341

342+
type machineManifest struct {
343+
Status struct {
344+
Addresses []clusterv1.MachineAddress `yaml:"addresses"`
345+
} `yaml:"status"`
346+
}
347+
348+
// extractIPAddress extracts the IP address from a machine manifest file in a
349+
// provider-agnostic way by reading only the "status" stanza, which should be
350+
// present in all providers.
351+
func extractIPAddress(manifestPath string) (string, error) {
352+
data, err := os.ReadFile(manifestPath)
353+
if err != nil {
354+
return "", fmt.Errorf("failed to read machine manifest %s: %w", manifestPath, err)
355+
}
356+
var manifest machineManifest
357+
if err := yaml.Unmarshal(data, &manifest); err != nil {
358+
return "", fmt.Errorf("failed to unmarshal manifest %s: %w", manifestPath, err)
359+
}
360+
361+
var ipAddr string
362+
for _, addr := range manifest.Status.Addresses {
363+
switch addr.Type {
364+
case clusterv1.MachineExternalIP:
365+
ipAddr = addr.Address
366+
case clusterv1.MachineInternalIP:
367+
// Prefer external IP when present
368+
if len(ipAddr) == 0 {
369+
ipAddr = addr.Address
370+
}
371+
default:
372+
continue
373+
}
374+
}
375+
376+
return ipAddr, nil
377+
}
378+
340379
// ExtractHostAddresses extracts the IPs of the bootstrap and control plane machines.
341380
func (i *InfraProvider) ExtractHostAddresses(dir string, config *types.InstallConfig, ha *infrastructure.HostAddresses) error {
381+
logrus.Debugf("Looking for machine manifests in %s", dir)
382+
383+
bootstrapFiles, err := filepath.Glob(filepath.Join(dir, "Machine\\-openshift\\-cluster\\-api\\-guests\\-*\\-bootstrap.yaml"))
384+
if err != nil {
385+
return fmt.Errorf("failed to list bootstrap manifests: %w", err)
386+
}
387+
logrus.Debugf("bootstrap manifests found: %v", bootstrapFiles)
388+
389+
if len(bootstrapFiles) != 1 {
390+
return fmt.Errorf("wrong number of bootstrap manifests found: %v. Expected exactly one", bootstrapFiles)
391+
}
392+
addr, err := extractIPAddress(bootstrapFiles[0])
393+
if err != nil {
394+
return fmt.Errorf("failed to extract IP address for bootstrap: %w", err)
395+
}
396+
logrus.Debugf("found bootstrap address: %s", addr)
397+
ha.Bootstrap = addr
398+
399+
masterFiles, err := filepath.Glob(filepath.Join(dir, "Machine\\-openshift\\-cluster\\-api\\-guests\\-*\\-master\\-?.yaml"))
400+
if err != nil {
401+
return fmt.Errorf("failed to list master machine manifests: %w", err)
402+
}
403+
logrus.Debugf("master machine manifests found: %v", masterFiles)
404+
405+
if replicas := int(*config.ControlPlane.Replicas); replicas != len(masterFiles) {
406+
logrus.Warnf("not all master manifests found: %d. Expected %d.", len(masterFiles), replicas)
407+
}
408+
for _, manifest := range masterFiles {
409+
addr, err := extractIPAddress(manifest)
410+
if err != nil {
411+
// Log the error but keep parsing the remaining files
412+
logrus.Warnf("failed to extract IP address for %s: %v", manifest, err)
413+
continue
414+
}
415+
logrus.Debugf("found master address: %s", addr)
416+
ha.Masters = append(ha.Masters, addr)
417+
}
418+
342419
return nil
343420
}
344421

0 commit comments

Comments
 (0)