Skip to content

Commit

Permalink
Refactor the webhook/main.go to use latest knative/pkg 🔁
Browse files Browse the repository at this point in the history
This is way simpler to follow.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Jan 29, 2020
1 parent 288108e commit e00dbf3
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 60 deletions.
119 changes: 109 additions & 10 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,127 @@ package main

import (
"context"
"flag"
"log"
"os"

apiconfig "github.com/tektoncd/pipeline/pkg/apis/config"
defaultconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/contexts"
tklogging "github.com/tektoncd/pipeline/pkg/logging"
"github.com/tektoncd/pipeline/pkg/system"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
"knative.dev/pkg/logging/logkey"
"knative.dev/pkg/signals"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/configmaps"
"knative.dev/pkg/webhook/resourcesemantics"
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
"knative.dev/pkg/webhook/resourcesemantics/validation"
)

// WebhookLogKey is the name of the logger for the webhook cmd
const WebhookLogKey = "webhook"

var types = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
v1alpha1.SchemeGroupVersion.WithKind("Pipeline"): &v1alpha1.Pipeline{},
v1alpha1.SchemeGroupVersion.WithKind("Task"): &v1alpha1.Task{},
v1alpha1.SchemeGroupVersion.WithKind("ClusterTask"): &v1alpha1.ClusterTask{},
v1alpha1.SchemeGroupVersion.WithKind("TaskRun"): &v1alpha1.TaskRun{},
v1alpha1.SchemeGroupVersion.WithKind("PipelineRun"): &v1alpha1.PipelineRun{},
v1alpha1.SchemeGroupVersion.WithKind("Condition"): &v1alpha1.Condition{},
v1alpha1.SchemeGroupVersion.WithKind("PipelineResource"): &v1alpha1.PipelineResource{},
}

func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
// Decorate contexts with the current state of the config.
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
store.WatchConfigs(cmw)

return defaulting.NewAdmissionController(ctx,

// Name of the resource webhook.
"webhook.pipeline.tekton.dev",

// The path on which to serve the webhook.
"/defaulting",

// The resources to validate and default.
types,

// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
// FIXME(vdemeester) uncomment that for auto-conversion
// return v1alpha2.WithUpgradeViaDefaulting(store.ToContext(ctx))
return contexts.WithDefaultConfigurationName(store.ToContext(ctx))
},

// Whether to disallow unknown fields.
true,
)
}

func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return validation.NewAdmissionController(ctx,

// Name of the resource webhook.
"validation.webhook.pipeline.tekton.dev",

// The path on which to serve the webhook.
"/resource-validation",

// The resources to validate and default.
types,

// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
return ctx
},

// Whether to disallow unknown fields.
true,
)
}

func NewConfigValidationController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return configmaps.NewAdmissionController(ctx,

// Name of the configmap webhook.
"config.webhook.pipeline.tekton.dev",

// The path on which to serve the webhook.
"/config-validation",

// The configmaps to validate.
configmap.Constructors{
logging.ConfigMapName(): logging.NewConfigFromConfigMap,
defaultconfig.DefaultsConfigName: defaultconfig.NewDefaultsFromConfigMap,
},
)
}

func main() {
serviceName := os.Getenv("WEBHOOK_SERVICE_NAME")
if serviceName == "" {
serviceName = "tekton-pipelines-webhook"
}

// Set up a signal context with our webhook options
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
ServiceName: serviceName,
Port: 8443,
SecretName: "webhook-certs",
})

sharedmain.MainWithContext(ctx, "webhook",
certificates.NewController,
NewDefaultingAdmissionController,
NewValidationAdmissionController,
NewConfigValidationController,
)
}

/*
func main() {
flag.Parse()
cm, err := configmap.Load("/etc/config-logging")
Expand Down Expand Up @@ -96,7 +194,7 @@ func main() {
WebhookName: "webhook.tekton.dev",
ResourceAdmissionControllerPath: "/",
}
resourceHandlers := map[schema.GroupVersionKind]webhook.GenericCRD{
resourceHandlers := map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
v1alpha1.SchemeGroupVersion.WithKind("Pipeline"): &v1alpha1.Pipeline{},
v1alpha1.SchemeGroupVersion.WithKind("Task"): &v1alpha1.Task{},
v1alpha1.SchemeGroupVersion.WithKind("ClusterTask"): &v1alpha1.ClusterTask{},
Expand Down Expand Up @@ -125,3 +223,4 @@ func main() {
logger.Fatal("Error running admission controller", zap.Error(err))
}
}
*/
2 changes: 2 additions & 0 deletions config/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ spec:
fieldPath: metadata.namespace
- name: WEBHOOK_SERVICE_NAME
value: tekton-pipelines-webhook
- name: METRICS_DOMAIN
value: tekton.dev/pipeline
volumes:
- name: config-logging
configMap:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/imdario/mergo v0.3.8 // indirect
github.com/jenkins-x/go-scm v1.5.65
github.com/json-iterator/go v1.1.8 // indirect
github.com/markbates/inflect v1.0.4 // indirect
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/envy v1.6.5 h1:X3is06x7v0nW2xiy2yFbbIjwHz57CD6z6MkvqULTCm8=
github.com/gobuffalo/envy v1.6.5/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9kaifseQ=
github.com/gogo/googleapis v1.1.0 h1:kFkMAZBNAn4j7K0GiZr8cRYzejq68VbheufiV3YuyFI=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -304,6 +306,8 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
Expand Down Expand Up @@ -340,6 +344,8 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/markbates/inflect v1.0.4 h1:5fh1gzTFhfae06u3hzHYO9xe3l3v3nW5Pwt3naLTP5g=
github.com/markbates/inflect v1.0.4/go.mod h1:1fR9+pO2KHEO9ZRtto13gDwwZaAKstQzferVeWqbgNs=
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a h1:+J2gw7Bw77w/fbK7wnNJJDKmw1IbWft2Ul5BzrG1Qm8=
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
14 changes: 7 additions & 7 deletions pkg/apis/pipeline/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/resourcesemantics"
)

func TestTypes(t *testing.T) {
// Assert that types satisfy webhook interface.
var _ webhook.GenericCRD = (*v1alpha1.ClusterTask)(nil)
var _ webhook.GenericCRD = (*v1alpha1.TaskRun)(nil)
var _ webhook.GenericCRD = (*v1alpha1.PipelineResource)(nil)
var _ webhook.GenericCRD = (*v1alpha1.Task)(nil)
var _ webhook.GenericCRD = (*v1alpha1.TaskRun)(nil)
var _ webhook.GenericCRD = (*v1alpha1.Condition)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.ClusterTask)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.TaskRun)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.PipelineResource)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.Task)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.TaskRun)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.Condition)(nil)
}
13 changes: 6 additions & 7 deletions pkg/apis/pipeline/v1alpha2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ import (
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/resourcesemantics"
)

func TestTypes(t *testing.T) {
// Assert that types satisfy webhook interface.
// var _ webhook.GenericCRD = (*v1alpha2.ClusterTask)(nil)
// var _ webhook.GenericCRD = (*v1alpha2.TaskRun)(nil)
// var _ webhook.GenericCRD = (*v1alpha2.PipelineResource)(nil)
var _ webhook.GenericCRD = (*v1alpha2.Task)(nil)
// var _ webhook.GenericCRD = (*v1alpha2.TaskRun)(nil)
// var _ webhook.GenericCRD = (*v1alpha2.Condition)(nil)
// var _ resourcesemantics.GenericCRD = (*v1alpha2.ClusterTask)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha2.TaskRun)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha2.Task)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha2.Pipeline)(nil)
// var _ resourcesemantics.GenericCRD = (*v1alpha2.Condition)(nil)
}
4 changes: 2 additions & 2 deletions pkg/apis/resource/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"testing"

"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/resourcesemantics"
)

func TestTypes(t *testing.T) {
// Assert that types satisfy webhook interface.
var _ webhook.GenericCRD = (*v1alpha1.PipelineResource)(nil)
var _ resourcesemantics.GenericCRD = (*v1alpha1.PipelineResource)(nil)
}
22 changes: 16 additions & 6 deletions pkg/reconciler/pipelinerun/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/metrics/metricstest"

// Required to setup metrics env for testing
_ "knative.dev/pkg/metrics/testing"
rtesting "knative.dev/pkg/reconciler/testing"
)

Expand All @@ -44,11 +47,12 @@ func TestRecordPipelineRunDurationCount(t *testing.T) {
startTime := time.Now()

testData := []struct {
name string
taskRun *v1alpha1.PipelineRun
expectedTags map[string]string
expectedDuration float64
expectedCount int64
name string
taskRun *v1alpha1.PipelineRun
expectedTags map[string]string
expectedCountTags map[string]string
expectedDuration float64
expectedCount int64
}{{
name: "for_succeeded_pipeline",
taskRun: tb.PipelineRun("pipelinerun-1", "ns",
Expand All @@ -67,6 +71,9 @@ func TestRecordPipelineRunDurationCount(t *testing.T) {
"namespace": "ns",
"status": "success",
},
expectedCountTags: map[string]string{
"status": "success",
},
expectedDuration: 60,
expectedCount: 1,
}, {
Expand All @@ -87,6 +94,9 @@ func TestRecordPipelineRunDurationCount(t *testing.T) {
"namespace": "ns",
"status": "failed",
},
expectedCountTags: map[string]string{
"status": "failed",
},
expectedDuration: 60,
expectedCount: 1,
}}
Expand All @@ -101,7 +111,7 @@ func TestRecordPipelineRunDurationCount(t *testing.T) {
err = metrics.DurationAndCount(test.taskRun)
assertErrIsNil(err, "DurationAndCount recording recording got an error", t)
metricstest.CheckDistributionData(t, "pipelinerun_duration_seconds", test.expectedTags, 1, test.expectedDuration, test.expectedDuration)
metricstest.CheckCountData(t, "pipelinerun_count", test.expectedTags, test.expectedCount)
metricstest.CheckCountData(t, "pipelinerun_count", test.expectedCountTags, test.expectedCount)

})
}
Expand Down
Loading

0 comments on commit e00dbf3

Please sign in to comment.