controller_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ipam
  14. import (
  15. "net"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset"
  18. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/test"
  19. )
  20. func TestOccupyServiceCIDR(t *testing.T) {
  21. const clusterCIDR = "10.1.0.0/16"
  22. TestCase:
  23. for _, tc := range []struct {
  24. serviceCIDR string
  25. }{
  26. {"10.0.255.0/24"},
  27. {"10.1.0.0/24"},
  28. {"10.1.255.0/24"},
  29. {"10.2.0.0/24"},
  30. } {
  31. serviceCIDR := test.MustParseCIDR(tc.serviceCIDR)
  32. set, err := cidrset.NewCIDRSet(test.MustParseCIDR(clusterCIDR), 24)
  33. if err != nil {
  34. t.Errorf("test case %+v: NewCIDRSet() = %v, want nil", tc, err)
  35. }
  36. if err := occupyServiceCIDR(set, test.MustParseCIDR(clusterCIDR), serviceCIDR); err != nil {
  37. t.Errorf("test case %+v: occupyServiceCIDR() = %v, want nil", tc, err)
  38. }
  39. // Allocate until full.
  40. var cidrs []*net.IPNet
  41. for {
  42. cidr, err := set.AllocateNext()
  43. if err != nil {
  44. if err == cidrset.ErrCIDRRangeNoCIDRsRemaining {
  45. break
  46. }
  47. t.Errorf("set.AllocateNext() = %v, want %v", err, cidrset.ErrCIDRRangeNoCIDRsRemaining)
  48. continue TestCase
  49. }
  50. cidrs = append(cidrs, cidr)
  51. }
  52. // No allocated CIDR range should intersect with serviceCIDR.
  53. for _, c := range cidrs {
  54. if c.Contains(serviceCIDR.IP) || serviceCIDR.Contains(c.IP) {
  55. t.Errorf("test case %+v: allocated CIDR %v from service range", tc, c)
  56. }
  57. }
  58. }
  59. }