|
4 | 4 | "context" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
7 | 9 | "time" |
8 | 10 |
|
9 | 11 | "github.com/sirupsen/logrus" |
@@ -364,8 +366,83 @@ func (i *InfraProvider) DestroyBootstrap(dir string) error { |
364 | 366 | return nil |
365 | 367 | } |
366 | 368 |
|
| 369 | +type machineManifest struct { |
| 370 | + Status struct { |
| 371 | + Addresses []clusterv1.MachineAddress `yaml:"addresses"` |
| 372 | + } `yaml:"status"` |
| 373 | +} |
| 374 | + |
| 375 | +// extractIPAddress extracts the IP address from a machine manifest file in a |
| 376 | +// provider-agnostic way by reading only the "status" stanza, which should be |
| 377 | +// present in all providers. |
| 378 | +func extractIPAddress(manifestPath string) (string, error) { |
| 379 | + data, err := os.ReadFile(manifestPath) |
| 380 | + if err != nil { |
| 381 | + return "", fmt.Errorf("failed to read machine manifest %s: %w", manifestPath, err) |
| 382 | + } |
| 383 | + var manifest machineManifest |
| 384 | + if err := yaml.Unmarshal(data, &manifest); err != nil { |
| 385 | + return "", fmt.Errorf("failed to unmarshal manifest %s: %w", manifestPath, err) |
| 386 | + } |
| 387 | + |
| 388 | + var ipAddr string |
| 389 | + for _, addr := range manifest.Status.Addresses { |
| 390 | + switch addr.Type { |
| 391 | + case clusterv1.MachineExternalIP: |
| 392 | + ipAddr = addr.Address |
| 393 | + case clusterv1.MachineInternalIP: |
| 394 | + // Prefer external IP when present |
| 395 | + if len(ipAddr) == 0 { |
| 396 | + ipAddr = addr.Address |
| 397 | + } |
| 398 | + default: |
| 399 | + continue |
| 400 | + } |
| 401 | + } |
| 402 | + |
| 403 | + return ipAddr, nil |
| 404 | +} |
| 405 | + |
367 | 406 | // ExtractHostAddresses extracts the IPs of the bootstrap and control plane machines. |
368 | 407 | func (i *InfraProvider) ExtractHostAddresses(dir string, config *types.InstallConfig, ha *infrastructure.HostAddresses) error { |
| 408 | + logrus.Debugf("Looking for machine manifests in %s", dir) |
| 409 | + |
| 410 | + bootstrapFiles, err := filepath.Glob(filepath.Join(dir, "Machine\\-openshift\\-cluster\\-api\\-guests\\-*\\-bootstrap.yaml")) |
| 411 | + if err != nil { |
| 412 | + return fmt.Errorf("failed to list bootstrap manifests: %w", err) |
| 413 | + } |
| 414 | + logrus.Debugf("bootstrap manifests found: %v", bootstrapFiles) |
| 415 | + |
| 416 | + if len(bootstrapFiles) != 1 { |
| 417 | + return fmt.Errorf("wrong number of bootstrap manifests found: %v. Expected exactly one", bootstrapFiles) |
| 418 | + } |
| 419 | + addr, err := extractIPAddress(bootstrapFiles[0]) |
| 420 | + if err != nil { |
| 421 | + return fmt.Errorf("failed to extract IP address for bootstrap: %w", err) |
| 422 | + } |
| 423 | + logrus.Debugf("found bootstrap address: %s", addr) |
| 424 | + ha.Bootstrap = addr |
| 425 | + |
| 426 | + masterFiles, err := filepath.Glob(filepath.Join(dir, "Machine\\-openshift\\-cluster\\-api\\-guests\\-*\\-master\\-?.yaml")) |
| 427 | + if err != nil { |
| 428 | + return fmt.Errorf("failed to list master machine manifests: %w", err) |
| 429 | + } |
| 430 | + logrus.Debugf("master machine manifests found: %v", masterFiles) |
| 431 | + |
| 432 | + if replicas := int(*config.ControlPlane.Replicas); replicas != len(masterFiles) { |
| 433 | + logrus.Warnf("not all master manifests found: %d. Expected %d.", len(masterFiles), replicas) |
| 434 | + } |
| 435 | + for _, manifest := range masterFiles { |
| 436 | + addr, err := extractIPAddress(manifest) |
| 437 | + if err != nil { |
| 438 | + // Log the error but keep parsing the remaining files |
| 439 | + logrus.Warnf("failed to extract IP address for %s: %v", manifest, err) |
| 440 | + continue |
| 441 | + } |
| 442 | + logrus.Debugf("found master address: %s", addr) |
| 443 | + ha.Masters = append(ha.Masters, addr) |
| 444 | + } |
| 445 | + |
369 | 446 | return nil |
370 | 447 | } |
371 | 448 |
|
|
0 commit comments