Skip to content

Commit

Permalink
Merge pull request #1398 from decker502/server-snippet
Browse files Browse the repository at this point in the history
Surpport snippet for server section by the annotation of the ingess
  • Loading branch information
aledbf authored Sep 27, 2017
2 parents ca9a46f + 5dfee7b commit 75cccbb
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ stream {
{{ end }}
{{ end }}

{{ if not (empty $server.ServerSnippet) }}
{{ $server.ServerSnippet }}
{{ end }}

{{ range $location := $server.Locations }}
{{ $path := buildLocation $location }}
{{ $authPath := buildAuthLocation $location }}
Expand Down
42 changes: 42 additions & 0 deletions core/pkg/ingress/annotations/serversnippet/main.go
Original file line number Diff line number Diff line change
@@ -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/api/extensions/v1beta1"

"k8s.io/ingress/core/pkg/ingress/annotations/parser"
)

const (
annotation = "ingress.kubernetes.io/server-snippet"
)

type serverSnippet struct {
}

// NewParser creates a new server snippet 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 changes: 58 additions & 0 deletions core/pkg/ingress/annotations/serversnippet/main_test.go
Original file line number Diff line number Diff line change
@@ -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"

api "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

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)
}
}
}
8 changes: 8 additions & 0 deletions core/pkg/ingress/controller/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/ingress/core/pkg/ingress/annotations/redirect"
"k8s.io/ingress/core/pkg/ingress/annotations/rewrite"
"k8s.io/ingress/core/pkg/ingress/annotations/secureupstream"
"k8s.io/ingress/core/pkg/ingress/annotations/serversnippet"
"k8s.io/ingress/core/pkg/ingress/annotations/serviceupstream"
"k8s.io/ingress/core/pkg/ingress/annotations/sessionaffinity"
"k8s.io/ingress/core/pkg/ingress/annotations/snippet"
Expand Down Expand Up @@ -83,6 +84,7 @@ func newAnnotationExtractor(cfg extractorConfig) annotationExtractor {
"DefaultBackend": defaultbackend.NewParser(cfg),
"UpstreamVhost": upstreamvhost.NewParser(),
"VtsFilterKey": vtsfilterkey.NewParser(),
"ServerSnippet": serversnippet.NewParser(),
},
}
}
Expand Down Expand Up @@ -128,6 +130,7 @@ const (
serverAlias = "Alias"
clientBodyBufferSize = "ClientBodyBufferSize"
certificateAuth = "CertificateAuth"
serverSnippet = "ServerSnippet"
)

func (e *annotationExtractor) ServiceUpstream(ing *extensions.Ingress) bool {
Expand Down Expand Up @@ -181,3 +184,8 @@ func (e *annotationExtractor) CertificateAuth(ing *extensions.Ingress) *authtls.
secure := val.(*authtls.AuthSSLConfig)
return secure
}

func (e *annotationExtractor) ServerSnippet(ing *extensions.Ingress) string {
val, _ := e.annotations[serverSnippet].Parse(ing)
return val.(string)
}
13 changes: 13 additions & 0 deletions core/pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ func (ic *GenericController) createServers(data []*extensions.Ingress,
for _, ing := range data {
// setup server-alias based on annotations
aliasAnnotation := ic.annotations.Alias(ing)
srvsnippet := ic.annotations.ServerSnippet(ing)

for _, rule := range ing.Spec.Rules {
host := rule.Host
Expand All @@ -1003,6 +1004,17 @@ func (ic *GenericController) createServers(data []*extensions.Ingress,
}
}

//notifying the user that it has already been configured.
if servers[host].ServerSnippet != "" && srvsnippet != "" {
glog.Warningf("ingress %v/%v for host %v contains a Server Snippet section that it has already been configured.",
ing.Namespace, ing.Name, host)
}

// only add a server snippet if the server does not have one previously configured
if servers[host].ServerSnippet == "" && srvsnippet != "" {
servers[host].ServerSnippet = srvsnippet
}

// only add a certificate if the server does not have one previously configured
if servers[host].SSLCertificate != "" {
continue
Expand Down Expand Up @@ -1066,6 +1078,7 @@ func (ic *GenericController) createServers(data []*extensions.Ingress,
servers[host].Alias = ""
}
}

return servers
}

Expand Down
4 changes: 4 additions & 0 deletions core/pkg/ingress/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ type Server struct {
// CertificateAuth indicates the this server requires mutual authentication
// +optional
CertificateAuth authtls.AuthSSLConfig `json:"certificateAuth"`

// ServerSnippet returns the snippet of server
// +optional
ServerSnippet string `json:"serverSnippet"`
}

// Location describes an URI inside a server.
Expand Down

0 comments on commit 75cccbb

Please sign in to comment.