bucket.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2016 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 clients
  14. import (
  15. b64 "encoding/base64"
  16. "fmt"
  17. "github.com/aws/aws-sdk-go/service/s3"
  18. bktv1alpha1 "github.com/kube-object-storage/lib-bucket-provisioner/pkg/apis/objectbucket.io/v1alpha1"
  19. rgw "github.com/rook/rook/pkg/operator/ceph/object"
  20. "github.com/rook/rook/tests/framework/installer"
  21. "github.com/rook/rook/tests/framework/utils"
  22. )
  23. // BucketOperation is a wrapper for rook bucket operations
  24. type BucketOperation struct {
  25. k8sh *utils.K8sHelper
  26. manifests installer.CephManifests
  27. }
  28. // CreateBucketOperation creates a new bucket client
  29. func CreateBucketOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *BucketOperation {
  30. return &BucketOperation{k8sh, manifests}
  31. }
  32. func (b *BucketOperation) CreateBucketStorageClass(namespace string, storeName string, storageClassName string, reclaimPolicy string) error {
  33. return b.k8sh.ResourceOperation("create", b.manifests.GetBucketStorageClass(storeName, storageClassName, reclaimPolicy))
  34. }
  35. func (b *BucketOperation) DeleteBucketStorageClass(namespace string, storeName string, storageClassName string, reclaimPolicy string) error {
  36. err := b.k8sh.ResourceOperation("delete", b.manifests.GetBucketStorageClass(storeName, storageClassName, reclaimPolicy))
  37. return err
  38. }
  39. func (b *BucketOperation) CreateObc(obcName string, storageClassName string, bucketName string, maxObject string, createBucket bool) error {
  40. return b.k8sh.ResourceOperation("create", b.manifests.GetOBC(obcName, storageClassName, bucketName, maxObject, createBucket))
  41. }
  42. func (b *BucketOperation) CreateObcNotification(obcName string, storageClassName string, bucketName string, notification string, createBucket bool) error {
  43. return b.k8sh.ResourceOperation("create", b.manifests.GetOBCNotification(obcName, storageClassName, bucketName, notification, createBucket))
  44. }
  45. func (b *BucketOperation) DeleteObc(obcName string, storageClassName string, bucketName string, maxObject string, createBucket bool) error {
  46. return b.k8sh.ResourceOperation("delete", b.manifests.GetOBC(obcName, storageClassName, bucketName, maxObject, createBucket))
  47. }
  48. func (b *BucketOperation) UpdateObc(obcName string, storageClassName string, bucketName string, maxObject string, createBucket bool) error {
  49. return b.k8sh.ResourceOperation("apply", b.manifests.GetOBC(obcName, storageClassName, bucketName, maxObject, createBucket))
  50. }
  51. func (b *BucketOperation) UpdateObcNotificationAdd(obcName string, storageClassName string, bucketName string, notification string, createBucket bool) error {
  52. return b.k8sh.ResourceOperation("apply", b.manifests.GetOBCNotification(obcName, storageClassName, bucketName, notification, createBucket))
  53. }
  54. func (b *BucketOperation) UpdateObcNotificationRemove(obcName string, storageClassName string, bucketName string, maxObject string, createBucket bool) error {
  55. return b.k8sh.ResourceOperation("apply", b.manifests.GetOBC(obcName, storageClassName, bucketName, maxObject, createBucket))
  56. }
  57. // CheckOBC, returns true if the obc, secret and configmap are all in the "check" state,
  58. // and returns false if any of these resources are not in the "check" state.
  59. // Check state values:
  60. //
  61. // "created", all must exist,
  62. // "bound", all must exist and OBC in Bound phase
  63. // "deleted", all must be missing.
  64. func (b *BucketOperation) CheckOBC(obcName, check string) bool {
  65. resources := []string{"obc", "secret", "configmap"}
  66. shouldBeBound := (check == "bound")
  67. shouldExist := (shouldBeBound || check == "created") // bound implies created
  68. for _, res := range resources {
  69. _, err := b.k8sh.GetResource(res, obcName)
  70. // note: we assume a `GetResource` error is a missing resource
  71. if shouldExist == (err != nil) {
  72. return false
  73. }
  74. logger.Infof("%s %s %s", res, obcName, check)
  75. }
  76. logger.Infof("%s resources %v all %s", obcName, resources, check)
  77. if shouldBeBound {
  78. // OBC should be in bound phase as well as existing
  79. state, _ := b.k8sh.GetResource("obc", obcName, "--output", "jsonpath={.status.phase}")
  80. boundPhase := bktv1alpha1.ObjectBucketClaimStatusPhaseBound // i.e., "Bound"
  81. if state != boundPhase {
  82. logger.Infof(`resources exist, but OBC is not in %q phase: %q`, boundPhase, state)
  83. return false
  84. }
  85. // Regression test: OBC should have spec.objectBucketName set
  86. obName, _ := b.k8sh.GetResource("obc", obcName, "--output", "jsonpath={.spec.objectBucketName}")
  87. if obName == "" {
  88. logger.Error("failed regression: OBC spec.objectBucketName is not set")
  89. return false
  90. }
  91. // Regression test: OB should have claim ref to OBC
  92. refName, _ := b.k8sh.GetResource("ob", obName, "--output", "jsonpath={.spec.claimRef.name}")
  93. if refName != obcName {
  94. logger.Errorf("failed regression: OB spec.claimRef.name (%q) does not match expected OBC name (%q)", refName, obcName)
  95. return false
  96. }
  97. logger.Infof("OBC is %q", boundPhase)
  98. }
  99. return true
  100. }
  101. // Fetch SecretKey, AccessKey for s3 client.
  102. func (b *BucketOperation) GetAccessKey(obcName string) (string, error) {
  103. args := []string{"get", "secret", obcName, "-o", "jsonpath={@.data.AWS_ACCESS_KEY_ID}"}
  104. AccessKey, err := b.k8sh.Kubectl(args...)
  105. if err != nil {
  106. return "", fmt.Errorf("Unable to find access key -- %s", err)
  107. }
  108. decode, _ := b64.StdEncoding.DecodeString(AccessKey)
  109. return string(decode), nil
  110. }
  111. func (b *BucketOperation) GetSecretKey(obcName string) (string, error) {
  112. args := []string{"get", "secret", obcName, "-o", "jsonpath={@.data.AWS_SECRET_ACCESS_KEY}"}
  113. SecretKey, err := b.k8sh.Kubectl(args...)
  114. if err != nil {
  115. return "", fmt.Errorf("Unable to find secret key-- %s", err)
  116. }
  117. decode, _ := b64.StdEncoding.DecodeString(SecretKey)
  118. return string(decode), nil
  119. }
  120. // Checks whether MaxObject is updated for ob
  121. func (b *BucketOperation) CheckOBMaxObject(obcName, maxobject string) bool {
  122. obName, _ := b.k8sh.GetResource("obc", obcName, "--output", "jsonpath={.spec.objectBucketName}")
  123. fetchMaxObject, _ := b.k8sh.GetResource("ob", obName, "--output", "jsonpath={.spec.endpoint.additionalConfig.maxObjects}")
  124. return maxobject == fetchMaxObject
  125. }
  126. // Checks the bucket notifications set on RGW backend bucket
  127. func (b *BucketOperation) CheckBucketNotificationSetonRGW(namespace, storeName, obcName, bucketname, notificationName string, helper *TestClient, tlsEnabled bool) bool {
  128. var s3client *rgw.S3Agent
  129. var err error
  130. s3endpoint, _ := helper.ObjectClient.GetEndPointUrl(namespace, storeName)
  131. s3AccessKey, _ := helper.BucketClient.GetAccessKey(obcName)
  132. s3SecretKey, _ := helper.BucketClient.GetSecretKey(obcName)
  133. if tlsEnabled {
  134. s3client, err = rgw.NewInsecureS3Agent(s3AccessKey, s3SecretKey, s3endpoint, true)
  135. } else {
  136. s3client, err = rgw.NewS3Agent(s3AccessKey, s3SecretKey, s3endpoint, true, nil)
  137. }
  138. if err != nil {
  139. logger.Infof("failed to s3client due to %v", err)
  140. return false
  141. }
  142. logger.Infof("endpoint (%s) Accesskey (%s) secret (%s)", s3endpoint, s3AccessKey, s3SecretKey)
  143. notifications, err := s3client.Client.GetBucketNotificationConfiguration(&s3.GetBucketNotificationConfigurationRequest{
  144. Bucket: &bucketname,
  145. })
  146. if err != nil {
  147. logger.Infof("failed to fetch bucket notifications configuration due to %v", err)
  148. return false
  149. }
  150. logger.Infof("%d bucket notifications found in: %+v", len(notifications.TopicConfigurations), notifications)
  151. for _, notification := range notifications.TopicConfigurations {
  152. if *notification.Id == notificationName {
  153. return true
  154. }
  155. logger.Infof("bucket notifications name mismatch %q != %q", *notification.Id, notificationName)
  156. }
  157. return false
  158. }