“684d6b360222f31b6b9be9a63aa5c6ed5674c890”上不存在“git@gitcode.net:openanolis/cloud-kernel.git”
provider.go 3.8 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 The 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 (
D
Duan Jiong 已提交
20
	corev1 "k8s.io/api/core/v1"
D
Duan Jiong 已提交
21
	"k8s.io/apimachinery/pkg/runtime"
D
Duan Jiong 已提交
22
	k8sinformers "k8s.io/client-go/informers"
D
Duan Jiong 已提交
23
	clientset "k8s.io/client-go/kubernetes"
D
Duan Jiong 已提交
24 25 26
	"k8s.io/client-go/util/workqueue"
	networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
	kubesphereclient "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
D
Duan Jiong 已提交
27 28
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
	calicoclient "kubesphere.io/kubesphere/pkg/simple/client/network/ippool/calico"
D
Duan Jiong 已提交
29 30 31 32 33 34 35 36 37 38
	"kubesphere.io/kubesphere/pkg/simple/client/network/ippool/ipam"
)

type Provider interface {
	// canDelete indicates whether the address pool is being used or not.
	DeleteIPPool(pool *networkv1alpha1.IPPool) (canDelete bool, err error)
	CreateIPPool(pool *networkv1alpha1.IPPool) error
	UpdateIPPool(pool *networkv1alpha1.IPPool) error
	GetIPPoolStats(pool *networkv1alpha1.IPPool) (*networkv1alpha1.IPPool, error)
	SyncStatus(stopCh <-chan struct{}, q workqueue.RateLimitingInterface) error
D
Duan Jiong 已提交
39
	UpdateNamespace(ns *corev1.Namespace, pools []string) error
D
Duan Jiong 已提交
40 41
	Type() string
	Default(obj runtime.Object) error
D
Duan Jiong 已提交
42 43 44 45 46 47 48
}

type provider struct {
	kubesphereClient kubesphereclient.Interface
	ipamclient       ipam.IPAMClient
}

D
Duan Jiong 已提交
49 50 51 52 53 54 55 56
func (p provider) Type() string {
	return networkv1alpha1.IPPoolTypeLocal
}

func (p provider) Default(obj runtime.Object) error {
	return nil
}

D
Duan Jiong 已提交
57 58 59 60
func (p provider) UpdateNamespace(ns *corev1.Namespace, pools []string) error {
	return nil
}

D
Duan Jiong 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
func (p provider) DeleteIPPool(pool *networkv1alpha1.IPPool) (bool, error) {
	blocks, err := p.ipamclient.ListBlocks(pool.Name)
	if err != nil {
		return false, err
	}

	for _, block := range blocks {
		if block.Empty() {
			if err = p.ipamclient.DeleteBlock(&block); err != nil {
				return false, err
			}
		} else {
			return false, nil
		}
	}

	return true, nil
}

func (p provider) CreateIPPool(pool *networkv1alpha1.IPPool) error {
	return nil
}

func (p provider) UpdateIPPool(pool *networkv1alpha1.IPPool) error {
	return nil
}

func (p provider) SyncStatus(stopCh <-chan struct{}, q workqueue.RateLimitingInterface) error {
	return nil
}

func (p provider) GetIPPoolStats(pool *networkv1alpha1.IPPool) (*networkv1alpha1.IPPool, error) {
	stats, err := p.ipamclient.GetUtilization(ipam.GetUtilizationArgs{
		Pools: []string{pool.Name},
	})
	if err != nil {
		return nil, err
	}

	stat := stats[0]
D
Duan Jiong 已提交
101 102 103 104 105 106 107 108 109
	clone := pool.DeepCopy()
	clone.Status = networkv1alpha1.IPPoolStatus{
		Allocations: stat.Allocate,
		Unallocated: stat.Unallocated,
		Reserved:    stat.Reserved,
		Capacity:    stat.Capacity,
		Synced:      true,
	}
	return clone, nil
D
Duan Jiong 已提交
110 111
}

D
Duan Jiong 已提交
112 113
func newProvider(clientset kubesphereclient.Interface) provider {
	return provider{
D
Duan Jiong 已提交
114 115 116
		kubesphereClient: clientset,
		ipamclient:       ipam.NewIPAMClient(clientset, networkv1alpha1.VLAN),
	}
D
Duan Jiong 已提交
117 118
}

D
Duan Jiong 已提交
119
func NewProvider(k8sInformer k8sinformers.SharedInformerFactory, clientset kubesphereclient.Interface, client clientset.Interface, pt string, k8sOptions *k8s.KubernetesOptions) Provider {
D
Duan Jiong 已提交
120 121 122 123 124 125 126 127 128
	var p Provider

	switch pt {
	case networkv1alpha1.IPPoolTypeLocal:
		p = provider{
			kubesphereClient: clientset,
			ipamclient:       ipam.NewIPAMClient(clientset, networkv1alpha1.VLAN),
		}
	case networkv1alpha1.IPPoolTypeCalico:
D
Duan Jiong 已提交
129
		p = calicoclient.NewProvider(k8sInformer, clientset, client, k8sOptions)
D
Duan Jiong 已提交
130
	}
D
Duan Jiong 已提交
131

D
Duan Jiong 已提交
132
	return p
D
Duan Jiong 已提交
133
}