Skip to content

Commit 38f6e7b

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. - Adds test to validate the logic introduced with the kubebuilder marker. Signed-off-by: Varsha Prasad Narsing <[email protected]>
1 parent 0594982 commit 38f6e7b

Some content is hidden

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

87 files changed

+2004
-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.

test/e2e/env.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
package e2e
55

66
import (
7+
"fmt"
78
"os"
89
"strings"
910
"testing"
11+
12+
"k8s.io/apimachinery/pkg/version"
13+
"k8s.io/client-go/kubernetes"
14+
"k8s.io/client-go/rest"
1015
)
1116

1217
type Env struct {
@@ -40,3 +45,22 @@ func (e Env) Validate(t *testing.T) {
4045
t.Fatalf("%s", strings.Join(errStrs, "\n"))
4146
}
4247
}
48+
49+
// GetServerVersion returns the cluster info.
50+
func (e Env) GetServerVersion() (*version.Info, error) {
51+
config, err := rest.InClusterConfig()
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
clientset, err := kubernetes.NewForConfig(config)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
serverVersion, err := clientset.Discovery().ServerVersion()
62+
if err != nil {
63+
return nil, fmt.Errorf("Error getting server version: %v", err)
64+
}
65+
return serverVersion, nil
66+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
// Check the Kubernetes version and skip the test if it's less than 1.26
24+
kubeVersion, err := env.GetServerVersion()
25+
if err != nil || kubeVersion == nil {
26+
t.Fatalf("Error getting Kubernetes version: %v", err)
27+
}
28+
29+
if kubeVersion.Major < "1" || (kubeVersion.Major == "1" && kubeVersion.Minor < "26") {
30+
t.Skip("Skipping test for Kubernetes versions less than 1.26")
31+
}
32+
33+
name := "incorrect-spec-without-sa-cluster"
34+
35+
appYAML := fmt.Sprintf(`
36+
---
37+
apiVersion: kappctrl.k14s.io/v1alpha1
38+
kind: App
39+
metadata:
40+
name: %s
41+
annotations:
42+
kapp.k14s.io/change-group: kappctrl-e2e.k14s.io/apps
43+
spec:
44+
fetch:
45+
- inline:
46+
paths:
47+
file.yml: |
48+
apiVersion: v1
49+
kind: ConfigMap
50+
metadata:
51+
name: configmap
52+
template:
53+
- ytt: {}
54+
deploy:
55+
- kapp: {}
56+
`, name)
57+
58+
pkginstallYAML := fmt.Sprintf(`
59+
---
60+
apiVersion: packaging.carvel.dev/v1alpha1
61+
kind: PackageInstall
62+
metadata:
63+
name: %[2]s
64+
namespace: %[1]s
65+
annotations:
66+
kapp.k14s.io/change-group: kappctrl-e2e.k14s.io/packageinstalls
67+
spec:
68+
packageRef:
69+
refName: pkg.incorrect.carvel.dev
70+
versionSelection:
71+
constraints: 1.0.0
72+
`, env.Namespace, name)
73+
74+
logger.Section("Create App CR with kubectl", func() {
75+
_, err := kubectl.RunWithOpts([]string{"apply", "-f", "-"}, e2e.RunOpts{StdinReader: strings.NewReader(appYAML), AllowError: true})
76+
require.Error(t, err)
77+
require.ErrorContains(t, err, "Expected service account or cluster.")
78+
})
79+
80+
logger.Section("Create PackageInstall with kapp", func() {
81+
_, err := kapp.RunWithOpts([]string{"deploy", "-a", name, "-f", "-"}, e2e.RunOpts{StdinReader: strings.NewReader(pkginstallYAML), AllowError: true})
82+
require.Error(t, err)
83+
require.ErrorContains(t, err, "Expected service account or cluster.")
84+
})
85+
}

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)