add ippool resource api
add ippool webhook and fix some bugs Signed-off-by: Duan Jiong <djduanjiong@gmail.com>
This commit is contained in:
@@ -22,10 +22,12 @@ import (
|
||||
"time"
|
||||
|
||||
cnet "github.com/projectcalico/libcalico-go/lib/net"
|
||||
podv1 "k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
@@ -39,6 +41,7 @@ import (
|
||||
kubesphereclient "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
networkInformer "kubesphere.io/kubesphere/pkg/client/informers/externalversions/network/v1alpha1"
|
||||
"kubesphere.io/kubesphere/pkg/controller/network/utils"
|
||||
"kubesphere.io/kubesphere/pkg/controller/network/webhooks"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/network/ippool"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
)
|
||||
@@ -62,8 +65,6 @@ type IPPoolController struct {
|
||||
|
||||
client clientset.Interface
|
||||
kubesphereClient kubesphereclient.Interface
|
||||
|
||||
options ippool.Options
|
||||
}
|
||||
|
||||
func (c *IPPoolController) ippoolHandle(obj interface{}) {
|
||||
@@ -112,35 +113,86 @@ func (c *IPPoolController) removeFinalizer(pool *networkv1alpha1.IPPool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// check cidr overlap
|
||||
func (c *IPPoolController) checkIPPool(pool *networkv1alpha1.IPPool) (bool, error) {
|
||||
_, poolCIDR, err := cnet.ParseCIDR(pool.Spec.CIDR)
|
||||
func (c *IPPoolController) ValidateCreate(obj runtime.Object) error {
|
||||
b := obj.(*networkv1alpha1.IPPool)
|
||||
_, cidr, err := cnet.ParseCIDR(b.Spec.CIDR)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return fmt.Errorf("invalid cidr")
|
||||
}
|
||||
|
||||
size, _ := cidr.Mask.Size()
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
pools, err := c.kubesphereClient.NetworkV1alpha1().IPPools().List(metav1.ListOptions{
|
||||
LabelSelector: labels.SelectorFromSet(labels.Set{
|
||||
networkv1alpha1.IPPoolIDLabel: fmt.Sprintf("%d", pool.ID()),
|
||||
networkv1alpha1.IPPoolIDLabel: fmt.Sprintf("%d", b.ID()),
|
||||
}).String(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
return err
|
||||
}
|
||||
|
||||
for _, p := range pools.Items {
|
||||
_, cidr, err := cnet.ParseCIDR(p.Spec.CIDR)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if cidr.IsNetOverlap(poolCIDR.IPNet) {
|
||||
return false, ErrCIDROverlap
|
||||
if b.Overlapped(p) {
|
||||
return fmt.Errorf("ippool cidr is overlapped with %s", p.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *IPPoolController) disableIPPool(old *networkv1alpha1.IPPool) error {
|
||||
@@ -159,18 +211,19 @@ func (c *IPPoolController) disableIPPool(old *networkv1alpha1.IPPool) error {
|
||||
func (c *IPPoolController) updateIPPoolStatus(old *networkv1alpha1.IPPool) error {
|
||||
new, err := c.provider.GetIPPoolStats(old)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to get ippool %s status %v", old.Name, err)
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(old.Status, new.Status) {
|
||||
return nil
|
||||
}
|
||||
|
||||
clone := old.DeepCopy()
|
||||
clone.Status = new.Status
|
||||
old, err = c.kubesphereClient.NetworkV1alpha1().IPPools().Update(clone)
|
||||
_, err = c.kubesphereClient.NetworkV1alpha1().IPPools().UpdateStatus(new)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update ippool %s status %v", old.Name, err)
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *IPPoolController) processIPPool(name string) (*time.Duration, error) {
|
||||
@@ -181,10 +234,16 @@ func (c *IPPoolController) processIPPool(name string) (*time.Duration, error) {
|
||||
}()
|
||||
|
||||
pool, err := c.ippoolInformer.Lister().Get(name)
|
||||
if apierrors.IsNotFound(err) {
|
||||
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)
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if utils.IsDeletionCandidate(pool, networkv1alpha1.IPPoolFinalizer) {
|
||||
@@ -199,6 +258,7 @@ func (c *IPPoolController) processIPPool(name string) (*time.Duration, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if canDelete {
|
||||
return nil, c.removeFinalizer(pool)
|
||||
}
|
||||
@@ -209,14 +269,6 @@ func (c *IPPoolController) processIPPool(name string) (*time.Duration, error) {
|
||||
}
|
||||
|
||||
if utils.NeedToAddFinalizer(pool, networkv1alpha1.IPPoolFinalizer) {
|
||||
valid, err := c.checkIPPool(pool)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
err = c.addFinalizer(pool)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -310,7 +362,6 @@ func NewIPPoolController(
|
||||
ipamblockInformer networkInformer.IPAMBlockInformer,
|
||||
client clientset.Interface,
|
||||
kubesphereClient kubesphereclient.Interface,
|
||||
options ippool.Options,
|
||||
provider ippool.Provider) *IPPoolController {
|
||||
|
||||
broadcaster := record.NewBroadcaster()
|
||||
@@ -318,7 +369,7 @@ func NewIPPoolController(
|
||||
klog.Info(fmt.Sprintf(format, args))
|
||||
})
|
||||
broadcaster.StartRecordingToSink(&corev1.EventSinkImpl{Interface: client.CoreV1().Events("")})
|
||||
recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cluster-controller"})
|
||||
recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "ippool-controller"})
|
||||
|
||||
c := &IPPoolController{
|
||||
eventBroadcaster: broadcaster,
|
||||
@@ -330,7 +381,6 @@ func NewIPPoolController(
|
||||
ipamblockSynced: ipamblockInformer.Informer().HasSynced,
|
||||
client: client,
|
||||
kubesphereClient: kubesphereClient,
|
||||
options: options,
|
||||
provider: provider,
|
||||
}
|
||||
|
||||
@@ -350,5 +400,11 @@ func NewIPPoolController(
|
||||
DeleteFunc: c.ipamblockHandle,
|
||||
})
|
||||
|
||||
//register ippool webhook
|
||||
webhooks.RegisterValidator(networkv1alpha1.SchemeGroupVersion.WithKind(networkv1alpha1.ResourceKindIPPool).String(),
|
||||
&webhooks.ValidatorWrap{Obj: &networkv1alpha1.IPPool{}, Helper: c})
|
||||
webhooks.RegisterDefaulter(podv1.SchemeGroupVersion.WithKind("Pod").String(),
|
||||
&webhooks.DefaulterWrap{Obj: &podv1.Pod{}, Helper: provider})
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ package ippool
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -29,7 +32,6 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/controller/network/utils"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/network/ippool"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/network/ippool/ipam"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIPPoolSuit(t *testing.T) {
|
||||
@@ -49,31 +51,52 @@ var _ = Describe("test ippool", func() {
|
||||
Name: "testippool",
|
||||
},
|
||||
Spec: v1alpha1.IPPoolSpec{
|
||||
Type: v1alpha1.VLAN,
|
||||
CIDR: "192.168.0.0/24",
|
||||
BlockSize: 24,
|
||||
Type: v1alpha1.VLAN,
|
||||
CIDR: "192.168.0.0/24",
|
||||
},
|
||||
Status: v1alpha1.IPPoolStatus{},
|
||||
}
|
||||
|
||||
ksclient := ksfake.NewSimpleClientset()
|
||||
k8sclinet := k8sfake.NewSimpleClientset()
|
||||
options := ippool.Options{}
|
||||
p := ippool.NewProvider(ksclient, options)
|
||||
p := ippool.NewProvider(nil, ksclient, k8sclinet, v1alpha1.IPPoolTypeLocal, nil)
|
||||
ipamClient := ipam.NewIPAMClient(ksclient, v1alpha1.VLAN)
|
||||
|
||||
ksInformer := ksinformers.NewSharedInformerFactory(ksclient, 0)
|
||||
ippoolInformer := ksInformer.Network().V1alpha1().IPPools()
|
||||
ipamblockInformer := ksInformer.Network().V1alpha1().IPAMBlocks()
|
||||
c := NewIPPoolController(ippoolInformer, ipamblockInformer, k8sclinet, ksclient, options, p)
|
||||
c := NewIPPoolController(ippoolInformer, ipamblockInformer, k8sclinet, ksclient, p)
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
go ksInformer.Start(stopCh)
|
||||
go c.Start(stopCh)
|
||||
|
||||
It("test create ippool", func() {
|
||||
_, err := ksclient.NetworkV1alpha1().IPPools().Create(pool)
|
||||
clone := pool.DeepCopy()
|
||||
clone.Spec.CIDR = "testxxx"
|
||||
Expect(c.ValidateCreate(clone)).Should(HaveOccurred())
|
||||
|
||||
clone = pool.DeepCopy()
|
||||
clone.Spec.CIDR = "192.168.0.0/24"
|
||||
clone.Spec.RangeStart = "192.168.0.100"
|
||||
clone.Spec.RangeEnd = "192.168.0.99"
|
||||
Expect(c.ValidateCreate(clone)).Should(HaveOccurred())
|
||||
|
||||
clone = pool.DeepCopy()
|
||||
clone.Spec.CIDR = "192.168.0.0/24"
|
||||
clone.Spec.RangeStart = "192.168.3.100"
|
||||
clone.Spec.RangeEnd = "192.168.3.111"
|
||||
Expect(c.ValidateCreate(clone)).Should(HaveOccurred())
|
||||
|
||||
clone = pool.DeepCopy()
|
||||
clone.Spec.CIDR = "192.168.0.0/24"
|
||||
clone.Spec.BlockSize = 23
|
||||
Expect(c.ValidateCreate(clone)).Should(HaveOccurred())
|
||||
|
||||
clone = pool.DeepCopy()
|
||||
_, err := ksclient.NetworkV1alpha1().IPPools().Create(clone)
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
|
||||
Eventually(func() bool {
|
||||
result, _ := ksclient.NetworkV1alpha1().IPPools().Get(pool.Name, v1.GetOptions{})
|
||||
if len(result.Labels) != 3 {
|
||||
@@ -85,7 +108,17 @@ var _ = Describe("test ippool", func() {
|
||||
}
|
||||
|
||||
return true
|
||||
}).Should(Equal(true))
|
||||
}, 3*time.Second).Should(Equal(true))
|
||||
|
||||
clone = pool.DeepCopy()
|
||||
Expect(c.ValidateCreate(clone)).Should(HaveOccurred())
|
||||
})
|
||||
|
||||
It("test update ippool", func() {
|
||||
old, _ := ksclient.NetworkV1alpha1().IPPools().Get(pool.Name, v1.GetOptions{})
|
||||
new := old.DeepCopy()
|
||||
new.Spec.CIDR = "192.168.1.0/24"
|
||||
Expect(c.ValidateUpdate(old, new)).Should(HaveOccurred())
|
||||
})
|
||||
|
||||
It("test ippool stats", func() {
|
||||
@@ -102,10 +135,13 @@ var _ = Describe("test ippool", func() {
|
||||
}
|
||||
|
||||
return true
|
||||
}).Should(Equal(true))
|
||||
}, 3*time.Second).Should(Equal(true))
|
||||
})
|
||||
|
||||
It("test delete pool", func() {
|
||||
result, _ := ksclient.NetworkV1alpha1().IPPools().Get(pool.Name, v1.GetOptions{})
|
||||
Expect(c.ValidateDelete(result)).Should(HaveOccurred())
|
||||
|
||||
ipamClient.ReleaseByHandle("testhandle")
|
||||
Eventually(func() bool {
|
||||
result, _ := ksclient.NetworkV1alpha1().IPPools().Get(pool.Name, v1.GetOptions{})
|
||||
@@ -114,7 +150,7 @@ var _ = Describe("test ippool", func() {
|
||||
}
|
||||
|
||||
return true
|
||||
}).Should(Equal(true))
|
||||
}, 3*time.Second).Should(Equal(true))
|
||||
|
||||
err := ksclient.NetworkV1alpha1().IPPools().Delete(pool.Name, &v1.DeleteOptions{})
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
|
||||
@@ -18,7 +18,6 @@ package nsnetworkpolicy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kubesphere.io/kubesphere/pkg/controller/network/types"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -62,7 +61,7 @@ const (
|
||||
|
||||
NodeNSNPAnnotationKey = "kubesphere.io/snat-node-ips"
|
||||
|
||||
AnnotationNPNAME = types.NSNPPrefix + "network-isolate"
|
||||
AnnotationNPNAME = v1alpha1.NSNPPrefix + "network-isolate"
|
||||
|
||||
//TODO: configure it
|
||||
DNSLocalIP = "169.254.25.10"
|
||||
@@ -222,7 +221,7 @@ func (c *NSNetworkPolicyController) convertPeer(peer v1alpha1.NetworkPolicyPeer,
|
||||
func (c *NSNetworkPolicyController) convertToK8sNP(n *v1alpha1.NamespaceNetworkPolicy) (*netv1.NetworkPolicy, error) {
|
||||
np := &netv1.NetworkPolicy{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: types.NSNPPrefix + n.Name,
|
||||
Name: v1alpha1.NSNPPrefix + n.Name,
|
||||
Namespace: n.Namespace,
|
||||
},
|
||||
Spec: netv1.NetworkPolicySpec{
|
||||
@@ -564,7 +563,7 @@ func (c *NSNetworkPolicyController) syncNSNP(key string) error {
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
klog.V(4).Infof("NSNP %v has been deleted", key)
|
||||
c.provider.Delete(c.provider.GetKey(types.NSNPPrefix+name, namespace))
|
||||
c.provider.Delete(c.provider.GetKey(v1alpha1.NSNPPrefix+name, namespace))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ package provider
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kubesphere.io/kubesphere/pkg/controller/network/types"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -36,6 +35,7 @@ import (
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -246,7 +246,7 @@ func NewNsNetworkPolicyProvider(client kubernetes.Interface, npInformer informer
|
||||
// Filter in only objects that are written by policy controller.
|
||||
m := make(map[string]interface{})
|
||||
for _, policy := range policies {
|
||||
if strings.HasPrefix(policy.Name, types.NSNPPrefix) {
|
||||
if strings.HasPrefix(policy.Name, v1alpha1.NSNPPrefix) {
|
||||
policy.ObjectMeta = metav1.ObjectMeta{Name: policy.Name, Namespace: policy.Namespace}
|
||||
k := c.GetKey(policy.Name, policy.Namespace)
|
||||
m[k] = *policy
|
||||
|
||||
@@ -1,190 +0,0 @@
|
||||
/*
|
||||
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 nsnetworkpolicy
|
||||
|
||||
import (
|
||||
"context"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
k8snet "k8s.io/api/networking/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
|
||||
"net"
|
||||
"net/http"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
||||
)
|
||||
|
||||
type NSNPValidator struct {
|
||||
Client client.Client
|
||||
decoder *admission.Decoder
|
||||
}
|
||||
|
||||
func (v *NSNPValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
|
||||
nsnp := &networkv1alpha1.NamespaceNetworkPolicy{}
|
||||
|
||||
err := v.decoder.Decode(req, nsnp)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
allErrs := field.ErrorList{}
|
||||
allErrs = append(allErrs, v.ValidateNSNPSpec(&nsnp.Spec, field.NewPath("spec"))...)
|
||||
|
||||
if len(allErrs) != 0 {
|
||||
return admission.Denied(allErrs.ToAggregate().Error())
|
||||
}
|
||||
|
||||
return admission.Allowed("")
|
||||
}
|
||||
|
||||
func (v *NSNPValidator) InjectDecoder(d *admission.Decoder) error {
|
||||
v.decoder = d
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateNetworkPolicyPort validates a NetworkPolicyPort
|
||||
func (v *NSNPValidator) ValidateNetworkPolicyPort(port *k8snet.NetworkPolicyPort, portPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
if port.Protocol != nil && *port.Protocol != corev1.ProtocolTCP && *port.Protocol != corev1.ProtocolUDP && *port.Protocol != corev1.ProtocolSCTP {
|
||||
allErrs = append(allErrs, field.NotSupported(portPath.Child("protocol"), *port.Protocol, []string{string(corev1.ProtocolTCP), string(corev1.ProtocolUDP), string(corev1.ProtocolSCTP)}))
|
||||
}
|
||||
if port.Port != nil {
|
||||
if port.Port.Type == intstr.Int {
|
||||
for _, msg := range validation.IsValidPortNum(int(port.Port.IntVal)) {
|
||||
allErrs = append(allErrs, field.Invalid(portPath.Child("port"), port.Port.IntVal, msg))
|
||||
}
|
||||
} else {
|
||||
for _, msg := range validation.IsValidPortName(port.Port.StrVal) {
|
||||
allErrs = append(allErrs, field.Invalid(portPath.Child("port"), port.Port.StrVal, msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func (v *NSNPValidator) ValidateServiceSelector(serviceSelector *networkv1alpha1.ServiceSelector, fldPath *field.Path) field.ErrorList {
|
||||
service := &corev1.Service{}
|
||||
allErrs := field.ErrorList{}
|
||||
|
||||
err := v.Client.Get(context.TODO(), client.ObjectKey{Namespace: serviceSelector.Namespace, Name: serviceSelector.Name}, service)
|
||||
if err != nil {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, serviceSelector, "cannot get service"))
|
||||
return allErrs
|
||||
}
|
||||
|
||||
if len(service.Spec.Selector) <= 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, serviceSelector, "service should have selector"))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// ValidateCIDR validates whether a CIDR matches the conventions expected by net.ParseCIDR
|
||||
func ValidateCIDR(cidr string) (*net.IPNet, error) {
|
||||
_, net, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return net, nil
|
||||
}
|
||||
|
||||
// ValidateIPBlock validates a cidr and the except fields of an IpBlock NetworkPolicyPeer
|
||||
func (v *NSNPValidator) ValidateIPBlock(ipb *k8snet.IPBlock, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
if len(ipb.CIDR) == 0 || ipb.CIDR == "" {
|
||||
allErrs = append(allErrs, field.Required(fldPath.Child("cidr"), ""))
|
||||
return allErrs
|
||||
}
|
||||
cidrIPNet, err := ValidateCIDR(ipb.CIDR)
|
||||
if err != nil {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("cidr"), ipb.CIDR, "not a valid CIDR"))
|
||||
return allErrs
|
||||
}
|
||||
exceptCIDR := ipb.Except
|
||||
for i, exceptIP := range exceptCIDR {
|
||||
exceptPath := fldPath.Child("except").Index(i)
|
||||
exceptCIDR, err := ValidateCIDR(exceptIP)
|
||||
if err != nil {
|
||||
allErrs = append(allErrs, field.Invalid(exceptPath, exceptIP, "not a valid CIDR"))
|
||||
return allErrs
|
||||
}
|
||||
if !cidrIPNet.Contains(exceptCIDR.IP) {
|
||||
allErrs = append(allErrs, field.Invalid(exceptPath, exceptCIDR.IP, "not within CIDR range"))
|
||||
}
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// ValidateNSNPPeer validates a NetworkPolicyPeer
|
||||
func (v *NSNPValidator) ValidateNSNPPeer(peer *networkv1alpha1.NetworkPolicyPeer, peerPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
numPeers := 0
|
||||
|
||||
if peer.ServiceSelector != nil {
|
||||
numPeers++
|
||||
allErrs = append(allErrs, v.ValidateServiceSelector(peer.ServiceSelector, peerPath.Child("service"))...)
|
||||
}
|
||||
if peer.NamespaceSelector != nil {
|
||||
numPeers++
|
||||
}
|
||||
if peer.IPBlock != nil {
|
||||
numPeers++
|
||||
allErrs = append(allErrs, v.ValidateIPBlock(peer.IPBlock, peerPath.Child("ipBlock"))...)
|
||||
}
|
||||
|
||||
if numPeers == 0 {
|
||||
allErrs = append(allErrs, field.Required(peerPath, "must specify a peer"))
|
||||
} else if numPeers > 1 && peer.IPBlock != nil {
|
||||
allErrs = append(allErrs, field.Forbidden(peerPath, "may not specify both ipBlock and another peer"))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func (v *NSNPValidator) ValidateNSNPSpec(spec *networkv1alpha1.NamespaceNetworkPolicySpec, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
|
||||
// Validate ingress rules.
|
||||
for i, ingress := range spec.Ingress {
|
||||
ingressPath := fldPath.Child("ingress").Index(i)
|
||||
for i, port := range ingress.Ports {
|
||||
portPath := ingressPath.Child("ports").Index(i)
|
||||
allErrs = append(allErrs, v.ValidateNetworkPolicyPort(&port, portPath)...)
|
||||
}
|
||||
for i, from := range ingress.From {
|
||||
fromPath := ingressPath.Child("from").Index(i)
|
||||
allErrs = append(allErrs, v.ValidateNSNPPeer(&from, fromPath)...)
|
||||
}
|
||||
}
|
||||
// Validate egress rules
|
||||
for i, egress := range spec.Egress {
|
||||
egressPath := fldPath.Child("egress").Index(i)
|
||||
for i, port := range egress.Ports {
|
||||
portPath := egressPath.Child("ports").Index(i)
|
||||
allErrs = append(allErrs, v.ValidateNetworkPolicyPort(&port, portPath)...)
|
||||
}
|
||||
for i, to := range egress.To {
|
||||
toPath := egressPath.Child("to").Index(i)
|
||||
allErrs = append(allErrs, v.ValidateNSNPPeer(&to, toPath)...)
|
||||
}
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
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 types
|
||||
|
||||
const (
|
||||
NSNPPrefix = "nsnp-"
|
||||
)
|
||||
97
pkg/controller/network/webhooks/default.go
Normal file
97
pkg/controller/network/webhooks/default.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package webhooks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
||||
)
|
||||
|
||||
// Defaulter defines functions for setting defaults on resources
|
||||
type Defaulter interface {
|
||||
Default(obj runtime.Object) error
|
||||
}
|
||||
|
||||
type DefaulterWrap struct {
|
||||
Obj runtime.Object
|
||||
Helper Defaulter
|
||||
}
|
||||
|
||||
type MutatingHandler struct {
|
||||
C client.Client
|
||||
decoder *admission.Decoder
|
||||
}
|
||||
|
||||
var _ admission.DecoderInjector = &MutatingHandler{}
|
||||
|
||||
// InjectDecoder injects the decoder into a MutatingHandler.
|
||||
func (h *MutatingHandler) InjectDecoder(d *admission.Decoder) error {
|
||||
h.decoder = d
|
||||
return nil
|
||||
}
|
||||
|
||||
type defaulters struct {
|
||||
ds map[string]*DefaulterWrap
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
var (
|
||||
ds defaulters
|
||||
)
|
||||
|
||||
func init() {
|
||||
ds = defaulters{
|
||||
ds: make(map[string]*DefaulterWrap),
|
||||
lock: sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterDefaulter(name string, d *DefaulterWrap) {
|
||||
ds.lock.Lock()
|
||||
defer ds.lock.Unlock()
|
||||
|
||||
ds.ds[name] = d
|
||||
}
|
||||
|
||||
func UnRegisterDefaulter(name string) {
|
||||
ds.lock.Lock()
|
||||
defer ds.lock.Unlock()
|
||||
|
||||
delete(ds.ds, name)
|
||||
}
|
||||
|
||||
func GetDefaulter(name string) *DefaulterWrap {
|
||||
ds.lock.Lock()
|
||||
defer ds.lock.Unlock()
|
||||
|
||||
return ds.ds[name]
|
||||
}
|
||||
|
||||
// Handle handles admission requests.
|
||||
func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
|
||||
defaulter := GetDefaulter(req.Kind.String())
|
||||
if defaulter == nil {
|
||||
return admission.Denied("crd has webhook configured, but the controller does not register the corresponding processing logic and refuses the operation by default.")
|
||||
}
|
||||
|
||||
// Get the object in the request
|
||||
obj := defaulter.Obj.DeepCopyObject()
|
||||
err := h.decoder.Decode(req, obj)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
// Default the object
|
||||
defaulter.Helper.Default(obj)
|
||||
marshalled, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
// Create the patch
|
||||
return admission.PatchResponseFromRaw(req.Object.Raw, marshalled)
|
||||
}
|
||||
146
pkg/controller/network/webhooks/validator.go
Normal file
146
pkg/controller/network/webhooks/validator.go
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
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 webhooks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"k8s.io/api/admission/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
||||
)
|
||||
|
||||
// Validator defines functions for validating an operation
|
||||
type Validator interface {
|
||||
ValidateCreate(obj runtime.Object) error
|
||||
ValidateUpdate(old runtime.Object, new runtime.Object) error
|
||||
ValidateDelete(obj runtime.Object) error
|
||||
}
|
||||
|
||||
type ValidatorWrap struct {
|
||||
Obj runtime.Object
|
||||
Helper Validator
|
||||
}
|
||||
|
||||
type validators struct {
|
||||
vs map[string]*ValidatorWrap
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
var (
|
||||
vs validators
|
||||
)
|
||||
|
||||
func init() {
|
||||
vs = validators{
|
||||
vs: make(map[string]*ValidatorWrap),
|
||||
lock: sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterValidator(name string, v *ValidatorWrap) {
|
||||
vs.lock.Lock()
|
||||
defer vs.lock.Unlock()
|
||||
|
||||
vs.vs[name] = v
|
||||
}
|
||||
|
||||
func UnRegisterValidator(name string) {
|
||||
vs.lock.Lock()
|
||||
defer vs.lock.Unlock()
|
||||
|
||||
delete(vs.vs, name)
|
||||
}
|
||||
|
||||
func GetValidator(name string) *ValidatorWrap {
|
||||
vs.lock.Lock()
|
||||
defer vs.lock.Unlock()
|
||||
|
||||
return vs.vs[name]
|
||||
}
|
||||
|
||||
type ValidatingHandler struct {
|
||||
C client.Client
|
||||
decoder *admission.Decoder
|
||||
}
|
||||
|
||||
var _ admission.DecoderInjector = &ValidatingHandler{}
|
||||
|
||||
// InjectDecoder injects the decoder into a ValidatingHandler.
|
||||
func (h *ValidatingHandler) InjectDecoder(d *admission.Decoder) error {
|
||||
h.decoder = d
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle handles admission requests.
|
||||
func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
|
||||
validator := GetValidator(req.Kind.String())
|
||||
if validator == nil {
|
||||
return admission.Denied("crd has webhook configured, but the controller does not register the corresponding processing logic and refuses the operation by default.")
|
||||
}
|
||||
|
||||
// Get the object in the request
|
||||
obj := validator.Obj.DeepCopyObject()
|
||||
if req.Operation == v1beta1.Create {
|
||||
err := h.decoder.Decode(req, obj)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
err = validator.Helper.ValidateCreate(obj)
|
||||
if err != nil {
|
||||
return admission.Denied(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if req.Operation == v1beta1.Update {
|
||||
oldObj := obj.DeepCopyObject()
|
||||
|
||||
err := h.decoder.DecodeRaw(req.Object, obj)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
err = h.decoder.DecodeRaw(req.OldObject, oldObj)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
err = validator.Helper.ValidateUpdate(oldObj, obj)
|
||||
if err != nil {
|
||||
return admission.Denied(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if req.Operation == v1beta1.Delete {
|
||||
// In reference to PR: https://github.com/kubernetes/kubernetes/pull/76346
|
||||
// OldObject contains the object being deleted
|
||||
err := h.decoder.DecodeRaw(req.OldObject, obj)
|
||||
if err != nil {
|
||||
return admission.Errored(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
err = validator.Helper.ValidateDelete(obj)
|
||||
if err != nil {
|
||||
return admission.Denied(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return admission.Allowed("")
|
||||
}
|
||||
Reference in New Issue
Block a user