config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2019 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 kubelet
  14. import (
  15. "crypto/tls"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "regexp"
  21. "strconv"
  22. "time"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. "k8s.io/client-go/kubernetes/scheme"
  25. kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
  26. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  27. "k8s.io/kubernetes/test/e2e/framework"
  28. e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
  29. )
  30. // GetCurrentKubeletConfig fetches the current Kubelet Config for the given node
  31. func GetCurrentKubeletConfig(nodeName, namespace string, useProxy bool) (*kubeletconfig.KubeletConfiguration, error) {
  32. resp := pollConfigz(5*time.Minute, 5*time.Second, nodeName, namespace, useProxy)
  33. if resp == nil {
  34. return nil, fmt.Errorf("failed to fetch /configz from %q", nodeName)
  35. }
  36. kubeCfg, err := decodeConfigz(resp)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return kubeCfg, nil
  41. }
  42. // returns a status 200 response from the /configz endpoint or nil if fails
  43. func pollConfigz(timeout time.Duration, pollInterval time.Duration, nodeName, namespace string, useProxy bool) *http.Response {
  44. endpoint := ""
  45. if useProxy {
  46. // start local proxy, so we can send graceful deletion over query string, rather than body parameter
  47. framework.Logf("Opening proxy to cluster")
  48. tk := e2ekubectl.NewTestKubeconfig(framework.TestContext.CertDir, framework.TestContext.Host, framework.TestContext.KubeConfig, framework.TestContext.KubeContext, framework.TestContext.KubectlPath, namespace)
  49. cmd := tk.KubectlCmd("proxy", "-p", "0")
  50. stdout, stderr, err := framework.StartCmdAndStreamOutput(cmd)
  51. framework.ExpectNoError(err)
  52. defer stdout.Close()
  53. defer stderr.Close()
  54. defer framework.TryKill(cmd)
  55. buf := make([]byte, 128)
  56. var n int
  57. n, err = stdout.Read(buf)
  58. framework.ExpectNoError(err)
  59. output := string(buf[:n])
  60. proxyRegexp := regexp.MustCompile("Starting to serve on 127.0.0.1:([0-9]+)")
  61. match := proxyRegexp.FindStringSubmatch(output)
  62. framework.ExpectEqual(len(match), 2)
  63. port, err := strconv.Atoi(match[1])
  64. framework.ExpectNoError(err)
  65. framework.Logf("http requesting node kubelet /configz")
  66. endpoint = fmt.Sprintf("http://127.0.0.1:%d/api/v1/nodes/%s/proxy/configz", port, nodeName)
  67. } else {
  68. endpoint = fmt.Sprintf("http://127.0.0.1:8080/api/v1/nodes/%s/proxy/configz", framework.TestContext.NodeName)
  69. }
  70. tr := &http.Transport{
  71. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  72. }
  73. client := &http.Client{Transport: tr}
  74. req, err := http.NewRequest("GET", endpoint, nil)
  75. framework.ExpectNoError(err)
  76. req.Header.Add("Accept", "application/json")
  77. var resp *http.Response
  78. wait.PollImmediate(pollInterval, timeout, func() (bool, error) {
  79. resp, err = client.Do(req)
  80. if err != nil {
  81. framework.Logf("Failed to get /configz, retrying. Error: %v", err)
  82. return false, nil
  83. }
  84. if resp.StatusCode != 200 {
  85. framework.Logf("/configz response status not 200, retrying. Response was: %+v", resp)
  86. return false, nil
  87. }
  88. return true, nil
  89. })
  90. return resp
  91. }
  92. // Decodes the http response from /configz and returns a kubeletconfig.KubeletConfiguration (internal type).
  93. func decodeConfigz(resp *http.Response) (*kubeletconfig.KubeletConfiguration, error) {
  94. // This hack because /configz reports the following structure:
  95. // {"kubeletconfig": {the JSON representation of kubeletconfigv1beta1.KubeletConfiguration}}
  96. type configzWrapper struct {
  97. ComponentConfig kubeletconfigv1beta1.KubeletConfiguration `json:"kubeletconfig"`
  98. }
  99. configz := configzWrapper{}
  100. kubeCfg := kubeletconfig.KubeletConfiguration{}
  101. contentsBytes, err := ioutil.ReadAll(resp.Body)
  102. if err != nil {
  103. return nil, err
  104. }
  105. err = json.Unmarshal(contentsBytes, &configz)
  106. if err != nil {
  107. return nil, err
  108. }
  109. err = scheme.Scheme.Convert(&configz.ComponentConfig, &kubeCfg, nil)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &kubeCfg, nil
  114. }