|
| 1 | +/* |
| 2 | +Copyright 2023. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + |
| 23 | + condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 24 | + "github.com/openstack-k8s-operators/lib-common/modules/common/helper" |
| 25 | + "github.com/openstack-k8s-operators/lib-common/modules/common/util" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | +) |
| 28 | + |
| 29 | +// IsReady - returns true if Memcached is reconciled successfully |
| 30 | +func (instance Memcached) IsReady() bool { |
| 31 | + return instance.Status.Conditions.IsTrue(condition.ReadyCondition) |
| 32 | +} |
| 33 | + |
| 34 | +// RbacConditionsSet - set the conditions for the rbac object |
| 35 | +func (instance Memcached) RbacConditionsSet(c *condition.Condition) { |
| 36 | + instance.Status.Conditions.Set(c) |
| 37 | +} |
| 38 | + |
| 39 | +// RbacNamespace - return the namespace |
| 40 | +func (instance Memcached) RbacNamespace() string { |
| 41 | + return instance.Namespace |
| 42 | +} |
| 43 | + |
| 44 | +// RbacResourceName - return the name to be used for rbac objects (serviceaccount, role, rolebinding) |
| 45 | +func (instance Memcached) RbacResourceName() string { |
| 46 | + return "memcached-" + instance.Name |
| 47 | +} |
| 48 | + |
| 49 | +// SetupDefaults - initializes any CRD field defaults based on environment variables (the defaulting mechanism itself is implemented via webhooks) |
| 50 | +func SetupDefaults() { |
| 51 | + // Acquire environmental defaults and initialize Memcached defaults with them |
| 52 | + memcachedDefaults := MemcachedDefaults{ |
| 53 | + ContainerImageURL: util.GetEnvVar("RELATED_IMAGE_INFRA_MEMCACHED_IMAGE_URL_DEFAULT", MemcachedContainerImage), |
| 54 | + } |
| 55 | + |
| 56 | + SetupMemcachedDefaults(memcachedDefaults) |
| 57 | +} |
| 58 | + |
| 59 | +// GetMemcachedServerListString - return the memcached servers as comma separated list |
| 60 | +// to be used in OpenStack config. |
| 61 | +func (instance *Memcached) GetMemcachedServerListString() string { |
| 62 | + return strings.Join(instance.Status.ServerList, ",") |
| 63 | +} |
| 64 | + |
| 65 | +// GetMemcachedServerListWithInetString - return the memcached servers as comma separated list |
| 66 | +// to be used in OpenStack config. |
| 67 | +func (instance *Memcached) GetMemcachedServerListWithInetString() string { |
| 68 | + return strings.Join(instance.Status.ServerListWithInet, ",") |
| 69 | +} |
| 70 | + |
| 71 | +// GetMemcachedTLSSupport - return the TLS support of the memcached instance |
| 72 | +func (instance *Memcached) GetMemcachedTLSSupport() bool { |
| 73 | + return instance.Status.TLSSupport |
| 74 | +} |
| 75 | + |
| 76 | +// GetMemcachedByName - gets the Memcached instance |
| 77 | +func GetMemcachedByName( |
| 78 | + ctx context.Context, |
| 79 | + h *helper.Helper, |
| 80 | + name string, |
| 81 | + namespace string, |
| 82 | +) (*Memcached, error) { |
| 83 | + memcached := &Memcached{} |
| 84 | + err := h.GetClient().Get( |
| 85 | + ctx, |
| 86 | + types.NamespacedName{ |
| 87 | + Name: name, |
| 88 | + Namespace: namespace, |
| 89 | + }, |
| 90 | + memcached) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + return memcached, err |
| 95 | +} |
0 commit comments