Skip to content

Commit d3893a1

Browse files
[v1.6.x] Fix bug in cluster scoped ansible operators (#4858)
Dependent object creation passing through the proxy failed to add an owner annotation, causing the objects not to be watched. Signed-off-by: austin <[email protected]> Co-authored-by: austin <[email protected]>
1 parent 81969a1 commit d3893a1

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: >
5+
For Anible-based operators, fixed a bug that caused to be unable watch
6+
cluster scoped dependent resources and dependent resources in other
7+
namespaces.
8+
9+
# kind is one of:
10+
# - addition
11+
# - change
12+
# - deprecation
13+
# - removal
14+
# - bugfix
15+
kind: "bugfix"
16+
17+
# Is this a breaking change?
18+
breaking: false
19+
20+
# NOTE: ONLY USE `pull_request_override` WHEN ADDING THIS
21+
# FILE FOR A PREVIOUSLY MERGED PULL_REQUEST!
22+
#
23+
# The generator auto-detects the PR number from the commit
24+
# message in which this file was originally added.
25+
#
26+
# What is the pull request number (without the "#")?
27+
# pull_request_override: 0
28+
29+
30+
# Migration can be defined to automatically add a section to
31+
# the migration guide. This is required for breaking changes.
32+
migration:
33+
header: Ensure that existing dependent resources have owner annotations
34+
body: >
35+
Ansible-based operators use owner references (for resources in the same
36+
namespace) and owner annotations(for cluster scoped resources and
37+
resources in other namespaces). If dependent resources were created
38+
without the references/annotations, they cannot be cleaned up by the
39+
operator, and update/deletes will not trigger a reconcile. Follow these
40+
instructions to add owner references/annotations to existing objects.
41+
https://sdk.operatorframework.io/docs/building-operators/ansible/reference/retroactively-owned-resources/

hack/generate/samples/internal/ansible/advanced_molecule.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func (ma *AdvancedMolecule) updateConfig() {
126126
- ""
127127
resources:
128128
- configmaps
129+
- namespaces
129130
verbs:
130131
- create
131132
- delete
@@ -471,19 +472,56 @@ func (ma *AdvancedMolecule) updatePlaybooks() {
471472
originalPlaybookFragment,
472473
subresourcesPlaybook)
473474
pkg.CheckError("adding playbook for subresourcestest", err)
475+
476+
log.Infof("adding playbook for clusterannotationtest")
477+
const clusterAnnotationTest = `---
478+
- hosts: localhost
479+
gather_facts: no
480+
collections:
481+
- community.kubernetes
482+
tasks:
483+
484+
- name: create externalnamespace
485+
k8s:
486+
name: "externalnamespace"
487+
api_version: v1
488+
kind: "Namespace"
489+
definition:
490+
metadata:
491+
labels:
492+
foo: bar
493+
494+
- name: create configmap
495+
k8s:
496+
definition:
497+
apiVersion: v1
498+
kind: ConfigMap
499+
metadata:
500+
namespace: "externalnamespace"
501+
name: '{{ meta.name }}'
502+
data:
503+
foo: bar
504+
`
505+
err = util.ReplaceInFile(
506+
filepath.Join(ma.ctx.Dir, "playbooks", "clusterannotationtest.yml"),
507+
originalPlaybookFragment,
508+
clusterAnnotationTest)
509+
pkg.CheckError("adding playbook for clusterannotationtest", err)
510+
474511
}
475512

476513
func (ma *AdvancedMolecule) addPlaybooks() {
477514
allPlaybookKinds := []string{
478515
"ArgsTest",
479516
"CaseTest",
480517
"CollectionTest",
518+
"ClusterAnnotationTest",
481519
"ReconciliationTest",
482520
"SelectorTest",
483521
"SubresourcesTest",
484522
}
485523

486-
// Crate API
524+
// Create API
487525
for _, k := range allPlaybookKinds {
488526
logMsgForKind := fmt.Sprintf("creating an API %s", k)
489527
log.Infof(logMsgForKind)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
- name: Create the test.example.com/v1alpha1.ClusterAnnotationTest
3+
k8s:
4+
state: present
5+
namespace: '{{ namespace }}'
6+
definition: "{{ lookup('template', '/'.join([samples_dir, cr_file])) | from_yaml }}"
7+
wait: yes
8+
wait_timeout: 300
9+
wait_condition:
10+
type: Running
11+
reason: Successful
12+
status: "True"
13+
vars:
14+
cr_file: 'test_v1alpha1_clusterannotationtest.yaml'
15+
16+
- name: retrieve configmap
17+
k8s_info:
18+
api_version: v1
19+
kind: ConfigMap
20+
namespace: "externalnamespace"
21+
name: "clusterannotationtest-sample"
22+
register: configmap
23+
until: (configmap.resources | length) == 1
24+
25+
- assert:
26+
that:
27+
- configmap.resources[0].metadata.annotations["operator-sdk/primary-resource"] == primary
28+
- configmap.resources[0].metadata.annotations["operator-sdk/primary-resource-type"] == primary_type
29+
vars:
30+
primary: "osdk-test/clusterannotationtest-sample"
31+
primary_type: "ClusterAnnotationTest.test.example.com"
32+
33+
- name: change the namespace labels
34+
k8s:
35+
name: "externalnamespace"
36+
api_version: v1
37+
kind: "Namespace"
38+
wait: yes
39+
definition:
40+
metadata:
41+
labels:
42+
foo: baz
43+
44+
- name: Make sure the label is changed back
45+
k8s_info:
46+
api_version: v1
47+
kind: Namespace
48+
name: "externalnamespace"
49+
register: external_namespace
50+
until: external_namespace.resources[0].metadata.labels["foo"] == "bar"
51+

hack/generate/samples/internal/ansible/testdata/watches.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@
6060
playbook: playbooks/reconciliationtest.yml
6161
vars:
6262
meta: '{{ ansible_operator_meta }}'
63+
64+
- version: v1alpha1
65+
group: test.example.com
66+
kind: ClusterAnnotationTest
67+
playbook: playbooks/clusterannotationtest.yml
68+
watchClusterScopedResources: true
69+
vars:
70+
meta: '{{ ansible_operator_meta }}'
6371
#+kubebuilder:scaffold:watch

internal/ansible/proxy/inject_owner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (i *injectOwnerReferenceHandler) ServeHTTP(w http.ResponseWriter, req *http
146146
if addOwnerRef {
147147
data.SetOwnerReferences(append(data.GetOwnerReferences(), owner.OwnerReference))
148148
} else {
149-
err := handler.SetOwnerAnnotations(data, ownerObject)
149+
err := handler.SetOwnerAnnotations(ownerObject, data)
150150
if err != nil {
151151
m := "Could not set owner annotations"
152152
log.Error(err, m)

0 commit comments

Comments
 (0)