diff --git a/cmd/spiderpool-agent/cmd/coordinator.go b/cmd/spiderpool-agent/cmd/coordinator.go index 950fbb02b5..10753bb8cb 100644 --- a/cmd/spiderpool-agent/cmd/coordinator.go +++ b/cmd/spiderpool-agent/cmd/coordinator.go @@ -5,10 +5,12 @@ package cmd import ( "fmt" + "github.com/go-openapi/runtime/middleware" "github.com/spidernet-io/spiderpool/api/v1/agent/models" "github.com/spidernet-io/spiderpool/api/v1/agent/server/restapi/daemonset" "github.com/spidernet-io/spiderpool/pkg/constant" + "github.com/spidernet-io/spiderpool/pkg/coordinatormanager" spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1" corev1 "k8s.io/api/core/v1" ) @@ -31,6 +33,11 @@ func (g *_unixGetCoordinatorConfig) Handle(params daemonset.GetCoordinatorConfig if len(coordList.Items) == 0 { return daemonset.NewGetCoordinatorConfigFailure().WithPayload(models.Error("coordinator config not found")) } + coord := coordList.Items[0] + + if coord.Status.Phase != coordinatormanager.Synced { + return daemonset.NewGetCoordinatorConfigFailure().WithPayload(models.Error(fmt.Sprintf("spidercoordinator: %s no ready", coord.Name))) + } var pod *corev1.Pod var err error @@ -39,7 +46,6 @@ func (g *_unixGetCoordinatorConfig) Handle(params daemonset.GetCoordinatorConfig return daemonset.NewGetCoordinatorConfigFailure().WithPayload(models.Error(fmt.Sprintf("failed to get coordinator config: pod %s/%s not found", params.GetCoordinatorConfig.PodNamespace, params.GetCoordinatorConfig.PodName))) } - coord := coordList.Items[0] var prefix string if coord.Spec.PodMACPrefix != nil { prefix = *coord.Spec.PodMACPrefix diff --git a/pkg/coordinatormanager/calico_ippool_informer.go b/pkg/coordinatormanager/calico_ippool_informer.go index 4854399441..cb2895b2c7 100644 --- a/pkg/coordinatormanager/calico_ippool_informer.go +++ b/pkg/coordinatormanager/calico_ippool_informer.go @@ -67,12 +67,12 @@ func (r *calicoIPPoolReconciler) Reconcile(ctx context.Context, req ctrl.Request return ctrl.Result{Requeue: true}, err } - if coordinator.Status.Phase == synced && reflect.DeepEqual(coordinator.Status.OverlayPodCIDR, podCIDR) { + if coordinator.Status.Phase == Synced && reflect.DeepEqual(coordinator.Status.OverlayPodCIDR, podCIDR) { return ctrl.Result{}, nil } origin := coordinator.DeepCopy() - coordinator.Status.Phase = synced + coordinator.Status.Phase = Synced coordinator.Status.OverlayPodCIDR = podCIDR if err := r.client.Status().Patch(ctx, &coordinator, client.MergeFrom(origin)); err != nil { return ctrl.Result{Requeue: true}, err diff --git a/pkg/coordinatormanager/coordinator_informer.go b/pkg/coordinatormanager/coordinator_informer.go index 958577e66c..449ce33fcb 100644 --- a/pkg/coordinatormanager/coordinator_informer.go +++ b/pkg/coordinatormanager/coordinator_informer.go @@ -53,8 +53,8 @@ const ( ) const ( - notReady = "NotReady" - synced = "Synced" + NotReady = "NotReady" + Synced = "Synced" ) const messageEnqueueCoordiantor = "Enqueue Coordinator" @@ -314,8 +314,10 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam err.Error(), ) - if coordCopy.Status.Phase != notReady { - coordCopy.Status.Phase = notReady + if coordCopy.Status.Phase != NotReady { + coordCopy.Status.Phase = NotReady + coordCopy.Status.OverlayPodCIDR = []string{} + coordCopy.Status.ServiceCIDR = []string{} if err := cc.Client.Status().Patch(ctx, coordCopy, client.MergeFrom(coord)); err != nil { return err } @@ -332,8 +334,10 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam msg, ) - if coordCopy.Status.Phase != notReady { - coordCopy.Status.Phase = notReady + if coordCopy.Status.Phase != NotReady { + coordCopy.Status.Phase = NotReady + coordCopy.Status.OverlayPodCIDR = []string{} + coordCopy.Status.ServiceCIDR = []string{} if err := cc.Client.Status().Patch(ctx, coordCopy, client.MergeFrom(coord)); err != nil { return err } @@ -349,7 +353,7 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam cc.caliCtrlCanncel() cc.caliCtrlCanncel = nil } - coordCopy.Status.Phase = synced + coordCopy.Status.Phase = Synced coordCopy.Status.OverlayPodCIDR = k8sPodCIDR case calico: if _, err := cc.ConfigmapLister.ConfigMaps(metav1.NamespaceSystem).Get(calicoConfig); err != nil { @@ -361,8 +365,10 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam "Calico needs to be installed first", ) } - if coordCopy.Status.Phase != notReady { - coordCopy.Status.Phase = notReady + if coordCopy.Status.Phase != NotReady { + coordCopy.Status.Phase = NotReady + coordCopy.Status.OverlayPodCIDR = []string{} + coordCopy.Status.ServiceCIDR = []string{} if err := cc.Client.Status().Patch(ctx, coordCopy, client.MergeFrom(coord)); err != nil { return err } @@ -407,8 +413,10 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam "Cilium needs to be installed first", ) } - if coordCopy.Status.Phase != notReady { - coordCopy.Status.Phase = notReady + if coordCopy.Status.Phase != NotReady { + coordCopy.Status.Phase = NotReady + coordCopy.Status.OverlayPodCIDR = []string{} + coordCopy.Status.ServiceCIDR = []string{} if err := cc.Client.Status().Patch(ctx, coordCopy, client.MergeFrom(coord)); err != nil { return err } @@ -418,15 +426,17 @@ func (cc *CoordinatorController) syncHandler(ctx context.Context, coordinatorNam ipam, ciliumPodCIDR, err := extractCiliumCIDR(ccm) if err != nil { logger.Sugar().Warnf("Failed to gather Pod CIDR form Cilium: %v", err) - if coordCopy.Status.Phase != notReady { - coordCopy.Status.Phase = notReady + if coordCopy.Status.Phase != NotReady { + coordCopy.Status.Phase = NotReady + coordCopy.Status.OverlayPodCIDR = []string{} + coordCopy.Status.ServiceCIDR = []string{} if err := cc.Client.Status().Patch(ctx, coordCopy, client.MergeFrom(coord)); err != nil { return err } } break } - coordCopy.Status.Phase = synced + coordCopy.Status.Phase = Synced coordCopy.Status.OverlayPodCIDR = ciliumPodCIDR if ipam == "kubernetes" { coordCopy.Status.OverlayPodCIDR = k8sPodCIDR