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

Commit

Permalink
Have the BuildTemplate controller instantiate Image resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmoor committed Sep 14, 2018
1 parent f719ff4 commit d039546
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

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

6 changes: 4 additions & 2 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,21 @@ func main() {

go kubeInformerFactory.Start(stopCh)
go buildInformerFactory.Start(stopCh)
go cachingInformerFactory.Start(stopCh)

for i, synced := range []cache.InformerSynced{
buildInformer.Informer().HasSynced,
buildTemplateInformer.Informer().HasSynced,
clusterBuildTemplateInformer.Informer().HasSynced,
// TODO(mattmoor): Start waiting for sync after something sets up an event otherwise it hangs here.
// imageInformer.Informer().HasSynced,
imageInformer.Informer().HasSynced,
} {
if ok := cache.WaitForCacheSync(stopCh, synced); !ok {
logger.Fatalf("failed to wait for cache at index %v to sync", i)
}
}

logger.Infof("SYNCED")

// Start all of the controllers.
for _, ctrlr := range controllers {
go func(ctrlr controller.Interface) {
Expand Down
2 changes: 1 addition & 1 deletion config/200-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ rules:
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
- apiGroups: ["caching.internal.knative.dev"]
resources: ["images"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
verbs: ["get", "list", "create", "update", "delete", "deletecollection", "patch", "watch"]
76 changes: 72 additions & 4 deletions pkg/reconciler/buildtemplate/buildtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ import (
"context"

"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"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/resources"
caching "github.com/knative/caching/pkg/apis/caching/v1alpha1"
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

buildTemplatesLister listers.BuildTemplateLister
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,
buildTemplatesLister: buildTemplateInformer.Lister(),
imageLister: imageInformer.Lister(),
imagesLister: imageInformer.Lister(),
Logger: logger,
}
impl := controller.NewImpl(r, logger, "BuildTemplates")
Expand All @@ -99,6 +105,14 @@ func NewController(
UpdateFunc: controller.PassNew(impl.Enqueue),
})

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

return impl
}

Expand All @@ -114,14 +128,68 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
}

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

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

return nil
}

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

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

// 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
}
}

// Delete any Image caches relevant to older versions of this resource.
propPolicy := metav1.DeletePropagationForeground
return c.cachingclientset.CachingV1alpha1().Images(bt.Namespace).DeleteCollection(
&metav1.DeleteOptions{PropagationPolicy: &propPolicy},
metav1.ListOptions{LabelSelector: kmeta.MakeOldVersionLabelSelector(bt).String()},
)
}

func allCached(desired []caching.Image, observed []*caching.Image) (missing []caching.Image) {
for _, d := range desired {
found := false
for _, o := range observed {
if d.Name == o.Name {
found = true
break
}
}
if !found {
missing = append(missing, d)
}
}
return missing
}
33 changes: 33 additions & 0 deletions third_party/VENDOR-LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -3500,6 +3500,39 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



===========================================================
Import: github.com/knative/build/vendor/golang.org/x/sync

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



===========================================================
Import: github.com/knative/build/vendor/golang.org/x/sys

Expand Down
3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/AUTHORS

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

3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/CONTRIBUTORS

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

27 changes: 27 additions & 0 deletions vendor/golang.org/x/sync/LICENSE

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

22 changes: 22 additions & 0 deletions vendor/golang.org/x/sync/PATENTS

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

67 changes: 67 additions & 0 deletions vendor/golang.org/x/sync/errgroup/errgroup.go

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

0 comments on commit d039546

Please sign in to comment.