Skip to content

Commit eaf5876

Browse files
Merge pull request #192 from stuggi/memcached_tls_client_config
[tlse] add TLS status config, func to return server list and tls support
2 parents 038a5ec + 2c64377 commit eaf5876

File tree

6 files changed

+145
-36
lines changed

6 files changed

+145
-36
lines changed

apis/bases/memcached.openstack.org_memcacheds.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ spec:
134134
items:
135135
type: string
136136
type: array
137+
tlsSupport:
138+
description: Whether TLS is supported by the memcached instance
139+
type: boolean
137140
type: object
138141
type: object
139142
served: true
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

apis/memcached/v1beta1/memcached_types.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package v1beta1
1919
import (
2020
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
2121
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
22-
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
2322
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2423
)
2524

@@ -69,6 +68,9 @@ type MemcachedStatus struct {
6968

7069
// ServerListWithInet - List of memcached endpoints with inet(6) prefix
7170
ServerListWithInet []string `json:"serverListWithInet,omitempty" optional:"true"`
71+
72+
// Whether TLS is supported by the memcached instance
73+
TLSSupport bool `json:"tlsSupport,omitempty"`
7274
}
7375

7476
// +kubebuilder:object:root=true
@@ -97,33 +99,3 @@ type MemcachedList struct {
9799
func init() {
98100
SchemeBuilder.Register(&Memcached{}, &MemcachedList{})
99101
}
100-
101-
// IsReady - returns true if Memcached is reconciled successfully
102-
func (instance Memcached) IsReady() bool {
103-
return instance.Status.Conditions.IsTrue(condition.ReadyCondition)
104-
}
105-
106-
// RbacConditionsSet - set the conditions for the rbac object
107-
func (instance Memcached) RbacConditionsSet(c *condition.Condition) {
108-
instance.Status.Conditions.Set(c)
109-
}
110-
111-
// RbacNamespace - return the namespace
112-
func (instance Memcached) RbacNamespace() string {
113-
return instance.Namespace
114-
}
115-
116-
// RbacResourceName - return the name to be used for rbac objects (serviceaccount, role, rolebinding)
117-
func (instance Memcached) RbacResourceName() string {
118-
return "memcached-" + instance.Name
119-
}
120-
121-
// SetupDefaults - initializes any CRD field defaults based on environment variables (the defaulting mechanism itself is implemented via webhooks)
122-
func SetupDefaults() {
123-
// Acquire environmental defaults and initialize Memcached defaults with them
124-
memcachedDefaults := MemcachedDefaults{
125-
ContainerImageURL: util.GetEnvVar("RELATED_IMAGE_INFRA_MEMCACHED_IMAGE_URL_DEFAULT", MemcachedContainerImage),
126-
}
127-
128-
SetupMemcachedDefaults(memcachedDefaults)
129-
}

config/crd/bases/memcached.openstack.org_memcacheds.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ spec:
134134
items:
135135
type: string
136136
type: array
137+
tlsSupport:
138+
description: Whether TLS is supported by the memcached instance
139+
type: boolean
137140
type: object
138141
type: object
139142
served: true

controllers/memcached/memcached_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,10 @@ func (r *Reconciler) generateConfigMaps(
341341
"-o ssl_chain_cert=/etc/pki/tls/certs/memcached.crt " +
342342
"-o ssl_key=/etc/pki/tls/private/memcached.key " +
343343
"-o ssl_ca_cert=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"
344+
instance.Status.TLSSupport = true
344345
} else {
345346
memcachedTLSConfig = ""
347+
instance.Status.TLSSupport = false
346348
}
347349
templateParameters := map[string]interface{}{
348350
"memcachedTLSConfig": memcachedTLSConfig,
@@ -460,7 +462,7 @@ func (r *Reconciler) GetServerLists(
460462
}
461463

462464
for i := int32(0); i < *(instance.Spec.Replicas); i++ {
463-
server := fmt.Sprintf("%s-%d.%s", instance.Name, i, instance.Name)
465+
server := fmt.Sprintf("%s-%d.%s.%s.svc", instance.Name, i, instance.Name, instance.Namespace)
464466
serverList = append(serverList, fmt.Sprintf("%s:%d", server, memcached.MemcachedPort))
465467

466468
// python-memcached requires inet(6) prefix according to the IP version

tests/kuttl/tests/memcached/01-assert.yaml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,41 @@ spec:
1212
replicas: 1
1313
status:
1414
readyCount: 1
15-
serverList:
16-
- 'memcached-0.memcached:11211'
17-
serverListWithInet:
18-
- 'inet:[memcached-0.memcached]:11211'
15+
---
16+
# the namespace of the fqdn of the serverList is namespace
17+
# dependent, so we can't rely on kuttl asserts to check them. This short script
18+
# gathers the first entry and checks that it matches the regex
19+
apiVersion: kuttl.dev/v1beta1
20+
kind: TestAssert
21+
commands:
22+
- script: |
23+
# get the first memcached from serverList and validate
24+
template='{{ (index .status.serverList 0) }}'
25+
regex="memcached-0.memcached.$NAMESPACE.svc:11211"
26+
memcached=$(oc get -n $NAMESPACE memcached memcached -o go-template="$template")
27+
matches=$(echo "$memcached" | sed -e "s?$regex??")
28+
if [ -z "$matches" ]; then
29+
exit 0
30+
else
31+
echo "Memcached Server: $memcached do not match regex"
32+
exit 1
33+
fi
34+
---
35+
# the namespace of the fqdn of the serverListWithInet is namespace
36+
# dependent, so we can't rely on kuttl asserts to check them. This short script
37+
# gathers the first entry and checks that it matches the regex
38+
apiVersion: kuttl.dev/v1beta1
39+
kind: TestAssert
40+
commands:
41+
- script: |
42+
# get the first memcached from serverListWithInet and validate
43+
template='{{ (index .status.serverListWithInet 0) }}'
44+
regex="inet:\[memcached-0.memcached.$NAMESPACE.svc\]:11211"
45+
memcached=$(oc get -n $NAMESPACE memcached memcached -o go-template="$template")
46+
matches=$(echo "$memcached" | sed -e "s?$regex??")
47+
if [ -z "$matches" ]; then
48+
exit 0
49+
else
50+
echo "Memcached ServerListWithInet: $memcached do not match regex"
51+
exit 1
52+
fi

0 commit comments

Comments
 (0)