Skip to content

Commit

Permalink
Implement some more of the webhook validation skeleton for Pipelines.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlorenc authored and knative-prow-robot committed Oct 4, 2018
1 parent d2a6825 commit 859ac08
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
40 changes: 40 additions & 0 deletions pkg/apis/pipeline/v1alpha1/metadata_validation.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_defaults.go
Original file line number Diff line number Diff line change
@@ -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()
}
37 changes: 37 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_validation.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 859ac08

Please sign in to comment.