|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package options |
| 18 | + |
| 19 | +import ( |
| 20 | + "net" |
| 21 | + "testing" |
| 22 | + |
| 23 | + utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 24 | + featuregatetesting "k8s.io/component-base/featuregate/testing" |
| 25 | + "k8s.io/kubernetes/pkg/features" |
| 26 | +) |
| 27 | + |
| 28 | +func makeOptionsWithCIDRs(serviceCIDR string, secondaryServiceCIDR string) *ServerRunOptions { |
| 29 | + value := serviceCIDR |
| 30 | + if len(secondaryServiceCIDR) > 0 { |
| 31 | + value = value + "," + secondaryServiceCIDR |
| 32 | + } |
| 33 | + |
| 34 | + var primaryCIDR, secondaryCIDR net.IPNet |
| 35 | + if len(serviceCIDR) > 0 { |
| 36 | + _, cidr, _ := net.ParseCIDR(serviceCIDR) |
| 37 | + if cidr != nil { |
| 38 | + primaryCIDR = *(cidr) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if len(secondaryServiceCIDR) > 0 { |
| 43 | + _, cidr, _ := net.ParseCIDR(secondaryServiceCIDR) |
| 44 | + if cidr != nil { |
| 45 | + secondaryCIDR = *(cidr) |
| 46 | + } |
| 47 | + } |
| 48 | + return &ServerRunOptions{ |
| 49 | + ServiceClusterIPRanges: value, |
| 50 | + PrimaryServiceClusterIPRange: primaryCIDR, |
| 51 | + SecondaryServiceClusterIPRange: secondaryCIDR, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestClusterSerivceIPRange(t *testing.T) { |
| 56 | + testCases := []struct { |
| 57 | + name string |
| 58 | + options *ServerRunOptions |
| 59 | + enableDualStack bool |
| 60 | + expectErrors bool |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "no service cidr", |
| 64 | + expectErrors: true, |
| 65 | + options: makeOptionsWithCIDRs("", ""), |
| 66 | + enableDualStack: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "only secondary service cidr, dual stack gate on", |
| 70 | + expectErrors: true, |
| 71 | + options: makeOptionsWithCIDRs("", "10.0.0.0/16"), |
| 72 | + enableDualStack: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "only secondary service cidr, dual stack gate off", |
| 76 | + expectErrors: true, |
| 77 | + options: makeOptionsWithCIDRs("", "10.0.0.0/16"), |
| 78 | + enableDualStack: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "primary and secondary are provided but not dual stack v4-v4", |
| 82 | + expectErrors: true, |
| 83 | + options: makeOptionsWithCIDRs("10.0.0.0/16", "11.0.0.0/16"), |
| 84 | + enableDualStack: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "primary and secondary are provided but not dual stack v6-v6", |
| 88 | + expectErrors: true, |
| 89 | + options: makeOptionsWithCIDRs("2000::/108", "3000::/108"), |
| 90 | + enableDualStack: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "valid dual stack with gate disabled", |
| 94 | + expectErrors: true, |
| 95 | + options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/108"), |
| 96 | + enableDualStack: false, |
| 97 | + }, |
| 98 | + /* success cases */ |
| 99 | + { |
| 100 | + name: "valid primary", |
| 101 | + expectErrors: false, |
| 102 | + options: makeOptionsWithCIDRs("10.0.0.0/16", ""), |
| 103 | + enableDualStack: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "valid v4-v6 dual stack + gate on", |
| 107 | + expectErrors: false, |
| 108 | + options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/108"), |
| 109 | + enableDualStack: true, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "valid v6-v4 dual stack + gate on", |
| 113 | + expectErrors: false, |
| 114 | + options: makeOptionsWithCIDRs("3000::/108", "10.0.0.0/16"), |
| 115 | + enableDualStack: true, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tc := range testCases { |
| 120 | + t.Run(tc.name, func(t *testing.T) { |
| 121 | + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)() |
| 122 | + errs := validateClusterIPFlags(tc.options) |
| 123 | + if len(errs) > 0 && !tc.expectErrors { |
| 124 | + t.Errorf("expected no errors, errors found %+v", errs) |
| 125 | + } |
| 126 | + |
| 127 | + if len(errs) == 0 && tc.expectErrors { |
| 128 | + t.Errorf("expected errors, no errors found") |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments