helpers.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2016 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 componentconfig
  14. import (
  15. "fmt"
  16. "net"
  17. utilnet "k8s.io/kubernetes/pkg/util/net"
  18. )
  19. // used for validating command line opts
  20. // TODO(mikedanese): remove these when we remove command line flags
  21. type IPVar struct {
  22. Val *string
  23. }
  24. func (v IPVar) Set(s string) error {
  25. if net.ParseIP(s) == nil {
  26. return fmt.Errorf("%q is not a valid IP address", s)
  27. }
  28. if v.Val == nil {
  29. // it's okay to panic here since this is programmer error
  30. panic("the string pointer passed into IPVar should not be nil")
  31. }
  32. *v.Val = s
  33. return nil
  34. }
  35. func (v IPVar) String() string {
  36. if v.Val == nil {
  37. return ""
  38. }
  39. return *v.Val
  40. }
  41. func (v IPVar) Type() string {
  42. return "ip"
  43. }
  44. func (m *ProxyMode) Set(s string) error {
  45. *m = ProxyMode(s)
  46. return nil
  47. }
  48. func (m *ProxyMode) String() string {
  49. if m != nil {
  50. return string(*m)
  51. }
  52. return ""
  53. }
  54. func (m *ProxyMode) Type() string {
  55. return "ProxyMode"
  56. }
  57. type PortRangeVar struct {
  58. Val *string
  59. }
  60. func (v PortRangeVar) Set(s string) error {
  61. if _, err := utilnet.ParsePortRange(s); err != nil {
  62. return fmt.Errorf("%q is not a valid port range: %v", s, err)
  63. }
  64. if v.Val == nil {
  65. // it's okay to panic here since this is programmer error
  66. panic("the string pointer passed into PortRangeVar should not be nil")
  67. }
  68. *v.Val = s
  69. return nil
  70. }
  71. func (v PortRangeVar) String() string {
  72. if v.Val == nil {
  73. return ""
  74. }
  75. return *v.Val
  76. }
  77. func (v PortRangeVar) Type() string {
  78. return "port-range"
  79. }