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 over the resolver framework from Resolution. #5380

Merged
merged 1 commit into from
Aug 26, 2022
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
86 changes: 86 additions & 0 deletions pkg/resolution/resolver/framework/configstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2022 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 framework

import (
"context"

corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/configmap"
)

// resolverConfigKey is the contenxt key associated with configuration
// for one specific resolver, and is only used if that resolver
// implements the optional framework.ConfigWatcher interface.
var resolverConfigKey = struct{}{}

// DataFromConfigMap returns a copy of the contents of a configmap or an
// empty map if the configmap doesn't have any data.
func DataFromConfigMap(config *corev1.ConfigMap) (map[string]string, error) {
resolverConfig := map[string]string{}
if config == nil {
return resolverConfig, nil
}
for key, value := range config.Data {
resolverConfig[key] = value
}
return resolverConfig, nil
}

// ConfigStore wraps a knative untyped store and provides helper methods
// for working with a resolver's configuration data.
type ConfigStore struct {
resolverConfigName string
untyped *configmap.UntypedStore
}

// GetResolverConfig returns a copy of the resolver's current
// configuration or an empty map if the stored config is nil or invalid.
func (store *ConfigStore) GetResolverConfig() map[string]string {
resolverConfig := map[string]string{}
untypedConf := store.untyped.UntypedLoad(store.resolverConfigName)
if conf, ok := untypedConf.(map[string]string); ok {
for key, val := range conf {
resolverConfig[key] = val
}
}
return resolverConfig
}

// ToContext returns a new context with the resolver's configuration
// data stored in it.
func (store *ConfigStore) ToContext(ctx context.Context) context.Context {
conf := store.GetResolverConfig()
return InjectResolverConfigToContext(ctx, conf)
}

// InjectResolverConfigToContext returns a new context with a
// map stored in it for a resolvers config.
func InjectResolverConfigToContext(ctx context.Context, conf map[string]string) context.Context {
return context.WithValue(ctx, resolverConfigKey, conf)
}

// GetResolverConfigFromContext returns any resolver-specific
// configuration that has been stored or an empty map if none exists.
func GetResolverConfigFromContext(ctx context.Context) map[string]string {
conf := map[string]string{}
storedConfig := ctx.Value(resolverConfigKey)
if resolverConfig, ok := storedConfig.(map[string]string); ok {
conf = resolverConfig
}
return conf
}
92 changes: 92 additions & 0 deletions pkg/resolution/resolver/framework/configstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2022 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 framework

import (
"testing"

corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/configmap"
logtesting "knative.dev/pkg/logging/testing"
)

// TestDataFromConfigMap checks that configmaps are correctly converted
// into a map[string]string
func TestDataFromConfigMap(t *testing.T) {
for _, tc := range []struct {
configMap *corev1.ConfigMap
expected map[string]string
}{{
configMap: nil,
expected: map[string]string{},
}, {
configMap: &corev1.ConfigMap{
Data: nil,
},
expected: map[string]string{},
}, {
configMap: &corev1.ConfigMap{
Data: map[string]string{},
},
expected: map[string]string{},
}, {
configMap: &corev1.ConfigMap{
Data: map[string]string{
"foo": "bar",
},
},
expected: map[string]string{
"foo": "bar",
},
}} {
out, err := DataFromConfigMap(tc.configMap)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !mapsAreEqual(tc.expected, out) {
t.Fatalf("expected %#v received %#v", tc.expected, out)
}
}
}

func TestGetResolverConfig(t *testing.T) {
_ = &ConfigStore{
resolverConfigName: "test",
untyped: configmap.NewUntypedStore(
"test-config",
logtesting.TestLogger(t),
configmap.Constructors{
"test": DataFromConfigMap,
},
),
}
}

func mapsAreEqual(m1, m2 map[string]string) bool {
if m1 == nil || m2 == nil {
return m1 == nil && m2 == nil
}
if len(m1) != len(m2) {
return false
}
for k, v := range m1 {
if m2[k] != v {
return false
}
}
return true
}
195 changes: 195 additions & 0 deletions pkg/resolution/resolver/framework/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
Copyright 2022 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 framework

import (
"context"
"fmt"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/resolution/v1alpha1"
rrclient "github.com/tektoncd/pipeline/pkg/client/resolution/injection/client"
rrinformer "github.com/tektoncd/pipeline/pkg/client/resolution/injection/informers/resolution/v1alpha1/resolutionrequest"
rrlister "github.com/tektoncd/pipeline/pkg/client/resolution/listers/resolution/v1alpha1"
"github.com/tektoncd/pipeline/pkg/resolution/common"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/clock"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/pkg/reconciler"
)

// ReconcilerModifier is a func that can access and modify a reconciler
// in the moments before a resolver is started. It allows for
// things like injecting a test clock.
type ReconcilerModifier = func(reconciler *Reconciler)

// NewController returns a knative controller for a Tekton Resolver.
// This sets up a lot of the boilerplate that individual resolvers
// shouldn't need to be concerned with since it's common to all of them.
func NewController(ctx context.Context, resolver Resolver, modifiers ...ReconcilerModifier) func(context.Context, configmap.Watcher) *controller.Impl {
if err := validateResolver(ctx, resolver); err != nil {
panic(err.Error())
}
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
kubeclientset := kubeclient.Get(ctx)
rrclientset := rrclient.Get(ctx)
rrInformer := rrinformer.Get(ctx)

if err := resolver.Initialize(ctx); err != nil {
panic(err.Error())
}

r := &Reconciler{
LeaderAwareFuncs: leaderAwareFuncs(rrInformer.Lister()),
kubeClientSet: kubeclientset,
resolutionRequestLister: rrInformer.Lister(),
resolutionRequestClientSet: rrclientset,
resolver: resolver,
}

watchConfigChanges(ctx, r, cmw)

// TODO(sbwsg): Do better sanitize.
resolverName := resolver.GetName(ctx)
resolverName = strings.ReplaceAll(resolverName, "/", "")
resolverName = strings.ReplaceAll(resolverName, " ", "")

applyModifiersAndDefaults(ctx, r, modifiers)

impl := controller.NewContext(ctx, r, controller.ControllerOptions{
WorkQueueName: "TektonResolverFramework." + resolverName,
Logger: logger,
})

rrInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: filterResolutionRequestsBySelector(resolver.GetSelector(ctx)),
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: func(oldObj, newObj interface{}) {
impl.Enqueue(newObj)
},
// TODO(sbwsg): should we deliver delete events
// to the resolver?
// DeleteFunc: impl.Enqueue,
},
})

return impl
}
}

func filterResolutionRequestsBySelector(selector map[string]string) func(obj interface{}) bool {
return func(obj interface{}) bool {
rr, ok := obj.(*v1alpha1.ResolutionRequest)
if !ok {
return false
}
if len(rr.ObjectMeta.Labels) == 0 {
return false
}
for key, val := range selector {
lookup, has := rr.ObjectMeta.Labels[key]
if !has {
return false
}
if lookup != val {
return false
}
}
return true
}
}

// TODO(sbwsg): I don't really understand the LeaderAwareness types beyond the
// fact that the controller crashes if they're missing. It looks
// like this is bucketing based on labels. Should we use the filter
// selector from above in the call to lister.List here?
func leaderAwareFuncs(lister rrlister.ResolutionRequestLister) reconciler.LeaderAwareFuncs {
return reconciler.LeaderAwareFuncs{
PromoteFunc: func(bkt reconciler.Bucket, enq func(reconciler.Bucket, types.NamespacedName)) error {
all, err := lister.List(labels.Everything())
if err != nil {
return err
}
for _, elt := range all {
enq(bkt, types.NamespacedName{
Namespace: elt.GetNamespace(),
Name: elt.GetName(),
})
}
return nil
},
}
}

// ErrorMissingTypeSelector is returned when a resolver does not return
// a selector with a type label from its GetSelector method.
var ErrorMissingTypeSelector = fmt.Errorf("invalid resolver: minimum selector must include %q", common.LabelKeyResolverType)

func validateResolver(ctx context.Context, r Resolver) error {
sel := r.GetSelector(ctx)
if sel == nil {
return ErrorMissingTypeSelector
}
if sel[common.LabelKeyResolverType] == "" {
return ErrorMissingTypeSelector
}
return nil
}

// watchConfigChanges binds a framework.Resolver to updates on its
// configmap, using knative's configmap helpers. This is only done if
// the resolver implements the framework.ConfigWatcher interface.
func watchConfigChanges(ctx context.Context, reconciler *Reconciler, cmw configmap.Watcher) {
if configWatcher, ok := reconciler.resolver.(ConfigWatcher); ok {
logger := logging.FromContext(ctx)
resolverConfigName := configWatcher.GetConfigName(ctx)
if resolverConfigName == "" {
panic("resolver returned empty config name")
}
reconciler.configStore = &ConfigStore{
resolverConfigName: resolverConfigName,
untyped: configmap.NewUntypedStore(
"resolver-config",
logger,
configmap.Constructors{
resolverConfigName: DataFromConfigMap,
},
),
}
reconciler.configStore.untyped.WatchConfigs(cmw)
}
}

// applyModifiersAndDefaults applies the given modifiers to
// a reconciler and, after doing so, sets any default values for things
// that weren't set by a modifier.
func applyModifiersAndDefaults(ctx context.Context, r *Reconciler, modifiers []ReconcilerModifier) {
for _, mod := range modifiers {
mod(r)
}

if r.Clock == nil {
r.Clock = clock.RealClock{}
}
}
Loading