Skip to content

Commit 1da3de9

Browse files
committed
Merge branch 'master' into crd-validation
2 parents 9b04d84 + d7a2032 commit 1da3de9

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

hack/tests/e2e-ansible-molecule.sh

+3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/memca
3232
cp "$ROOTDIR/test/ansible-memcached/asserts.yml" memcached-operator/molecule/default/asserts.yml
3333
cp "$ROOTDIR/test/ansible-memcached/molecule.yml" memcached-operator/molecule/test-local/molecule.yml
3434
cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/
35+
cp -a "$ROOTDIR/test/ansible-memcached/secret" memcached-operator/roles/
3536
cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml
3637
cat "$ROOTDIR/test/ansible-memcached/prepare-test-image.yml" >> memcached-operator/molecule/test-local/prepare.yml
38+
# Append v1 kind to watches to test watching already registered GVK
39+
cat "$ROOTDIR/test/ansible-memcached/watches-v1-kind.yaml" >> memcached-operator/watches.yaml
3740

3841

3942
# Test local

hack/tests/e2e-ansible.sh

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ cp "$ROOTDIR/test/ansible-memcached/tasks.yml" memcached-operator/roles/memcache
9393
cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/memcached/defaults/main.yml
9494
cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/
9595
cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml
96+
# Append Foo kind to watches to test watching multiple Kinds
9697
cat "$ROOTDIR/test/ansible-memcached/watches-foo-kind.yaml" >> memcached-operator/watches.yaml
9798

9899
pushd memcached-operator

pkg/ansible/controller/controller.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29+
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/apimachinery/pkg/runtime/schema"
3031
"sigs.k8s.io/controller-runtime/pkg/controller"
3132
crthandler "sigs.k8s.io/controller-runtime/pkg/handler"
@@ -64,12 +65,19 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
6465
ManageStatus: options.ManageStatus,
6566
}
6667

67-
// Register the GVK with the schema
68-
mgr.GetScheme().AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
69-
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
70-
Group: options.GVK.Group,
71-
Version: options.GVK.Version,
72-
})
68+
scheme := mgr.GetScheme()
69+
_, err := scheme.New(options.GVK)
70+
if runtime.IsNotRegisteredError(err) {
71+
// Register the GVK with the schema
72+
scheme.AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
73+
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
74+
Group: options.GVK.Group,
75+
Version: options.GVK.Version,
76+
})
77+
} else if err != nil {
78+
log.Error(err, "")
79+
os.Exit(1)
80+
}
7381

7482
//Create new controller runtime controller and set the controller to watch GVK.
7583
c, err := controller.New(fmt.Sprintf("%v-controller", strings.ToLower(options.GVK.Kind)), mgr, controller.Options{

test/ansible-memcached/asserts.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
resource_name=custom_resource.metadata.name
5757
)}}'
5858

59+
# This will verify that the `secret` role was executed
60+
- name: Verify that test-service was created
61+
assert:
62+
that: lookup('k8s', kind='Service', api_version='v1', namespace=namespace, resource_name='test-service')
63+
5964
- name: Delete the custom resource
6065
k8s:
6166
state: absent
@@ -74,10 +79,6 @@
7479
until: not cr.resources
7580
failed_when: cr.resources
7681

77-
- name: Verify the ConfigMap was deleted
78-
assert:
79-
that: not lookup('k8s', kind='ConfigMap', api_version='v1', namespace=namespace, resource_name='deleteme')
80-
8182
- name: Verify the Deployment was deleted (wait 30s)
8283
assert:
8384
that: not lookup('k8s', kind='Deployment', api_version='apps/v1', namespace=namespace, label_selector='app=memcached')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- name: Create test service
2+
k8s:
3+
definition:
4+
kind: Service
5+
api_version: v1
6+
metadata:
7+
name: test-service
8+
namespace: default
9+
spec:
10+
ports:
11+
- protocol: TCP
12+
port: 8332
13+
targetPort: 8332
14+
name: rpc

test/ansible-memcached/tasks.yml

+10
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@
4343
namespace: "{{ meta.namespace }}"
4444
status:
4545
test: "hello world"
46+
47+
- k8s:
48+
definition:
49+
kind: Secret
50+
apiVersion: v1
51+
metadata:
52+
name: test-secret
53+
namespace: "{{ meta.namespace }}"
54+
data:
55+
test: aGVsbG8K
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- version: v1
2+
group:
3+
kind: Secret
4+
role: /opt/ansible/roles/secret
5+
manageStatus: false

0 commit comments

Comments
 (0)