topic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2021 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. "github.com/rook/rook/tests/framework/installer"
  16. "github.com/rook/rook/tests/framework/utils"
  17. kerrors "k8s.io/apimachinery/pkg/api/errors"
  18. )
  19. // TopicOperation is a wrapper for rook topic operations
  20. type TopicOperation struct {
  21. k8sh *utils.K8sHelper
  22. manifests installer.CephManifests
  23. }
  24. // CreateTopicOperation creates a new topic client
  25. func CreateTopicOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *TopicOperation {
  26. return &TopicOperation{k8sh, manifests}
  27. }
  28. func (t *TopicOperation) CreateTopic(topicName string, storeName string, httpEndpointService string) error {
  29. return t.k8sh.ResourceOperation("create", t.manifests.GetBucketTopic(topicName, storeName, httpEndpointService))
  30. }
  31. func (t *TopicOperation) DeleteTopic(topicName string, storeName string, httpEndpointService string) error {
  32. return t.k8sh.ResourceOperation("delete", t.manifests.GetBucketTopic(topicName, storeName, httpEndpointService))
  33. }
  34. func (t *TopicOperation) UpdateTopic(topicName string, storeName string, httpEndpointService string) error {
  35. return t.k8sh.ResourceOperation("apply", t.manifests.GetBucketTopic(topicName, storeName, httpEndpointService))
  36. }
  37. // CheckTopic if topic has an ARN set in its status
  38. func (t *TopicOperation) CheckTopic(topicName string) bool {
  39. const resourceName = "cephbuckettopic"
  40. _, err := t.k8sh.GetResource(resourceName, topicName)
  41. if err != nil {
  42. logger.Infof("%q %q does not exist", resourceName, topicName)
  43. return false
  44. }
  45. topicARN, _ := t.k8sh.GetResource(resourceName, topicName, "--output", "jsonpath={.status.ARN}")
  46. if topicARN == "" {
  47. logger.Infof("%q %q exist, but ARN was not set", resourceName, topicName)
  48. return false
  49. }
  50. logger.Infof("topic ARN is %q", topicARN)
  51. return true
  52. }
  53. func (t *TopicOperation) CreateHTTPServer(serverName, namespace, port string) error {
  54. // TODO: Fix https://github.com/rook/rook/issues/9741, do not use third party image
  55. deployment := `
  56. ---
  57. apiVersion: apps/v1
  58. kind: Deployment
  59. metadata:
  60. name: ` + serverName + `
  61. namespace: ` + namespace + `
  62. spec:
  63. replicas: 1
  64. selector:
  65. matchLabels:
  66. app: ` + serverName + `
  67. template:
  68. metadata:
  69. labels:
  70. app: ` + serverName + `
  71. spec:
  72. containers:
  73. - name: ` + serverName + `
  74. image: quay.io/jthottan/pythonwebserver:latest
  75. ---
  76. apiVersion: v1
  77. kind: Service
  78. metadata:
  79. name: ` + serverName + `
  80. namespace: ` + namespace + `
  81. spec:
  82. type: NodePort
  83. selector:
  84. app: ` + serverName + `
  85. ports:
  86. - port: ` + port + `
  87. targetPort: ` + port + `
  88. `
  89. _, err := t.k8sh.KubectlWithStdin(deployment, []string{"apply", "-f", "-"}...)
  90. if err != nil && !kerrors.IsAlreadyExists(err) {
  91. return err
  92. }
  93. appLabel := "app=" + serverName
  94. err = t.k8sh.WaitForLabeledPodsToRun(appLabel, namespace)
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }