k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kube-controller-manager/app/options/nodeipamcontroller.go (about) 1 /* 2 Copyright 2018 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 "fmt" 21 "strings" 22 23 "github.com/spf13/pflag" 24 25 nodeipamconfig "k8s.io/kubernetes/pkg/controller/nodeipam/config" 26 ) 27 28 // NodeIPAMControllerOptions holds the NodeIpamController options. 29 type NodeIPAMControllerOptions struct { 30 *nodeipamconfig.NodeIPAMControllerConfiguration 31 } 32 33 // AddFlags adds flags related to NodeIpamController for controller manager to the specified FlagSet. 34 func (o *NodeIPAMControllerOptions) AddFlags(fs *pflag.FlagSet) { 35 if o == nil { 36 return 37 } 38 fs.StringVar(&o.ServiceCIDR, "service-cluster-ip-range", o.ServiceCIDR, "CIDR Range for Services in cluster. Requires --allocate-node-cidrs to be true") 39 fs.Int32Var(&o.NodeCIDRMaskSize, "node-cidr-mask-size", o.NodeCIDRMaskSize, "Mask size for node cidr in cluster. Default is 24 for IPv4 and 64 for IPv6.") 40 fs.Int32Var(&o.NodeCIDRMaskSizeIPv4, "node-cidr-mask-size-ipv4", o.NodeCIDRMaskSizeIPv4, "Mask size for IPv4 node cidr in dual-stack cluster. Default is 24.") 41 fs.Int32Var(&o.NodeCIDRMaskSizeIPv6, "node-cidr-mask-size-ipv6", o.NodeCIDRMaskSizeIPv6, "Mask size for IPv6 node cidr in dual-stack cluster. Default is 64.") 42 } 43 44 // ApplyTo fills up NodeIpamController config with options. 45 func (o *NodeIPAMControllerOptions) ApplyTo(cfg *nodeipamconfig.NodeIPAMControllerConfiguration) error { 46 if o == nil { 47 return nil 48 } 49 50 // split the cidrs list and assign primary and secondary 51 serviceCIDRList := strings.Split(o.ServiceCIDR, ",") 52 if len(serviceCIDRList) > 0 { 53 cfg.ServiceCIDR = serviceCIDRList[0] 54 } 55 if len(serviceCIDRList) > 1 { 56 cfg.SecondaryServiceCIDR = serviceCIDRList[1] 57 } 58 59 cfg.NodeCIDRMaskSize = o.NodeCIDRMaskSize 60 cfg.NodeCIDRMaskSizeIPv4 = o.NodeCIDRMaskSizeIPv4 61 cfg.NodeCIDRMaskSizeIPv6 = o.NodeCIDRMaskSizeIPv6 62 63 return nil 64 } 65 66 // Validate checks validation of NodeIPAMControllerOptions. 67 func (o *NodeIPAMControllerOptions) Validate() []error { 68 if o == nil { 69 return nil 70 } 71 errs := make([]error, 0) 72 73 serviceCIDRList := strings.Split(o.ServiceCIDR, ",") 74 if len(serviceCIDRList) > 2 { 75 errs = append(errs, fmt.Errorf("--service-cluster-ip-range can not contain more than two entries")) 76 } 77 78 return errs 79 }