Skip to content

Commit

Permalink
Add more skeleton code for Task validation.
Browse files Browse the repository at this point in the history
The SetDefaults() and Validate() methods are required to implement the
knantive.pkg.webhook.GenericCRD interface, which allows registration in
the webhook.AdmissionController server.
  • Loading branch information
dlorenc authored and knative-prow-robot committed Oct 8, 2018
1 parent f2d09ef commit d97057a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func main() {
Options: options,
Handlers: map[schema.GroupVersionKind]webhook.GenericCRD{
v1alpha1.SchemeGroupVersion.WithKind("Pipeline"): &v1alpha1.Pipeline{},
v1alpha1.SchemeGroupVersion.WithKind("Task"): &v1alpha1.Task{},
},
Logger: logger,
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_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 (t *Task) SetDefaults() {
t.Spec.SetDefaults()
}

func (ts *TaskSpec) SetDefaults() {
return
}
7 changes: 6 additions & 1 deletion pkg/apis/pipeline/v1alpha1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package v1alpha1

import (
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"

"github.com/knative/pkg/webhook"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -26,7 +28,7 @@ type TaskSpec struct {
// +optional
Inputs *Inputs `json:"inputs,omitempty"`
// +optional
Outputs *Outputs `json:"outputs,omitempty"`
Outputs *Outputs `json:"outputs,omitempty"`
BuildSpec buildv1alpha1.BuildSpec `json:"buildSpec"`
}

Expand All @@ -36,6 +38,9 @@ type TaskStatus struct {
// Important: Run "make" to regenerate code after modifying this file
}

// Assert that Task implements the GenericCRD interface.
var _ webhook.GenericCRD = (*Task)(nil)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down
37 changes: 37 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_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 (t *Task) Validate() *apis.FieldError {
if err := validateObjectMetadata(t.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return nil
}

func (ts *TaskSpec) Validate() *apis.FieldError {
if equality.Semantic.DeepEqual(ts, &TaskSpec{}) {
return apis.ErrMissingField(apis.CurrentField)
}

return nil
}

0 comments on commit d97057a

Please sign in to comment.