validation_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2019 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 options
  14. import (
  15. "net"
  16. "testing"
  17. utilfeature "k8s.io/apiserver/pkg/util/feature"
  18. featuregatetesting "k8s.io/component-base/featuregate/testing"
  19. "k8s.io/kubernetes/pkg/features"
  20. )
  21. func makeOptionsWithCIDRs(serviceCIDR string, secondaryServiceCIDR string) *ServerRunOptions {
  22. value := serviceCIDR
  23. if len(secondaryServiceCIDR) > 0 {
  24. value = value + "," + secondaryServiceCIDR
  25. }
  26. var primaryCIDR, secondaryCIDR net.IPNet
  27. if len(serviceCIDR) > 0 {
  28. _, cidr, _ := net.ParseCIDR(serviceCIDR)
  29. if cidr != nil {
  30. primaryCIDR = *(cidr)
  31. }
  32. }
  33. if len(secondaryServiceCIDR) > 0 {
  34. _, cidr, _ := net.ParseCIDR(secondaryServiceCIDR)
  35. if cidr != nil {
  36. secondaryCIDR = *(cidr)
  37. }
  38. }
  39. return &ServerRunOptions{
  40. ServiceClusterIPRanges: value,
  41. PrimaryServiceClusterIPRange: primaryCIDR,
  42. SecondaryServiceClusterIPRange: secondaryCIDR,
  43. }
  44. }
  45. func TestClusterSerivceIPRange(t *testing.T) {
  46. testCases := []struct {
  47. name string
  48. options *ServerRunOptions
  49. enableDualStack bool
  50. expectErrors bool
  51. }{
  52. {
  53. name: "no service cidr",
  54. expectErrors: true,
  55. options: makeOptionsWithCIDRs("", ""),
  56. enableDualStack: false,
  57. },
  58. {
  59. name: "only secondary service cidr, dual stack gate on",
  60. expectErrors: true,
  61. options: makeOptionsWithCIDRs("", "10.0.0.0/16"),
  62. enableDualStack: true,
  63. },
  64. {
  65. name: "only secondary service cidr, dual stack gate off",
  66. expectErrors: true,
  67. options: makeOptionsWithCIDRs("", "10.0.0.0/16"),
  68. enableDualStack: false,
  69. },
  70. {
  71. name: "primary and secondary are provided but not dual stack v4-v4",
  72. expectErrors: true,
  73. options: makeOptionsWithCIDRs("10.0.0.0/16", "11.0.0.0/16"),
  74. enableDualStack: true,
  75. },
  76. {
  77. name: "primary and secondary are provided but not dual stack v6-v6",
  78. expectErrors: true,
  79. options: makeOptionsWithCIDRs("2000::/108", "3000::/108"),
  80. enableDualStack: true,
  81. },
  82. {
  83. name: "valid dual stack with gate disabled",
  84. expectErrors: true,
  85. options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/108"),
  86. enableDualStack: false,
  87. },
  88. /* success cases */
  89. {
  90. name: "valid primary",
  91. expectErrors: false,
  92. options: makeOptionsWithCIDRs("10.0.0.0/16", ""),
  93. enableDualStack: false,
  94. },
  95. {
  96. name: "valid v4-v6 dual stack + gate on",
  97. expectErrors: false,
  98. options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/108"),
  99. enableDualStack: true,
  100. },
  101. {
  102. name: "valid v6-v4 dual stack + gate on",
  103. expectErrors: false,
  104. options: makeOptionsWithCIDRs("3000::/108", "10.0.0.0/16"),
  105. enableDualStack: true,
  106. },
  107. }
  108. for _, tc := range testCases {
  109. t.Run(tc.name, func(t *testing.T) {
  110. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)()
  111. errs := validateClusterIPFlags(tc.options)
  112. if len(errs) > 0 && !tc.expectErrors {
  113. t.Errorf("expected no errors, errors found %+v", errs)
  114. }
  115. if len(errs) == 0 && tc.expectErrors {
  116. t.Errorf("expected errors, no errors found")
  117. }
  118. })
  119. }
  120. }