snapshot.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright 2020 The Rook Authors. All rights reserved.
  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 utils
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. )
  23. const (
  24. // snapshotterVersion from which the snapshotcontroller and CRD will be
  25. // installed
  26. snapshotterVersion = "v5.0.1"
  27. repoURL = "https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter"
  28. rbacPath = "deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml"
  29. controllerPath = "deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yaml"
  30. // snapshot CRD path
  31. snapshotClassCRDPath = "client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml"
  32. volumeSnapshotContentsCRDPath = "client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml"
  33. volumeSnapshotCRDPath = "client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml"
  34. )
  35. // CheckSnapshotISReadyToUse checks snapshot is ready to use
  36. func (k8sh *K8sHelper) CheckSnapshotISReadyToUse(name, namespace string, retries int) (bool, error) {
  37. for i := 0; i < retries; i++ {
  38. // sleep first and try to check snapshot is ready to cover the error cases.
  39. time.Sleep(time.Duration(i) * time.Second)
  40. ready, err := k8sh.executor.ExecuteCommandWithOutput("kubectl", "get", "volumesnapshot", name, "--namespace", namespace, "-o", "jsonpath={.status.readyToUse}")
  41. if err != nil {
  42. return false, err
  43. }
  44. val, err := strconv.ParseBool(ready)
  45. if err != nil {
  46. logger.Errorf("failed to parse ready state of snapshot %q in namespace %q: error %+v", name, namespace, err)
  47. continue
  48. }
  49. if val {
  50. return true, nil
  51. }
  52. }
  53. return false, fmt.Errorf("giving up waiting for %q snapshot in namespace %q", name, namespace)
  54. }
  55. // snapshotController creates or deletes the snapshotcontroller and required RBAC
  56. func (k8sh *K8sHelper) snapshotController(action string) error {
  57. controllerURL := fmt.Sprintf("%s/%s/%s", repoURL, snapshotterVersion, controllerPath)
  58. controllerManifest, err := getManifestFromURL(controllerURL)
  59. if err != nil {
  60. return err
  61. }
  62. controllerManifest = strings.Replace(controllerManifest, "canary", snapshotterVersion, -1)
  63. logger.Infof("snapshot controller: %s", controllerManifest)
  64. _, err = k8sh.KubectlWithStdin(controllerManifest, action, "-f", "-")
  65. if err != nil {
  66. return err
  67. }
  68. rbac := fmt.Sprintf("%s/%s/%s", repoURL, snapshotterVersion, rbacPath)
  69. _, err = k8sh.Kubectl(action, "-f", rbac)
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. // WaitForSnapshotController check snapshotcontroller is ready within given
  76. // retries count.
  77. func (k8sh *K8sHelper) WaitForSnapshotController(retries int) error {
  78. namespace := "kube-system"
  79. ctx := context.TODO()
  80. snapshotterName := "snapshot-controller"
  81. for i := 0; i < retries; i++ {
  82. ss, err := k8sh.Clientset.AppsV1().Deployments(namespace).Get(ctx, snapshotterName, metav1.GetOptions{})
  83. if err != nil && !apierrors.IsNotFound(err) {
  84. return err
  85. }
  86. if ss.Status.ReadyReplicas > 0 && ss.Status.ReadyReplicas == ss.Status.Replicas {
  87. return nil
  88. }
  89. logger.Infof("waiting for %q deployment in namespace %q (readyreplicas %d < replicas %d)", snapshotterName, namespace, ss.Status.ReadyReplicas, ss.Status.Replicas)
  90. time.Sleep(RetryInterval * time.Second)
  91. }
  92. return fmt.Errorf("giving up waiting for %q deployment in namespace %q", snapshotterName, namespace)
  93. }
  94. // CreateSnapshotController creates the snapshotcontroller and required RBAC
  95. func (k8sh *K8sHelper) CreateSnapshotController() error {
  96. return k8sh.snapshotController("create")
  97. }
  98. // DeleteSnapshotController delete the snapshotcontroller and required RBAC
  99. func (k8sh *K8sHelper) DeleteSnapshotController() error {
  100. return k8sh.snapshotController("delete")
  101. }
  102. // snapshotCRD can be used for creating or deleting the snapshot CRD's
  103. func (k8sh *K8sHelper) snapshotCRD(action string) error {
  104. // setting validate=false to skip CRD validation during create operation to
  105. // support lower Kubernetes versions.
  106. args := func(crdpath string) []string {
  107. a := []string{
  108. action,
  109. "-f",
  110. crdpath,
  111. }
  112. if action == "create" {
  113. a = append(a, "--validate=false")
  114. }
  115. return a
  116. }
  117. snapshotClassCRD := fmt.Sprintf("%s/%s/%s", repoURL, snapshotterVersion, snapshotClassCRDPath)
  118. _, err := k8sh.Kubectl(args(snapshotClassCRD)...)
  119. if err != nil {
  120. return err
  121. }
  122. snapshotContentsCRD := fmt.Sprintf("%s/%s/%s", repoURL, snapshotterVersion, volumeSnapshotContentsCRDPath)
  123. _, err = k8sh.Kubectl(args(snapshotContentsCRD)...)
  124. if err != nil {
  125. return err
  126. }
  127. snapshotCRD := fmt.Sprintf("%s/%s/%s", repoURL, snapshotterVersion, volumeSnapshotCRDPath)
  128. _, err = k8sh.Kubectl(args(snapshotCRD)...)
  129. if err != nil {
  130. return err
  131. }
  132. return nil
  133. }
  134. // CreateSnapshotCRD creates the snapshot CRD
  135. func (k8sh *K8sHelper) CreateSnapshotCRD() error {
  136. return k8sh.snapshotCRD("create")
  137. }
  138. // DeleteSnapshotCRD deletes the snapshot CRD
  139. func (k8sh *K8sHelper) DeleteSnapshotCRD() error {
  140. return k8sh.snapshotCRD("delete")
  141. }