Skip to content

Commit 6e3d62c

Browse files
authored
Merge pull request kubernetes#111904 from pandaamanda/controller_codeclean
refactor: move attachdetach controller param validation ahead
2 parents bf2e850 + 3d87919 commit 6e3d62c

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

cmd/kube-controller-manager/app/core.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,11 @@ func startNodeIpamController(ctx context.Context, controllerContext ControllerCo
105105
return nil, false, nil
106106
}
107107

108-
// failure: bad cidrs in config
109-
clusterCIDRs, dualStack, err := processCIDRs(controllerContext.ComponentConfig.KubeCloudShared.ClusterCIDR)
108+
clusterCIDRs, err := validateCIDRs(controllerContext.ComponentConfig.KubeCloudShared.ClusterCIDR)
110109
if err != nil {
111110
return nil, false, err
112111
}
113112

114-
// failure: more than one cidr but they are not configured as dual stack
115-
if len(clusterCIDRs) > 1 && !dualStack {
116-
return nil, false, fmt.Errorf("len of ClusterCIDRs==%v and they are not configured as dual stack (at least one from each IPFamily)", len(clusterCIDRs))
117-
}
118-
119-
// failure: more than cidrs is not allowed even with dual stack
120-
if len(clusterCIDRs) > 2 {
121-
return nil, false, fmt.Errorf("len of clusters is:%v > more than max allowed of 2", len(clusterCIDRs))
122-
}
123-
124113
// service cidr processing
125114
if len(strings.TrimSpace(controllerContext.ComponentConfig.NodeIPAMController.ServiceCIDR)) != 0 {
126115
_, serviceCIDR, err = netutils.ParseCIDRSloppy(controllerContext.ComponentConfig.NodeIPAMController.ServiceCIDR)
@@ -238,22 +227,11 @@ func startRouteController(ctx context.Context, controllerContext ControllerConte
238227
return nil, false, nil
239228
}
240229

241-
// failure: bad cidrs in config
242-
clusterCIDRs, dualStack, err := processCIDRs(controllerContext.ComponentConfig.KubeCloudShared.ClusterCIDR)
230+
clusterCIDRs, err := validateCIDRs(controllerContext.ComponentConfig.KubeCloudShared.ClusterCIDR)
243231
if err != nil {
244232
return nil, false, err
245233
}
246234

247-
// failure: more than one cidr but they are not configured as dual stack
248-
if len(clusterCIDRs) > 1 && !dualStack {
249-
return nil, false, fmt.Errorf("len of ClusterCIDRs==%v and they are not configured as dual stack (at least one from each IPFamily", len(clusterCIDRs))
250-
}
251-
252-
// failure: more than cidrs is not allowed even with dual stack
253-
if len(clusterCIDRs) > 2 {
254-
return nil, false, fmt.Errorf("length of clusterCIDRs is:%v more than max allowed of 2", len(clusterCIDRs))
255-
}
256-
257235
routeController := routecontroller.New(routes,
258236
controllerContext.ClientBuilder.ClientOrDie("route-controller"),
259237
controllerContext.InformerFactory.Core().V1().Nodes(),
@@ -297,10 +275,6 @@ func startPersistentVolumeBinderController(ctx context.Context, controllerContex
297275
}
298276

299277
func startAttachDetachController(ctx context.Context, controllerContext ControllerContext) (controller.Interface, bool, error) {
300-
if controllerContext.ComponentConfig.AttachDetachController.ReconcilerSyncLoopPeriod.Duration < time.Second {
301-
return nil, true, fmt.Errorf("duration time must be greater than one second as set via command line option reconcile-sync-loop-period")
302-
}
303-
304278
csiNodeInformer := controllerContext.InformerFactory.Storage().V1().CSINodes()
305279
csiDriverInformer := controllerContext.InformerFactory.Storage().V1().CSIDrivers()
306280

@@ -581,6 +555,29 @@ func startTTLAfterFinishedController(ctx context.Context, controllerContext Cont
581555
return nil, true, nil
582556
}
583557

558+
// processCIDRs is a helper function that works on a comma separated cidrs and returns
559+
// a list of typed cidrs
560+
// error if failed to parse any of the cidrs or invalid length of cidrs
561+
func validateCIDRs(cidrsList string) ([]*net.IPNet, error) {
562+
// failure: bad cidrs in config
563+
clusterCIDRs, dualStack, err := processCIDRs(cidrsList)
564+
if err != nil {
565+
return nil, err
566+
}
567+
568+
// failure: more than one cidr but they are not configured as dual stack
569+
if len(clusterCIDRs) > 1 && !dualStack {
570+
return nil, fmt.Errorf("len of ClusterCIDRs==%v and they are not configured as dual stack (at least one from each IPFamily", len(clusterCIDRs))
571+
}
572+
573+
// failure: more than cidrs is not allowed even with dual stack
574+
if len(clusterCIDRs) > 2 {
575+
return nil, fmt.Errorf("length of clusterCIDRs is:%v more than max allowed of 2", len(clusterCIDRs))
576+
}
577+
578+
return clusterCIDRs, nil
579+
}
580+
584581
// processCIDRs is a helper function that works on a comma separated cidrs and returns
585582
// a list of typed cidrs
586583
// a flag if cidrs represents a dual stack

cmd/kube-controller-manager/app/options/attachdetachcontroller.go

+7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package options
1818

1919
import (
20+
"fmt"
2021
"github.com/spf13/pflag"
22+
"time"
2123

2224
attachdetachconfig "k8s.io/kubernetes/pkg/controller/volume/attachdetach/config"
2325
)
@@ -56,5 +58,10 @@ func (o *AttachDetachControllerOptions) Validate() []error {
5658
}
5759

5860
errs := []error{}
61+
62+
if o.ReconcilerSyncLoopPeriod.Duration < time.Second {
63+
errs = append(errs, fmt.Errorf("duration time must be greater than one second as set via command line option reconcile-sync-loop-period"))
64+
}
65+
5966
return errs
6067
}

0 commit comments

Comments
 (0)