Skip to content

Commit 0b75f99

Browse files
Merge pull request #807 from adambkaplan/fix-finalizer-rbac
Fix RBAC so Shipwright Build works with the OwnerReferencesPermissionEnforcement admission controller
2 parents f7667c2 + 9579ced commit 0b75f99

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ jobs:
104104
version: v0.10.0
105105
node_image: kindest/node:${{ matrix.kubernetes }}
106106
cluster_name: kind
107+
config: test/kind/config.yaml
107108
wait: 120s
108109
- name: Verify kind cluster
109110
run: |

deploy/200-role.yaml

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
rules:
77
- apiGroups: ['']
88
resources: ['configmaps']
9-
verbs: ['create', 'get', 'update']
9+
verbs: ['get', 'create', 'update']
1010

1111
- apiGroups: ['']
1212
resources: ['events']
@@ -21,7 +21,15 @@ metadata:
2121
rules:
2222
- apiGroups: ['shipwright.io']
2323
resources: ['buildruns']
24-
verbs: ['get', 'list', 'update', 'watch']
24+
# The build-run-deletion annotation sets an owner ref on BuildRun objects.
25+
# With the OwnerReferencesPermissionEnforcement admission controller enabled, controllers need the "delete" permission on objects that they set owner references on.
26+
verbs: ['get', 'list', 'watch', 'update', 'delete']
27+
28+
- apiGroups: ['shipwright.io']
29+
# BuildRuns are set as the owners of Tekton TaskRuns.
30+
# With the OwnerReferencesPermissionEnforcement admission controller enabled, controllers need the "update" permission on the finalizer of the parent object in the owner reference.
31+
resources: ['buildruns/finalizers']
32+
verbs: ['update']
2533

2634
- apiGroups: ['shipwright.io']
2735
resources: ['buildruns/status']
@@ -31,6 +39,12 @@ rules:
3139
resources: ['builds']
3240
verbs: ['get', 'list', 'watch']
3341

42+
- apiGroups: ['shipwright.io']
43+
# The build-run-deletion annotation makes Builds an owner of BuildRun objects.
44+
# With the OwnerReferencesPermissionEnforcement admission controller enabled, controllers need the "update" permission on the finalizer of the parent object in the owner reference.
45+
resources: ['builds/finalizers']
46+
verbs: ['update']
47+
3448
- apiGroups: ['shipwright.io']
3549
resources: ['builds/status']
3650
verbs: ['update']
@@ -45,7 +59,9 @@ rules:
4559

4660
- apiGroups: ['tekton.dev']
4761
resources: ['taskruns']
48-
verbs: ['get', 'create', 'list', 'watch']
62+
# BuildRuns are set as the owners of Tekton TaskRuns.
63+
# With the OwnerReferencesPermissionEnforcement admission controller enabled, controllers need the "delete" permission on objects that they set owner references on.
64+
verbs: ['get', 'list', 'watch', 'create', 'delete']
4965

5066
- apiGroups: ['']
5167
resources: ['pods']
@@ -57,4 +73,4 @@ rules:
5773

5874
- apiGroups: ['']
5975
resources: ['serviceaccounts']
60-
verbs: ['create', 'delete', 'get', 'list', 'update', 'watch']
76+
verbs: ['get', 'list', 'watch', 'create', 'update', 'delete']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
apiVersion: shipwright.io/v1alpha1
3+
kind: Build
4+
metadata:
5+
name: buildpack-golang-build
6+
annotations:
7+
build.shipwright.io/build-run-deletion: "true"
8+
spec:
9+
source:
10+
url: https://github.com/shipwright-io/sample-go
11+
contextDir: source-build
12+
strategy:
13+
name: buildpacks-v3
14+
kind: ClusterBuildStrategy
15+
output:
16+
image: image-registry.openshift-image-registry.svc:5000/build-examples/taxi-app

test/e2e/e2e_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
. "github.com/onsi/ginkgo"
1111
. "github.com/onsi/gomega"
12+
"k8s.io/apimachinery/pkg/api/errors"
1213

1314
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
1415
)
@@ -254,6 +255,42 @@ var _ = Describe("For a Kubernetes cluster with Tekton and build installed", fun
254255
})
255256
})
256257

258+
Context("when a build uses the build-run-deletion annotation", func() {
259+
260+
BeforeEach(func() {
261+
testID = generateTestID("buildpacks-v3-golang")
262+
263+
// create the build definition
264+
build = createBuild(
265+
testBuild,
266+
testID,
267+
"test/data/build_buildpacks-v3_golang_delete_cr.yaml",
268+
)
269+
})
270+
271+
It("successfully deletes the BuildRun after the Build is deleted", func() {
272+
By("running a build and expecting it to succeed")
273+
buildRun, err = buildRunTestData(testBuild.Namespace, testID, "test/data/buildrun_buildpacks-v3_golang_cr.yaml")
274+
Expect(err).ToNot(HaveOccurred(), "Error retrieving buildrun test data")
275+
276+
validateBuildRunToSucceed(testBuild, buildRun)
277+
278+
By("deleting the parent Build object")
279+
err = testBuild.DeleteBuild(build.Name)
280+
Expect(err).NotTo(HaveOccurred(), "error deleting the parent Build")
281+
Eventually(func() bool {
282+
_, err = testBuild.GetBR(buildRun.Name)
283+
if err == nil {
284+
return false
285+
}
286+
if !errors.IsNotFound(err) {
287+
return false
288+
}
289+
return true
290+
}).Should(BeTrue())
291+
})
292+
})
293+
257294
Context("when a Buildpacks v3 build is defined for a java runtime", func() {
258295

259296
BeforeEach(func() {

test/kind/config.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
kind: Cluster
2+
apiVersion: kind.x-k8s.io/v1alpha4
3+
nodes:
4+
- role: control-plane
5+
kubeadmConfigPatches:
6+
- |
7+
kind: ClusterConfiguration
8+
apiServer:
9+
extraArgs:
10+
enable-admission-plugins: CertificateApproval,CertificateSigning,CertificateSubjectRestriction,DefaultIngressClass,DefaultStorageClass,DefaultTolerationSeconds,LimitRanger,MutatingAdmissionWebhook,NamespaceLifecycle,NodeRestriction,OwnerReferencesPermissionEnforcement,PersistentVolumeClaimResize,PersistentVolumeLabel,PodNodeSelector,PodTolerationRestriction,Priority,ResourceQuota,RuntimeClass,ServiceAccount,StorageObjectInUseProtection,TaintNodesByCondition,ValidatingAdmissionWebhook

0 commit comments

Comments
 (0)