Skip to content

Commit

Permalink
Merge pull request #9361 from shiftstack/fix-local-controlplane
Browse files Browse the repository at this point in the history
OCPBUGS-48228: Envtest: Configure IPv6 service network for API Service
  • Loading branch information
openshift-merge-bot[bot] authored Jan 29, 2025
2 parents fb88402 + 9346289 commit ecd4e42
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/clusterapi/localcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type localControlPlane struct {
KubeconfigPath string
EtcdLog *os.File
APIServerLog *os.File
APIServerArgs map[string]string
}

// Run launches the local control plane.
Expand Down Expand Up @@ -98,12 +99,16 @@ func (c *localControlPlane) Run(ctx context.Context) error {
Out: c.EtcdLog,
Err: c.EtcdLog,
},
APIServer: &envtest.APIServer{
Out: c.APIServerLog,
Err: c.APIServerLog,
},
},
}
apiServer := &envtest.APIServer{
Out: c.APIServerLog,
Err: c.APIServerLog,
}
for key, value := range c.APIServerArgs {
apiServer.Configure().Set(key, value)
}
c.Env.ControlPlane.APIServer = apiServer
c.Cfg, err = c.Env.Start()
if err != nil {
return err
Expand Down
37 changes: 37 additions & 0 deletions pkg/clusterapi/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"net"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -86,6 +87,30 @@ type system struct {
logWriter *io.PipeWriter
}

// hostHasIPv4Address verifies if the host that launches the host control plane has IPv4 address.
func hostHasIPv4Address() (bool, error) {
interfaces, err := net.Interfaces()
if err != nil {
return false, err
}
for _, intf := range interfaces {
if intf.Flags&net.FlagUp == 0 || intf.Flags&net.FlagLoopback != 0 {
continue
}
addrs, err := intf.Addrs()
if err != nil {
return false, err
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && ipNet.IP.To4() != nil {
return true, nil
}
}
}
return false, nil
}

// Run launches the cluster-api system.
func (c *system) Run(ctx context.Context) error { //nolint:gocyclo
c.Lock()
Expand All @@ -97,6 +122,18 @@ func (c *system) Run(ctx context.Context) error { //nolint:gocyclo

// Create the local control plane.
lcp := &localControlPlane{}

ipv4, err := hostHasIPv4Address()
if err != nil {
return err
}
// If the host has no IPv4 available, the default value of service network should be modified to IPv6 CIDR.
if !ipv4 {
lcp.APIServerArgs = map[string]string{
"service-cluster-ip-range": "fd02::/112",
}
}

if err := lcp.Run(ctx); err != nil {
return fmt.Errorf("failed to run local control plane: %w", err)
}
Expand Down

0 comments on commit ecd4e42

Please sign in to comment.