Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Have the ClusterBuildTemplate controller instantiate Image resources.
Browse files Browse the repository at this point in the history
Unlike the BuildTemplate controller, which colocates the Image references,
this places the Image resources (which are namespaced) in the knative-build
namespace.
  • Loading branch information
mattmoor committed Sep 17, 2018
1 parent b7ec08b commit 16a016b
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 22 deletions.
39 changes: 23 additions & 16 deletions pkg/reconciler/buildtemplate/buildtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,8 @@ func (c *Reconciler) reconcileImageCaches(ctx context.Context, bt *v1alpha1.Buil
}

// Make sure we have all of the desired caching resources.
if missing := allCached(ics, eics); len(missing) > 0 {
grp, _ := errgroup.WithContext(ctx)

for _, m := range missing {
m := m
grp.Go(func() error {
_, err := c.cachingclientset.CachingV1alpha1().Images(m.Namespace).Create(&m)
return err
})
}

// Wait for the creates to complete.
if err := grp.Wait(); err != nil {
return err
}
if err := CreateMissingImageCaches(ctx, c.cachingclientset, ics, eics); err != nil {
return err
}

// Delete any Image caches relevant to older versions of this resource.
Expand All @@ -178,7 +165,7 @@ func (c *Reconciler) reconcileImageCaches(ctx context.Context, bt *v1alpha1.Buil
)
}

func allCached(desired []caching.Image, observed []*caching.Image) (missing []caching.Image) {
func missingImageCaches(desired []caching.Image, observed []*caching.Image) (missing []caching.Image) {
for _, d := range desired {
found := false
for _, o := range observed {
Expand All @@ -193,3 +180,23 @@ func allCached(desired []caching.Image, observed []*caching.Image) (missing []ca
}
return missing
}

func CreateMissingImageCaches(ctx context.Context, client cachingclientset.Interface,
desired []caching.Image, observed []*caching.Image) error {
missing := missingImageCaches(desired, observed)
if len(missing) == 0 {
return nil
}

grp, _ := errgroup.WithContext(ctx)
for _, m := range missing {
m := m
grp.Go(func() error {
_, err := client.CachingV1alpha1().Images(m.Namespace).Create(&m)
return err
})
}

// Wait for the creates to complete.
return grp.Wait()
}
4 changes: 2 additions & 2 deletions pkg/reconciler/buildtemplate/resources/imagecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

// Note: namespace is passed separately because this may be used for
// cluster-scoped stuff as well.
func makeImageCachesFromSpec(
func MakeImageCachesFromSpec(
namespace string,
bt names.ImageCacheable,
) []caching.Image {
Expand Down Expand Up @@ -67,5 +67,5 @@ func makeImageCachesFromSpec(
}

func MakeImageCaches(bt *v1alpha1.BuildTemplate) []caching.Image {
return makeImageCachesFromSpec(bt.Namespace, bt)
return MakeImageCachesFromSpec(bt.Namespace, bt)
}
47 changes: 43 additions & 4 deletions pkg/reconciler/clusterbuildtemplate/clusterbuildtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ import (

"go.uber.org/zap"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"

"github.com/knative/pkg/controller"
"github.com/knative/pkg/kmeta"
"github.com/knative/pkg/logging"
"github.com/knative/pkg/logging/logkey"

"github.com/knative/build/pkg/apis/build/v1alpha1"
clientset "github.com/knative/build/pkg/client/clientset/versioned"
buildscheme "github.com/knative/build/pkg/client/clientset/versioned/scheme"
informers "github.com/knative/build/pkg/client/informers/externalversions/build/v1alpha1"
listers "github.com/knative/build/pkg/client/listers/build/v1alpha1"
"github.com/knative/build/pkg/reconciler/buildtemplate"
"github.com/knative/build/pkg/reconciler/clusterbuildtemplate/resources"
"github.com/knative/build/pkg/system"
cachingclientset "github.com/knative/caching/pkg/client/clientset/versioned"
cachinginformers "github.com/knative/caching/pkg/client/informers/externalversions/caching/v1alpha1"
cachinglisters "github.com/knative/caching/pkg/client/listers/caching/v1alpha1"
Expand All @@ -50,7 +56,7 @@ type Reconciler struct {
cachingclientset cachingclientset.Interface

clusterBuildTemplatesLister listers.ClusterBuildTemplateLister
imageLister cachinglisters.ImageLister
imagesLister cachinglisters.ImageLister

// Sugared logger is easier to use but is not as performant as the
// raw logger. In performance critical paths, call logger.Desugar()
Expand Down Expand Up @@ -87,7 +93,7 @@ func NewController(
buildclientset: buildclientset,
cachingclientset: cachingclientset,
clusterBuildTemplatesLister: clusterBuildTemplateInformer.Lister(),
imageLister: imageInformer.Lister(),
imagesLister: imageInformer.Lister(),
Logger: logger,
}
impl := controller.NewImpl(r, logger, "ClusterBuildTemplates")
Expand All @@ -99,6 +105,14 @@ func NewController(
UpdateFunc: controller.PassNew(impl.Enqueue),
})

imageInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("ClusterBuildTemplate")),
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: impl.EnqueueControllerOf,
UpdateFunc: controller.PassNew(impl.EnqueueControllerOf),
},
})

return impl
}

Expand All @@ -107,14 +121,39 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
logger := logging.FromContext(ctx)

// Get the ClusterBuildTemplate resource with this key
if _, err := c.clusterBuildTemplatesLister.Get(key); errors.IsNotFound(err) {
cbt, err := c.clusterBuildTemplatesLister.Get(key)
if errors.IsNotFound(err) {
// The ClusterBuildTemplate resource may no longer exist, in which case we stop processing.
logger.Errorf("clusterbuildtemplate %q in work queue no longer exists", key)
return nil
} else if err != nil {
return err
}

// TODO: Meaningful reconciliation.
if err := c.reconcileImageCaches(ctx, cbt); err != nil {
return err
}

return nil
}

func (c *Reconciler) reconcileImageCaches(ctx context.Context, cbt *v1alpha1.ClusterBuildTemplate) error {
ics := resources.MakeImageCaches(cbt)

eics, err := c.imagesLister.Images(system.Namespace).List(kmeta.MakeVersionLabelSelector(cbt))
if err != nil {
return err
}

// Make sure we have all of the desired caching resources.
if err := buildtemplate.CreateMissingImageCaches(ctx, c.cachingclientset, ics, eics); err != nil {
return err
}

// Delete any Image caches relevant to older versions of this resource.
propPolicy := metav1.DeletePropagationForeground
return c.cachingclientset.CachingV1alpha1().Images(system.Namespace).DeleteCollection(
&metav1.DeleteOptions{PropagationPolicy: &propPolicy},
metav1.ListOptions{LabelSelector: kmeta.MakeOldVersionLabelSelector(cbt).String()},
)
}
28 changes: 28 additions & 0 deletions pkg/reconciler/clusterbuildtemplate/resources/imagecache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
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 resources

import (
"github.com/knative/build/pkg/apis/build/v1alpha1"
buildtemplateresources "github.com/knative/build/pkg/reconciler/buildtemplate/resources"
"github.com/knative/build/pkg/system"
caching "github.com/knative/caching/pkg/apis/caching/v1alpha1"
)

func MakeImageCaches(bt *v1alpha1.ClusterBuildTemplate) []caching.Image {
return buildtemplateresources.MakeImageCachesFromSpec(system.Namespace, bt)
}
Loading

0 comments on commit 16a016b

Please sign in to comment.