Move API annotations into annotation_key_constants and remove api/annotations package

This commit is contained in:
Zihong Zheng
2017-05-16 15:30:29 -07:00
parent a82676a126
commit c0920f75cf
26 changed files with 215 additions and 325 deletions

View File

@@ -1,44 +0,0 @@
/*
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 service
const (
// AnnotationLoadBalancerSourceRangesKey is the key of the annotation on a service to set allowed ingress ranges on their LoadBalancers
//
// It should be a comma-separated list of CIDRs, e.g. `0.0.0.0/0` to
// allow full access (the default) or `18.0.0.0/8,56.0.0.0/8` to allow
// access only from the CIDRs currently allocated to MIT & the USPS.
//
// Not all cloud providers support this annotation, though AWS & GCE do.
AnnotationLoadBalancerSourceRangesKey = "service.beta.kubernetes.io/load-balancer-source-ranges"
// AnnotationValueExternalTrafficLocal Value of annotation to specify local endpoints behavior.
AnnotationValueExternalTrafficLocal = "OnlyLocal"
// AnnotationValueExternalTrafficGlobal Value of annotation to specify global (legacy) behavior.
AnnotationValueExternalTrafficGlobal = "Global"
// TODO: The beta annotations have been deprecated, remove them when we release k8s 1.8.
// BetaAnnotationHealthCheckNodePort Annotation specifying the healthcheck nodePort for the service.
// If not specified, annotation is created by the service api backend with the allocated nodePort.
// Will use user-specified nodePort value if specified by the client.
BetaAnnotationHealthCheckNodePort = "service.beta.kubernetes.io/healthcheck-nodeport"
// BetaAnnotationExternalTraffic An annotation that denotes if this Service desires to route
// external traffic to local endpoints only. This preserves Source IP and avoids a second hop.
BetaAnnotationExternalTraffic = "service.beta.kubernetes.io/external-traffic"
)

View File

@@ -56,7 +56,7 @@ func GetLoadBalancerSourceRanges(service *api.Service) (netsets.IPNet, error) {
return nil, fmt.Errorf("service.Spec.LoadBalancerSourceRanges: %v is not valid. Expecting a list of IP ranges. For example, 10.0.0.0/24. Error msg: %v", specs, err)
}
} else {
val := service.Annotations[AnnotationLoadBalancerSourceRangesKey]
val := service.Annotations[api.AnnotationLoadBalancerSourceRangesKey]
val = strings.TrimSpace(val)
if val == "" {
val = defaultLoadBalancerSourceRanges
@@ -64,7 +64,7 @@ func GetLoadBalancerSourceRanges(service *api.Service) (netsets.IPNet, error) {
specs := strings.Split(val, ",")
ipnets, err = netsets.ParseIPNets(specs...)
if err != nil {
return nil, fmt.Errorf("%s: %s is not valid. Expecting a comma-separated list of source IP ranges. For example, 10.0.0.0/24,192.168.2.0/24", AnnotationLoadBalancerSourceRangesKey, val)
return nil, fmt.Errorf("%s: %s is not valid. Expecting a comma-separated list of source IP ranges. For example, 10.0.0.0/24,192.168.2.0/24", api.AnnotationLoadBalancerSourceRangesKey, val)
}
}
return ipnets, nil
@@ -80,14 +80,14 @@ func RequestsOnlyLocalTraffic(service *api.Service) bool {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if l, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if l, ok := service.Annotations[api.BetaAnnotationExternalTraffic]; ok {
switch l {
case AnnotationValueExternalTrafficLocal:
case api.AnnotationValueExternalTrafficLocal:
return true
case AnnotationValueExternalTrafficGlobal:
case api.AnnotationValueExternalTrafficGlobal:
return false
default:
glog.Errorf("Invalid value for annotation %v: %v", BetaAnnotationExternalTraffic, l)
glog.Errorf("Invalid value for annotation %v: %v", api.BetaAnnotationExternalTraffic, l)
return false
}
}
@@ -107,10 +107,10 @@ func GetServiceHealthCheckNodePort(service *api.Service) int32 {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if l, ok := service.Annotations[BetaAnnotationHealthCheckNodePort]; ok {
if l, ok := service.Annotations[api.BetaAnnotationHealthCheckNodePort]; ok {
p, err := strconv.Atoi(l)
if err != nil {
glog.Errorf("Failed to parse annotation %v: %v", BetaAnnotationHealthCheckNodePort, err)
glog.Errorf("Failed to parse annotation %v: %v", api.BetaAnnotationHealthCheckNodePort, err)
return 0
}
return int32(p)
@@ -122,7 +122,7 @@ func GetServiceHealthCheckNodePort(service *api.Service) int32 {
// for NodePort / LoadBalancer service to Global for consistency.
// TODO: Move this default logic to default.go once beta annotation is deprecated.
func SetDefaultExternalTrafficPolicyIfNeeded(service *api.Service) {
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if _, ok := service.Annotations[api.BetaAnnotationExternalTraffic]; ok {
// Don't default this field if beta annotation exists.
return
} else if (service.Spec.Type == api.ServiceTypeNodePort ||
@@ -137,8 +137,8 @@ func ClearExternalTrafficPolicy(service *api.Service) {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
delete(service.Annotations, BetaAnnotationExternalTraffic)
if _, ok := service.Annotations[api.BetaAnnotationExternalTraffic]; ok {
delete(service.Annotations, api.BetaAnnotationExternalTraffic)
return
}
service.Spec.ExternalTrafficPolicy = api.ServiceExternalTrafficPolicyType("")
@@ -150,11 +150,11 @@ func SetServiceHealthCheckNodePort(service *api.Service, hcNodePort int32) {
// First check the beta annotation and then the first class field. This is so that
// existing Services continue to work till the user decides to transition to the
// first class field.
if _, ok := service.Annotations[BetaAnnotationExternalTraffic]; ok {
if _, ok := service.Annotations[api.BetaAnnotationExternalTraffic]; ok {
if hcNodePort == 0 {
delete(service.Annotations, BetaAnnotationHealthCheckNodePort)
delete(service.Annotations, api.BetaAnnotationHealthCheckNodePort)
} else {
service.Annotations[BetaAnnotationHealthCheckNodePort] = fmt.Sprintf("%d", hcNodePort)
service.Annotations[api.BetaAnnotationHealthCheckNodePort] = fmt.Sprintf("%d", hcNodePort)
}
return
}

View File

@@ -33,7 +33,7 @@ import (
func TestGetLoadBalancerSourceRanges(t *testing.T) {
checkError := func(v string) {
annotations := make(map[string]string)
annotations[AnnotationLoadBalancerSourceRangesKey] = v
annotations[api.AnnotationLoadBalancerSourceRangesKey] = v
svc := api.Service{}
svc.Annotations = annotations
_, err := GetLoadBalancerSourceRanges(&svc)
@@ -56,7 +56,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) {
checkOK := func(v string) netsets.IPNet {
annotations := make(map[string]string)
annotations[AnnotationLoadBalancerSourceRangesKey] = v
annotations[api.AnnotationLoadBalancerSourceRangesKey] = v
svc := api.Service{}
svc.Annotations = annotations
cidrs, err := GetLoadBalancerSourceRanges(&svc)
@@ -101,7 +101,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) {
}
// check SourceRanges annotation is empty
annotations := make(map[string]string)
annotations[AnnotationLoadBalancerSourceRangesKey] = ""
annotations[api.AnnotationLoadBalancerSourceRangesKey] = ""
svc = api.Service{}
svc.Annotations = annotations
cidrs, err = GetLoadBalancerSourceRanges(&svc)
@@ -226,7 +226,7 @@ func TestNeedsHealthCheck(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: "invalid",
api.BetaAnnotationExternalTraffic: "invalid",
},
},
})
@@ -236,7 +236,7 @@ func TestNeedsHealthCheck(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficGlobal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
},
},
})
@@ -246,7 +246,7 @@ func TestNeedsHealthCheck(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficLocal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
},
},
})
@@ -291,8 +291,8 @@ func TestGetServiceHealthCheckNodePort(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficLocal,
BetaAnnotationHealthCheckNodePort: "34567",
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
api.BetaAnnotationHealthCheckNodePort: "34567",
},
},
})
@@ -350,7 +350,7 @@ func TestSetDefaultExternalTrafficPolicyIfNeeded(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficLocal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
},
},
},
@@ -360,7 +360,7 @@ func TestSetDefaultExternalTrafficPolicyIfNeeded(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficLocal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
},
},
},
@@ -372,7 +372,7 @@ func TestSetDefaultExternalTrafficPolicyIfNeeded(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficGlobal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
},
},
},
@@ -382,7 +382,7 @@ func TestSetDefaultExternalTrafficPolicyIfNeeded(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficGlobal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
},
},
},
@@ -419,7 +419,7 @@ func TestClearExternalTrafficPolicy(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficLocal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
},
},
},
@@ -428,7 +428,7 @@ func TestClearExternalTrafficPolicy(t *testing.T) {
for i, tc := range testCases {
ClearExternalTrafficPolicy(tc.inputService)
if _, ok := tc.inputService.Annotations[BetaAnnotationExternalTraffic]; ok ||
if _, ok := tc.inputService.Annotations[api.BetaAnnotationExternalTraffic]; ok ||
tc.inputService.Spec.ExternalTrafficPolicy != "" {
t.Errorf("%v: failed to clear ExternalTrafficPolicy", i)
spew.Dump(tc)
@@ -471,7 +471,7 @@ func TestSetServiceHealthCheckNodePort(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficGlobal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
},
},
},
@@ -485,7 +485,7 @@ func TestSetServiceHealthCheckNodePort(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BetaAnnotationExternalTraffic: AnnotationValueExternalTrafficGlobal,
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficGlobal,
},
},
},
@@ -501,7 +501,7 @@ func TestSetServiceHealthCheckNodePort(t *testing.T) {
t.Errorf("%v: got HealthCheckNodePort %v, want %v", i, tc.inputService.Spec.HealthCheckNodePort, tc.hcNodePort)
}
} else {
l, ok := tc.inputService.Annotations[BetaAnnotationHealthCheckNodePort]
l, ok := tc.inputService.Annotations[api.BetaAnnotationHealthCheckNodePort]
if tc.hcNodePort == 0 {
if ok {
t.Errorf("%v: HealthCheckNodePort set, want it to be cleared", i)