Skip to content

Commit 6191d5d

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 b9ef721 commit 6191d5d

File tree

7 files changed

+17
-40
lines changed

7 files changed

+17
-40
lines changed

charts/kubernikus/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
apiVersion: v1
22
description: A Helm chart for Kubernetes
33
name: kubernikus
4-
version: 0.3.5
4+
version: 0.3.6

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 }}
@@ -74,4 +73,4 @@ spec:
7473
{{- end }}
7574
ports:
7675
- name: metrics
77-
containerPort: {{ .Values.operator.metrics_port }}
76+
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
@@ -49,7 +49,7 @@ func NewOperatorOptions() *Options {
4949
options.AuthDomain = "Default"
5050
options.KubernikusDomain = "kluster.staging.cloud.sap"
5151
options.Namespace = "kubernikus"
52-
options.MetricPort = 9091
52+
options.MetricsAddress = ":9091"
5353
options.Controllers = []string{"groundctl", "launchctl", "deorbiter", "routegc", "flight", "migration", "hammertime", "servicing", "certs"}
5454
options.Region = "eu-de-1"
5555
return options
@@ -71,7 +71,9 @@ func (o *Options) BindFlags(flags *pflag.FlagSet) {
7171
flags.StringVar(&o.KubernikusProjectID, "kubernikus-projectid", o.KubernikusProjectID, "ID of the project the k*s control plane.")
7272
flags.StringVar(&o.KubernikusNetworkID, "kubernikus-networkid", o.KubernikusNetworkID, "ID of the network the k*s control plane.")
7373
flags.StringVar(&o.Namespace, "namespace", o.Namespace, "Restrict operator to resources in the given namespace")
74-
flags.IntVar(&o.MetricPort, "metric-port", o.MetricPort, "Port on which metrics are exposed")
74+
flags.Int("metric-port", 9091, "Port on which metrics are exposed")
75+
flags.MarkDeprecated("metric-port", "use --metrics-address instead")
76+
flags.StringVar(&o.MetricsAddress, "metrics-address", o.MetricsAddress, "Expose metrics on this address")
7577
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, ", ")))
7678
flags.IntVar(&o.LogLevel, "v", 0, "log level")
7779
}
@@ -104,7 +106,7 @@ func (o *Options) Run(c *cobra.Command) error {
104106
}
105107

106108
go operator.Run(stop, wg)
107-
go metrics.ExposeMetrics("0.0.0.0", o.MetricPort, stop, wg, logger)
109+
go metrics.ExposeMetrics(o.MetricsAddress, stop, wg, logger)
108110
go func() {
109111
host := "127.0.0.1:7353"
110112
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"
@@ -156,14 +155,13 @@ func init() {
156155
)
157156
}
158157

159-
func ExposeMetrics(host string, metricPort int, stopCh <-chan struct{}, wg *sync.WaitGroup, logger log.Logger) {
158+
func ExposeMetrics(address string, stopCh <-chan struct{}, wg *sync.WaitGroup, logger log.Logger) {
160159
wg.Add(1)
161160
defer wg.Done()
162-
ln, err := net.Listen("tcp", fmt.Sprintf("%v:%v", host, metricPort))
161+
ln, err := net.Listen("tcp", address)
163162
logger.Log(
164163
"msg", "Exposing metrics",
165-
"host", host,
166-
"port", metricPort,
164+
"listen", address,
167165
"err", err)
168166
if err != nil {
169167
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

0 commit comments

Comments
 (0)