joinconfiguration.go 5.7 KB

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