Skip to content

Commit

Permalink
Support getting serviceCIDR from Containers.Args
Browse files Browse the repository at this point in the history
Signed-off-by: ty-dc <tao.yang@daocloud.io>
  • Loading branch information
ty-dc committed Feb 26, 2024
1 parent d826534 commit 50a75b7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/coordinatormanager/coordinator_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam
return
}

// Spiderpool can automatically fetch ClusterCIDR.
// First try to get the ClusterCIDR from kube-system/kubeadm-config,
// If that fails, it will try to get the ClusterCIDR from the Spec.Containers[0].Command or Spec.Containers[0].Args of the kube-controller-manager Pod.
func (cc *CoordinatorController) fetchPodAndServerCIDR(ctx context.Context, logger *zap.Logger, coordCopy *spiderpoolv2beta1.SpiderCoordinator) (*spiderpoolv2beta1.SpiderCoordinator, error) {
if *coordCopy.Spec.PodCIDRType == auto {
podCidrType, err := fetchType(cc.DefaultCniConfDir)
Expand Down Expand Up @@ -378,6 +381,7 @@ func (cc *CoordinatorController) fetchPodAndServerCIDR(ctx context.Context, logg
return coordCopy, err
}
k8sPodCIDR, k8sServiceCIDR = ExtractK8sCIDRFromKCMPod(&cmPodList.Items[0])
logger.Sugar().Infof("kube-controller-manager k8sPodCIDR %v, k8sServiceCIDR %v", k8sPodCIDR, k8sServiceCIDR)
}

switch *coordCopy.Spec.PodCIDRType {
Expand Down Expand Up @@ -652,18 +656,31 @@ func ExtractK8sCIDRFromKCMPod(kcm *corev1.Pod) ([]string, []string) {
serviceReg := regexp.MustCompile(`--service-cluster-ip-range=(.*)`)

var podSubnets, serviceSubnets []string
for _, l := range kcm.Spec.Containers[0].Command {
findSubnets := func(l string) {
if len(podSubnets) == 0 {
podSubnets = podReg.FindStringSubmatch(l)
}
if len(serviceSubnets) == 0 {
serviceSubnets = serviceReg.FindStringSubmatch(l)
}
}

for _, l := range kcm.Spec.Containers[0].Command {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break
}
}

if len(podSubnets) == 0 || len(serviceSubnets) == 0 {
for _, l := range kcm.Spec.Containers[0].Args {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break
}
}
}

if len(podSubnets) != 0 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
Expand Down

0 comments on commit 50a75b7

Please sign in to comment.