forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configmap for clusteringress (knative#2486)
* add clusteringress config * fix test naming
- Loading branch information
1 parent
d2201f1
commit e513e2f
Showing
9 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
93
pkg/reconciler/v1alpha1/clusteringress/config/istio_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
pkg/reconciler/v1alpha1/clusteringress/config/store_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/reconciler/v1alpha1/clusteringress/config/testdata/config-istio.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../../config/config-istio.yaml |
37 changes: 37 additions & 0 deletions
37
pkg/reconciler/v1alpha1/clusteringress/config/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.