Skip to content

Commit 470ac80

Browse files
[Add] CEL rules for validating App and PackageInstall Spec
This PR: - Adds KB marker to ensure that either spec.ServiceAccount or spec.Cluster is present in App and PackageInstall CR. - Bumps controller-tools to 0.12.1 to support CEL based validation marker. That is the latest version compatible with the k8s release the project is currently at. Signed-off-by: Varsha Prasad Narsing <[email protected]>
1 parent 6d83c43 commit 470ac80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1970
-875
lines changed

config/config/crds.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,9 @@ spec:
14341434
required:
14351435
- spec
14361436
type: object
1437+
x-kubernetes-validations:
1438+
- message: Expected service account or cluster.
1439+
rule: has(self.spec.serviceAccountName) || has(self.spec.cluster)
14371440
served: true
14381441
storage: true
14391442
subresources:
@@ -1594,6 +1597,9 @@ spec:
15941597
required:
15951598
- spec
15961599
type: object
1600+
x-kubernetes-validations:
1601+
- message: Expected service account or cluster.
1602+
rule: has(self.spec.serviceAccountName) || has(self.spec.cluster)
15971603
served: true
15981604
storage: true
15991605
subresources:

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/vmware-tanzu/carvel-kapp-controller
33
go 1.21
44

55
require (
6-
github.com/fatih/color v1.13.0 // indirect
6+
github.com/fatih/color v1.15.0 // indirect
77
github.com/gogo/protobuf v1.3.2
88
github.com/google/go-cmp v0.5.9 // indirect
99
github.com/prometheus/client_golang v1.15.1
@@ -21,7 +21,7 @@ require (
2121
k8s.io/kube-aggregator v0.22.17
2222
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f
2323
sigs.k8s.io/controller-runtime v0.15.3
24-
sigs.k8s.io/controller-tools v0.7.0
24+
sigs.k8s.io/controller-tools v0.12.1
2525
sigs.k8s.io/yaml v1.3.0
2626
)
2727

@@ -30,7 +30,7 @@ require (
3030
github.com/cppforlife/go-cli-ui v0.0.0-20220425131040-94f26b16bc14
3131
github.com/go-logr/logr v1.2.4
3232
github.com/k14s/semver/v4 v4.0.1-0.20210701191048-266d47ac6115
33-
github.com/spf13/cobra v1.6.1
33+
github.com/spf13/cobra v1.7.0
3434
golang.org/x/sync v0.2.0
3535
gopkg.in/yaml.v2 v2.4.0
3636
k8s.io/component-base v0.27.7
@@ -62,7 +62,7 @@ require (
6262
github.com/go-openapi/jsonpointer v0.19.6 // indirect
6363
github.com/go-openapi/jsonreference v0.20.1 // indirect
6464
github.com/go-openapi/swag v0.22.3 // indirect
65-
github.com/gobuffalo/flect v0.2.3 // indirect
65+
github.com/gobuffalo/flect v1.0.2 // indirect
6666
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
6767
github.com/golang/protobuf v1.5.3 // indirect
6868
github.com/google/cel-go v0.12.7 // indirect
@@ -73,11 +73,11 @@ require (
7373
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
7474
github.com/hashicorp/go-version v1.2.1 // indirect
7575
github.com/imdario/mergo v0.3.12 // indirect
76-
github.com/inconshreveable/mousetrap v1.0.1 // indirect
76+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
7777
github.com/josharian/intern v1.0.0 // indirect
7878
github.com/json-iterator/go v1.1.12 // indirect
7979
github.com/mailru/easyjson v0.7.7 // indirect
80-
github.com/mattn/go-colorable v0.1.12 // indirect
80+
github.com/mattn/go-colorable v0.1.13 // indirect
8181
github.com/mattn/go-isatty v0.0.17 // indirect
8282
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
8383
github.com/mitchellh/mapstructure v1.4.1 // indirect

go.sum

Lines changed: 15 additions & 65 deletions
Large diffs are not rendered by default.

pkg/apis/kappctrl/v1alpha1/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// +kubebuilder:printcolumn:name=Description,JSONPath=.status.friendlyDescription,description=Friendly description,type=string
1515
// +kubebuilder:printcolumn:name=Since-Deploy,JSONPath=.status.deploy.startedAt,description=Last time app started being deployed. Does not mean anything was changed.,type=date
1616
// +kubebuilder:printcolumn:name=Age,JSONPath=.metadata.creationTimestamp,description=Time since creation,type=date
17+
// +kubebuilder:validation:XValidation:rule="has(self.spec.serviceAccountName) || has(self.spec.cluster)", message="Expected service account or cluster."
1718
// +protobuf=false
1819
// An App is a set of Kubernetes resources. These resources could span any number of namespaces or could be cluster-wide (e.g. CRDs). An App is represented in kapp-controller using a App CR.
1920
// The App CR comprises of three main sections:

pkg/apis/packaging/v1alpha1/package_install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
// +kubebuilder:printcolumn:name=Package version,JSONPath=.status.version,description=PackageMetadata version,type=string
1818
// +kubebuilder:printcolumn:name=Description,JSONPath=.status.friendlyDescription,description=Friendly description,type=string
1919
// +kubebuilder:printcolumn:name=Age,JSONPath=.metadata.creationTimestamp,description=Time since creation,type=date
20+
// +kubebuilder:validation:XValidation:rule="has(self.spec.serviceAccountName) || has(self.spec.cluster)", message="Expected service account or cluster."
2021
// A Package Install is an actual installation of a package and its underlying resources on a Kubernetes cluster.
2122
// It is represented in kapp-controller by a PackageInstall CR.
2223
// A PackageInstall CR must reference a Package CR.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2021 VMware, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package kappcontroller
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
"github.com/vmware-tanzu/carvel-kapp-controller/test/e2e"
13+
)
14+
15+
// TestSAandClusterCELValidation tests packageInstall and App CR
16+
// to ensure that either SA or cluster is validated at admission.
17+
func TestSAandClusterCELValidation(t *testing.T) {
18+
env := e2e.BuildEnv(t)
19+
logger := e2e.Logger{}
20+
kapp := e2e.Kapp{t, env.Namespace, logger}
21+
kubectl := e2e.Kubectl{t, env.Namespace, logger}
22+
23+
name := "incorrect-spec-without-sa-cluster"
24+
25+
appYAML := fmt.Sprintf(`
26+
---
27+
apiVersion: kappctrl.k14s.io/v1alpha1
28+
kind: App
29+
metadata:
30+
name: %s
31+
annotations:
32+
kapp.k14s.io/change-group: kappctrl-e2e.k14s.io/apps
33+
spec:
34+
fetch:
35+
- inline:
36+
paths:
37+
file.yml: |
38+
apiVersion: v1
39+
kind: ConfigMap
40+
metadata:
41+
name: configmap
42+
template:
43+
- ytt: {}
44+
deploy:
45+
- kapp: {}
46+
`, name)
47+
48+
pkginstallYAML := fmt.Sprintf(`
49+
---
50+
apiVersion: packaging.carvel.dev/v1alpha1
51+
kind: PackageInstall
52+
metadata:
53+
name: %[2]s
54+
namespace: %[1]s
55+
annotations:
56+
kapp.k14s.io/change-group: kappctrl-e2e.k14s.io/packageinstalls
57+
spec:
58+
packageRef:
59+
refName: pkg.incorrect.carvel.dev
60+
versionSelection:
61+
constraints: 1.0.0
62+
`, env.Namespace, name)
63+
64+
logger.Section("Create App CR with kubectl", func() {
65+
_, err := kubectl.RunWithOpts([]string{"apply", "-f", "-"}, e2e.RunOpts{StdinReader: strings.NewReader(appYAML), AllowError: true})
66+
require.Error(t, err)
67+
require.ErrorContains(t, err, "Expected service account or cluster.")
68+
})
69+
70+
logger.Section("Create PackageInstall with kapp", func() {
71+
_, err := kapp.RunWithOpts([]string{"deploy", "-a", name, "-f", "-"}, e2e.RunOpts{StdinReader: strings.NewReader(pkginstallYAML), AllowError: true})
72+
require.Error(t, err)
73+
require.ErrorContains(t, err, "Expected service account or cluster.")
74+
})
75+
}

vendor/github.com/fatih/color/README.md

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fatih/color/color.go

Lines changed: 22 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fatih/color/color_windows.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)