bootstrap_token_cleaner.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "time"
  16. . "github.com/onsi/ginkgo"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. "k8s.io/kubernetes/test/e2e/lifecycle"
  22. )
  23. var secretNeedClean string
  24. var _ = lifecycle.SIGDescribe("[Feature:BootstrapTokens]", func() {
  25. var c clientset.Interface
  26. f := framework.NewDefaultFramework("bootstrap-token-cleaner")
  27. BeforeEach(func() {
  28. c = f.ClientSet
  29. })
  30. AfterEach(func() {
  31. if len(secretNeedClean) > 0 {
  32. By("delete the bootstrap token secret")
  33. err := c.CoreV1().Secrets(metav1.NamespaceSystem).Delete(secretNeedClean, &metav1.DeleteOptions{})
  34. secretNeedClean = ""
  35. framework.ExpectNoError(err)
  36. }
  37. })
  38. It("should delete the token secret when the secret expired", func() {
  39. By("create a new expired bootstrap token secret")
  40. tokenId, err := GenerateTokenId()
  41. framework.ExpectNoError(err)
  42. tokenSecret, err := GenerateTokenSecret()
  43. framework.ExpectNoError(err)
  44. secret := newTokenSecret(tokenId, tokenSecret)
  45. addSecretExpiration(secret, TimeStringFromNow(-time.Hour))
  46. _, err = c.CoreV1().Secrets(metav1.NamespaceSystem).Create(secret)
  47. framework.ExpectNoError(err)
  48. By("wait for the bootstrap token secret be deleted")
  49. err = WaitForBootstrapTokenSecretToDisappear(c, tokenId)
  50. framework.ExpectNoError(err)
  51. })
  52. It("should not delete the token secret when the secret is not expired", func() {
  53. By("create a new expired bootstrap token secret")
  54. tokenId, err := GenerateTokenId()
  55. framework.ExpectNoError(err)
  56. tokenSecret, err := GenerateTokenSecret()
  57. framework.ExpectNoError(err)
  58. secret := newTokenSecret(tokenId, tokenSecret)
  59. addSecretExpiration(secret, TimeStringFromNow(time.Hour))
  60. _, err = c.CoreV1().Secrets(metav1.NamespaceSystem).Create(secret)
  61. secretNeedClean = bootstrapapi.BootstrapTokenSecretPrefix + tokenId
  62. framework.ExpectNoError(err)
  63. By("wait for the bootstrap token secret not be deleted")
  64. err = WaitForBootstrapTokenSecretNotDisappear(c, tokenId, 20*time.Second)
  65. framework.ExpectNoError(err)
  66. })
  67. })