util.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 bootstrap
  14. import (
  15. "context"
  16. "crypto/rand"
  17. "encoding/hex"
  18. "errors"
  19. "time"
  20. "k8s.io/api/core/v1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. clientset "k8s.io/client-go/kubernetes"
  25. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. )
  28. func newTokenSecret(tokenID, tokenSecret string) *v1.Secret {
  29. return &v1.Secret{
  30. ObjectMeta: metav1.ObjectMeta{
  31. Namespace: metav1.NamespaceSystem,
  32. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  33. },
  34. Type: bootstrapapi.SecretTypeBootstrapToken,
  35. Data: map[string][]byte{
  36. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  37. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  38. bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"),
  39. },
  40. }
  41. }
  42. // GenerateTokenID generates tokenID.
  43. func GenerateTokenID() (string, error) {
  44. tokenID, err := randBytes(TokenIDBytes)
  45. if err != nil {
  46. return "", err
  47. }
  48. return tokenID, nil
  49. }
  50. // GenerateTokenSecret generates tokenSecret.
  51. func GenerateTokenSecret() (string, error) {
  52. tokenSecret, err := randBytes(TokenSecretBytes)
  53. if err != nil {
  54. return "", err
  55. }
  56. return tokenSecret, err
  57. }
  58. func randBytes(length int) (string, error) {
  59. b := make([]byte, length)
  60. _, err := rand.Read(b)
  61. if err != nil {
  62. return "", err
  63. }
  64. return hex.EncodeToString(b), nil
  65. }
  66. func addSecretExpiration(s *v1.Secret, expiration string) {
  67. s.Data[bootstrapapi.BootstrapTokenExpirationKey] = []byte(expiration)
  68. }
  69. // TimeStringFromNow returns the time as a string from now.
  70. // e.g: 2019-12-03T14:30:40+08:00.
  71. func TimeStringFromNow(delta time.Duration) string {
  72. return time.Now().Add(delta).Format(time.RFC3339)
  73. }
  74. // WaitforSignedClusterInfoByBootStrapToken waits for signed cluster info by bootstrap token.
  75. func WaitforSignedClusterInfoByBootStrapToken(c clientset.Interface, tokenID string) error {
  76. return wait.Poll(framework.Poll, 2*time.Minute, func() (bool, error) {
  77. cfgMap, err := c.CoreV1().ConfigMaps(metav1.NamespacePublic).Get(context.TODO(), bootstrapapi.ConfigMapClusterInfo, metav1.GetOptions{})
  78. if err != nil {
  79. framework.Failf("Failed to get cluster-info configMap: %v", err)
  80. return false, err
  81. }
  82. _, ok := cfgMap.Data[bootstrapapi.JWSSignatureKeyPrefix+tokenID]
  83. if !ok {
  84. return false, nil
  85. }
  86. return true, nil
  87. })
  88. }
  89. // WaitForSignedClusterInfoGetUpdatedByBootstrapToken waits for signed cluster info to be updated by bootstrap token.
  90. func WaitForSignedClusterInfoGetUpdatedByBootstrapToken(c clientset.Interface, tokenID string, signedToken string) error {
  91. return wait.Poll(framework.Poll, 2*time.Minute, func() (bool, error) {
  92. cfgMap, err := c.CoreV1().ConfigMaps(metav1.NamespacePublic).Get(context.TODO(), bootstrapapi.ConfigMapClusterInfo, metav1.GetOptions{})
  93. if err != nil {
  94. framework.Failf("Failed to get cluster-info configMap: %v", err)
  95. return false, err
  96. }
  97. updated, ok := cfgMap.Data[bootstrapapi.JWSSignatureKeyPrefix+tokenID]
  98. if !ok || updated == signedToken {
  99. return false, nil
  100. }
  101. return true, nil
  102. })
  103. }
  104. // WaitForSignedClusterInfoByBootstrapTokenToDisappear waits for signed cluster info to be disappeared by bootstrap token.
  105. func WaitForSignedClusterInfoByBootstrapTokenToDisappear(c clientset.Interface, tokenID string) error {
  106. return wait.Poll(framework.Poll, 2*time.Minute, func() (bool, error) {
  107. cfgMap, err := c.CoreV1().ConfigMaps(metav1.NamespacePublic).Get(context.TODO(), bootstrapapi.ConfigMapClusterInfo, metav1.GetOptions{})
  108. if err != nil {
  109. framework.Failf("Failed to get cluster-info configMap: %v", err)
  110. return false, err
  111. }
  112. _, ok := cfgMap.Data[bootstrapapi.JWSSignatureKeyPrefix+tokenID]
  113. if ok {
  114. return false, nil
  115. }
  116. return true, nil
  117. })
  118. }
  119. // WaitForBootstrapTokenSecretToDisappear waits for bootstrap token secret to be disappeared.
  120. func WaitForBootstrapTokenSecretToDisappear(c clientset.Interface, tokenID string) error {
  121. return wait.Poll(framework.Poll, 1*time.Minute, func() (bool, error) {
  122. _, err := c.CoreV1().Secrets(metav1.NamespaceSystem).Get(context.TODO(), bootstrapapi.BootstrapTokenSecretPrefix+tokenID, metav1.GetOptions{})
  123. if apierrors.IsNotFound(err) {
  124. return true, nil
  125. }
  126. return false, nil
  127. })
  128. }
  129. // WaitForBootstrapTokenSecretNotDisappear waits for bootstrap token secret not to be disappeared and takes time for the specified timeout as success path.
  130. func WaitForBootstrapTokenSecretNotDisappear(c clientset.Interface, tokenID string, t time.Duration) error {
  131. err := wait.Poll(framework.Poll, t, func() (bool, error) {
  132. secret, err := c.CoreV1().Secrets(metav1.NamespaceSystem).Get(context.TODO(), bootstrapapi.BootstrapTokenSecretPrefix+tokenID, metav1.GetOptions{})
  133. if apierrors.IsNotFound(err) {
  134. return true, errors.New("secret not exists")
  135. }
  136. if secret != nil {
  137. return false, nil
  138. }
  139. return true, err
  140. })
  141. if err == wait.ErrWaitTimeout {
  142. return nil
  143. }
  144. return err
  145. }