Surpport snippet for server section by the annotation of the ingess
This commit is contained in:
parent
245e6b0b0e
commit
b1e73f2e37
7 changed files with 141 additions and 1 deletions
|
@ -3,7 +3,7 @@ all: push
|
||||||
BUILDTAGS=
|
BUILDTAGS=
|
||||||
|
|
||||||
# Use the 0.0 tag for testing, it shouldn't clobber any release builds
|
# Use the 0.0 tag for testing, it shouldn't clobber any release builds
|
||||||
RELEASE?=0.9.0-beta.8
|
RELEASE?=0.9.0-beta.8.hack
|
||||||
PREFIX?=gcr.io/google_containers/nginx-ingress-controller
|
PREFIX?=gcr.io/google_containers/nginx-ingress-controller
|
||||||
GOOS?=linux
|
GOOS?=linux
|
||||||
DOCKER?=gcloud docker --
|
DOCKER?=gcloud docker --
|
||||||
|
|
|
@ -286,6 +286,10 @@ http {
|
||||||
|
|
||||||
{{ if $cfg.EnableVtsStatus }}vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;{{ end }}
|
{{ if $cfg.EnableVtsStatus }}vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;{{ end }}
|
||||||
|
|
||||||
|
{{ if not (empty $server.ServerSnippet) }}
|
||||||
|
{{ $server.ServerSnippet }}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
{{ range $location := $server.Locations }}
|
{{ range $location := $server.Locations }}
|
||||||
{{ $path := buildLocation $location }}
|
{{ $path := buildLocation $location }}
|
||||||
{{ $authPath := buildAuthLocation $location }}
|
{{ $authPath := buildAuthLocation $location }}
|
||||||
|
|
42
core/pkg/ingress/annotations/serversnippet/main.go
Normal file
42
core/pkg/ingress/annotations/serversnippet/main.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes 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 serversnippet
|
||||||
|
|
||||||
|
import (
|
||||||
|
extensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
||||||
|
|
||||||
|
"k8s.io/ingress/core/pkg/ingress/annotations/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
annotation = "ingress.kubernetes.io/server-snippet"
|
||||||
|
)
|
||||||
|
|
||||||
|
type serverSnippet struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewParser creates a new CORS annotation parser
|
||||||
|
func NewParser() parser.IngressAnnotation {
|
||||||
|
return serverSnippet{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse parses the annotations contained in the ingress rule
|
||||||
|
// used to indicate if the location/s contains a fragment of
|
||||||
|
// configuration to be included inside the paths of the rules
|
||||||
|
func (a serverSnippet) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||||
|
return parser.GetStringAnnotation(annotation, ing)
|
||||||
|
}
|
58
core/pkg/ingress/annotations/serversnippet/main_test.go
Normal file
58
core/pkg/ingress/annotations/serversnippet/main_test.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes 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 serversnippet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
api "k8s.io/client-go/pkg/api/v1"
|
||||||
|
extensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParse(t *testing.T) {
|
||||||
|
ap := NewParser()
|
||||||
|
if ap == nil {
|
||||||
|
t.Fatalf("expected a parser.IngressAnnotation but returned nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
annotations map[string]string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{map[string]string{annotation: "more_headers"}, "more_headers"},
|
||||||
|
{map[string]string{annotation: "false"}, "false"},
|
||||||
|
{map[string]string{}, ""},
|
||||||
|
{nil, ""},
|
||||||
|
}
|
||||||
|
|
||||||
|
ing := &extensions.Ingress{
|
||||||
|
ObjectMeta: meta_v1.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: api.NamespaceDefault,
|
||||||
|
},
|
||||||
|
Spec: extensions.IngressSpec{},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
ing.SetAnnotations(testCase.annotations)
|
||||||
|
result, _ := ap.Parse(ing)
|
||||||
|
if result != testCase.expected {
|
||||||
|
t.Errorf("expected %v but returned %v, annotations: %s", testCase.expected, result, testCase.annotations)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -31,6 +31,7 @@ import (
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/ratelimit"
|
"k8s.io/ingress/core/pkg/ingress/annotations/ratelimit"
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/rewrite"
|
"k8s.io/ingress/core/pkg/ingress/annotations/rewrite"
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/secureupstream"
|
"k8s.io/ingress/core/pkg/ingress/annotations/secureupstream"
|
||||||
|
"k8s.io/ingress/core/pkg/ingress/annotations/serversnippet"
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/sessionaffinity"
|
"k8s.io/ingress/core/pkg/ingress/annotations/sessionaffinity"
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/snippet"
|
"k8s.io/ingress/core/pkg/ingress/annotations/snippet"
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/sslpassthrough"
|
"k8s.io/ingress/core/pkg/ingress/annotations/sslpassthrough"
|
||||||
|
@ -67,6 +68,7 @@ func newAnnotationExtractor(cfg extractorConfig) annotationExtractor {
|
||||||
"SessionAffinity": sessionaffinity.NewParser(),
|
"SessionAffinity": sessionaffinity.NewParser(),
|
||||||
"SSLPassthrough": sslpassthrough.NewParser(),
|
"SSLPassthrough": sslpassthrough.NewParser(),
|
||||||
"ConfigurationSnippet": snippet.NewParser(),
|
"ConfigurationSnippet": snippet.NewParser(),
|
||||||
|
"ServerSnippet": serversnippet.NewParser(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +106,7 @@ const (
|
||||||
healthCheck = "HealthCheck"
|
healthCheck = "HealthCheck"
|
||||||
sslPassthrough = "SSLPassthrough"
|
sslPassthrough = "SSLPassthrough"
|
||||||
sessionAffinity = "SessionAffinity"
|
sessionAffinity = "SessionAffinity"
|
||||||
|
serverSnippet = "ServerSnippet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *annotationExtractor) SecureUpstream(ing *extensions.Ingress) *secureupstream.Secure {
|
func (e *annotationExtractor) SecureUpstream(ing *extensions.Ingress) *secureupstream.Secure {
|
||||||
|
@ -129,3 +132,11 @@ func (e *annotationExtractor) SessionAffinity(ing *extensions.Ingress) *sessiona
|
||||||
val, _ := e.annotations[sessionAffinity].Parse(ing)
|
val, _ := e.annotations[sessionAffinity].Parse(ing)
|
||||||
return val.(*sessionaffinity.AffinityConfig)
|
return val.(*sessionaffinity.AffinityConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *annotationExtractor) ServerSnippet(ing *extensions.Ingress) string {
|
||||||
|
val, err := e.annotations[serverSnippet].Parse(ing)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("error parsing server snippet: %v", err)
|
||||||
|
}
|
||||||
|
return val.(string)
|
||||||
|
}
|
||||||
|
|
|
@ -1028,6 +1028,28 @@ func (ic *GenericController) createServers(data []interface{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// configure server snippet
|
||||||
|
for _, ingIf := range data {
|
||||||
|
ing := ingIf.(*extensions.Ingress)
|
||||||
|
if !class.IsValid(ing, ic.cfg.IngressClass, ic.cfg.DefaultIngressClass) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rule := range ing.Spec.Rules {
|
||||||
|
host := rule.Host
|
||||||
|
if host == "" {
|
||||||
|
host = defServerName
|
||||||
|
}
|
||||||
|
|
||||||
|
srvsnippet := ic.annotations.ServerSnippet(ing)
|
||||||
|
// only add a server snippet if the server does not have one previously configured
|
||||||
|
|
||||||
|
if servers[host].ServerSnippet == "" && srvsnippet != "" {
|
||||||
|
servers[host].ServerSnippet = srvsnippet
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return servers
|
return servers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,6 +211,9 @@ type Server struct {
|
||||||
// used to determine if the secret changed without the use of file
|
// used to determine if the secret changed without the use of file
|
||||||
// system notifications
|
// system notifications
|
||||||
SSLPemChecksum string `json:"sslPemChecksum"`
|
SSLPemChecksum string `json:"sslPemChecksum"`
|
||||||
|
|
||||||
|
// ServerSnippet returns the snippet of server
|
||||||
|
ServerSnippet string `json:"serverSnippet"`
|
||||||
// Locations list of URIs configured in the server.
|
// Locations list of URIs configured in the server.
|
||||||
Locations []*Location `json:"locations,omitempty"`
|
Locations []*Location `json:"locations,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue