object.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2018 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 v1
  14. import (
  15. "github.com/pkg/errors"
  16. )
  17. const ServiceServingCertKey = "service.beta.openshift.io/serving-cert-secret-name"
  18. // 38 is the max length of a ceph store name as total length of the resource name cannot be more than 63 characters limit
  19. // and there is a configmap which is formed by appending `rook-ceph-rgw-<STORE-NAME>-mime-types`
  20. // so over all it brings up to (63-14-11 = 38) characters for the store name
  21. const objectStoreNameMaxLen = 38
  22. func (s *ObjectStoreSpec) IsMultisite() bool {
  23. return s.Zone.Name != ""
  24. }
  25. func (s *ObjectStoreSpec) IsTLSEnabled() bool {
  26. return s.Gateway.SecurePort != 0 && (s.Gateway.SSLCertificateRef != "" || s.GetServiceServingCert() != "")
  27. }
  28. func (s *ObjectStoreSpec) IsRGWDashboardEnabled() bool {
  29. return s.Gateway.DashboardEnabled == nil || *s.Gateway.DashboardEnabled
  30. }
  31. func (s *ObjectStoreSpec) GetPort() (int32, error) {
  32. if s.IsTLSEnabled() {
  33. return s.Gateway.SecurePort, nil
  34. } else if s.Gateway.Port != 0 {
  35. return s.Gateway.Port, nil
  36. }
  37. return -1, errors.New("At least one of Port or SecurePort should be non-zero")
  38. }
  39. func (s *ObjectStoreSpec) IsExternal() bool {
  40. return len(s.Gateway.ExternalRgwEndpoints) != 0
  41. }
  42. func (s *ObjectStoreSpec) IsHostNetwork(c *ClusterSpec) bool {
  43. if s.Gateway.HostNetwork != nil {
  44. return *s.Gateway.HostNetwork
  45. }
  46. return c.Network.IsHost()
  47. }
  48. func (s *ObjectRealmSpec) IsPullRealm() bool {
  49. return s.Pull.Endpoint != ""
  50. }
  51. // ValidateObjectSpec validate the object store arguments
  52. func ValidateObjectSpec(gs *CephObjectStore) error {
  53. if gs.Name == "" {
  54. return errors.New("missing name")
  55. }
  56. if gs.Namespace == "" {
  57. return errors.New("missing namespace")
  58. }
  59. // validate the object store name only if it is not an external cluster
  60. // as external cluster won't create the rgw daemon and it's other resources
  61. // and there is some legacy external cluster which has more length of objectstore
  62. // so to run them successfully we are not validating the objectstore name
  63. if !gs.Spec.IsExternal() {
  64. if len(gs.Name) > objectStoreNameMaxLen {
  65. return errors.New("object store name cannot be longer than 38 characters")
  66. }
  67. }
  68. securePort := gs.Spec.Gateway.SecurePort
  69. if securePort < 0 || securePort > 65535 {
  70. return errors.Errorf("securePort value of %d must be between 0 and 65535", securePort)
  71. }
  72. if gs.Spec.Gateway.Port <= 0 && gs.Spec.Gateway.SecurePort <= 0 {
  73. return errors.New("invalid create: either of port or securePort fields should be not be zero")
  74. }
  75. return nil
  76. }
  77. func (s *ObjectStoreSpec) GetServiceServingCert() string {
  78. if s.Gateway.Service != nil {
  79. return s.Gateway.Service.Annotations[ServiceServingCertKey]
  80. }
  81. return ""
  82. }
  83. func (c *CephObjectStore) GetStatusConditions() *[]Condition {
  84. return &c.Status.Conditions
  85. }
  86. func (z *CephObjectZone) GetStatusConditions() *[]Condition {
  87. return &z.Status.Conditions
  88. }
  89. // String returns an addressable string representation of the EndpointAddress.
  90. func (e *EndpointAddress) String() string {
  91. // hostname is easier to read, and it is probably less likely to change, so prefer it over IP
  92. if e.Hostname != "" {
  93. return e.Hostname
  94. }
  95. return e.IP
  96. }