Skip to content
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
1 change: 1 addition & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ${GOPATH}/bin/deepcopy-gen \
-O zz_generated.deepcopy \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \
-i github.com/knative/serving/pkg/reconciler/v1alpha1/revision/config \
-i github.com/knative/serving/pkg/reconciler/v1alpha1/route/config \
-i github.com/knative/serving/pkg/autoscaler \
-i github.com/knative/serving/pkg/logging

Expand Down
158 changes: 158 additions & 0 deletions pkg/reconciler/config/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
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

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.
*/

package config

import (
"reflect"
"sync/atomic"

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

// The UntypedStore expects a logger that conforms to this interface
// The store will log when updates succeed or fail
type Logger interface {
Infof(string, ...interface{})
Fatalf(string, ...interface{})
Errorf(string, ...interface{})
}

// Constructors is a map for specifying config names to
// their function constructors
//
// The values of this map must be functions with the definition
//
// func(*k8s.io/api/core/v1.ConfigMap) (... , error)
//
// These functions can return any type along with an error
type Constructors map[string]interface{}

// An UntypedStore is a responsible for storing and
// constructing configs from Kubernetes ConfigMaps
//
// WatchConfigs should be used with a configmap,Watcher
// in order for this store to remain up to date
type UntypedStore struct {
name string
logger Logger

storages map[string]*atomic.Value
constructors map[string]reflect.Value
}

// NewUntypedStore creates an UntypedStore with given name,
// Logger and Constructors
//
// The Logger must not be nil
//
// The values in the Constructors map must be functions with
// the definition
//
// func(*k8s.io/api/core/v1.ConfigMap) (... , error)
//
// These functions can return any type along with an error.
// If the function definition differs then NewUntypedStore
// will panic.
func NewUntypedStore(
name string,
logger Logger,
constructors Constructors) *UntypedStore {

store := &UntypedStore{
name: name,
logger: logger,
storages: make(map[string]*atomic.Value),
constructors: make(map[string]reflect.Value),
}

for configName, constructor := range constructors {
store.registerConfig(configName, constructor)
}

return store
}

func (s *UntypedStore) registerConfig(name string, constructor interface{}) {
cType := reflect.TypeOf(constructor)

if cType.Kind() != reflect.Func {
panic("config constructor must be a function")
}

if cType.NumIn() != 1 || cType.In(0) != reflect.TypeOf(&corev1.ConfigMap{}) {
panic("config constructor must be of the type func(*k8s.io/api/core/v1/ConfigMap) (..., error)")
}

errorType := reflect.TypeOf((*error)(nil)).Elem()

if cType.NumOut() != 2 || !cType.Out(1).Implements(errorType) {
panic("config constructor must be of the type func(*k8s.io/api/core/v1/ConfigMap) (..., error)")
}

s.storages[name] = &atomic.Value{}
s.constructors[name] = reflect.ValueOf(constructor)
}

// WatchConfigs uses the provided configmap.Watcher
// to setup watches for the config names provided in the
// Constructors map
func (s *UntypedStore) WatchConfigs(w configmap.Watcher) {
for configMapName := range s.constructors {
w.Watch(configMapName, s.OnConfigChanged)
}
}

// UntypedLoad will return the constructed value for a given
// ConfigMap name
func (s *UntypedStore) UntypedLoad(name string) interface{} {
storage := s.storages[name]
return storage.Load()
}

// OnConfigChanged will invoke the mapped constructor against
// a Kubernetes ConfigMap. If successful it will be stored.
// If construction fails during the first appearance the store
// will log a fatal error. If construction fails while updating
// the store will log an error message.
func (s *UntypedStore) OnConfigChanged(c *corev1.ConfigMap) {
name := c.ObjectMeta.Name

storage := s.storages[name]
constructor := s.constructors[name]

inputs := []reflect.Value{
reflect.ValueOf(c),
}

outputs := constructor.Call(inputs)
result := outputs[0].Interface()
errVal := outputs[1]

if !errVal.IsNil() {
err := errVal.Interface()
if storage.Load() != nil {
s.logger.Errorf("Error updating %s config %q: %q", s.name, name, err)
} else {
s.logger.Fatalf("Error initializing %s config %q: %q", s.name, name, err)
}
return
}

s.logger.Infof("%s config %q config was added or updated: %v", s.name, name, result)
storage.Store(result)
}
224 changes: 224 additions & 0 deletions pkg/reconciler/config/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
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 config

import (
"errors"
"fmt"
"os"
"os/exec"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/knative/pkg/configmap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/knative/pkg/logging/testing"
)

func TestStoreBadConstructors(t *testing.T) {
tests := []struct {
name string
constructor interface{}
}{{
name: "not a function",
constructor: "i'm pretending to be a function",
}, {
name: "no function arguments",
constructor: func() (bool, error) { return true, nil },
}, {
name: "single argument is not a configmap",
constructor: func(bool) (bool, error) { return true, nil },
}, {
name: "single return",
constructor: func(*corev1.ConfigMap) error { return nil },
}, {
name: "wrong second return",
constructor: func(*corev1.ConfigMap) (bool, bool) { return true, true },
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected NewUntypedStore to panic")
}
}()

NewUntypedStore("store", nil, Constructors{
"test": test.constructor,
})
})
}
}

func TestStoreWatchConfigs(t *testing.T) {
constructor := func(c *corev1.ConfigMap) (interface{}, error) {
return c.Name, nil
}

store := NewUntypedStore(
"name",
TestLogger(t),
Constructors{
"config-name-1": constructor,
"config-name-2": constructor,
},
)

watcher := &mockWatcher{}
store.WatchConfigs(watcher)

want := []string{
"config-name-1",
"config-name-2",
}

got := watcher.watches

if diff := cmp.Diff(want, got, sortStrings); diff != "" {
t.Errorf("Unexpected configmap watches (-want, +got): %v", diff)
}
}

func TestStoreConfigChange(t *testing.T) {
constructor := func(c *corev1.ConfigMap) (interface{}, error) {
return c.Name, nil
}

store := NewUntypedStore(
"name",
TestLogger(t),
Constructors{
"config-name-1": constructor,
"config-name-2": constructor,
},
)

store.OnConfigChanged(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-name-1",
},
})

result := store.UntypedLoad("config-name-1")

if diff := cmp.Diff(result, "config-name-1"); diff != "" {
t.Errorf("Expected loaded value diff: %s", diff)
}

result = store.UntypedLoad("config-name-2")

if diff := cmp.Diff(result, nil); diff != "" {
t.Errorf("Unexpected loaded value diff: %s", diff)
}

store.OnConfigChanged(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-name-2",
},
})

result = store.UntypedLoad("config-name-2")

if diff := cmp.Diff(result, "config-name-2"); diff != "" {
t.Errorf("Expected loaded value diff: %s", diff)
}
}

func TestStoreFailedFirstConversionCrashes(t *testing.T) {
if os.Getenv("CRASH") == "1" {
constructor := func(c *corev1.ConfigMap) (interface{}, error) {
return nil, errors.New("failure")
}

store := NewUntypedStore("name", TestLogger(t),
Constructors{"config-name-1": constructor},
)

store.OnConfigChanged(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-name-1",
},
})
return
}

cmd := exec.Command(os.Args[0], fmt.Sprintf("-test.run=%s", t.Name()))
cmd.Env = append(os.Environ(), "CRASH=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process should have exited with status 1 - err %v", err)
}

func TestStoreFailedUpdate(t *testing.T) {
induceConstructorFailure := false

constructor := func(c *corev1.ConfigMap) (interface{}, error) {
if induceConstructorFailure {
return nil, errors.New("failure")
}

return time.Now().String(), nil
}

store := NewUntypedStore("name", TestLogger(t),
Constructors{"config-name-1": constructor},
)

store.OnConfigChanged(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-name-1",
},
})

firstLoad := store.UntypedLoad("config-name-1")

induceConstructorFailure = true
store.OnConfigChanged(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-name-1",
},
})

secondLoad := store.UntypedLoad("config-name-1")

if diff := cmp.Diff(firstLoad, secondLoad); diff != "" {
t.Errorf("Expected loaded value to remain the same dff: %s", diff)
}
}

type mockWatcher struct {
watches []string
}

func (w *mockWatcher) Watch(config string, o configmap.Observer) {
w.watches = append(w.watches, config)
}

func (*mockWatcher) Start(<-chan struct{}) error { return nil }

var _ configmap.Watcher = (*mockWatcher)(nil)

var sortStrings = cmpopts.SortSlices(func(x, y string) bool {
return x < y
})
Loading