Skip to content

Commit

Permalink
pkg/ansible/controller; Check if GVK is already registered (#991)
Browse files Browse the repository at this point in the history
* pkg/ansible/controller; Check if GVK is already registered
* Add CI tests
  • Loading branch information
dymurray authored and Shawn Hurley committed Jan 29, 2019
1 parent cc5fe88 commit c8f9b94
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
4 changes: 4 additions & 0 deletions hack/tests/e2e-ansible-molecule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/memca
cp "$ROOTDIR/test/ansible-memcached/asserts.yml" memcached-operator/molecule/default/asserts.yml
cp "$ROOTDIR/test/ansible-memcached/molecule.yml" memcached-operator/molecule/test-local/molecule.yml
cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/
cp -a "$ROOTDIR/test/ansible-memcached/secret" memcached-operator/roles/
cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml
cat "$ROOTDIR/test/ansible-memcached/prepare-test-image.yml" >> memcached-operator/molecule/test-local/prepare.yml
# Append v1 kind to watches to test watching already registered GVK
cat "$ROOTDIR/test/ansible-memcached/watches-v1-kind.yaml" >> memcached-operator/watches.yaml


# Test local
Expand Down
2 changes: 2 additions & 0 deletions hack/tests/e2e-ansible.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ cp "$ROOTDIR/test/ansible-memcached/tasks.yml" memcached-operator/roles/memcache
cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/memcached/defaults/main.yml
cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/
cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml
# Append Foo kind to watches to test watching multiple Kinds
cat "$ROOTDIR/test/ansible-memcached/watches-foo-kind.yaml" >> memcached-operator/watches.yaml

pushd memcached-operator
sed -i 's|\(FROM quay.io/operator-framework/ansible-operator\)\(:.*\)\?|\1:dev|g' build/Dockerfile
Expand Down
25 changes: 13 additions & 12 deletions pkg/ansible/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/controller"
crthandler "sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -64,19 +65,19 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
ManageStatus: options.ManageStatus,
}

if mgr.GetScheme().IsVersionRegistered(schema.GroupVersion{
Group: options.GVK.Group,
Version: options.GVK.Version,
}) {
log.Info("Version already registered... skipping")
return nil
scheme := mgr.GetScheme()
_, err := scheme.New(options.GVK)
if runtime.IsNotRegisteredError(err) {
// Register the GVK with the schema
scheme.AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
Group: options.GVK.Group,
Version: options.GVK.Version,
})
} else if err != nil {
log.Error(err, "")
os.Exit(1)
}
// Register the GVK with the schema
mgr.GetScheme().AddKnownTypeWithName(options.GVK, &unstructured.Unstructured{})
metav1.AddToGroupVersion(mgr.GetScheme(), schema.GroupVersion{
Group: options.GVK.Group,
Version: options.GVK.Version,
})

//Create new controller runtime controller and set the controller to watch GVK.
c, err := controller.New(fmt.Sprintf("%v-controller", strings.ToLower(options.GVK.Kind)), mgr, controller.Options{
Expand Down
18 changes: 13 additions & 5 deletions test/ansible-memcached/asserts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
data:
delete: me

- name: Delete the custom resource
k8s:
state: absent
namespace: '{{ namespace }}'
definition: '{{ custom_resource }}'
# This will verify that the `secret` role was executed
- name: Verify that test-service was created
assert:
that: lookup('k8s', kind='Service', api_version='v1', namespace=namespace, resource_name='test-service')

- name: Delete the custom resource
k8s:
state: absent
Expand All @@ -63,10 +73,8 @@
until: not cr.resources
failed_when: cr.resources

- name: Verify the ConfigMap was deleted
assert:
that: not lookup('k8s', kind='ConfigMap', api_version='v1', namespace=namespace, resource_name='deleteme')

- name: Verify the Deployment was deleted
assert:
assert:
that: not lookup('k8s', kind='Deployment', api_version='apps/v1', namespace=namespace, label_selector='app=memcached')
retries: 10
delay: 3
14 changes: 14 additions & 0 deletions test/ansible-memcached/secret/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- name: Create test service
k8s:
definition:
kind: Service
api_version: v1
metadata:
name: test-service
namespace: default
spec:
ports:
- protocol: TCP
port: 8332
targetPort: 8332
name: rpc
18 changes: 18 additions & 0 deletions test/ansible-memcached/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@
port: 11211
initialDelaySeconds: 3
periodSeconds: 3

- k8s_status:
api_version: ansible.example.com/v1alpha1
kind: Memcached
name: "{{ meta.name }}"
namespace: "{{ meta.namespace }}"
status:
test: "hello world"

- k8s:
definition:
kind: Secret
apiVersion: v1
metadata:
name: test-secret
namespace: "{{ meta.namespace }}"
data:
test: aGVsbG8K
5 changes: 5 additions & 0 deletions test/ansible-memcached/watches-v1-kind.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- version: v1
group:
kind: Secret
role: /opt/ansible/roles/secret
manageStatus: false

0 comments on commit c8f9b94

Please sign in to comment.