Skip to content
Open
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
58 changes: 58 additions & 0 deletions pkg/expansion/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,32 @@ metadata:
app: nginx
name: nginx-deployment-pod
namespace: default
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
spec:
containers:
- args:
- "/bin/sh"
image: nginx:1.14.2
name: nginx
ports:
- containerPort: '80'
`

PodNoMutateWithNs = `
apiVersion: v1
kind: Pod
metadata:
labels:
app: nginx
name: nginx-deployment-pod
namespace: not-default
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
spec:
containers:
- args:
Expand All @@ -209,6 +235,10 @@ metadata:
app: nginx
name: nginx-deployment-pod
namespace: default
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
spec:
containers:
- args:
Expand All @@ -228,6 +258,10 @@ metadata:
app: nginx
name: nginx-deployment-pod
namespace: not-default
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
spec:
containers:
- args:
Expand All @@ -247,6 +281,10 @@ metadata:
app: nginx
name: nginx-deployment-pod
namespace: default
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
spec:
containers:
- args:
Expand All @@ -267,6 +305,10 @@ metadata:
owner: admin
name: nginx-deployment-pod
namespace: default
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
spec:
containers:
- args:
Expand Down Expand Up @@ -546,6 +588,10 @@ metadata:
fluffy: extremely
name: big-chungus-kitten
namespace: default
ownerReferences:
- apiVersion: cat.myapp.sh/v1alpha1
kind: Cat
name: big-chungus
spec:
breed: calico
weight: 10
Expand All @@ -560,6 +606,10 @@ metadata:
shouldPet: manytimes
name: big-chungus-purr
namespace: default
ownerReferences:
- apiVersion: cat.myapp.sh/v1alpha1
kind: Cat
name: big-chungus
spec:
loud: very
`
Expand Down Expand Up @@ -591,6 +641,10 @@ kind: Job
metadata:
name: my-cronjob-job
namespace: default
ownerReferences:
- apiVersion: batch/v1
kind: CronJob
name: my-cronjob
spec:
template:
spec:
Expand All @@ -612,6 +666,10 @@ metadata:
owner: admin
name: my-cronjob-job-pod
namespace: default
ownerReferences:
- apiVersion: batch/v1
kind: Job
name: my-cronjob-job
spec:
containers:
- args:
Expand Down
43 changes: 43 additions & 0 deletions pkg/expansion/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,53 @@ func expandResource(obj *unstructured.Unstructured, ns *corev1.Namespace, templa
}

resource.SetName(mockNameForResource(obj, resultantGVK))
ensureOwnerReference(resource, obj)

return resource, nil
}

// ensureOwnerReference appends an OwnerReference describing parent to the resultant
// resource if one is not already present.
func ensureOwnerReference(resultant, parent *unstructured.Unstructured) {
if resultant == nil || parent == nil {
return
}

parentAPIVersion := parent.GetAPIVersion()
parentKind := parent.GetKind()
parentName := parent.GetName()
if parentAPIVersion == "" || parentKind == "" || parentName == "" {
return
}

ownerRef := map[string]interface{}{
"apiVersion": parentAPIVersion,
"kind": parentKind,
"name": parentName,
}

refs, found, err := unstructured.NestedSlice(resultant.Object, "metadata", "ownerReferences")
if err != nil {
return
}
if found {
for _, ref := range refs {
refMap, ok := ref.(map[string]interface{})
if !ok {
continue
}
if refMap["apiVersion"] == parentAPIVersion && refMap["kind"] == parentKind && refMap["name"] == parentName {
return
}
}
} else {
refs = []interface{}{}
}

refs = append(refs, ownerRef)
_ = unstructured.SetNestedSlice(resultant.Object, refs, "metadata", "ownerReferences")
}

// mockNameForResource returns a mock name for a resultant resource created
// from expanding `gen`. The name will be of the form:
// "<generator name>-<resultant kind>". For example, a deployment named
Expand Down
Loading
Loading