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
32 changes: 27 additions & 5 deletions pkg/apis/operator/v1alpha1/common_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,38 @@ import (
"knative.dev/pkg/apis"
)

var reservedSystemNamespaces = []string{
"kube-system",
"kube-public",
"kube-node-lease",
"default",
}

func (ta *CommonSpec) validate(path string) *apis.FieldError {
var errs *apis.FieldError
targetNamespace := ta.GetTargetNamespace()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cleaner than calling GetTargetNamespace() multile time

targetNamespacePath := fmt.Sprintf("%s.targetNamespace", path)
if ta.GetTargetNamespace() == "" {
if targetNamespace == "" {
errs = errs.Also(apis.ErrMissingField(targetNamespacePath))
} else if IsOpenShiftPlatform() {
// "openshift-operators" namespace restricted in openshift environment
if ta.GetTargetNamespace() == "openshift-operators" {
errs = errs.Also(apis.ErrInvalidValue(ta.GetTargetNamespace(), targetNamespacePath, "'openshift-operators' namespace is not allowed"))
} else {
if isReservedSystemNamespace(targetNamespace) {
errs = errs.Also(apis.ErrInvalidValue(targetNamespace, targetNamespacePath, fmt.Sprintf("'%s' is a reserved system namespace and is not allowed", targetNamespace)))
}
if IsOpenShiftPlatform() {
// "openshift-operators" namespace restricted in openshift environment
if targetNamespace == "openshift-operators" {
errs = errs.Also(apis.ErrInvalidValue(targetNamespace, targetNamespacePath, "'openshift-operators' namespace is not allowed"))
}
}
}
return errs
}

func isReservedSystemNamespace(namespace string) bool {
for _, reservedNamespace := range reservedSystemNamespaces {
if namespace == reservedNamespace {
return true
}
}
return false
}
6 changes: 5 additions & 1 deletion pkg/apis/operator/v1alpha1/common_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ func TestValidateCommonTargetNamespace(t *testing.T) {
{name: "empty-value", targetNamespace: "", err: "missing field(s): spec.targetNamespace", isOpenshift: false},
{name: "ns-tekton-pipelines", targetNamespace: "tekton-pipelines", err: "", isOpenshift: false},
{name: "ns-hello", targetNamespace: "hello", err: "", isOpenshift: false},
{name: "ns-default", targetNamespace: "default", err: "", isOpenshift: false},
{name: "ns-kube-system", targetNamespace: "kube-system", err: "invalid value: kube-system: spec.targetNamespace\n'kube-system' is a reserved system namespace and is not allowed", isOpenshift: false},
{name: "ns-kube-public", targetNamespace: "kube-public", err: "invalid value: kube-public: spec.targetNamespace\n'kube-public' is a reserved system namespace and is not allowed", isOpenshift: false},
{name: "ns-kube-node-lease", targetNamespace: "kube-node-lease", err: "invalid value: kube-node-lease: spec.targetNamespace\n'kube-node-lease' is a reserved system namespace and is not allowed", isOpenshift: false},
{name: "ns-default", targetNamespace: "default", err: "invalid value: default: spec.targetNamespace\n'default' is a reserved system namespace and is not allowed", isOpenshift: false},
{name: "ns-openshift-operators", targetNamespace: "openshift-operators", err: "", isOpenshift: false},
{name: "openshift-ns-default", targetNamespace: "default", err: "invalid value: default: spec.targetNamespace\n'default' is a reserved system namespace and is not allowed", isOpenshift: true},
{name: "openshift-ns-openshift-operators", targetNamespace: "openshift-operators", err: "invalid value: openshift-operators: spec.targetNamespace\n'openshift-operators' namespace is not allowed", isOpenshift: true},
{name: "openshift-ns-openshift-pipelines", targetNamespace: "openshift-pipelines", err: "", isOpenshift: true},
Comment on lines 41 to 44
{name: "openshift-ns-openshift-xyz", targetNamespace: "openshift-xyz", err: "", isOpenshift: true},
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/operator/v1alpha1/tektonresult_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (tp *TektonResult) Validate(ctx context.Context) (errs *apis.FieldError) {
}

func (trs *TektonResultSpec) validate(path string) (errs *apis.FieldError) {
// validate the embedded CommonSpec (e.g. targetNamespace denylist),
// which TektonResult would otherwise bypass since it does not call
// CommonSpec.validate the way the other components do.
errs = errs.Also(trs.CommonSpec.validate(path))

if trs.LokiStackName != "" {
if strings.ToLower(trs.LogsType) != LogsTypeLoki && trs.LogsType != "" {
errMsg := fmt.Sprintf("Loki stack is only supported when logs_type is loki or empty, got logs_type: %s", trs.LogsType)
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/operator/v1alpha1/tektonresult_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ func TestTektonResult_Validate(t *testing.T) {
assert.Equal(t, "invalid value: wrong-name: metadata.name, Only one instance of TektonResult is allowed by name, result", err.Error())
}

func TestTektonResult_ValidateTargetNamespaceDenylist(t *testing.T) {
tr := &TektonResult{
ObjectMeta: metav1.ObjectMeta{
Name: "result",
Namespace: "namespace",
},
Spec: TektonResultSpec{
CommonSpec: CommonSpec{
TargetNamespace: "kube-system",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this skips validation kube-public, kube-node-lease, default, and the OpenShift openshift-operators case, without any failing test at the TektonResult level.

},
},
}

errs := tr.Validate(context.TODO())
assert.Equal(t, "invalid value: kube-system: spec.targetNamespace\n'kube-system' is a reserved system namespace and is not allowed", errs.Error())
}

func TestTektonResultWatcherPerformancePropertiesValidate(t *testing.T) {
tr := &TektonResult{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading