Skip to content

Commit

Permalink
feat: support configurable service ClusterIP range
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrabovcin committed Jan 9, 2024
1 parent 814d1ad commit b266aa8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
51 changes: 44 additions & 7 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
)

type serveOptions struct {
kubeconfigPath string
proxyAddress string
envtestArch string
kubeconfigPath string
proxyAddress string
envtestArch string
serviceClusterIPRange string
}

// NewServeCommand serves the provided bundle.
Expand Down Expand Up @@ -58,6 +59,11 @@ func NewServeCommand(out output.Output) *cobra.Command {
"arch value for k8s server assets",
)

cmd.Flags().StringVar(
&options.serviceClusterIPRange, "service-cluster-ip-range", options.serviceClusterIPRange,
"override k8s api server service ClusterIP range. Mask must be >= /12 range.",
)

return cmd
}

Expand Down Expand Up @@ -115,18 +121,49 @@ func startK8sServer(
if err != nil {
return nil, fmt.Errorf("failed to prepare k8s environment: %w", err)
}
ipRange, err := bundle.DetectServiceSubnetRange(supportBundle)

testEnv.ControlPlane.GetAPIServer().Out = out.V(5).InfoWriter()
testEnv.ControlPlane.GetAPIServer().Err = out.V(5).InfoWriter()

serviceClusterIPRange, err := resolveServiceClusterIPRange(
opts.serviceClusterIPRange, supportBundle, out)
if err != nil {
return nil, err
}
if ipRange != "" {
out.V(1).Infof("Detected k8s service cluster IP range: %s", ipRange)
testEnv.ControlPlane.GetAPIServer().Configure().Append("service-cluster-ip-range", ipRange)
if serviceClusterIPRange != "" {
testEnv.ControlPlane.GetAPIServer().Configure().Append("service-cluster-ip-range", serviceClusterIPRange)
}

_, err = testEnv.Start()
if err != nil {
return nil, err
}

return testEnv, nil
}

func resolveServiceClusterIPRange(ipRangeFlag string, supportBundle bundle.Bundle, out output.Output) (string, error) {
// Manually provided via CLI flag
if ipRangeFlag != "" {
return ipRangeFlag, nil
}

// Detected from the bundle
ipRangeFromBundle, err := bundle.DetectServiceSubnetRange(supportBundle)
if err != nil {
return "", err
}
if ipRangeFromBundle != "" {
out.V(1).Infof("Detected service cluster IP range: %s", ipRangeFromBundle)
return ipRangeFromBundle, nil
}

// Fallback default
out.Warnf(
"Service ClusterIP range could not be detected from support bundle, using default %q. Use "+
"%q flag to override this value.",
kubernetes.DefaultServiceClusterIPRange,
"service-cluster-ip-range",
)
return kubernetes.DefaultServiceClusterIPRange, nil
}
7 changes: 7 additions & 0 deletions pkg/kubernetes/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// DefaultServiceClusterIPRange is the fallback value for service ClusterIP
// range of k8s API server configuration. This value is used when the cluster
// cannot be detected from the bundle itself. This happens mostly for managed
// k8s platforms like EKS, AKS which will not return API server pod in the list
// of pods.
const DefaultServiceClusterIPRange = "10.0.0.0/12"

// WriteProxyKubeconfig creates a KUBECONFIG file for http proxy server. If path
// for kubeconfig is not provided then default value is create in `CWD`.
func WriteProxyKubeconfig(host, path string) (string, error) {
Expand Down

0 comments on commit b266aa8

Please sign in to comment.