Skip to content

Commit

Permalink
Add configmap support for eventing operator and update the manifest o…
Browse files Browse the repository at this point in the history
…f eventing (#25) (#28)

* Add support of configuring ConfigMaps with CR

* Update the manifest of eventing
  • Loading branch information
Vincent authored Apr 23, 2020
1 parent 755bf3b commit a115cb1
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 154 deletions.
284 changes: 140 additions & 144 deletions cmd/eventing-operator/kodata/knative-eventing/1-eventing.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ metadata:
name: v0.14.0-upgrade
namespace: knative-eventing
labels:
eventing.knative.dev/release: "v0.14.0"
eventing.knative.dev/release: "v0.14.1"
spec:
template:
metadata:
annotations:
sidecar.istio.io/inject: "false"
spec:
serviceAccountName: eventing-controller
restartPolicy: Never
containers:
- name: upgrade-brokers
image: gcr.io/knative-releases/knative.dev/eventing/cmd/upgrade/v0.14.0@sha256:3f796b810f763325678730849021bf53ce7b6016848f880f27298e4db9112d0f
image: gcr.io/knative-releases/knative.dev/eventing/cmd/upgrade/v0.14.0@sha256:574b61b38c5bba600c6a00de39eb7d8b75bb38f551755c21eda938653b74bef3

---
8 changes: 8 additions & 0 deletions config/300-operator-v1alpha1-knative-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ spec:
spec:
description: Spec defines the desired state of KnativeEventing
properties:
config:
additionalProperties:
additionalProperties:
type: string
type: object
description: A means to override the corresponding entries in the upstream
configmaps
type: object
registry:
description: A means to override the corresponding deployment images in the upstream.
This affects both apps/v1.Deployment and caching.internal.knative.dev/v1alpha1.Image.
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/operator/v1alpha1/knativeeventing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type KnativeEventing struct {
// KnativeEventingSpec defines the desired state of KnativeEventing
// +k8s:openapi-gen=true
type KnativeEventingSpec struct {
// A means to override the corresponding entries in the upstream configmaps
// +optional
Config map[string]map[string]string `json:"config,omitempty"`

// A means to override the corresponding deployment images in the upstream.
// If no registry is provided, the knative release images will be used.
// +optional
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/operator/v1alpha1/zz_generated.deepcopy.go

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

52 changes: 52 additions & 0 deletions pkg/reconciler/knativeeventing/common/config_maps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2020 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 common

import (
mf "github.com/manifestival/manifestival"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
eventingv1alpha1 "knative.dev/operator/pkg/apis/operator/v1alpha1"
)

// ConfigMapTransform updates the ConfigMap with the values specified in operator CR
func ConfigMapTransform(instance *eventingv1alpha1.KnativeEventing, log *zap.SugaredLogger) mf.Transformer {
return func(u *unstructured.Unstructured) error {
// Let any config in instance override everything else
if u.GetKind() == "ConfigMap" {
if data, ok := instance.Spec.Config[u.GetName()[len(`config-`):]]; ok {
UpdateConfigMap(u, data, log)
}
}
return nil
}
}

// UpdateConfigMap set some data in a configmap, only overwriting common keys if they differ
func UpdateConfigMap(cm *unstructured.Unstructured, data map[string]string, log *zap.SugaredLogger) {
for k, v := range data {
message := []interface{}{"map", cm.GetName(), k, v}
if x, found, _ := unstructured.NestedFieldNoCopy(cm.Object, "data", k); found {
if v == x {
continue
}
message = append(message, "previous", x)
}
log.Infow("Setting", message...)
unstructured.SetNestedField(cm.Object, v, "data", k)
}
}
121 changes: 121 additions & 0 deletions pkg/reconciler/knativeeventing/common/config_maps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2020 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 common

import (
"testing"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
eventingv1alpha1 "knative.dev/operator/pkg/apis/operator/v1alpha1"
)

type configMapData struct {
name string
data map[string]string
}

type updateConfigMapTest struct {
name string
config configMapData
configMap corev1.ConfigMap
expected corev1.ConfigMap
}

func makeconfigMapData(name string, data map[string]string) configMapData {
return configMapData{
name: name,
data: data,
}
}

func createConfigMapTests(t *testing.T) []updateConfigMapTest {
return []updateConfigMapTest{
{
name: "change-config-logging",
config: makeconfigMapData("logging", map[string]string{
"loglevel.controller": "debug",
"loglevel.webhook": "debug",
}),
configMap: createConfigMap("config-logging", map[string]string{
"loglevel.controller": "info",
"loglevel.webhook": "info",
}),
expected: createConfigMap("config-logging", map[string]string{
"loglevel.controller": "debug",
"loglevel.webhook": "debug",
}),
},
{
name: "change-config-logging-empty-data",
config: makeconfigMapData("logging", map[string]string{
"loglevel.controller": "debug",
"loglevel.webhook": "debug",
}),
configMap: createConfigMap("config-logging", nil),
expected: createConfigMap("config-logging", map[string]string{
"loglevel.controller": "debug",
"loglevel.webhook": "debug",
}),
},
}
}

func createConfigMap(name string, data map[string]string) corev1.ConfigMap {
return corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Data: data,
}
}

func TestConfigMapTransform(t *testing.T) {
for _, tt := range createConfigMapTests(t) {
t.Run(tt.name, func(t *testing.T) {
runConfigMapTransformTest(t, &tt)
})
}
}

func runConfigMapTransformTest(t *testing.T, tt *updateConfigMapTest) {
unstructuredConfigMap := makeUnstructured(t, &tt.configMap)
instance := &eventingv1alpha1.KnativeEventing{
Spec: eventingv1alpha1.KnativeEventingSpec{
Config: map[string]map[string]string{
tt.config.name: tt.config.data,
},
},
}

configMapTransform := ConfigMapTransform(instance, log)
configMapTransform(&unstructuredConfigMap)
validateConfigMapChanged(t, tt, &unstructuredConfigMap)
}

func validateConfigMapChanged(t *testing.T, tt *updateConfigMapTest, u *unstructured.Unstructured) {
var configMap = &corev1.ConfigMap{}
err := scheme.Scheme.Convert(u, configMap, nil)
assertEqual(t, err, nil)
assertDeepEqual(t, configMap.Data, tt.expected.Data)
}
1 change: 1 addition & 0 deletions pkg/reconciler/knativeeventing/common/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (platforms Platforms) Transformers(kubeClientSet kubernetes.Interface, inst
mf.InjectNamespace(instance.GetNamespace()),
DeploymentTransform(instance, log),
DefaultBrokerConfigMapTransform(instance, log),
ConfigMapTransform(instance, log),
}
for _, fn := range platforms {
transformer, err := fn(kubeClientSet, log)
Expand Down
12 changes: 6 additions & 6 deletions pkg/reconciler/knativeeventing/common/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ func TestTransformers(t *testing.T) {

results, err := platform.Transformers(kubeclient.Get(ctx), ke, logger)
assertEqual(t, err, nil)
// By default, there are 4 functions.
assertEqual(t, len(results), 4)
// By default, there are 5 functions.
assertEqual(t, len(results), 5)

platform = append(platform, fakePlatform)
results, err = platform.Transformers(kubeclient.Get(ctx), ke, logger)
assertEqual(t, err, nil)
// There is one function in existing platform, so there will be 5 functions in total.
assertEqual(t, len(results), 5)
// There is one function in existing platform, so there will be 6 functions in total.
assertEqual(t, len(results), 6)

platformErr = append(platformErr, fakePlatformErr)
results, err = platformErr.Transformers(kubeclient.Get(ctx), ke, logger)
assertEqual(t, err.Error(), "Test Error")
// By default, there are 4 functions.
assertEqual(t, len(results), 4)
// By default, there are 5 functions.
assertEqual(t, len(results), 5)
}

func fakePlatformErr(kubeClient kubernetes.Interface, logger *zap.SugaredLogger) (mf.Transformer, error) {
Expand Down
6 changes: 4 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package version

var (
// Version is the version that will be applied to the KnativeServing resource.
// ServingVersion is the version that will be applied to the KnativeServing resource.
ServingVersion = "0.14.0"
EventingVersion = "0.14.0"

// EventingVersion is the version that will be applied to the KnativeEventing resource.
EventingVersion = "0.14.1"
)

0 comments on commit a115cb1

Please sign in to comment.