Skip to content

Commit

Permalink
Merge pull request #92 from bowei/minor-cleanup
Browse files Browse the repository at this point in the history
Minor cleanup
  • Loading branch information
bowei authored Dec 28, 2017
2 parents 2291f5d + 8de4032 commit 7691da3
Show file tree
Hide file tree
Showing 23 changed files with 486 additions and 192 deletions.
9 changes: 5 additions & 4 deletions cmd/glbc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"

"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/backends"
"k8s.io/ingress-gce/pkg/context"
"k8s.io/ingress-gce/pkg/controller"
"k8s.io/ingress-gce/pkg/loadbalancers"
neg "k8s.io/ingress-gce/pkg/networkendpointgroup"
"k8s.io/ingress-gce/pkg/storage"
"k8s.io/ingress-gce/pkg/utils"

"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
)

// Entrypoint of GLBC. Example invocation:
Expand Down Expand Up @@ -247,7 +248,7 @@ func main() {
// The default backend is known to be HTTP
defaultBackendNodePort := backends.ServicePort{
Port: int64(nodePort),
Protocol: utils.ProtocolHTTP,
Protocol: annotations.ProtocolHTTP,
SvcName: types.NamespacedName{Namespace: parts[0], Name: parts[1]},
SvcPort: intstr.FromInt(int(port)),
}
Expand Down
79 changes: 22 additions & 57 deletions pkg/annotations/annotations.go → pkg/annotations/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ limitations under the License.
package annotations

import (
"encoding/json"
"fmt"
"strconv"

"k8s.io/ingress-gce/pkg/utils"
extensions "k8s.io/api/extensions/v1beta1"
)

const (
// StatusPrefix is the prefix used in annotations used to record
// debug information in the Ingress annotations.
StatusPrefix = "ingress.kubernetes.io"

// AllowHTTPKey tells the Ingress controller to allow/block HTTP access.
// If either unset or set to true, the controller will create a
// forwarding-rule for port 80, and any additional rules based on the TLS
Expand All @@ -46,12 +48,6 @@ const (
// to the target proxies of the Ingress.
PreSharedCertKey = "ingress.gcp.kubernetes.io/pre-shared-cert"

// ServiceApplicationProtocolKey is a stringified JSON map of port names to
// protocol strings. Possible values are HTTP, HTTPS
// Example:
// '{"my-https-port":"HTTPS","my-http-port":"HTTP"}'
ServiceApplicationProtocolKey = "service.alpha.kubernetes.io/app-protocols"

// IngressClassKey picks a specific "class" for the Ingress. The controller
// only processes Ingresses with this annotation either unset, or set
// to either gceIngessClass or the empty string.
Expand All @@ -68,23 +64,21 @@ const (
// This is read only for users. Controller will overrite any user updates.
// This is only set for ingresses with ingressClass = "gce-multi-cluster"
InstanceGroupsAnnotationKey = "ingress.gcp.kubernetes.io/instance-groups"

// NetworkEndpointGroupAlphaAnnotation is the annotation key to enable GCE NEG feature for ingress backend services.
// To enable this feature, the value of the annotation must be "true".
// This annotation should be specified on services that are backing ingresses.
// WARNING: The feature will NOT be effective in the following circumstances:
// 1. NEG feature is not enabled in feature gate.
// 2. Service is not referenced in any ingress.
// 3. Adding this annotation on ingress.
NetworkEndpointGroupAlphaAnnotation = "alpha.cloud.google.com/load-balancer-neg"
)

// IngAnnotations represents ingress annotations.
type IngAnnotations map[string]string
// Ingress represents ingress annotations.
type Ingress struct {
v map[string]string
}

// FromIngress extracts the annotations from an Ingress definition.
func FromIngress(ing *extensions.Ingress) *Ingress {
return &Ingress{ing.Annotations}
}

// AllowHTTP returns the allowHTTP flag. True by default.
func (ing IngAnnotations) AllowHTTP() bool {
val, ok := ing[AllowHTTPKey]
func (ing *Ingress) AllowHTTP() bool {
val, ok := ing.v[AllowHTTPKey]
if !ok {
return true
}
Expand All @@ -96,56 +90,27 @@ func (ing IngAnnotations) AllowHTTP() bool {
}

// UseNamedTLS returns the name of the GCE SSL certificate. Empty by default.
func (ing IngAnnotations) UseNamedTLS() string {
val, ok := ing[PreSharedCertKey]
func (ing *Ingress) UseNamedTLS() string {
val, ok := ing.v[PreSharedCertKey]
if !ok {
return ""
}

return val
}

func (ing IngAnnotations) StaticIPName() string {
val, ok := ing[StaticIPNameKey]
func (ing *Ingress) StaticIPName() string {
val, ok := ing.v[StaticIPNameKey]
if !ok {
return ""
}
return val
}

func (ing IngAnnotations) IngressClass() string {
val, ok := ing[IngressClassKey]
func (ing *Ingress) IngressClass() string {
val, ok := ing.v[IngressClassKey]
if !ok {
return ""
}
return val
}

// SvcAnnotations represents Service annotations.
type SvcAnnotations map[string]string

func (svc SvcAnnotations) ApplicationProtocols() (map[string]utils.AppProtocol, error) {
val, ok := svc[ServiceApplicationProtocolKey]
if !ok {
return map[string]utils.AppProtocol{}, nil
}

var portToProtos map[string]utils.AppProtocol
err := json.Unmarshal([]byte(val), &portToProtos)

// Verify protocol is an accepted value
for _, proto := range portToProtos {
switch proto {
case utils.ProtocolHTTP, utils.ProtocolHTTPS:
default:
return nil, fmt.Errorf("invalid port application protocol: %v", proto)
}
}

return portToProtos, err
}

func (svc SvcAnnotations) NEGEnabled() bool {
v, ok := svc[NetworkEndpointGroupAlphaAnnotation]
return ok && v == "true"
}
69 changes: 69 additions & 0 deletions pkg/annotations/ingress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 annotations

import (
"testing"

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

func TestIngress(t *testing.T) {
for _, tc := range []struct {
ing *extensions.Ingress
allowHTTP bool
useNamedTLS string
staticIPName string
ingressClass string
}{
{
ing: &extensions.Ingress{},
allowHTTP: true, // defaults to true.
},
{
ing: &extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
AllowHTTPKey: "false",
IngressClassKey: "gce",
PreSharedCertKey: "shared-cert-key",
StaticIPNameKey: "1.2.3.4",
},
},
},
allowHTTP: false,
useNamedTLS: "shared-cert-key",
staticIPName: "1.2.3.4",
ingressClass: "gce",
},
} {
ing := FromIngress(tc.ing)
if x := ing.AllowHTTP(); x != tc.allowHTTP {
t.Errorf("ingress %+v; AllowHTTP() = %v, want %v", tc.ing, x, tc.allowHTTP)
}
if x := ing.UseNamedTLS(); x != tc.useNamedTLS {
t.Errorf("ingress %+v; UseNamedTLS() = %v, want %v", tc.ing, x, tc.useNamedTLS)
}
if x := ing.StaticIPName(); x != tc.staticIPName {
t.Errorf("ingress %+v; StaticIPName() = %v, want %v", tc.ing, x, tc.staticIPName)
}
if x := ing.IngressClass(); x != tc.ingressClass {
t.Errorf("ingress %+v; IngressClass() = %v, want %v", tc.ing, x, tc.ingressClass)
}
}
}
88 changes: 88 additions & 0 deletions pkg/annotations/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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 annotations

import (
"encoding/json"
"fmt"

"k8s.io/api/core/v1"
)

const (
// ServiceApplicationProtocolKey is a stringified JSON map of port names to
// protocol strings. Possible values are HTTP, HTTPS
// Example:
// '{"my-https-port":"HTTPS","my-http-port":"HTTP"}'
ServiceApplicationProtocolKey = "service.alpha.kubernetes.io/app-protocols"

// NetworkEndpointGroupAlphaAnnotation is the annotation key to enable GCE NEG feature for ingress backend services.
// To enable this feature, the value of the annotation must be "true".
// This annotation should be specified on services that are backing ingresses.
// WARNING: The feature will NOT be effective in the following circumstances:
// 1. NEG feature is not enabled in feature gate.
// 2. Service is not referenced in any ingress.
// 3. Adding this annotation on ingress.
NetworkEndpointGroupAlphaAnnotation = "alpha.cloud.google.com/load-balancer-neg"

// ProtocolHTTP protocol for a service
ProtocolHTTP AppProtocol = "HTTP"
// ProtocolHTTPS protocol for a service
ProtocolHTTPS AppProtocol = "HTTPS"
)

// AppProtocol describes the service protocol.
type AppProtocol string

// Service represents Service annotations.
type Service struct {
v map[string]string
}

// FromService extracts the annotations from an Service definition.
func FromService(obj *v1.Service) *Service {
return &Service{obj.Annotations}
}

// ApplicationProtocols returns a map of port (name or number) to the protocol
// on the port.
func (svc Service) ApplicationProtocols() (map[string]AppProtocol, error) {
val, ok := svc.v[ServiceApplicationProtocolKey]
if !ok {
return map[string]AppProtocol{}, nil
}

var portToProtos map[string]AppProtocol
err := json.Unmarshal([]byte(val), &portToProtos)

// Verify protocol is an accepted value
for _, proto := range portToProtos {
switch proto {
case ProtocolHTTP, ProtocolHTTPS:
default:
return nil, fmt.Errorf("invalid port application protocol: %v", proto)
}
}

return portToProtos, err
}

// NEGEnabled is true if the service uses NEGs.
func (svc Service) NEGEnabled() bool {
v, ok := svc.v[NetworkEndpointGroupAlphaAnnotation]
return ok && v == "true"
}
Loading

0 comments on commit 7691da3

Please sign in to comment.