Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/ansible/controller; Check if GVK is already registered (#991) #1019

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hack/tests/e2e-ansible-molecule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +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
1 change: 1 addition & 0 deletions hack/tests/e2e-ansible.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ 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
Expand Down
20 changes: 14 additions & 6 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,12 +65,19 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
ManageStatus: options.ManageStatus,
}

// 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,
})
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)
}

//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 @@ -61,6 +61,16 @@
that: cr.resources[0].status.get("test") == "hello world"
when: cr is defined

- 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 @@ -79,10 +89,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
10 changes: 10 additions & 0 deletions test/ansible-memcached/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@
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