Skip to content

Commit

Permalink
Improve macOS dev experience
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
databus23 committed Aug 23, 2022
1 parent 7a7baab commit 88854cb
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 39 deletions.
3 changes: 1 addition & 2 deletions charts/kubernikus/templates/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ spec:
{{- end }}
- --kubernikus-domain={{ .Values.domain }}
- --namespace={{ default "kubernikus" .Values.namespace }}
- --metric-port={{ default 9091 .Values.operator.metrics_port }}
{{- if .Values.operator.controllers }}
- --controllers={{ join "," .Values.operator.controllers }}
{{- end }}
Expand Down Expand Up @@ -77,4 +76,4 @@ spec:
{{- end }}
ports:
- name: metrics
containerPort: {{ .Values.operator.metrics_port }}
containerPort: 9091
1 change: 0 additions & 1 deletion charts/kubernikus/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ standalone: true
operator:
controllers: []
nodeAffinity: false
metrics_port: 9091
useOctavia: false

includeRBAC: false
33 changes: 6 additions & 27 deletions pkg/client/kubernetes/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"

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

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

local, err := getAvailablePort()
if err != nil {
return fmt.Errorf("could not find an available port: %s", err)
}
t.Local = local

ports := []string{fmt.Sprintf("%d:%d", t.Local, t.Remote)}
ports := []string{fmt.Sprintf("0:%d", t.Remote)}

pf, err := portforward.New(dialer, ports, t.stopChan, t.readyChan, t.Out, t.Out)
if err != nil {
Expand All @@ -100,24 +92,11 @@ func (t *Tunnel) ForwardPort() error {
case err = <-errChan:
return fmt.Errorf("forwarding ports: %v", err)
case <-pf.Ready:
ports, err := pf.GetPorts()
if err != nil {
return fmt.Errorf("Failed to get forwarded ports: %w", err)
}
t.Local = int(ports[0].Local)
return nil
}
}

func getAvailablePort() (int, error) {
l, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer l.Close()

_, p, err := net.SplitHostPort(l.Addr().String())
if err != nil {
return 0, err
}
port, err := strconv.Atoi(p)
if err != nil {
return 0, err
}
return port, err
}
8 changes: 5 additions & 3 deletions pkg/cmd/kubernikus/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewOperatorOptions() *Options {
options.AuthDomain = "Default"
options.KubernikusDomain = "kluster.staging.cloud.sap"
options.Namespace = "kubernikus"
options.MetricPort = 9091
options.MetricsAddress = ":9091"
options.Controllers = []string{"groundctl", "launchctl", "deorbiter", "routegc", "flight", "migration", "hammertime", "servicing", "certs"}
options.Region = "eu-de-1"
options.NodeUpdateHoldoff = 7 * 24 * time.Hour
Expand All @@ -73,7 +73,9 @@ func (o *Options) BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.KubernikusProjectID, "kubernikus-projectid", o.KubernikusProjectID, "ID of the project the k*s control plane.")
flags.StringVar(&o.KubernikusNetworkID, "kubernikus-networkid", o.KubernikusNetworkID, "ID of the network the k*s control plane.")
flags.StringVar(&o.Namespace, "namespace", o.Namespace, "Restrict operator to resources in the given namespace")
flags.IntVar(&o.MetricPort, "metric-port", o.MetricPort, "Port on which metrics are exposed")
flags.Int("metric-port", 9091, "Port on which metrics are exposed")
flags.MarkDeprecated("metric-port", "use --metrics-address instead")
flags.StringVar(&o.MetricsAddress, "metrics-address", o.MetricsAddress, "Expose metrics on this address")
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, ", ")))
flags.IntVar(&o.LogLevel, "v", 0, "log level")

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

go operator.Run(stop, wg)
go metrics.ExposeMetrics("0.0.0.0", o.MetricPort, stop, wg, logger)
go metrics.ExposeMetrics(o.MetricsAddress, stop, wg, logger)
go func() {
host := "127.0.0.1:7353"
ln, err := net.Listen("tcp", "127.0.0.1:7353")
Expand Down
8 changes: 3 additions & 5 deletions pkg/controller/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package metrics

import (
"fmt"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -142,14 +141,13 @@ func init() {
)
}

func ExposeMetrics(host string, metricPort int, stopCh <-chan struct{}, wg *sync.WaitGroup, logger log.Logger) {
func ExposeMetrics(address string, stopCh <-chan struct{}, wg *sync.WaitGroup, logger log.Logger) {
wg.Add(1)
defer wg.Done()
ln, err := net.Listen("tcp", fmt.Sprintf("%v:%v", host, metricPort))
ln, err := net.Listen("tcp", address)
logger.Log(
"msg", "Exposing metrics",
"host", host,
"port", metricPort,
"listen", address,
"err", err)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type KubernikusOperatorOptions struct {
KubernikusNetworkID string
Namespace string
Controllers []string
MetricPort int
MetricsAddress string
LogLevel int

NodeUpdateHoldoff time.Duration
Expand Down

0 comments on commit 88854cb

Please sign in to comment.