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

Move nopImage and entrypointImage from pkg/… package to cmd/controller #1348

Merged
merged 2 commits into from
Oct 2, 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
19 changes: 17 additions & 2 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package main

import (
"flag"

"knative.dev/pkg/injection/sharedmain"

"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun"
)
Expand All @@ -28,9 +31,21 @@ const (
ControllerLogKey = "controller"
)

var (
entrypointImage = flag.String("entrypoint-image", "override-with-entrypoint:latest",
"The container image containing our entrypoint binary.")
nopImage = flag.String("nop-image", "override-with-nop:latest",
"The container image used to kill sidecars")
)

func main() {
flag.Parse()
images := reconciler.Images{
EntryPointImage: *entrypointImage,
NopImage: *nopImage,
}
sharedmain.Main(ControllerLogKey,
taskrun.NewController,
pipelinerun.NewController,
taskrun.NewController(images),
pipelinerun.NewController(images),
)
}
25 changes: 25 additions & 0 deletions pkg/reconciler/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 reconciler

// Images holds the images reference for a number of container images used accross tektoncd pipelines
type Images struct {
// EntryPointImage is container image containing our entrypoint binary.
EntryPointImage string
// NopImage is the container image used to kill sidecars
NopImage string
}
99 changes: 49 additions & 50 deletions pkg/reconciler/pipelinerun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,61 +42,60 @@ const (
resyncPeriod = 10 * time.Hour
)

func NewController(
ctx context.Context,
cmw configmap.Watcher,
) *controller.Impl {
logger := logging.FromContext(ctx)
kubeclientset := kubeclient.Get(ctx)
pipelineclientset := pipelineclient.Get(ctx)
taskRunInformer := taskruninformer.Get(ctx)
taskInformer := taskinformer.Get(ctx)
clusterTaskInformer := clustertaskinformer.Get(ctx)
pipelineRunInformer := pipelineruninformer.Get(ctx)
pipelineInformer := pipelineinformer.Get(ctx)
resourceInformer := resourceinformer.Get(ctx)
conditionInformer := conditioninformer.Get(ctx)
timeoutHandler := reconciler.NewTimeoutHandler(ctx.Done(), logger)
func NewController(images reconciler.Images) func(context.Context, configmap.Watcher) *controller.Impl {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
kubeclientset := kubeclient.Get(ctx)
pipelineclientset := pipelineclient.Get(ctx)
taskRunInformer := taskruninformer.Get(ctx)
taskInformer := taskinformer.Get(ctx)
clusterTaskInformer := clustertaskinformer.Get(ctx)
pipelineRunInformer := pipelineruninformer.Get(ctx)
pipelineInformer := pipelineinformer.Get(ctx)
resourceInformer := resourceinformer.Get(ctx)
conditionInformer := conditioninformer.Get(ctx)
timeoutHandler := reconciler.NewTimeoutHandler(ctx.Done(), logger)

opt := reconciler.Options{
KubeClientSet: kubeclientset,
PipelineClientSet: pipelineclientset,
ConfigMapWatcher: cmw,
ResyncPeriod: resyncPeriod,
Logger: logger,
}
opt := reconciler.Options{
KubeClientSet: kubeclientset,
PipelineClientSet: pipelineclientset,
ConfigMapWatcher: cmw,
ResyncPeriod: resyncPeriod,
Logger: logger,
}

c := &Reconciler{
Base: reconciler.NewBase(opt, pipelineRunAgentName),
pipelineRunLister: pipelineRunInformer.Lister(),
pipelineLister: pipelineInformer.Lister(),
taskLister: taskInformer.Lister(),
clusterTaskLister: clusterTaskInformer.Lister(),
taskRunLister: taskRunInformer.Lister(),
resourceLister: resourceInformer.Lister(),
conditionLister: conditionInformer.Lister(),
timeoutHandler: timeoutHandler,
}
impl := controller.NewImpl(c, c.Logger, pipelineRunControllerName)
c := &Reconciler{
Base: reconciler.NewBase(opt, pipelineRunAgentName, images),
pipelineRunLister: pipelineRunInformer.Lister(),
pipelineLister: pipelineInformer.Lister(),
taskLister: taskInformer.Lister(),
clusterTaskLister: clusterTaskInformer.Lister(),
taskRunLister: taskRunInformer.Lister(),
resourceLister: resourceInformer.Lister(),
conditionLister: conditionInformer.Lister(),
timeoutHandler: timeoutHandler,
}
impl := controller.NewImpl(c, c.Logger, pipelineRunControllerName)

timeoutHandler.SetPipelineRunCallbackFunc(impl.Enqueue)
timeoutHandler.CheckTimeouts(kubeclientset, pipelineclientset)
timeoutHandler.SetPipelineRunCallbackFunc(impl.Enqueue)
timeoutHandler.CheckTimeouts(kubeclientset, pipelineclientset)

c.Logger.Info("Setting up event handlers")
pipelineRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
DeleteFunc: impl.Enqueue,
})
c.Logger.Info("Setting up event handlers")
pipelineRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
DeleteFunc: impl.Enqueue,
})

c.tracker = tracker.New(impl.EnqueueKey, 30*time.Minute)
taskRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: controller.PassNew(impl.EnqueueControllerOf),
})
c.tracker = tracker.New(impl.EnqueueKey, 30*time.Minute)
taskRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: controller.PassNew(impl.EnqueueControllerOf),
})

c.Logger.Info("Setting up ConfigMap receivers")
c.configStore = config.NewStore(c.Logger.Named("config-store"))
c.configStore.WatchConfigs(opt.ConfigMapWatcher)
c.Logger.Info("Setting up ConfigMap receivers")
c.configStore = config.NewStore(c.Logger.Named("config-store"))
c.configStore.WatchConfigs(opt.ConfigMapWatcher)

return impl
return impl
}
}
12 changes: 7 additions & 5 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
taskrunresources "github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
"github.com/tektoncd/pipeline/pkg/system"
Expand All @@ -44,6 +45,10 @@ import (

var (
ignoreLastTransitionTime = cmpopts.IgnoreTypes(apis.Condition{}.LastTransitionTime.Inner.Time)
images = reconciler.Images{
EntryPointImage: "override-with-entrypoint:latest",
NopImage: "override-with-nop:latest",
}
)

func getRunName(pr *v1alpha1.PipelineRun) string {
Expand All @@ -58,11 +63,8 @@ func getPipelineRunController(t *testing.T, d test.Data) (test.TestAssets, func(
configMapWatcher := configmap.NewInformedWatcher(c.Kube, system.GetNamespace())
ctx, cancel := context.WithCancel(ctx)
return test.TestAssets{
Controller: NewController(
ctx,
configMapWatcher,
),
Clients: c,
Controller: NewController(images)(ctx, configMapWatcher),
Clients: c,
}, cancel
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ type Base struct {
// performance benefits, raw logger also preserves type-safety at
// the expense of slightly greater verbosity.
Logger *zap.SugaredLogger

// Images contains images to use for certain internal container
Images Images
}

// NewBase instantiates a new instance of Base implementing
// the common & boilerplate code between our reconcilers.
func NewBase(opt Options, controllerAgentName string) *Base {
func NewBase(opt Options, controllerAgentName string, images Images) *Base {
// Enrich the logs with controller name
logger := opt.Logger.Named(controllerAgentName).With(zap.String(logkey.ControllerType, controllerAgentName))

Expand All @@ -110,6 +113,7 @@ func NewBase(opt Options, controllerAgentName string) *Base {
ConfigMapWatcher: opt.ConfigMapWatcher,
Recorder: recorder,
Logger: logger,
Images: images,
}

return base
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestRecorderOptions(t *testing.T) {
Logger: zap.New(observer).Sugar(),
KubeClientSet: c.Kube,
PipelineClientSet: c.Pipeline,
}, "test")
}, "test", Images{})

if strings.Compare(reflect.TypeOf(b.Recorder).String(), "*record.recorderImpl") != 0 {
t.Errorf("Expected recorder type '*record.recorderImpl' but actual type is: %s", reflect.TypeOf(b.Recorder).String())
Expand All @@ -81,7 +81,7 @@ func TestRecorderOptions(t *testing.T) {
KubeClientSet: c.Kube,
PipelineClientSet: c.Pipeline,
Recorder: fr,
}, "test")
}, "test", Images{})

if strings.Compare(reflect.TypeOf(b.Recorder).String(), "*record.FakeRecorder") != 0 {
t.Errorf("Expected recorder type '*record.FakeRecorder' but actual type is: %s", reflect.TypeOf(b.Recorder).String())
Expand Down
113 changes: 56 additions & 57 deletions pkg/reconciler/taskrun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,62 +42,61 @@ const (
resyncPeriod = 10 * time.Hour
)

func NewController(
ctx context.Context,
cmw configmap.Watcher,
) *controller.Impl {
logger := logging.FromContext(ctx)
kubeclientset := kubeclient.Get(ctx)
pipelineclientset := pipelineclient.Get(ctx)
taskRunInformer := taskruninformer.Get(ctx)
taskInformer := taskinformer.Get(ctx)
clusterTaskInformer := clustertaskinformer.Get(ctx)
podInformer := podinformer.Get(ctx)
resourceInformer := resourceinformer.Get(ctx)
timeoutHandler := reconciler.NewTimeoutHandler(ctx.Done(), logger)

opt := reconciler.Options{
KubeClientSet: kubeclientset,
PipelineClientSet: pipelineclientset,
ConfigMapWatcher: cmw,
ResyncPeriod: resyncPeriod,
Logger: logger,
func NewController(images reconciler.Images) func(context.Context, configmap.Watcher) *controller.Impl {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
kubeclientset := kubeclient.Get(ctx)
pipelineclientset := pipelineclient.Get(ctx)
taskRunInformer := taskruninformer.Get(ctx)
taskInformer := taskinformer.Get(ctx)
clusterTaskInformer := clustertaskinformer.Get(ctx)
podInformer := podinformer.Get(ctx)
resourceInformer := resourceinformer.Get(ctx)
timeoutHandler := reconciler.NewTimeoutHandler(ctx.Done(), logger)

opt := reconciler.Options{
KubeClientSet: kubeclientset,
PipelineClientSet: pipelineclientset,
ConfigMapWatcher: cmw,
ResyncPeriod: resyncPeriod,
Logger: logger,
}

c := &Reconciler{
Base: reconciler.NewBase(opt, taskRunAgentName, images),
taskRunLister: taskRunInformer.Lister(),
taskLister: taskInformer.Lister(),
clusterTaskLister: clusterTaskInformer.Lister(),
resourceLister: resourceInformer.Lister(),
timeoutHandler: timeoutHandler,
cloudEventClient: cloudeventclient.Get(ctx),
}
impl := controller.NewImpl(c, c.Logger, taskRunControllerName)

timeoutHandler.SetTaskRunCallbackFunc(impl.Enqueue)
timeoutHandler.CheckTimeouts(kubeclientset, pipelineclientset)

c.Logger.Info("Setting up event handlers")
taskRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
})

c.tracker = tracker.New(impl.EnqueueKey, controller.GetTrackerLease(ctx))

podInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("TaskRun")),
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})

// FIXME(vdemeester) it was never set
//entrypoint cache will be initialized by controller if not provided
c.Logger.Info("Setting up Entrypoint cache")
c.cache = nil
if c.cache == nil {
c.cache, _ = entrypoint.NewCache()
}

return impl
}

c := &Reconciler{
Base: reconciler.NewBase(opt, taskRunAgentName),
taskRunLister: taskRunInformer.Lister(),
taskLister: taskInformer.Lister(),
clusterTaskLister: clusterTaskInformer.Lister(),
resourceLister: resourceInformer.Lister(),
timeoutHandler: timeoutHandler,
cloudEventClient: cloudeventclient.Get(ctx),
}
impl := controller.NewImpl(c, c.Logger, taskRunControllerName)

timeoutHandler.SetTaskRunCallbackFunc(impl.Enqueue)
timeoutHandler.CheckTimeouts(kubeclientset, pipelineclientset)

c.Logger.Info("Setting up event handlers")
taskRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
})

c.tracker = tracker.New(impl.EnqueueKey, controller.GetTrackerLease(ctx))

podInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("TaskRun")),
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})

// FIXME(vdemeester) it was never set
//entrypoint cache will be initialized by controller if not provided
c.Logger.Info("Setting up Entrypoint cache")
c.cache = nil
if c.cache == nil {
c.cache, _ = entrypoint.NewCache()
}

return impl
}
9 changes: 2 additions & 7 deletions pkg/reconciler/taskrun/entrypoint/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package entrypoint

import (
"flag"
"fmt"
"strconv"

Expand Down Expand Up @@ -56,10 +55,6 @@ var downwardMount = corev1.VolumeMount{
Name: DownwardMountName,
MountPath: DownwardMountPoint,
}
var (
entrypointImage = flag.String("entrypoint-image", "override-with-entrypoint:latest",
"The container image containing our entrypoint binary.")
)

// Cache is a simple caching mechanism allowing for caching the results of
// getting the Entrypoint of a container image from a remote registry. The
Expand Down Expand Up @@ -96,10 +91,10 @@ func AddToEntrypointCache(c *Cache, sha string, ep []string) {
// copy the entrypoint binary from the entrypoint image into the
// volume mounted at MountPoint, so that it can be mounted by
// subsequent steps and used to capture logs.
func AddCopyStep(spec *v1alpha1.TaskSpec) {
func AddCopyStep(entrypointImage string, spec *v1alpha1.TaskSpec) {
cp := corev1.Container{
Name: InitContainerName,
Image: *entrypointImage,
Image: entrypointImage,
Command: []string{"/bin/sh"},
// based on the ko version, the binary could be in one of two different locations
Args: []string{"-c", fmt.Sprintf("cp /ko-app/entrypoint %s", BinaryLocation)},
Expand Down
Loading