diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index 42c339060d7..913dacfef19 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -20,6 +20,8 @@ import ( "flag" "log" + "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" + "go.uber.org/zap" "github.com/knative/build-pipeline/pkg/logging" @@ -78,10 +80,12 @@ func main() { } //TODO add validations here controller := webhook.AdmissionController{ - Client: kubeClient, - Options: options, - Handlers: map[schema.GroupVersionKind]webhook.GenericCRD{}, - Logger: logger, + Client: kubeClient, + Options: options, + Handlers: map[schema.GroupVersionKind]webhook.GenericCRD{ + v1alpha1.SchemeGroupVersion.WithKind("Pipeline"): &v1alpha1.Pipeline{}, + }, + Logger: logger, } if err != nil { logger.Fatal("Failed to create the admission controller", zap.Error(err)) diff --git a/pkg/apis/pipeline/v1alpha1/metadata_validation.go b/pkg/apis/pipeline/v1alpha1/metadata_validation.go new file mode 100644 index 00000000000..64a064f2917 --- /dev/null +++ b/pkg/apis/pipeline/v1alpha1/metadata_validation.go @@ -0,0 +1,40 @@ +/* +Copyright 2018 The Knative Authors +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "strings" + + "github.com/knative/pkg/apis" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func validateObjectMetadata(meta metav1.Object) *apis.FieldError { + name := meta.GetName() + + if strings.Contains(name, ".") { + return &apis.FieldError{ + Message: "Invalid resource name: special character . must not be present", + Paths: []string{"name"}, + } + } + + if len(name) > 63 { + return &apis.FieldError{ + Message: "Invalid resource name: length must be no more than 63 characters", + Paths: []string{"name"}, + } + } + return nil +} diff --git a/pkg/apis/pipeline/v1alpha1/pipeline_defaults.go b/pkg/apis/pipeline/v1alpha1/pipeline_defaults.go new file mode 100644 index 00000000000..31d81c538a4 --- /dev/null +++ b/pkg/apis/pipeline/v1alpha1/pipeline_defaults.go @@ -0,0 +1,25 @@ +/* +Copyright 2018 The Knative Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +func (p *Pipeline) SetDefaults() { + p.Spec.SetDefaults() +} + +func (ps *PipelineSpec) SetDefaults() { + ps.SetDefaults() +} diff --git a/pkg/apis/pipeline/v1alpha1/pipeline_validation.go b/pkg/apis/pipeline/v1alpha1/pipeline_validation.go new file mode 100644 index 00000000000..bf918a1990a --- /dev/null +++ b/pkg/apis/pipeline/v1alpha1/pipeline_validation.go @@ -0,0 +1,37 @@ +/* +Copyright 2018 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "github.com/knative/pkg/apis" + "k8s.io/apimachinery/pkg/api/equality" +) + +func (p *Pipeline) Validate() *apis.FieldError { + if err := validateObjectMetadata(p.GetObjectMeta()); err != nil { + return err.ViaField("metadata") + } + return nil +} + +func (ps *PipelineSpec) Validate() *apis.FieldError { + if equality.Semantic.DeepEqual(ps, &PipelineSpec{}) { + return apis.ErrMissingField(apis.CurrentField) + } + + return nil +}