ippool_controller.go 16.1 KB
Newer Older
D
Duan Jiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
Copyright 2020 KubeSphere Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ippool

import (
Z
Zack Zhang 已提交
20
	"context"
D
Duan Jiong 已提交
21 22
	"fmt"
	cnet "github.com/projectcalico/libcalico-go/lib/net"
D
Duan Jiong 已提交
23
	corev1 "k8s.io/api/core/v1"
D
Duan Jiong 已提交
24 25 26
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
D
Duan Jiong 已提交
27
	"k8s.io/apimachinery/pkg/runtime"
D
Duan Jiong 已提交
28 29
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/util/wait"
D
Duan Jiong 已提交
30 31
	k8sinformers "k8s.io/client-go/informers"
	coreinfomers "k8s.io/client-go/informers/core/v1"
D
Duan Jiong 已提交
32 33
	clientset "k8s.io/client-go/kubernetes"
	"k8s.io/client-go/kubernetes/scheme"
D
Duan Jiong 已提交
34
	clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
D
Duan Jiong 已提交
35 36 37 38 39
	"k8s.io/client-go/tools/cache"
	"k8s.io/client-go/tools/record"
	"k8s.io/client-go/util/workqueue"
	"k8s.io/klog"
	networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
D
Duan Jiong 已提交
40
	tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
D
Duan Jiong 已提交
41
	kubesphereclient "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
D
Duan Jiong 已提交
42
	ksinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
D
Duan Jiong 已提交
43
	networkInformer "kubesphere.io/kubesphere/pkg/client/informers/externalversions/network/v1alpha1"
D
Duan Jiong 已提交
44 45
	tenantv1alpha1informers "kubesphere.io/kubesphere/pkg/client/informers/externalversions/tenant/v1alpha1"
	"kubesphere.io/kubesphere/pkg/constants"
D
Duan Jiong 已提交
46
	"kubesphere.io/kubesphere/pkg/controller/network/utils"
D
Duan Jiong 已提交
47
	"kubesphere.io/kubesphere/pkg/controller/network/webhooks"
D
Duan Jiong 已提交
48
	"kubesphere.io/kubesphere/pkg/simple/client/network/ippool"
D
Duan Jiong 已提交
49
	"reflect"
D
Duan Jiong 已提交
50
	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
D
Duan Jiong 已提交
51
	"time"
D
Duan Jiong 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
)

var (
	ErrCIDROverlap = fmt.Errorf("CIDR is overlap")
)

type IPPoolController struct {
	eventBroadcaster record.EventBroadcaster
	eventRecorder    record.EventRecorder

	provider ippool.Provider

	ippoolInformer networkInformer.IPPoolInformer
	ippoolSynced   cache.InformerSynced
	ippoolQueue    workqueue.RateLimitingInterface

D
Duan Jiong 已提交
68 69 70 71 72 73 74
	wsInformer tenantv1alpha1informers.WorkspaceInformer
	wsSynced   cache.InformerSynced

	nsInformer coreinfomers.NamespaceInformer
	nsSynced   cache.InformerSynced
	nsQueue    workqueue.RateLimitingInterface

D
Duan Jiong 已提交
75 76 77 78 79 80 81
	ipamblockInformer networkInformer.IPAMBlockInformer
	ipamblockSynced   cache.InformerSynced

	client           clientset.Interface
	kubesphereClient kubesphereclient.Interface
}

D
Duan Jiong 已提交
82
func (c *IPPoolController) enqueueIPPools(obj interface{}) {
D
Duan Jiong 已提交
83 84 85 86 87 88
	pool, ok := obj.(*networkv1alpha1.IPPool)
	if !ok {
		utilruntime.HandleError(fmt.Errorf("IPPool informer returned non-ippool object: %#v", obj))
		return
	}

D
Duan Jiong 已提交
89
	c.ippoolQueue.Add(pool.Name)
D
Duan Jiong 已提交
90 91 92 93 94
}

func (c *IPPoolController) addFinalizer(pool *networkv1alpha1.IPPool) error {
	clone := pool.DeepCopy()
	controllerutil.AddFinalizer(clone, networkv1alpha1.IPPoolFinalizer)
D
Duan Jiong 已提交
95 96
	if clone.Labels == nil {
		clone.Labels = make(map[string]string)
D
Duan Jiong 已提交
97
	}
D
Duan Jiong 已提交
98 99 100
	clone.Labels[networkv1alpha1.IPPoolNameLabel] = clone.Name
	clone.Labels[networkv1alpha1.IPPoolTypeLabel] = clone.Spec.Type
	clone.Labels[networkv1alpha1.IPPoolIDLabel] = fmt.Sprintf("%d", clone.ID())
Z
Zack Zhang 已提交
101
	pool, err := c.kubesphereClient.NetworkV1alpha1().IPPools().Update(context.TODO(), clone, metav1.UpdateOptions{})
D
Duan Jiong 已提交
102 103 104 105 106 107 108 109 110 111 112
	if err != nil {
		klog.V(3).Infof("Error adding  finalizer to pool %s: %v", pool.Name, err)
		return err
	}
	klog.V(3).Infof("Added finalizer to pool %s", pool.Name)
	return nil
}

func (c *IPPoolController) removeFinalizer(pool *networkv1alpha1.IPPool) error {
	clone := pool.DeepCopy()
	controllerutil.RemoveFinalizer(clone, networkv1alpha1.IPPoolFinalizer)
Z
Zack Zhang 已提交
113
	pool, err := c.kubesphereClient.NetworkV1alpha1().IPPools().Update(context.TODO(), clone, metav1.UpdateOptions{})
D
Duan Jiong 已提交
114 115 116 117 118 119 120 121
	if err != nil {
		klog.V(3).Infof("Error removing  finalizer from pool %s: %v", pool.Name, err)
		return err
	}
	klog.V(3).Infof("Removed protection finalizer from pool %s", pool.Name)
	return nil
}

D
Duan Jiong 已提交
122 123
func (c *IPPoolController) ValidateCreate(obj runtime.Object) error {
	b := obj.(*networkv1alpha1.IPPool)
D
Duan Jiong 已提交
124
	ip, cidr, err := cnet.ParseCIDR(b.Spec.CIDR)
D
Duan Jiong 已提交
125
	if err != nil {
D
Duan Jiong 已提交
126 127 128 129
		return fmt.Errorf("invalid cidr")
	}

	size, _ := cidr.Mask.Size()
D
Duan Jiong 已提交
130 131 132
	if ip.IP.To4() != nil && size == 32 {
		return fmt.Errorf("the cidr mask must be less than 32")
	}
D
Duan Jiong 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	if b.Spec.BlockSize > 0 && b.Spec.BlockSize < size {
		return fmt.Errorf("the blocksize should be larger than the cidr mask")
	}

	if b.Spec.RangeStart != "" || b.Spec.RangeEnd != "" {
		iStart := cnet.ParseIP(b.Spec.RangeStart)
		iEnd := cnet.ParseIP(b.Spec.RangeEnd)
		if iStart == nil || iEnd == nil {
			return fmt.Errorf("invalid rangeStart or rangeEnd")
		}
		offsetStart, err := b.IPToOrdinal(*iStart)
		if err != nil {
			return err
		}
		offsetEnd, err := b.IPToOrdinal(*iEnd)
		if err != nil {
			return err
		}
		if offsetEnd < offsetStart {
			return fmt.Errorf("rangeStart should not big than rangeEnd")
		}
D
Duan Jiong 已提交
154 155
	}

Z
Zack Zhang 已提交
156
	pools, err := c.kubesphereClient.NetworkV1alpha1().IPPools().List(context.TODO(), metav1.ListOptions{
D
Duan Jiong 已提交
157
		LabelSelector: labels.SelectorFromSet(labels.Set{
D
Duan Jiong 已提交
158
			networkv1alpha1.IPPoolIDLabel: fmt.Sprintf("%d", b.ID()),
D
Duan Jiong 已提交
159 160 161
		}).String(),
	})
	if err != nil {
D
Duan Jiong 已提交
162
		return err
D
Duan Jiong 已提交
163 164 165
	}

	for _, p := range pools.Items {
D
Duan Jiong 已提交
166 167
		if b.Overlapped(p) {
			return fmt.Errorf("ippool cidr is overlapped with %s", p.Name)
D
Duan Jiong 已提交
168
		}
D
Duan Jiong 已提交
169
	}
D
Duan Jiong 已提交
170

D
Duan Jiong 已提交
171 172 173
	return nil
}

D
Duan Jiong 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
func (c *IPPoolController) validateDefaultIPPool(p *networkv1alpha1.IPPool) error {
	pools, err := c.kubesphereClient.NetworkV1alpha1().IPPools().List(context.TODO(), metav1.ListOptions{
		LabelSelector: labels.SelectorFromSet(
			labels.Set{
				networkv1alpha1.IPPoolDefaultLabel: "",
			}).String(),
	})
	if err != nil {
		return err
	}

	poolLen := len(pools.Items)
	if poolLen != 1 || pools.Items[0].Name != p.Name {
		return nil
	}

	return fmt.Errorf("Must ensure that there is at least one default ippool")
}

D
Duan Jiong 已提交
193 194 195 196 197 198
func (c *IPPoolController) ValidateUpdate(old runtime.Object, new runtime.Object) error {
	oldP := old.(*networkv1alpha1.IPPool)
	newP := new.(*networkv1alpha1.IPPool)

	if newP.Spec.CIDR != oldP.Spec.CIDR {
		return fmt.Errorf("cidr cannot be modified")
D
Duan Jiong 已提交
199 200
	}

D
Duan Jiong 已提交
201 202 203 204 205 206 207 208 209 210 211 212
	if newP.Spec.Type != oldP.Spec.Type {
		return fmt.Errorf("ippool type cannot be modified")
	}

	if newP.Spec.BlockSize != oldP.Spec.BlockSize {
		return fmt.Errorf("ippool blockSize cannot be modified")
	}

	if newP.Spec.RangeEnd != oldP.Spec.RangeEnd || newP.Spec.RangeStart != oldP.Spec.RangeStart {
		return fmt.Errorf("ippool rangeEnd/rangeStart cannot be modified")
	}

D
Duan Jiong 已提交
213 214 215 216 217 218 219 220 221
	_, defaultOld := oldP.Labels[networkv1alpha1.IPPoolDefaultLabel]
	_, defaultNew := newP.Labels[networkv1alpha1.IPPoolDefaultLabel]
	if !defaultNew && defaultOld != defaultNew {
		err := c.validateDefaultIPPool(newP)
		if err != nil {
			return err
		}
	}

D
Duan Jiong 已提交
222 223 224 225 226 227 228 229 230 231
	return nil
}

func (c *IPPoolController) ValidateDelete(obj runtime.Object) error {
	p := obj.(*networkv1alpha1.IPPool)

	if p.Status.Allocations > 0 {
		return fmt.Errorf("ippool is in use, please remove the workload before deleting")
	}

D
Duan Jiong 已提交
232
	return c.validateDefaultIPPool(p)
D
Duan Jiong 已提交
233 234 235 236 237 238 239 240 241 242
}

func (c *IPPoolController) disableIPPool(old *networkv1alpha1.IPPool) error {
	if old.Spec.Disabled {
		return nil
	}

	clone := old.DeepCopy()
	clone.Spec.Disabled = true

D
Duan Jiong 已提交
243
	_, err := c.kubesphereClient.NetworkV1alpha1().IPPools().Update(context.TODO(), clone, metav1.UpdateOptions{})
D
Duan Jiong 已提交
244 245 246 247 248 249 250

	return err
}

func (c *IPPoolController) updateIPPoolStatus(old *networkv1alpha1.IPPool) error {
	new, err := c.provider.GetIPPoolStats(old)
	if err != nil {
D
Duan Jiong 已提交
251
		return fmt.Errorf("failed to get ippool %s status %v", old.Name, err)
D
Duan Jiong 已提交
252 253 254 255 256 257
	}

	if reflect.DeepEqual(old.Status, new.Status) {
		return nil
	}

Z
Zack Zhang 已提交
258
	_, err = c.kubesphereClient.NetworkV1alpha1().IPPools().UpdateStatus(context.TODO(), new, metav1.UpdateOptions{})
D
Duan Jiong 已提交
259 260 261
	if err != nil {
		return fmt.Errorf("failed to update ippool %s status  %v", old.Name, err)
	}
D
Duan Jiong 已提交
262

D
Duan Jiong 已提交
263
	return nil
D
Duan Jiong 已提交
264 265 266 267 268 269 270 271 272 273
}

func (c *IPPoolController) processIPPool(name string) (*time.Duration, error) {
	klog.V(4).Infof("Processing IPPool %s", name)
	startTime := time.Now()
	defer func() {
		klog.V(4).Infof("Finished processing IPPool %s (%v)", name, time.Since(startTime))
	}()

	pool, err := c.ippoolInformer.Lister().Get(name)
D
Duan Jiong 已提交
274 275 276 277 278 279 280 281 282
	if err != nil {
		if apierrors.IsNotFound(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("failed to get ippool %s: %v", name, err)
	}

	if pool.Type() != c.provider.Type() {
		klog.V(4).Infof("pool %s type not match, ignored", pool.Name)
D
Duan Jiong 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		return nil, nil
	}

	if utils.IsDeletionCandidate(pool, networkv1alpha1.IPPoolFinalizer) {
		err = c.disableIPPool(pool)
		if err != nil {
			return nil, err
		}

		// Pool should be deleted. Check if it's used and remove finalizer if
		// it's not.
		canDelete, err := c.provider.DeleteIPPool(pool)
		if err != nil {
			return nil, err
		}
D
Duan Jiong 已提交
298

D
Duan Jiong 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		if canDelete {
			return nil, c.removeFinalizer(pool)
		}

		//The  ippool is being used, update status and try again later.
		delay := time.Second * 3
		return &delay, c.updateIPPoolStatus(pool)
	}

	if utils.NeedToAddFinalizer(pool, networkv1alpha1.IPPoolFinalizer) {
		err = c.addFinalizer(pool)
		if err != nil {
			return nil, err
		}

		err = c.provider.CreateIPPool(pool)
		if err != nil {
			klog.V(4).Infof("Provider failed to create IPPool %s, err=%v", pool.Name, err)
			return nil, err
		}

		return nil, c.updateIPPoolStatus(pool)
	}

	err = c.provider.UpdateIPPool(pool)
	if err != nil {
		klog.V(4).Infof("Provider failed to update IPPool %s, err=%v", pool.Name, err)
		return nil, err
	}

	return nil, c.updateIPPoolStatus(pool)
}

func (c *IPPoolController) Start(stopCh <-chan struct{}) error {
	go c.provider.SyncStatus(stopCh, c.ippoolQueue)
	return c.Run(5, stopCh)
}

func (c *IPPoolController) Run(workers int, stopCh <-chan struct{}) error {
	defer utilruntime.HandleCrash()
	defer c.ippoolQueue.ShutDown()

	klog.Info("starting ippool controller")
	defer klog.Info("shutting down ippool controller")

D
Duan Jiong 已提交
344
	if !cache.WaitForCacheSync(stopCh, c.ippoolSynced, c.ipamblockSynced, c.wsSynced, c.nsSynced) {
D
Duan Jiong 已提交
345 346 347 348
		return fmt.Errorf("failed to wait for caches to sync")
	}

	for i := 0; i < workers; i++ {
D
Duan Jiong 已提交
349 350
		go wait.Until(c.runIPPoolWorker, time.Second, stopCh)
		go wait.Until(c.runNSWorker, time.Second, stopCh)
D
Duan Jiong 已提交
351 352 353 354 355 356
	}

	<-stopCh
	return nil
}

D
Duan Jiong 已提交
357
func (c *IPPoolController) runIPPoolWorker() {
D
Duan Jiong 已提交
358 359 360 361 362 363 364 365 366 367 368
	for c.processIPPoolItem() {
	}
}

func (c *IPPoolController) processIPPoolItem() bool {
	key, quit := c.ippoolQueue.Get()
	if quit {
		return false
	}
	defer c.ippoolQueue.Done(key)

D
Duan Jiong 已提交
369
	delay, err := c.processIPPool(key.(string))
D
Duan Jiong 已提交
370 371 372 373 374
	if err == nil {
		c.ippoolQueue.Forget(key)
		return true
	}

375 376 377 378 379
	if delay != nil {
		c.ippoolQueue.AddAfter(key, *delay)
	} else {
		c.ippoolQueue.AddRateLimited(key)
	}
D
Duan Jiong 已提交
380 381 382 383
	utilruntime.HandleError(fmt.Errorf("error processing ippool %v (will retry): %v", key, err))
	return true
}

D
Duan Jiong 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
func (c *IPPoolController) runNSWorker() {
	for c.processNSItem() {
	}
}

func (c *IPPoolController) processNS(name string) error {
	ns, err := c.nsInformer.Lister().Get(name)
	if apierrors.IsNotFound(err) {
		return nil
	}

	var poolsName []string
	if ns.Labels != nil && ns.Labels[constants.WorkspaceLabelKey] != "" {
		pools, err := c.ippoolInformer.Lister().List(labels.SelectorFromSet(labels.Set{
			networkv1alpha1.IPPoolDefaultLabel: "",
		}))
		if err != nil {
			return err
		}

		for _, pool := range pools {
			poolsName = append(poolsName, pool.Name)
		}
	}

	clone := ns.DeepCopy()
	err = c.provider.UpdateNamespace(clone, poolsName)
	if err != nil {
		return err
	}
	if reflect.DeepEqual(clone, ns) {
		return nil
	}

	_, err = c.client.CoreV1().Namespaces().Update(context.TODO(), clone, metav1.UpdateOptions{})
	return err
}

func (c *IPPoolController) processNSItem() bool {
	key, quit := c.nsQueue.Get()
	if quit {
		return false
	}
	defer c.nsQueue.Done(key)

	err := c.processNS(key.(string))
	if err == nil {
		c.nsQueue.Forget(key)
		return true
	}

	c.nsQueue.AddRateLimited(key)
	utilruntime.HandleError(fmt.Errorf("error processing ns %v (will retry): %v", key, err))
	return true
}

func (c *IPPoolController) enqueueIPAMBlocks(obj interface{}) {
D
Duan Jiong 已提交
441 442 443 444 445 446 447 448 449
	block, ok := obj.(*networkv1alpha1.IPAMBlock)
	if !ok {
		return
	}

	poolName := block.Labels[networkv1alpha1.IPPoolNameLabel]
	c.ippoolQueue.Add(poolName)
}

D
Duan Jiong 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
func (c *IPPoolController) enqueueWorkspace(obj interface{}) {
	wk, ok := obj.(*tenantv1alpha1.Workspace)
	if !ok {
		return
	}

	pools, err := c.ippoolInformer.Lister().List(labels.SelectorFromSet(labels.Set{
		constants.WorkspaceLabelKey: wk.Name,
	}))
	if err != nil {
		klog.Errorf("failed to list ippools by worksapce %s, err=%v", wk.Name, err)
	}

	for _, pool := range pools {
		c.ippoolQueue.Add(pool.Name)
	}
}

func (c *IPPoolController) enqueueNamespace(old interface{}, new interface{}) {
	workspaceOld := ""
	if old != nil {
		nsOld := old.(*corev1.Namespace)
		if nsOld.Labels != nil {
			workspaceOld = nsOld.Labels[constants.WorkspaceLabelKey]
		}
	}

	nsNew := new.(*corev1.Namespace)
	workspaceNew := ""
	if nsNew.Labels != nil {
		workspaceNew = nsNew.Labels[constants.WorkspaceLabelKey]
	}

	if workspaceOld != workspaceNew {
		c.nsQueue.Add(nsNew.Name)
	}
}

D
Duan Jiong 已提交
488
func NewIPPoolController(
D
Duan Jiong 已提交
489 490
	kubesphereInformers ksinformers.SharedInformerFactory,
	kubernetesInformers k8sinformers.SharedInformerFactory,
D
Duan Jiong 已提交
491 492 493 494 495 496 497 498
	client clientset.Interface,
	kubesphereClient kubesphereclient.Interface,
	provider ippool.Provider) *IPPoolController {

	broadcaster := record.NewBroadcaster()
	broadcaster.StartLogging(func(format string, args ...interface{}) {
		klog.Info(fmt.Sprintf(format, args))
	})
D
Duan Jiong 已提交
499 500
	broadcaster.StartRecordingToSink(&clientcorev1.EventSinkImpl{Interface: client.CoreV1().Events("")})
	recorder := broadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "ippool-controller"})
D
Duan Jiong 已提交
501 502

	c := &IPPoolController{
D
Duan Jiong 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
		eventBroadcaster: broadcaster,
		eventRecorder:    recorder,
		ippoolQueue:      workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ippool"),
		nsQueue:          workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ippool-ns"),
		client:           client,
		kubesphereClient: kubesphereClient,
		provider:         provider,
	}
	c.ippoolInformer = kubesphereInformers.Network().V1alpha1().IPPools()
	c.ippoolSynced = c.ippoolInformer.Informer().HasSynced
	c.ipamblockInformer = kubesphereInformers.Network().V1alpha1().IPAMBlocks()
	c.ipamblockSynced = c.ipamblockInformer.Informer().HasSynced
	c.wsInformer = kubesphereInformers.Tenant().V1alpha1().Workspaces()
	c.wsSynced = c.wsInformer.Informer().HasSynced
	c.nsInformer = kubernetesInformers.Core().V1().Namespaces()
	c.nsSynced = c.nsInformer.Informer().HasSynced

	c.ippoolInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: c.enqueueIPPools,
D
Duan Jiong 已提交
522
		UpdateFunc: func(old, new interface{}) {
D
Duan Jiong 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535
			_, defaultOld := old.(*networkv1alpha1.IPPool).Labels[networkv1alpha1.IPPoolDefaultLabel]
			_, defaultNew := new.(*networkv1alpha1.IPPool).Labels[networkv1alpha1.IPPoolDefaultLabel]
			if defaultOld != defaultNew {
				nss, err := c.nsInformer.Lister().List(labels.Everything())
				if err != nil {
					return
				}

				for _, ns := range nss {
					c.enqueueNamespace(nil, ns)
				}
			}
			c.enqueueIPPools(new)
D
Duan Jiong 已提交
536 537 538 539
		},
	})

	//just for update ippool status
D
Duan Jiong 已提交
540 541
	c.ipamblockInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: c.enqueueIPAMBlocks,
D
Duan Jiong 已提交
542
		UpdateFunc: func(old, new interface{}) {
D
Duan Jiong 已提交
543 544 545 546 547 548 549 550 551 552 553 554
			c.enqueueIPAMBlocks(new)
		},
		DeleteFunc: c.enqueueIPAMBlocks,
	})

	c.wsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
		DeleteFunc: c.enqueueWorkspace,
	})

	c.nsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(new interface{}) {
			c.enqueueNamespace(nil, new)
D
Duan Jiong 已提交
555
		},
D
Duan Jiong 已提交
556
		UpdateFunc: c.enqueueNamespace,
D
Duan Jiong 已提交
557 558
	})

D
Duan Jiong 已提交
559 560 561
	//register ippool webhook
	webhooks.RegisterValidator(networkv1alpha1.SchemeGroupVersion.WithKind(networkv1alpha1.ResourceKindIPPool).String(),
		&webhooks.ValidatorWrap{Obj: &networkv1alpha1.IPPool{}, Helper: c})
D
Duan Jiong 已提交
562 563
	webhooks.RegisterDefaulter(corev1.SchemeGroupVersion.WithKind("Pod").String(),
		&webhooks.DefaulterWrap{Obj: &corev1.Pod{}, Helper: provider})
D
Duan Jiong 已提交
564

D
Duan Jiong 已提交
565 566
	return c
}