• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zalando-incubator / stackset-controller / 20349054833

18 Dec 2025 07:36PM UTC coverage: 49.953% (-0.1%) from 50.085%
20349054833

Pull #721

github

mikkeloscar
Improve forward feature

Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
Pull Request #721: Improve forward feature

14 of 30 new or added lines in 3 files covered. (46.67%)

137 existing lines in 3 files now uncovered.

2662 of 5329 relevant lines covered (49.95%)

0.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cmd/stackset-controller/main.go
1
package main
2

3
import (
4
        "context"
5
        "net"
6
        "net/http"
7
        "net/url"
8
        "os"
9
        "os/signal"
10
        "syscall"
11
        "time"
12

13
        "github.com/alecthomas/kingpin/v2"
14
        "github.com/prometheus/client_golang/prometheus"
15
        "github.com/prometheus/client_golang/prometheus/promhttp"
16
        log "github.com/sirupsen/logrus"
17
        "github.com/zalando-incubator/stackset-controller/controller"
18
        "github.com/zalando-incubator/stackset-controller/pkg/clientset"
19
        "github.com/zalando-incubator/stackset-controller/pkg/traffic"
20
        corev1 "k8s.io/api/core/v1"
21
        "k8s.io/client-go/rest"
22
        "k8s.io/client-go/transport"
23
)
24

25
const (
26
        defaultInterval               = "10s"
27
        defaultIngressSourceSwitchTTL = "5m"
28
        defaultMetricsAddress         = ":7979"
29
        defaultClientGOTimeout        = 30 * time.Second
30
        defaultReconcileWorkers       = "10"
31
)
32

33
var (
34
        config struct {
35
                Debug                       bool
36
                Interval                    time.Duration
37
                APIServer                   *url.URL
38
                Namespace                   string
39
                MetricsAddress              string
40
                ClusterDomains              []string
41
                NoTrafficScaledownTTL       time.Duration
42
                ControllerID                string
43
                BackendWeightsAnnotationKey string
44
                RouteGroupSupportEnabled    bool
45
                SyncIngressAnnotations      []string
46
                ReconcileWorkers            int
47
                ConfigMapSupportEnabled     bool
48
                SecretSupportEnabled        bool
49
                PCSSupportEnabled           bool
50
        }
51
)
52

UNCOV
53
func main() {
×
54
        kingpin.Flag("debug", "Enable debug logging.").BoolVar(&config.Debug)
×
55
        kingpin.Flag("interval", "Interval between syncing stacksets.").
×
56
                Default(defaultInterval).DurationVar(&config.Interval)
×
57
        kingpin.Flag("apiserver", "API server url.").URLVar(&config.APIServer)
×
58
        kingpin.Flag("namespace", "Limit scope to a particular namespace.").Default(corev1.NamespaceAll).StringVar(&config.Namespace)
×
59
        kingpin.Flag("metrics-address", "defines where to serve metrics").Default(defaultMetricsAddress).StringVar(&config.MetricsAddress)
×
60
        kingpin.Flag("controller-id", "ID of the controller used to determine ownership of StackSet resources").StringVar(&config.ControllerID)
×
61
        kingpin.Flag("reconcile-workers", "The amount of stacksets to reconcile in parallel at a time.").
×
62
                Default(defaultReconcileWorkers).IntVar(&config.ReconcileWorkers)
×
63
        kingpin.Flag("backend-weights-key", "Backend weights annotation key the controller will use to set current traffic values").Default(traffic.DefaultBackendWeightsAnnotationKey).StringVar(&config.BackendWeightsAnnotationKey)
×
64
        kingpin.Flag("cluster-domain", "Main domains of the cluster, used for generating Stack Ingress hostnames").Envar("CLUSTER_DOMAIN").Required().StringsVar(&config.ClusterDomains)
×
65
        kingpin.Flag("enable-routegroup-support", "Enable support for RouteGroups on StackSets.").Default("false").BoolVar(&config.RouteGroupSupportEnabled)
×
66
        kingpin.Flag(
×
67
                "sync-ingress-annotation",
×
68
                "Ingress/RouteGroup annotation to propagate to all traffic segments.",
×
69
        ).StringsVar(&config.SyncIngressAnnotations)
×
70
        kingpin.Flag("enable-configmap-support", "Enable support for ConfigMaps on StackSets.").Default("false").BoolVar(&config.ConfigMapSupportEnabled)
×
71
        kingpin.Flag("enable-secret-support", "Enable support for Secrets on StackSets.").Default("false").BoolVar(&config.SecretSupportEnabled)
×
72
        kingpin.Flag("enable-pcs-support", "Enable support for PlatformCredentialsSet on StackSets.").Default("false").BoolVar(&config.PCSSupportEnabled)
×
73
        kingpin.Parse()
×
74

×
75
        if config.Debug {
×
76
                log.SetLevel(log.DebugLevel)
×
77
        }
×
78

79
        stackSetConfig := controller.StackSetConfig{
×
UNCOV
80
                Namespace:    config.Namespace,
×
81
                ControllerID: config.ControllerID,
×
82

×
83
                ClusterDomains:              config.ClusterDomains,
×
84
                BackendWeightsAnnotationKey: config.BackendWeightsAnnotationKey,
×
85
                SyncIngressAnnotations:      config.SyncIngressAnnotations,
×
86

×
87
                ReconcileWorkers: config.ReconcileWorkers,
×
88
                Interval:         config.Interval,
×
89

×
90
                RouteGroupSupportEnabled: config.RouteGroupSupportEnabled,
×
91
                ConfigMapSupportEnabled:  config.ConfigMapSupportEnabled,
×
92
                SecretSupportEnabled:     config.SecretSupportEnabled,
×
93
                PcsSupportEnabled:        config.PCSSupportEnabled,
×
94
        }
×
95

×
96
        ctx, cancel := context.WithCancel(context.Background())
×
97
        kubeConfig, err := configureKubeConfig(config.APIServer, defaultClientGOTimeout, ctx.Done())
×
98
        if err != nil {
×
99
                log.Fatalf("Failed to setup Kubernetes config: %v", err)
×
100
        }
×
101

102
        client, err := clientset.NewForConfig(kubeConfig)
×
103
        if err != nil {
×
UNCOV
104
                log.Fatalf("Failed to initialize Kubernetes client: %v", err)
×
105
        }
×
106

107
        controller, err := controller.NewStackSetController(
×
108
                client,
×
UNCOV
109
                prometheus.DefaultRegisterer,
×
110
                stackSetConfig,
×
111
        )
×
112
        if err != nil {
×
113
                log.Fatalf("Failed to create Stackset controller: %v", err)
×
114
        }
×
115

116
        go handleSigterm(cancel)
×
117
        go serveMetrics(config.MetricsAddress)
×
UNCOV
118
        err = controller.Run(ctx)
×
119
        if err != nil {
×
120
                cancel()
×
121
                log.Fatalf("Failed to run controller: %v", err)
×
122
        }
×
123
}
124

125
// handleSigterm handles SIGTERM signal sent to the process.
UNCOV
126
func handleSigterm(cancelFunc func()) {
×
UNCOV
127
        signals := make(chan os.Signal, 1)
×
UNCOV
128
        signal.Notify(signals, syscall.SIGTERM)
×
129
        <-signals
×
130
        log.Info("Received Term signal. Terminating...")
×
131
        cancelFunc()
×
132
}
×
133

134
// configureKubeConfig configures a kubeconfig.
135
func configureKubeConfig(apiServerURL *url.URL, timeout time.Duration, stopCh <-chan struct{}) (*rest.Config, error) {
×
UNCOV
136
        tr := &http.Transport{
×
UNCOV
137
                DialContext: (&net.Dialer{
×
138
                        Timeout:   timeout,
×
139
                        KeepAlive: 30 * time.Second,
×
140
                        DualStack: false, // K8s do not work well with IPv6
×
141
                }).DialContext,
×
142
                TLSHandshakeTimeout:   timeout,
×
143
                ResponseHeaderTimeout: 10 * time.Second,
×
144
                MaxIdleConns:          10,
×
145
                MaxIdleConnsPerHost:   2,
×
146
                IdleConnTimeout:       20 * time.Second,
×
147
        }
×
148

×
149
        // We need this to reliably fade on DNS change, which is right
×
150
        // now not fixed with IdleConnTimeout in the http.Transport.
×
151
        // https://github.com/golang/go/issues/23427
×
152
        go func(d time.Duration) {
×
153
                for {
×
154
                        select {
×
155
                        case <-time.After(d):
×
156
                                tr.CloseIdleConnections()
×
157
                        case <-stopCh:
×
158
                                return
×
159
                        }
160
                }
161
        }(20 * time.Second)
162

UNCOV
163
        if apiServerURL != nil {
×
UNCOV
164
                return &rest.Config{
×
UNCOV
165
                        Host:      apiServerURL.String(),
×
166
                        Timeout:   timeout,
×
167
                        Transport: tr,
×
168
                        QPS:       100.0,
×
169
                        Burst:     500,
×
170
                }, nil
×
171
        }
×
172

173
        config, err := rest.InClusterConfig()
×
174
        if err != nil {
×
UNCOV
175
                return nil, err
×
176
        }
×
177

178
        // patch TLS config
179
        restTransportConfig, err := config.TransportConfig()
×
UNCOV
180
        if err != nil {
×
UNCOV
181
                return nil, err
×
182
        }
×
183
        restTLSConfig, err := transport.TLSConfigFor(restTransportConfig)
×
184
        if err != nil {
×
185
                return nil, err
×
186
        }
×
187
        tr.TLSClientConfig = restTLSConfig
×
188

×
189
        config.Timeout = timeout
×
190
        config.Transport = tr
×
191
        config.QPS = 100.0
×
192
        config.Burst = 500
×
193
        // disable TLSClientConfig to make the custom Transport work
×
194
        config.TLSClientConfig = rest.TLSClientConfig{}
×
195
        return config, nil
×
196
}
197

198
// gather go metrics
UNCOV
199
func serveMetrics(address string) {
×
UNCOV
200
        http.Handle("/metrics", promhttp.Handler())
×
UNCOV
201
        log.Fatal(http.ListenAndServe(address, nil))
×
202
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc