Skip to content

Commit

Permalink
Merge pull request #76 from mhrabovcin/service-ip-range
Browse files Browse the repository at this point in the history
feat: support configurable service ClusterIP range
  • Loading branch information
mhrabovcin authored Jan 9, 2024
2 parents 814d1ad + 599da2d commit 680b51a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
55 changes: 48 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,53 @@ 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(
ipRangeFromFlag string,
supportBundle bundle.Bundle,
out output.Output,
) (string, error) {
// Manually provided via CLI flag
if ipRangeFromFlag != "" {
return ipRangeFromFlag, 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 680b51a

Please sign in to comment.