Skip to content

Commit 562221c

Browse files
fix: updating expansion templates to add owner ref in expanded resources (open-policy-agent#4262)
Signed-off-by: Jaydip Gabani <[email protected]> Co-authored-by: Sertaç Özercan <[email protected]>
1 parent 00d3bfa commit 562221c

File tree

7 files changed

+383
-0
lines changed

7 files changed

+383
-0
lines changed

pkg/expansion/fixtures/fixtures.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,34 @@ metadata:
191191
app: nginx
192192
name: nginx-deployment-pod
193193
namespace: default
194+
ownerReferences:
195+
- apiVersion: apps/v1
196+
kind: Deployment
197+
name: nginx-deployment
198+
uid: ""
199+
spec:
200+
containers:
201+
- args:
202+
- "/bin/sh"
203+
image: nginx:1.14.2
204+
name: nginx
205+
ports:
206+
- containerPort: '80'
207+
`
208+
209+
PodNoMutateWithNs = `
210+
apiVersion: v1
211+
kind: Pod
212+
metadata:
213+
labels:
214+
app: nginx
215+
name: nginx-deployment-pod
216+
namespace: not-default
217+
ownerReferences:
218+
- apiVersion: apps/v1
219+
kind: Deployment
220+
name: nginx-deployment
221+
uid: ""
194222
spec:
195223
containers:
196224
- args:
@@ -209,6 +237,11 @@ metadata:
209237
app: nginx
210238
name: nginx-deployment-pod
211239
namespace: default
240+
ownerReferences:
241+
- apiVersion: apps/v1
242+
kind: Deployment
243+
name: nginx-deployment
244+
uid: ""
212245
spec:
213246
containers:
214247
- args:
@@ -228,6 +261,11 @@ metadata:
228261
app: nginx
229262
name: nginx-deployment-pod
230263
namespace: not-default
264+
ownerReferences:
265+
- apiVersion: apps/v1
266+
kind: Deployment
267+
name: nginx-deployment
268+
uid: ""
231269
spec:
232270
containers:
233271
- args:
@@ -247,6 +285,11 @@ metadata:
247285
app: nginx
248286
name: nginx-deployment-pod
249287
namespace: default
288+
ownerReferences:
289+
- apiVersion: apps/v1
290+
kind: Deployment
291+
name: nginx-deployment
292+
uid: ""
250293
spec:
251294
containers:
252295
- args:
@@ -267,6 +310,11 @@ metadata:
267310
owner: admin
268311
name: nginx-deployment-pod
269312
namespace: default
313+
ownerReferences:
314+
- apiVersion: apps/v1
315+
kind: Deployment
316+
name: nginx-deployment
317+
uid: ""
270318
spec:
271319
containers:
272320
- args:
@@ -546,6 +594,11 @@ metadata:
546594
fluffy: extremely
547595
name: big-chungus-kitten
548596
namespace: default
597+
ownerReferences:
598+
- apiVersion: cat.myapp.sh/v1alpha1
599+
kind: Cat
600+
name: big-chungus
601+
uid: ""
549602
spec:
550603
breed: calico
551604
weight: 10
@@ -560,6 +613,11 @@ metadata:
560613
shouldPet: manytimes
561614
name: big-chungus-purr
562615
namespace: default
616+
ownerReferences:
617+
- apiVersion: cat.myapp.sh/v1alpha1
618+
kind: Cat
619+
name: big-chungus
620+
uid: ""
563621
spec:
564622
loud: very
565623
`
@@ -591,6 +649,11 @@ kind: Job
591649
metadata:
592650
name: my-cronjob-job
593651
namespace: default
652+
ownerReferences:
653+
- apiVersion: batch/v1
654+
kind: CronJob
655+
name: my-cronjob
656+
uid: ""
594657
spec:
595658
template:
596659
spec:
@@ -612,6 +675,11 @@ metadata:
612675
owner: admin
613676
name: my-cronjob-job-pod
614677
namespace: default
678+
ownerReferences:
679+
- apiVersion: batch/v1
680+
kind: Job
681+
name: my-cronjob-job
682+
uid: ""
615683
spec:
616684
containers:
617685
- args:

pkg/expansion/system.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/open-policy-agent/gatekeeper/v3/pkg/mutation"
1313
mutationtypes "github.com/open-policy-agent/gatekeeper/v3/pkg/mutation/types"
1414
corev1 "k8s.io/api/core/v1"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1617
"k8s.io/apimachinery/pkg/runtime/schema"
1718
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -246,10 +247,41 @@ func expandResource(obj *unstructured.Unstructured, ns *corev1.Namespace, templa
246247
}
247248

248249
resource.SetName(mockNameForResource(obj, resultantGVK))
250+
ensureOwnerReference(resource, obj)
249251

250252
return resource, nil
251253
}
252254

255+
// ensureOwnerReference appends an OwnerReference describing parent to the resultant
256+
// resource if one is not already present.
257+
func ensureOwnerReference(resultant, parent *unstructured.Unstructured) {
258+
if resultant == nil || parent == nil {
259+
return
260+
}
261+
262+
parentAPIVersion := parent.GetAPIVersion()
263+
parentKind := parent.GetKind()
264+
parentName := parent.GetName()
265+
if parentAPIVersion == "" || parentKind == "" || parentName == "" {
266+
return
267+
}
268+
269+
newOwnerRef := metav1.OwnerReference{
270+
APIVersion: parentAPIVersion,
271+
Kind: parentKind,
272+
Name: parentName,
273+
}
274+
275+
existingRefs := resultant.GetOwnerReferences()
276+
for _, ref := range existingRefs {
277+
if ref.APIVersion == parentAPIVersion && ref.Kind == parentKind && ref.Name == parentName {
278+
return
279+
}
280+
}
281+
282+
resultant.SetOwnerReferences(append(existingRefs, newOwnerRef))
283+
}
284+
253285
// mockNameForResource returns a mock name for a resultant resource created
254286
// from expanding `gen`. The name will be of the form:
255287
// "<generator name>-<resultant kind>". For example, a deployment named

0 commit comments

Comments
 (0)