-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from bowei/minor-cleanup
Minor cleanup
- Loading branch information
Showing
23 changed files
with
486 additions
and
192 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
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,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) | ||
} | ||
} | ||
} |
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,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" | ||
} |
Oops, something went wrong.