Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / metrics / listener.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
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 metrics
18
19 import (
20         "fmt"
21         "net"
22 )
23
24 // DefaultBindAddress sets the default bind address for the metrics
25 // listener
26 // The metrics is off by default.
27 // TODO: Flip the default by changing DefaultBindAddress back to ":8080" in the v0.2.0.
28 var DefaultBindAddress = "0"
29
30 // NewListener creates a new TCP listener bound to the given address.
31 func NewListener(addr string) (net.Listener, error) {
32         if addr == "" {
33                 // If the metrics bind address is empty, default to ":8080"
34                 addr = DefaultBindAddress
35         }
36
37         // Add a case to disable metrics altogether
38         if addr == "0" {
39                 return nil, nil
40         }
41
42         ln, err := net.Listen("tcp", addr)
43         if err != nil {
44                 return nil, fmt.Errorf("error listening on %s: %v", addr, err)
45         }
46         return ln, nil
47 }