config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 componentconfigs
  14. import (
  15. "github.com/pkg/errors"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/version"
  19. clientset "k8s.io/client-go/kubernetes"
  20. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  22. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  23. kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
  24. )
  25. // GetFromKubeletConfigMap returns the pointer to the ComponentConfig API object read from the kubelet-config-version
  26. // ConfigMap map stored in the cluster
  27. func GetFromKubeletConfigMap(client clientset.Interface, version *version.Version) (runtime.Object, error) {
  28. // Read the ConfigMap from the cluster based on what version the kubelet is
  29. configMapName := kubeadmconstants.GetKubeletConfigMapName(version)
  30. kubeletCfg, err := apiclient.GetConfigMapWithRetry(client, metav1.NamespaceSystem, configMapName)
  31. if err != nil {
  32. return nil, err
  33. }
  34. kubeletConfigData, ok := kubeletCfg.Data[kubeadmconstants.KubeletBaseConfigurationConfigMapKey]
  35. if !ok {
  36. return nil, errors.Errorf("unexpected error when reading %s ConfigMap: %s key value pair missing",
  37. configMapName, kubeadmconstants.KubeletBaseConfigurationConfigMapKey)
  38. }
  39. // Decodes the kubeletConfigData into the internal component config
  40. obj := &kubeletconfig.KubeletConfiguration{}
  41. err = unmarshalObject(obj, []byte(kubeletConfigData))
  42. if err != nil {
  43. return nil, err
  44. }
  45. return obj, nil
  46. }
  47. // GetFromKubeProxyConfigMap returns the pointer to the ComponentConfig API object read from the kube-proxy
  48. // ConfigMap map stored in the cluster
  49. func GetFromKubeProxyConfigMap(client clientset.Interface, version *version.Version) (runtime.Object, error) {
  50. // Read the ConfigMap from the cluster
  51. kubeproxyCfg, err := apiclient.GetConfigMapWithRetry(client, metav1.NamespaceSystem, kubeadmconstants.KubeProxyConfigMap)
  52. if err != nil {
  53. return nil, err
  54. }
  55. kubeproxyConfigData, ok := kubeproxyCfg.Data[kubeadmconstants.KubeProxyConfigMapKey]
  56. if !ok {
  57. return nil, errors.Errorf("unexpected error when reading %s ConfigMap: %s key value pair missing",
  58. kubeadmconstants.KubeProxyConfigMap, kubeadmconstants.KubeProxyConfigMapKey)
  59. }
  60. // Decodes the Config map dat into the internal component config
  61. obj := &kubeproxyconfig.KubeProxyConfiguration{}
  62. err = unmarshalObject(obj, []byte(kubeproxyConfigData))
  63. if err != nil {
  64. return nil, err
  65. }
  66. return obj, nil
  67. }