Skip to content

Commit

Permalink
Merge pull request spidernet-io#3185 from Icarus9913/fix/wk/service-cidr
Browse files Browse the repository at this point in the history
add kubernetes serviceCIDR APIResource register validation
  • Loading branch information
weizhoublue authored Feb 5, 2024
2 parents acceb8e + b40613c commit 95bd0f1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
10 changes: 3 additions & 7 deletions cmd/spiderpool-controller/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func DaemonMain() {
// disturbed by an abnormal webhook.
checkWebhookReady()

setupInformers()
setupInformers(controllerContext.ClientSet)

monitorMetrics(controllerContext.InnerCtx)

Expand Down Expand Up @@ -447,20 +447,16 @@ func initDynamicClient() (*dynamic.DynamicClient, error) {

// setupInformers will run IPPool,Subnet... informers,
// because these informers count on webhook
func setupInformers() {
func setupInformers(k8sClient *kubernetes.Clientset) {
// start SpiderIPPool informer
crdClient, err := crdclientset.NewForConfig(ctrl.GetConfigOrDie())
if err != nil {
logger.Fatal(err.Error())
}

k8sClient, err := initK8sClientSet()
if nil != err {
logger.Fatal(err.Error())
}

logger.Info("Begin to set up Coordinator informer")
if err := (&coordinatormanager.CoordinatorController{
K8sClient: controllerContext.ClientSet,
Manager: controllerContext.CRDManager,
Client: controllerContext.CRDManager.GetClient(),
APIReader: controllerContext.CRDManager.GetAPIReader(),
Expand Down
1 change: 1 addition & 0 deletions pkg/constant/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
KindCronJob = "CronJob"
KindKubevirtVM = "VirtualMachine"
KindKubevirtVMI = "VirtualMachineInstance"
KindServiceCIDR = "ServiceCIDR"
)

var K8sKinds = []string{KindPod, KindDeployment, KindReplicaSet, KindDaemonSet, KindStatefulSet, KindJob, KindCronJob}
Expand Down
57 changes: 41 additions & 16 deletions pkg/coordinatormanager/coordinator_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,10 @@ import (
"sync/atomic"
"time"

"github.com/spidernet-io/spiderpool/pkg/constant"
"github.com/spidernet-io/spiderpool/pkg/election"
"github.com/spidernet-io/spiderpool/pkg/event"
spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1"
"github.com/spidernet-io/spiderpool/pkg/k8s/client/informers/externalversions"
spiderinformers "github.com/spidernet-io/spiderpool/pkg/k8s/client/informers/externalversions/spiderpool.spidernet.io/v2beta1"
spiderlisters "github.com/spidernet-io/spiderpool/pkg/k8s/client/listers/spiderpool.spidernet.io/v2beta1"
"github.com/spidernet-io/spiderpool/pkg/logutils"
"github.com/spidernet-io/spiderpool/pkg/utils"
stringutil "github.com/spidernet-io/spiderpool/pkg/utils/string"
networkingv1 "k8s.io/api/networking/v1alpha1"

"github.com/cilium/cilium/pkg/ipam/option"
clientset "github.com/spidernet-io/spiderpool/pkg/k8s/client/clientset/versioned"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
networkingv1alpha1 "k8s.io/api/networking/v1alpha1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -43,6 +31,18 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

"github.com/spidernet-io/spiderpool/pkg/constant"
"github.com/spidernet-io/spiderpool/pkg/election"
"github.com/spidernet-io/spiderpool/pkg/event"
spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1"
clientset "github.com/spidernet-io/spiderpool/pkg/k8s/client/clientset/versioned"
"github.com/spidernet-io/spiderpool/pkg/k8s/client/informers/externalversions"
spiderinformers "github.com/spidernet-io/spiderpool/pkg/k8s/client/informers/externalversions/spiderpool.spidernet.io/v2beta1"
spiderlisters "github.com/spidernet-io/spiderpool/pkg/k8s/client/listers/spiderpool.spidernet.io/v2beta1"
"github.com/spidernet-io/spiderpool/pkg/logutils"
"github.com/spidernet-io/spiderpool/pkg/utils"
stringutil "github.com/spidernet-io/spiderpool/pkg/utils/string"
)

const (
Expand Down Expand Up @@ -71,6 +71,7 @@ const messageEnqueueCoordiantor = "Enqueue Coordinator"
var InformerLogger *zap.Logger

type CoordinatorController struct {
K8sClient *kubernetes.Clientset
Manager ctrl.Manager
Client client.Client
APIReader client.Reader
Expand Down Expand Up @@ -551,15 +552,39 @@ func (cc *CoordinatorController) fetchServiceCIDR(ctx context.Context, logger *z
cc.serviceCtrlCancel = nil
}

var serviceCIDR networkingv1.ServiceCIDRList
err := cc.APIReader.List(ctx, &serviceCIDR)
if err != nil {
// check the current k8s whether registered the ServiceCIDR resource
isServiceCIDRInstalled := false
resourceList, err := cc.K8sClient.DiscoveryClient.ServerResourcesForGroupVersion(networkingv1alpha1.SchemeGroupVersion.String())
if nil != err {
event.EventRecorder.Eventf(
coordCopy,
corev1.EventTypeWarning,
"NetworkingV1alpha1NotFound",
"ServiceCIDR is available in k8s v1.29, Your cluster version maybe less than v1.29",
)
return fmt.Errorf("no '%s' API Version resources found in the current kubernetes cluster, error: %v", networkingv1alpha1.SchemeGroupVersion.String(), err)
}
for _, apiResources := range resourceList.APIResources {
if apiResources.Kind == constant.KindServiceCIDR {
isServiceCIDRInstalled = true
break
}
}
if !isServiceCIDRInstalled {
event.EventRecorder.Eventf(
coordCopy,
corev1.EventTypeWarning,
"ServiceCIDRNotFound",
"ServiceCIDR is available in k8s v1.29, Your cluster version maybe less than v1.29",
)
return fmt.Errorf("no '%s' API resource found in kubernetes cluster '%s' API Version", constant.KindServiceCIDR, networkingv1alpha1.SchemeGroupVersion.String())
}

// fetch kubernetes ServiceCIDR
logger.Sugar().Debugf("try to fetch kubernetes %s for coordinator", constant.KindServiceCIDR)
var serviceCIDR networkingv1alpha1.ServiceCIDRList
err = cc.APIReader.List(ctx, &serviceCIDR)
if err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/coordinatormanager/service_cidr_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"reflect"

"go.uber.org/zap"
networkingv1 "k8s.io/api/networking/v1alpha1"
networkingv1alpha1 "k8s.io/api/networking/v1alpha1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -39,7 +39,7 @@ func NewServiceCIDRController(mgr ctrl.Manager, logger *zap.Logger, coordinatorN
if err != nil {
return nil, err
}
if err := c.Watch(source.Kind(mgr.GetCache(), &networkingv1.ServiceCIDR{}), &handler.EnqueueRequestForObject{}); err != nil {
if err := c.Watch(source.Kind(mgr.GetCache(), &networkingv1alpha1.ServiceCIDR{}), &handler.EnqueueRequestForObject{}); err != nil {
return nil, err
}

Expand All @@ -53,7 +53,7 @@ type serviceCIDRReconciler struct {
}

func (r *serviceCIDRReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var svcPoolList networkingv1.ServiceCIDRList
var svcPoolList networkingv1alpha1.ServiceCIDRList
if err := r.client.List(ctx, &svcPoolList); err != nil {
return ctrl.Result{Requeue: true}, err
}
Expand Down

0 comments on commit 95bd0f1

Please sign in to comment.