dryrun.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2017 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 dryrun
  14. import (
  15. "fmt"
  16. "io"
  17. "io/ioutil"
  18. "path/filepath"
  19. "time"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. errorsutil "k8s.io/apimachinery/pkg/util/errors"
  22. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  23. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  24. )
  25. // FileToPrint represents a temporary file on disk that might want to be aliased when printing
  26. // Useful for things like loading a file from /tmp/ but saying to the user "Would write file foo to /etc/kubernetes/..."
  27. type FileToPrint struct {
  28. RealPath string
  29. PrintPath string
  30. }
  31. // NewFileToPrint makes a new instance of FileToPrint with the specified arguments
  32. func NewFileToPrint(realPath, printPath string) FileToPrint {
  33. return FileToPrint{
  34. RealPath: realPath,
  35. PrintPath: printPath,
  36. }
  37. }
  38. // PrintDryRunFile is a helper method around PrintDryRunFiles
  39. func PrintDryRunFile(fileName, realDir, printDir string, w io.Writer) error {
  40. return PrintDryRunFiles([]FileToPrint{
  41. NewFileToPrint(filepath.Join(realDir, fileName), filepath.Join(printDir, fileName)),
  42. }, w)
  43. }
  44. // PrintDryRunFiles prints the contents of the FileToPrints given to it to the writer w
  45. func PrintDryRunFiles(files []FileToPrint, w io.Writer) error {
  46. errs := []error{}
  47. for _, file := range files {
  48. if len(file.RealPath) == 0 {
  49. continue
  50. }
  51. fileBytes, err := ioutil.ReadFile(file.RealPath)
  52. if err != nil {
  53. errs = append(errs, err)
  54. continue
  55. }
  56. // Make it possible to fake the path of the file; i.e. you may want to tell the user
  57. // "Here is what would be written to /etc/kubernetes/admin.conf", although you wrote it to /tmp/kubeadm-dryrun/admin.conf and are loading it from there
  58. // Fall back to the "real" path if PrintPath is not set
  59. outputFilePath := file.PrintPath
  60. if len(outputFilePath) == 0 {
  61. outputFilePath = file.RealPath
  62. }
  63. fmt.Fprintf(w, "[dryrun] Would write file %q with content:\n", outputFilePath)
  64. apiclient.PrintBytesWithLinePrefix(w, fileBytes, "\t")
  65. }
  66. return errorsutil.NewAggregate(errs)
  67. }
  68. // Waiter is an implementation of apiclient.Waiter that should be used for dry-running
  69. type Waiter struct{}
  70. // NewWaiter returns a new Waiter object that talks to the given Kubernetes cluster
  71. func NewWaiter() apiclient.Waiter {
  72. return &Waiter{}
  73. }
  74. // WaitForAPI just returns a dummy nil, to indicate that the program should just proceed
  75. func (w *Waiter) WaitForAPI() error {
  76. fmt.Println("[dryrun] Would wait for the API Server's /healthz endpoint to return 'ok'")
  77. return nil
  78. }
  79. // WaitForPodsWithLabel just returns a dummy nil, to indicate that the program should just proceed
  80. func (w *Waiter) WaitForPodsWithLabel(kvLabel string) error {
  81. fmt.Printf("[dryrun] Would wait for the Pods with the label %q in the %s namespace to become Running\n", kvLabel, metav1.NamespaceSystem)
  82. return nil
  83. }
  84. // WaitForPodToDisappear just returns a dummy nil, to indicate that the program should just proceed
  85. func (w *Waiter) WaitForPodToDisappear(podName string) error {
  86. fmt.Printf("[dryrun] Would wait for the %q Pod in the %s namespace to be deleted\n", podName, metav1.NamespaceSystem)
  87. return nil
  88. }
  89. // WaitForHealthyKubelet blocks until the kubelet /healthz endpoint returns 'ok'
  90. func (w *Waiter) WaitForHealthyKubelet(_ time.Duration, healthzEndpoint string) error {
  91. fmt.Printf("[dryrun] Would make sure the kubelet %q endpoint is healthy\n", healthzEndpoint)
  92. return nil
  93. }
  94. // WaitForKubeletAndFunc is a wrapper for WaitForHealthyKubelet that also blocks for a function
  95. func (w *Waiter) WaitForKubeletAndFunc(f func() error) error {
  96. return nil
  97. }
  98. // SetTimeout is a no-op; we don't wait in this implementation
  99. func (w *Waiter) SetTimeout(_ time.Duration) {}
  100. // WaitForStaticPodControlPlaneHashes returns an empty hash for all control plane images;
  101. func (w *Waiter) WaitForStaticPodControlPlaneHashes(_ string) (map[string]string, error) {
  102. return map[string]string{
  103. constants.KubeAPIServer: "",
  104. constants.KubeControllerManager: "",
  105. constants.KubeScheduler: "",
  106. }, nil
  107. }
  108. // WaitForStaticPodSingleHash returns an empty hash
  109. // but the empty strings there are needed
  110. func (w *Waiter) WaitForStaticPodSingleHash(_ string, _ string) (string, error) {
  111. return "", nil
  112. }
  113. // WaitForStaticPodHashChange returns a dummy nil error in order for the flow to just continue as we're dryrunning
  114. func (w *Waiter) WaitForStaticPodHashChange(_, _, _ string) error {
  115. return nil
  116. }