Skip to content

Commit 88854cb

Browse files
committed
Improve macOS dev experience
Listening on anything but localhost on mac triggers a dialog whenever just compiled binary is started. For operator this can be avoided by having the metrics port and helm forwarded ports listen on localhost which is fine in the dev case.
1 parent 7a7baab commit 88854cb

File tree

6 files changed

+16
-39
lines changed

6 files changed

+16
-39
lines changed

charts/kubernikus/templates/operator.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ spec:
4646
{{- end }}
4747
- --kubernikus-domain={{ .Values.domain }}
4848
- --namespace={{ default "kubernikus" .Values.namespace }}
49-
- --metric-port={{ default 9091 .Values.operator.metrics_port }}
5049
{{- if .Values.operator.controllers }}
5150
- --controllers={{ join "," .Values.operator.controllers }}
5251
{{- end }}
@@ -77,4 +76,4 @@ spec:
7776
{{- end }}
7877
ports:
7978
- name: metrics
80-
containerPort: {{ .Values.operator.metrics_port }}
79+
containerPort: 9091

charts/kubernikus/values.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ standalone: true
5858
operator:
5959
controllers: []
6060
nodeAffinity: false
61-
metrics_port: 9091
6261
useOctavia: false
6362

6463
includeRBAC: false

pkg/client/kubernetes/tunnel.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"io/ioutil"
23-
"net"
2423
"net/http"
25-
"strconv"
2624

2725
"k8s.io/client-go/rest"
2826
"k8s.io/client-go/tools/portforward"
@@ -78,13 +76,7 @@ func (t *Tunnel) ForwardPort() error {
7876

7977
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", u)
8078

81-
local, err := getAvailablePort()
82-
if err != nil {
83-
return fmt.Errorf("could not find an available port: %s", err)
84-
}
85-
t.Local = local
86-
87-
ports := []string{fmt.Sprintf("%d:%d", t.Local, t.Remote)}
79+
ports := []string{fmt.Sprintf("0:%d", t.Remote)}
8880

8981
pf, err := portforward.New(dialer, ports, t.stopChan, t.readyChan, t.Out, t.Out)
9082
if err != nil {
@@ -100,24 +92,11 @@ func (t *Tunnel) ForwardPort() error {
10092
case err = <-errChan:
10193
return fmt.Errorf("forwarding ports: %v", err)
10294
case <-pf.Ready:
95+
ports, err := pf.GetPorts()
96+
if err != nil {
97+
return fmt.Errorf("Failed to get forwarded ports: %w", err)
98+
}
99+
t.Local = int(ports[0].Local)
103100
return nil
104101
}
105102
}
106-
107-
func getAvailablePort() (int, error) {
108-
l, err := net.Listen("tcp", ":0")
109-
if err != nil {
110-
return 0, err
111-
}
112-
defer l.Close()
113-
114-
_, p, err := net.SplitHostPort(l.Addr().String())
115-
if err != nil {
116-
return 0, err
117-
}
118-
port, err := strconv.Atoi(p)
119-
if err != nil {
120-
return 0, err
121-
}
122-
return port, err
123-
}

pkg/cmd/kubernikus/operator.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewOperatorOptions() *Options {
5050
options.AuthDomain = "Default"
5151
options.KubernikusDomain = "kluster.staging.cloud.sap"
5252
options.Namespace = "kubernikus"
53-
options.MetricPort = 9091
53+
options.MetricsAddress = ":9091"
5454
options.Controllers = []string{"groundctl", "launchctl", "deorbiter", "routegc", "flight", "migration", "hammertime", "servicing", "certs"}
5555
options.Region = "eu-de-1"
5656
options.NodeUpdateHoldoff = 7 * 24 * time.Hour
@@ -73,7 +73,9 @@ func (o *Options) BindFlags(flags *pflag.FlagSet) {
7373
flags.StringVar(&o.KubernikusProjectID, "kubernikus-projectid", o.KubernikusProjectID, "ID of the project the k*s control plane.")
7474
flags.StringVar(&o.KubernikusNetworkID, "kubernikus-networkid", o.KubernikusNetworkID, "ID of the network the k*s control plane.")
7575
flags.StringVar(&o.Namespace, "namespace", o.Namespace, "Restrict operator to resources in the given namespace")
76-
flags.IntVar(&o.MetricPort, "metric-port", o.MetricPort, "Port on which metrics are exposed")
76+
flags.Int("metric-port", 9091, "Port on which metrics are exposed")
77+
flags.MarkDeprecated("metric-port", "use --metrics-address instead")
78+
flags.StringVar(&o.MetricsAddress, "metrics-address", o.MetricsAddress, "Expose metrics on this address")
7779
flags.StringSliceVar(&o.Controllers, "controllers", o.Controllers, fmt.Sprintf("A list of controllers to enable. Default is to enable all. controllers: %s", strings.Join(o.Controllers, ", ")))
7880
flags.IntVar(&o.LogLevel, "v", 0, "log level")
7981

@@ -108,7 +110,7 @@ func (o *Options) Run(c *cobra.Command) error {
108110
}
109111

110112
go operator.Run(stop, wg)
111-
go metrics.ExposeMetrics("0.0.0.0", o.MetricPort, stop, wg, logger)
113+
go metrics.ExposeMetrics(o.MetricsAddress, stop, wg, logger)
112114
go func() {
113115
host := "127.0.0.1:7353"
114116
ln, err := net.Listen("tcp", "127.0.0.1:7353")

pkg/controller/metrics/metrics.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package metrics
22

33
import (
4-
"fmt"
54
"net"
65
"net/http"
76
"sync"
@@ -142,14 +141,13 @@ func init() {
142141
)
143142
}
144143

145-
func ExposeMetrics(host string, metricPort int, stopCh <-chan struct{}, wg *sync.WaitGroup, logger log.Logger) {
144+
func ExposeMetrics(address string, stopCh <-chan struct{}, wg *sync.WaitGroup, logger log.Logger) {
146145
wg.Add(1)
147146
defer wg.Done()
148-
ln, err := net.Listen("tcp", fmt.Sprintf("%v:%v", host, metricPort))
147+
ln, err := net.Listen("tcp", address)
149148
logger.Log(
150149
"msg", "Exposing metrics",
151-
"host", host,
152-
"port", metricPort,
150+
"listen", address,
153151
"err", err)
154152
if err != nil {
155153
return

pkg/controller/operator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type KubernikusOperatorOptions struct {
5454
KubernikusNetworkID string
5555
Namespace string
5656
Controllers []string
57-
MetricPort int
57+
MetricsAddress string
5858
LogLevel int
5959

6060
NodeUpdateHoldoff time.Duration

0 commit comments

Comments
 (0)