Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init-webhook #21

Merged
merged 2 commits into from
Jul 23, 2019
Merged
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
46 changes: 44 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/webhook/kodata/LICENSE
1 change: 1 addition & 0 deletions cmd/webhook/kodata/VENDOR-LICENSE
109 changes: 109 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Copyright 2019 The Tekton 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 main

import (
"flag"
"log"

"github.com/knative/pkg/configmap"
"github.com/knative/pkg/logging"
"github.com/knative/pkg/logging/logkey"
"github.com/knative/pkg/signals"
"github.com/knative/pkg/webhook"
"github.com/tektoncd/pipeline/pkg/system"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

// WebhookLogKey is the name of the logger for the webhook cmd
const (
WebhookLogKey = "webhook"
// ConfigName is the name of the ConfigMap that the logging config will be stored in
ConfigName = "config-logging-triggers"
)

func main() {
flag.Parse()
cm, err := configmap.Load("/etc/config-logging")
if err != nil {
log.Fatalf("Error loading logging configuration: %v", err)
}
config, err := logging.NewConfigFromMap(cm)
if err != nil {
log.Fatalf("Error parsing logging configuration: %v", err)
}
logger, atomicLevel := logging.NewLoggerFromConfig(config, WebhookLogKey)

defer func() {
err := logger.Sync()
if err != nil {
logger.Fatal("Failed to sync the logger", zap.Error(err))
}
}()

logger = logger.With(zap.String(logkey.ControllerType, "webhook"))

logger.Info("Starting the Configuration Webhook")

// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()

clusterConfig, err := rest.InClusterConfig()
if err != nil {
logger.Fatal("Failed to get in cluster config", zap.Error(err))
}

kubeClient, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
logger.Fatal("Failed to get the client set", zap.Error(err))
}
// Watch the logging config map and dynamically update logging levels.
configMapWatcher := configmap.NewInformedWatcher(kubeClient, system.GetNamespace())
configMapWatcher.Watch(ConfigName, logging.UpdateLevelFromConfigMap(logger, atomicLevel, WebhookLogKey))
if err = configMapWatcher.Start(stopCh); err != nil {
logger.Fatalf("failed to start configuration manager: %v", err)
}

options := webhook.ControllerOptions{
ServiceName: "tekton-triggers-webhook",
DeploymentName: "tekton-triggers-webhook",
Namespace: system.GetNamespace(),
Port: 8443,
SecretName: "triggers-webhook-certs",
WebhookName: "triggers-webhook.tekton.dev",
}
//TODO add validations here
controller := webhook.AdmissionController{
Client: kubeClient,
Options: options,
Handlers: map[schema.GroupVersionKind]webhook.GenericCRD{
v1alpha1.SchemeGroupVersion.WithKind("EventListener"): &v1alpha1.EventListener{},
v1alpha1.SchemeGroupVersion.WithKind("TriggerBinding"): &v1alpha1.TriggerBinding{},
v1alpha1.SchemeGroupVersion.WithKind("TriggerTemplate"): &v1alpha1.TriggerTemplate{},
},
Logger: logger,
ncskier marked this conversation as resolved.
Show resolved Hide resolved
DisallowUnknownFields: true,
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not entirely sure what the WithContext: field does in the pipeline webhook, but is there a reason why we don't want to include it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

The propose of this setting is try to put some default value in to context, I guess we still not ready for this part, anyway, I add one, just return the parameter itself.

WithContext: func(ctx context.Context) context.Context {
                        return ctx
                },

Copy link
Member

Choose a reason for hiding this comment

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

If it's not used, I think you can just skip this field altogether. It can be used to pass some value/key to tell the controller that it will need to update some version to set defaults, etc..


if err := controller.Run(stopCh); err != nil {
logger.Fatal("Error running admission controller", zap.Error(err))
}
}
9 changes: 9 additions & 0 deletions config/200-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tekton-triggers-admin
rules:
- apiGroups: [""]
resources: ["configmaps", "secrets"]
verbs: ["get", "list", "create", "watch"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get"]
- apiGroups: ["admissionregistration.k8s.io"]
resources: ["mutatingwebhookconfigurations"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
- apiGroups: ["tekton.dev"]
resources: ["eventlisteners", "triggerbindings", "triggertemplates"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
Expand Down
13 changes: 13 additions & 0 deletions config/webhook-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
labels:
app: tekton-triggers-webhook
name: tekton-triggers-webhook
namespace: tekton-pipelines
spec:
ports:
- port: 443
targetPort: 8443
selector:
app: tekton-triggers-webhook
44 changes: 44 additions & 0 deletions config/webhook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2019 The Tekton 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
#
# https://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.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: tekton-triggers-webhook
namespace: tekton-pipelines
spec:
replicas: 1
template:
metadata:
annotations:
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
labels:
app: tekton-triggers-webhook
spec:
serviceAccountName: tekton-triggers-controller
containers:
- name: webhook
image: github.com/tektoncd/triggers/cmd/webhook
env:
- name: SYSTEM_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: config-logging
mountPath: /etc/config-logging
ncskier marked this conversation as resolved.
Show resolved Hide resolved
volumes:
- name: config-logging
configMap:
name: config-logging-triggers
7 changes: 7 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v1alpha1

import (
"context"
)

func (el *EventListener) SetDefaults(ctx context.Context) {}
7 changes: 7 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_binding_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v1alpha1

import (
"context"
)

func (tb *TriggerBinding) SetDefaults(ctx context.Context) {}
30 changes: 30 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_binding_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2019 The Tekton 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 (
"context"
"github.com/knative/pkg/apis"
)

func (t *TriggerBinding) Validate(ctx context.Context) *apis.FieldError {
return t.Spec.Validate(ctx)
}

func (s *TriggerBindingSpec) Validate(ctx context.Context) *apis.FieldError {
return nil
}
7 changes: 7 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_template_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v1alpha1

import (
"context"
)

func (tt *TriggerTemplate) SetDefaults(ctx context.Context) {}
30 changes: 30 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_template_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2019 The Tekton 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 (
"context"
"github.com/knative/pkg/apis"
)

func (t *TriggerTemplate) Validate(ctx context.Context) *apis.FieldError {
return t.Spec.Validate(ctx)
}

func (s *TriggerTemplateSpec) Validate(ctx context.Context) *apis.FieldError {
return nil
}
Loading