joinconfiguration.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 config
  14. import (
  15. "io/ioutil"
  16. "github.com/pkg/errors"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/klog"
  19. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  20. kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
  21. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  22. "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
  23. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  24. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  25. "k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict"
  26. )
  27. // SetJoinDynamicDefaults checks and sets configuration values for the JoinConfiguration object
  28. func SetJoinDynamicDefaults(cfg *kubeadmapi.JoinConfiguration) error {
  29. addControlPlaneTaint := false
  30. if cfg.ControlPlane != nil {
  31. addControlPlaneTaint = true
  32. }
  33. if err := SetNodeRegistrationDynamicDefaults(&cfg.NodeRegistration, addControlPlaneTaint); err != nil {
  34. return err
  35. }
  36. return SetJoinControlPlaneDefaults(cfg.ControlPlane)
  37. }
  38. // SetJoinControlPlaneDefaults checks and sets configuration values for the JoinControlPlane object
  39. func SetJoinControlPlaneDefaults(cfg *kubeadmapi.JoinControlPlane) error {
  40. if cfg != nil {
  41. if err := SetAPIEndpointDynamicDefaults(&cfg.LocalAPIEndpoint); err != nil {
  42. return err
  43. }
  44. }
  45. return nil
  46. }
  47. // LoadOrDefaultJoinConfiguration takes a path to a config file and a versioned configuration that can serve as the default config
  48. // If cfgPath is specified, defaultversionedcfg will always get overridden. Otherwise, the default config (often populated by flags) will be used.
  49. // Then the external, versioned configuration is defaulted and converted to the internal type.
  50. // Right thereafter, the configuration is defaulted again with dynamic values (like IP addresses of a machine, etc)
  51. // Lastly, the internal config is validated and returned.
  52. func LoadOrDefaultJoinConfiguration(cfgPath string, defaultversionedcfg *kubeadmapiv1beta2.JoinConfiguration) (*kubeadmapi.JoinConfiguration, error) {
  53. if cfgPath != "" {
  54. // Loads configuration from config file, if provided
  55. // Nb. --config overrides command line flags, TODO: fix this
  56. return LoadJoinConfigurationFromFile(cfgPath)
  57. }
  58. return DefaultedJoinConfiguration(defaultversionedcfg)
  59. }
  60. // LoadJoinConfigurationFromFile loads versioned JoinConfiguration from file, converts it to internal, defaults and validates it
  61. func LoadJoinConfigurationFromFile(cfgPath string) (*kubeadmapi.JoinConfiguration, error) {
  62. klog.V(1).Infof("loading configuration from %q", cfgPath)
  63. b, err := ioutil.ReadFile(cfgPath)
  64. if err != nil {
  65. return nil, errors.Wrapf(err, "unable to read config from %q ", cfgPath)
  66. }
  67. gvkmap, err := kubeadmutil.SplitYAMLDocuments(b)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return documentMapToJoinConfiguration(gvkmap, false)
  72. }
  73. // documentMapToJoinConfiguration takes a map between GVKs and YAML documents (as returned by SplitYAMLDocuments),
  74. // finds a JoinConfiguration, decodes it, dynamically defaults it and then validates it prior to return.
  75. func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated bool) (*kubeadmapi.JoinConfiguration, error) {
  76. joinBytes := []byte{}
  77. for gvk, bytes := range gvkmap {
  78. // not interested in anything other than JoinConfiguration
  79. if gvk.Kind != constants.JoinConfigurationKind {
  80. continue
  81. }
  82. // check if this version is supported and possibly not deprecated
  83. if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated); err != nil {
  84. return nil, err
  85. }
  86. // verify the validity of the YAML
  87. strict.VerifyUnmarshalStrict(bytes, gvk)
  88. joinBytes = bytes
  89. }
  90. if len(joinBytes) == 0 {
  91. return nil, errors.Errorf("no %s found in the supplied config", constants.JoinConfigurationKind)
  92. }
  93. internalcfg := &kubeadmapi.JoinConfiguration{}
  94. if err := runtime.DecodeInto(kubeadmscheme.Codecs.UniversalDecoder(), joinBytes, internalcfg); err != nil {
  95. return nil, err
  96. }
  97. // Applies dynamic defaults to settings not provided with flags
  98. if err := SetJoinDynamicDefaults(internalcfg); err != nil {
  99. return nil, err
  100. }
  101. // Validates cfg (flags/configs + defaults)
  102. if err := validation.ValidateJoinConfiguration(internalcfg).ToAggregate(); err != nil {
  103. return nil, err
  104. }
  105. return internalcfg, nil
  106. }
  107. // DefaultedJoinConfiguration takes a versioned JoinConfiguration (usually filled in by command line parameters), defaults it, converts it to internal and validates it
  108. func DefaultedJoinConfiguration(defaultversionedcfg *kubeadmapiv1beta2.JoinConfiguration) (*kubeadmapi.JoinConfiguration, error) {
  109. internalcfg := &kubeadmapi.JoinConfiguration{}
  110. // Takes passed flags into account; the defaulting is executed once again enforcing assignment of
  111. // static default values to cfg only for values not provided with flags
  112. kubeadmscheme.Scheme.Default(defaultversionedcfg)
  113. kubeadmscheme.Scheme.Convert(defaultversionedcfg, internalcfg, nil)
  114. // Applies dynamic defaults to settings not provided with flags
  115. if err := SetJoinDynamicDefaults(internalcfg); err != nil {
  116. return nil, err
  117. }
  118. // Validates cfg (flags/configs + defaults)
  119. if err := validation.ValidateJoinConfiguration(internalcfg).ToAggregate(); err != nil {
  120. return nil, err
  121. }
  122. return internalcfg, nil
  123. }