Skip to content

Commit

Permalink
Add configmap for clusteringress (knative#2486)
Browse files Browse the repository at this point in the history
* add clusteringress config

* fix test naming
  • Loading branch information
lichuqiang authored and knative-prow-robot committed Nov 19, 2018
1 parent d2201f1 commit e513e2f
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config/config-istio.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-istio
namespace: knative-serving
data:
# Gateway used for knative traffic. Default to istio-ingressgateway.
ingress-gateway: "istio-ingressgateway.istio-system.svc.cluster.local"
1 change: 1 addition & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \
${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/clusteringress/config \
-i github.com/knative/serving/pkg/reconciler/v1alpha1/configuration/config \
-i github.com/knative/serving/pkg/reconciler/v1alpha1/revision/config \
-i github.com/knative/serving/pkg/reconciler/v1alpha1/route/config \
Expand Down
21 changes: 21 additions & 0 deletions pkg/reconciler/v1alpha1/clusteringress/config/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
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.
*/

// +k8s:deepcopy-gen=package

// Package config holds the typed objects that define the schemas for
// assorted ConfigMap objects on which the ClusterIngress controller depends.
package config
56 changes: 56 additions & 0 deletions pkg/reconciler/v1alpha1/clusteringress/config/istio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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 (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
)

const (
// IstioConfigName is the name of the configmap containing all
// customizations for istio related features.
IstioConfigName = "config-istio"

// IngressGatewayKey is the name of the configuration entry
// that specifies ingress gateway url.
IngressGatewayKey = "ingress-gateway"
)

// Istio contains istio related configuration defined in the
// istio config map.
type Istio struct {
// IngressGateway specifies the ingress gateway url.
IngressGateway string
}

// NewIstioFromConfigMap creates an Istio config from the supplied ConfigMap
func NewIstioFromConfigMap(configMap *corev1.ConfigMap) (*Istio, error) {
gateway, ok := configMap.Data[IngressGatewayKey]
if !ok {
return nil, fmt.Errorf("failed to fetch %s from configmap %s", IngressGatewayKey, IstioConfigName)
}
if errs := validation.IsDNS1123Subdomain(gateway); len(errs) > 0 {
return nil, fmt.Errorf("invalid gateway format: %v", errs)
}

return &Istio{
IngressGateway: gateway,
}, nil
}
93 changes: 93 additions & 0 deletions pkg/reconciler/v1alpha1/clusteringress/config/istio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
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 (
"testing"

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

. "github.com/knative/serving/pkg/reconciler/testing"
)

func TestIstio(t *testing.T) {
cm := ConfigMapFromTestFile(t, IstioConfigName)

if _, err := NewIstioFromConfigMap(cm); err != nil {
t.Errorf("NewIstioFromConfigMap() = %v", err)
}
}

func TestGatewayConfiguration(t *testing.T) {
gatewayConfigTests := []struct {
name string
wantErr bool
wantIstio interface{}
config *corev1.ConfigMap
}{{
name: "gateway configuration with no network input",
wantErr: true,
wantIstio: (*Istio)(nil),
config: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: system.Namespace,
Name: IstioConfigName,
},
}}, {
name: "gateway configuration with invalid url",
wantErr: true,
wantIstio: (*Istio)(nil),
config: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: system.Namespace,
Name: IstioConfigName,
},
Data: map[string]string{
IngressGatewayKey: "_invalid",
},
}}, {
name: "gateway configuration with valid url",
wantErr: false,
wantIstio: &Istio{
IngressGateway: "istio-ingressgateway.istio-system.svc.cluster.local",
},
config: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: system.Namespace,
Name: IstioConfigName,
},
Data: map[string]string{
IngressGatewayKey: "istio-ingressgateway.istio-system.svc.cluster.local",
},
}},
}

for _, tt := range gatewayConfigTests {
actualIstio, err := NewIstioFromConfigMap(tt.config)

if (err != nil) != tt.wantErr {
t.Fatalf("Test: %q; NewIstioFromConfigMap() error = %v, WantErr %v", tt.name, err, tt.wantErr)
}

if diff := cmp.Diff(actualIstio, tt.wantIstio); diff != "" {
t.Fatalf("Test: %q; want %v, but got %v", tt.name, tt.wantIstio, actualIstio)
}
}
}
83 changes: 83 additions & 0 deletions pkg/reconciler/v1alpha1/clusteringress/config/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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 (
"context"

"github.com/knative/pkg/configmap"
)

type cfgKey struct{}

// Config of Istio.
// +k8s:deepcopy-gen=false
type Config struct {
Istio *Istio
}

// FromContext fetch config from context.
func FromContext(ctx context.Context) *Config {
return ctx.Value(cfgKey{}).(*Config)
}

// ToContext adds config to given context.
func ToContext(ctx context.Context, c *Config) context.Context {
return context.WithValue(ctx, cfgKey{}, c)
}

// Store is configmap.UntypedStore based config store.
// +k8s:deepcopy-gen=false
type Store struct {
*configmap.UntypedStore
}

// NewStore creates a configmap.UntypedStore based config store.
//
// logger must be non-nil implementation of configmap.Logger (commonly used
// loggers conform)
//
// onAfterStore is a variadic list of callbacks to run
// after the ConfigMap has been processed and stored.
//
// See also: configmap.NewUntypedStore().
func NewStore(logger configmap.Logger, onAfterStore ...func(name string, value interface{})) *Store {
store := &Store{
UntypedStore: configmap.NewUntypedStore(
"clusteringress",
logger,
configmap.Constructors{
IstioConfigName: NewIstioFromConfigMap,
},
onAfterStore...,
),
}

return store
}

// ToContext adds Store contents to given context.
func (s *Store) ToContext(ctx context.Context) context.Context {
return ToContext(ctx, s.Load())
}

// Load fetches config from Store.
func (s *Store) Load() *Config {
return &Config{
Istio: s.UntypedLoad(IstioConfigName).(*Istio).DeepCopy(),
}
}
57 changes: 57 additions & 0 deletions pkg/reconciler/v1alpha1/clusteringress/config/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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 (
"context"
"testing"

"github.com/google/go-cmp/cmp"

. "github.com/knative/serving/pkg/reconciler/v1alpha1/testing"
)

func TestStoreLoadWithContext(t *testing.T) {
store := NewStore(TestLogger(t))

istioConfig := ConfigMapFromTestFile(t, IstioConfigName)
store.OnConfigChanged(istioConfig)
config := FromContext(store.ToContext(context.Background()))

t.Run("load istio", func(t *testing.T) {
expected, _ := NewIstioFromConfigMap(istioConfig)
if diff := cmp.Diff(expected, config.Istio); diff != "" {
t.Errorf("Unexpected istio config (-want, +got): %v", diff)
}
})
}

func TestStoreImmutableConfig(t *testing.T) {
store := NewStore(TestLogger(t))

store.OnConfigChanged(ConfigMapFromTestFile(t, IstioConfigName))

config := store.Load()

config.Istio.IngressGateway = "mutated"

newConfig := store.Load()

if newConfig.Istio.IngressGateway == "mutated" {
t.Error("Istio config is not immutable")
}
}

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

0 comments on commit e513e2f

Please sign in to comment.