nodeipamcontroller.go 2.6 KB

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