|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + cloudprovider "k8s.io/cloud-provider" |
| 7 | + nodeipamconfig "k8s.io/cloud-provider-gcp/pkg/controller/nodeipam/config" |
| 8 | + cloudcontrollerconfig "k8s.io/cloud-provider/app/config" |
| 9 | + "k8s.io/cloud-provider/config" |
| 10 | + genericcontrollermanager "k8s.io/controller-manager/app" |
| 11 | +) |
| 12 | + |
| 13 | +type fakeCloudProvider struct{} |
| 14 | + |
| 15 | +// Implements cloudprovider.Interface. |
| 16 | +var _ cloudprovider.Interface = &fakeCloudProvider{} |
| 17 | + |
| 18 | +func (f *fakeCloudProvider) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) { |
| 19 | +} |
| 20 | + |
| 21 | +func (f *fakeCloudProvider) LoadBalancer() (cloudprovider.LoadBalancer, bool) { |
| 22 | + return nil, false |
| 23 | +} |
| 24 | + |
| 25 | +func (f *fakeCloudProvider) Instances() (cloudprovider.Instances, bool) { |
| 26 | + return nil, false |
| 27 | +} |
| 28 | + |
| 29 | +func (f *fakeCloudProvider) InstancesV2() (cloudprovider.InstancesV2, bool) { |
| 30 | + return nil, false |
| 31 | +} |
| 32 | + |
| 33 | +func (f *fakeCloudProvider) Zones() (cloudprovider.Zones, bool) { |
| 34 | + return nil, false |
| 35 | +} |
| 36 | + |
| 37 | +func (f *fakeCloudProvider) Clusters() (cloudprovider.Clusters, bool) { |
| 38 | + return nil, false |
| 39 | +} |
| 40 | + |
| 41 | +func (f *fakeCloudProvider) Routes() (cloudprovider.Routes, bool) { |
| 42 | + return nil, false |
| 43 | +} |
| 44 | + |
| 45 | +func (f *fakeCloudProvider) ProviderName() string { |
| 46 | + return "fake" |
| 47 | +} |
| 48 | + |
| 49 | +func (f *fakeCloudProvider) HasClusterID() bool { |
| 50 | + return false |
| 51 | +} |
| 52 | + |
| 53 | +func TestStartNodeIpamController(t *testing.T) { |
| 54 | + testCases := []struct { |
| 55 | + desc string |
| 56 | + ccmConfig *cloudcontrollerconfig.Config |
| 57 | + nodeIPAMConfig nodeipamconfig.NodeIPAMControllerConfiguration |
| 58 | + wantErr bool |
| 59 | + }{ |
| 60 | + { |
| 61 | + desc: "Allocate node CIDRs disabled", |
| 62 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 63 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 64 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 65 | + AllocateNodeCIDRs: false, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + wantErr: true, |
| 70 | + }, |
| 71 | + { |
| 72 | + desc: "Unparseable cluster CIDRs", |
| 73 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 74 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 75 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 76 | + AllocateNodeCIDRs: true, |
| 77 | + ClusterCIDR: "invalid", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + wantErr: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + desc: "Multiple same stack type cluster CIDRs - ipv4", |
| 85 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 86 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 87 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 88 | + AllocateNodeCIDRs: true, |
| 89 | + ClusterCIDR: "10.0.0.0/16,10.1.0.0/16", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + wantErr: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + desc: "Multiple same stack type cluster CIDRs - ipv6", |
| 97 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 98 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 99 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 100 | + AllocateNodeCIDRs: true, |
| 101 | + ClusterCIDR: "2001:db8::/112,2001:db9::/112", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + wantErr: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + desc: "More than 2 cluster CIDRs", |
| 109 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 110 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 111 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 112 | + AllocateNodeCIDRs: true, |
| 113 | + ClusterCIDR: "10.0.0.0/16,10.1.0.0/16,10.2.0.0/16", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + wantErr: true, |
| 118 | + }, |
| 119 | + { |
| 120 | + desc: "Primary and secondary service CIDR same stack type - ipv4", |
| 121 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 122 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 123 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 124 | + AllocateNodeCIDRs: true, |
| 125 | + ClusterCIDR: "10.0.0.0/16", |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + nodeIPAMConfig: nodeipamconfig.NodeIPAMControllerConfiguration{ |
| 130 | + ServiceCIDR: "10.0.0.0/16", |
| 131 | + SecondaryServiceCIDR: "10.1.0.0/16", |
| 132 | + }, |
| 133 | + wantErr: true, |
| 134 | + }, |
| 135 | + { |
| 136 | + desc: "Primary and secondary service CIDR same stack type - ipv6", |
| 137 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 138 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 139 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 140 | + AllocateNodeCIDRs: true, |
| 141 | + ClusterCIDR: "10.0.0.0/16", |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + nodeIPAMConfig: nodeipamconfig.NodeIPAMControllerConfiguration{ |
| 146 | + ServiceCIDR: "2001:db8::/112", |
| 147 | + SecondaryServiceCIDR: "2001:db9::/112", |
| 148 | + }, |
| 149 | + wantErr: true, |
| 150 | + }, |
| 151 | + { |
| 152 | + desc: "NodeCIDRMaskSize used with a dual stack cluster", |
| 153 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 154 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 155 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 156 | + AllocateNodeCIDRs: true, |
| 157 | + ClusterCIDR: "10.0.0.0/16,2001:aa::/112", |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + nodeIPAMConfig: nodeipamconfig.NodeIPAMControllerConfiguration{ |
| 162 | + NodeCIDRMaskSize: 10, |
| 163 | + }, |
| 164 | + wantErr: true, |
| 165 | + }, |
| 166 | + { |
| 167 | + desc: "NodeCIDRMaskSize and NodeCIDRMaskSizeIPv4 used together", |
| 168 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 169 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 170 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 171 | + AllocateNodeCIDRs: true, |
| 172 | + ClusterCIDR: "10.0.0.0/16", |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + nodeIPAMConfig: nodeipamconfig.NodeIPAMControllerConfiguration{ |
| 177 | + NodeCIDRMaskSize: 10, |
| 178 | + NodeCIDRMaskSizeIPv4: 4, |
| 179 | + }, |
| 180 | + wantErr: true, |
| 181 | + }, |
| 182 | + { |
| 183 | + desc: "NodeCIDRMaskSize and NodeCIDRMaskSizeIPv6 used together", |
| 184 | + ccmConfig: &cloudcontrollerconfig.Config{ |
| 185 | + ComponentConfig: config.CloudControllerManagerConfiguration{ |
| 186 | + KubeCloudShared: config.KubeCloudSharedConfiguration{ |
| 187 | + AllocateNodeCIDRs: true, |
| 188 | + ClusterCIDR: "10.0.0.0/16", |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + nodeIPAMConfig: nodeipamconfig.NodeIPAMControllerConfiguration{ |
| 193 | + NodeCIDRMaskSize: 10, |
| 194 | + NodeCIDRMaskSizeIPv6: 4, |
| 195 | + }, |
| 196 | + wantErr: true, |
| 197 | + }, |
| 198 | + } |
| 199 | + |
| 200 | + for _, tc := range testCases { |
| 201 | + t.Run(tc.desc, func(t *testing.T) { |
| 202 | + ctx := genericcontrollermanager.ControllerContext{} |
| 203 | + _, _, err := startNodeIpamController(tc.ccmConfig.Complete(), tc.nodeIPAMConfig, ctx, &fakeCloudProvider{}) |
| 204 | + |
| 205 | + if err == nil && tc.wantErr { |
| 206 | + t.Fatalf("startNodeIpamController succeeded, want error") |
| 207 | + } |
| 208 | + if err != nil && !tc.wantErr { |
| 209 | + t.Fatalf("startNodeIpamController(): %v", err) |
| 210 | + } |
| 211 | + }) |
| 212 | + } |
| 213 | +} |
0 commit comments