github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/service/util.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package service 18 19 import ( 20 "fmt" 21 "strings" 22 23 netsets "k8s.io/kubernetes/pkg/util/net/sets" 24 ) 25 26 const ( 27 defaultLoadBalancerSourceRanges = "0.0.0.0/0" 28 ) 29 30 // IsAllowAll checks whether the netsets.IPNet allows traffic from 0.0.0.0/0 31 func IsAllowAll(ipnets netsets.IPNet) bool { 32 for _, s := range ipnets.StringSlice() { 33 if s == "0.0.0.0/0" { 34 return true 35 } 36 } 37 return false 38 } 39 40 // GetLoadBalancerSourceRanges verifies and parses the AnnotationLoadBalancerSourceRangesKey annotation from a service, 41 // extracting the source ranges to allow, and if not present returns a default (allow-all) value. 42 func GetLoadBalancerSourceRanges(annotations map[string]string) (netsets.IPNet, error) { 43 val := annotations[AnnotationLoadBalancerSourceRangesKey] 44 val = strings.TrimSpace(val) 45 if val == "" { 46 val = defaultLoadBalancerSourceRanges 47 } 48 specs := strings.Split(val, ",") 49 ipnets, err := netsets.ParseIPNets(specs...) 50 if err != nil { 51 return nil, fmt.Errorf("Service annotation %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) 52 } 53 return ipnets, nil 54 }