2021-08-12 17:13:11 -04:00
//go:build !providerless
2020-07-30 11:58:54 -04:00
// +build !providerless
2018-09-21 16:22:29 -04:00
/ *
Copyright 2018 The Kubernetes 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 nodeipam
import (
2022-09-22 02:37:36 -04:00
"context"
2023-03-02 09:00:47 -05:00
"errors"
2018-09-21 16:22:29 -04:00
"net"
2019-06-11 12:04:50 -04:00
"strings"
2018-09-21 16:22:29 -04:00
"testing"
2020-07-30 11:58:54 -04:00
v1 "k8s.io/api/core/v1"
2018-09-21 16:22:29 -04:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2023-03-01 06:45:15 -05:00
utilfeature "k8s.io/apiserver/pkg/util/feature"
2018-09-21 16:22:29 -04:00
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
2023-03-01 06:45:15 -05:00
featuregatetesting "k8s.io/component-base/featuregate/testing"
2023-03-02 09:00:47 -05:00
"k8s.io/klog/v2/ktesting"
2018-09-21 16:22:29 -04:00
"k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
"k8s.io/kubernetes/pkg/controller/testutil"
2023-03-01 06:45:15 -05:00
"k8s.io/kubernetes/pkg/features"
2019-05-02 19:49:13 -04:00
"k8s.io/legacy-cloud-providers/gce"
2019-06-11 12:04:50 -04:00
netutils "k8s.io/utils/net"
2018-09-21 16:22:29 -04:00
)
2022-09-22 02:37:36 -04:00
func newTestNodeIpamController ( ctx context . Context , clusterCIDR [ ] * net . IPNet , serviceCIDR * net . IPNet , secondaryServiceCIDR * net . IPNet , nodeCIDRMaskSizes [ ] int , allocatorType ipam . CIDRAllocatorType ) ( * Controller , error ) {
2018-09-21 16:22:29 -04:00
clientSet := fake . NewSimpleClientset ( )
fakeNodeHandler := & testutil . FakeNodeHandler {
Existing : [ ] * v1 . Node {
{ ObjectMeta : metav1 . ObjectMeta { Name : "node0" } } ,
} ,
Clientset : fake . NewSimpleClientset ( ) ,
}
fakeClient := & fake . Clientset { }
2023-03-01 06:45:15 -05:00
fakeInformerFactory := informers . NewSharedInformerFactory ( fakeClient , 0 )
2018-09-21 16:22:29 -04:00
fakeNodeInformer := fakeInformerFactory . Core ( ) . V1 ( ) . Nodes ( )
2022-08-05 15:24:18 -04:00
fakeClusterCIDRInformer := fakeInformerFactory . Networking ( ) . V1alpha1 ( ) . ClusterCIDRs ( )
2018-09-21 16:22:29 -04:00
for _ , node := range fakeNodeHandler . Existing {
fakeNodeInformer . Informer ( ) . GetStore ( ) . Add ( node )
}
2018-10-20 03:14:19 -04:00
fakeGCE := gce . NewFakeGCECloud ( gce . DefaultTestClusterValues ( ) )
2018-09-21 16:22:29 -04:00
return NewNodeIpamController (
2022-09-22 02:37:36 -04:00
ctx ,
2022-08-05 15:24:18 -04:00
fakeNodeInformer , fakeClusterCIDRInformer , fakeGCE , clientSet ,
2019-07-09 17:47:01 -04:00
clusterCIDR , serviceCIDR , secondaryServiceCIDR , nodeCIDRMaskSizes , allocatorType ,
2018-09-21 16:22:29 -04:00
)
}
// TestNewNodeIpamControllerWithCIDRMasks tests if the controller can be
// created with combinations of network CIDRs and masks.
func TestNewNodeIpamControllerWithCIDRMasks ( t * testing . T ) {
2019-08-19 16:53:18 -04:00
emptyServiceCIDR := ""
2018-09-21 16:22:29 -04:00
for _ , tc := range [ ] struct {
2019-08-19 16:53:18 -04:00
desc string
clusterCIDR string
serviceCIDR string
secondaryServiceCIDR string
2019-07-09 17:47:01 -04:00
maskSize [ ] int
2019-08-19 16:53:18 -04:00
allocatorType ipam . CIDRAllocatorType
2023-03-02 09:00:47 -05:00
expectedError error
2018-09-21 16:22:29 -04:00
} {
2023-03-02 09:00:47 -05:00
{ "valid_range_allocator" , "10.0.0.0/21" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . RangeAllocatorType , nil } ,
{ "valid_range_allocator_dualstack" , "10.0.0.0/21,2000::/48" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 , 64 } , ipam . RangeAllocatorType , nil } ,
{ "valid_range_allocator_dualstack_dualstackservice" , "10.0.0.0/21,2000::/48" , "10.1.0.0/21" , "3000::/112" , [ ] int { 24 , 64 } , ipam . RangeAllocatorType , nil } ,
{ "valid_cloud_allocator" , "10.0.0.0/21" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . CloudAllocatorType , nil } ,
{ "valid_ipam_from_cluster" , "10.0.0.0/21" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . IPAMFromClusterAllocatorType , nil } ,
{ "valid_ipam_from_cloud" , "10.0.0.0/21" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . IPAMFromCloudAllocatorType , nil } ,
{ "valid_skip_cluster_CIDR_validation_for_cloud_allocator" , "invalid" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . CloudAllocatorType , nil } ,
{ "valid_CIDR_larger_than_mask_cloud_allocator" , "10.0.0.0/16" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . CloudAllocatorType , nil } ,
{ "invalid_cluster_CIDR" , "" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . IPAMFromClusterAllocatorType , errors . New ( "Controller: Must specify --cluster-cidr if --allocate-node-cidrs is set" ) } ,
{ "invalid_CIDR_smaller_than_mask_other_allocators" , "10.0.0.0/26" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . IPAMFromCloudAllocatorType , errors . New ( "Controller: Invalid --cluster-cidr, mask size of cluster CIDR must be less than or equal to --node-cidr-mask-size configured for CIDR family" ) } ,
{ "invalid_serviceCIDR_contains_clusterCIDR" , "10.0.0.0/16" , "10.0.0.0/8" , emptyServiceCIDR , [ ] int { 24 } , ipam . IPAMFromClusterAllocatorType , errors . New ( "error creating ipam controller: failed after occupy serviceCIDR: CIDR allocation failed; there are no remaining CIDRs left to allocate in the accepted range" ) } ,
{ "invalid_CIDR_mask_size" , "10.0.0.0/24,2000::/64" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 , 48 } , ipam . IPAMFromClusterAllocatorType , errors . New ( "Controller: Invalid --cluster-cidr, mask size of cluster CIDR must be less than or equal to --node-cidr-mask-size configured for CIDR family" ) } ,
2018-09-21 16:22:29 -04:00
} {
2023-03-01 06:45:15 -05:00
test := tc
2023-03-02 09:00:47 -05:00
_ , ctx := ktesting . NewTestContext ( t )
2023-03-01 06:45:15 -05:00
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
clusterCidrs , err := netutils . ParseCIDRs ( strings . Split ( test . clusterCIDR , "," ) )
if err != nil {
clusterCidrs = nil
}
_ , serviceCIDRIpNet , err := netutils . ParseCIDRSloppy ( test . serviceCIDR )
if err != nil {
serviceCIDRIpNet = nil
2018-09-21 16:22:29 -04:00
}
2023-03-01 06:45:15 -05:00
_ , secondaryServiceCIDRIpNet , err := netutils . ParseCIDRSloppy ( test . secondaryServiceCIDR )
2018-09-21 16:22:29 -04:00
if err != nil {
2023-03-01 06:45:15 -05:00
secondaryServiceCIDRIpNet = nil
}
2023-03-02 09:00:47 -05:00
_ , err = newTestNodeIpamController ( ctx , clusterCidrs , serviceCIDRIpNet , secondaryServiceCIDRIpNet , test . maskSize , test . allocatorType )
if test . expectedError == nil {
if err != nil {
t . Errorf ( "Test %s, unexpected error: %v" , test . desc , err )
}
} else {
if err . Error ( ) != test . expectedError . Error ( ) {
t . Errorf ( "Test %s, got error: %v, expected error: %v" , test . desc , err , test . expectedError )
2018-09-21 16:22:29 -04:00
}
}
2023-03-01 06:45:15 -05:00
} )
}
}
// MultiCIDRRangeAllocatorType need enable feature gate
func TestNewNodeIpamControllerWithCIDRMasks2 ( t * testing . T ) {
emptyServiceCIDR := ""
for _ , tc := range [ ] struct {
desc string
clusterCIDR string
serviceCIDR string
secondaryServiceCIDR string
maskSize [ ] int
allocatorType ipam . CIDRAllocatorType
} {
2023-03-02 09:00:47 -05:00
{ "valid_multi_cidr_range_allocator" , "10.0.0.0/21" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 } , ipam . MultiCIDRRangeAllocatorType } ,
{ "valid_multi_cidr_range_allocator_dualstack" , "10.0.0.0/21,2000::/48" , "10.1.0.0/21" , emptyServiceCIDR , [ ] int { 24 , 64 } , ipam . MultiCIDRRangeAllocatorType } ,
2023-03-01 06:45:15 -05:00
} {
test := tc
2023-03-02 09:00:47 -05:00
_ , ctx := ktesting . NewTestContext ( t )
2023-03-01 06:45:15 -05:00
t . Run ( test . desc , func ( t * testing . T ) {
defer featuregatetesting . SetFeatureGateDuringTest ( t , utilfeature . DefaultFeatureGate , features . MultiCIDRRangeAllocator , true ) ( )
clusterCidrs , err := netutils . ParseCIDRs ( strings . Split ( test . clusterCIDR , "," ) )
if err != nil {
clusterCidrs = nil
}
_ , serviceCIDRIpNet , err := netutils . ParseCIDRSloppy ( test . serviceCIDR )
if err != nil {
serviceCIDRIpNet = nil
}
_ , secondaryServiceCIDRIpNet , err := netutils . ParseCIDRSloppy ( test . secondaryServiceCIDR )
if err != nil {
secondaryServiceCIDRIpNet = nil
}
2023-03-02 09:00:47 -05:00
_ , err = newTestNodeIpamController ( ctx , clusterCidrs , serviceCIDRIpNet , secondaryServiceCIDRIpNet , test . maskSize , test . allocatorType )
if err != nil {
t . Errorf ( "Test %s, got error %v" , test . desc , err )
2018-09-21 16:22:29 -04:00
}
} )
}
}