Files
kubesphere/pkg/simple/client/network/ippool/provider.go
Duan Jiong 43d1d6f243 implement ippool
1. support vlan ippool management
2. support calico ippool management

Signed-off-by: Duan Jiong <djduanjiong@gmail.com>
2020-10-29 09:59:27 +08:00

99 lines
2.7 KiB
Go

/*
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 (
"k8s.io/client-go/util/workqueue"
networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
kubesphereclient "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
"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
}
type provider struct {
kubesphereClient kubesphereclient.Interface
ipamclient ipam.IPAMClient
}
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]
return &networkv1alpha1.IPPool{
Status: networkv1alpha1.IPPoolStatus{
Allocations: stat.Allocate,
Unallocated: stat.Unallocated,
Reserved: stat.Reserved,
Capacity: stat.Capacity,
Synced: true,
},
}, nil
}
func NewProvider(clientset kubesphereclient.Interface, options Options) provider {
vlanProvider := provider{
kubesphereClient: clientset,
ipamclient: ipam.NewIPAMClient(clientset, networkv1alpha1.VLAN),
}
return vlanProvider
}