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

zalando-incubator / stackset-controller / 15634078789

13 Jun 2025 12:03PM UTC coverage: 55.441% (-0.04%) from 55.484%
15634078789

push

github

demonCoder95
refactor StackSetController config in a separate struct

41 of 66 new or added lines in 3 files covered. (62.12%)

3006 of 5422 relevant lines covered (55.44%)

0.62 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

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

NEW
79
        stackSetConfig := controller.StackSetConfig{
×
NEW
80
                Namespace:                   config.Namespace,
×
NEW
81
                ControllerID:                config.ControllerID,
×
NEW
82
                ReconcileWorkers:            config.ReconcileWorkers,
×
NEW
83
                BackendWeightsAnnotationKey: config.BackendWeightsAnnotationKey,
×
NEW
84
                ClusterDomains:              config.ClusterDomains,
×
NEW
85
                Interval:                    config.Interval,
×
NEW
86
                RouteGroupSupportEnabled:    config.RouteGroupSupportEnabled,
×
NEW
87
                SyncIngressAnnotations:      config.SyncIngressAnnotations,
×
NEW
88
                ConfigMapSupportEnabled:     config.ConfigMapSupportEnabled,
×
NEW
89
                SecretSupportEnabled:        config.SecretSupportEnabled,
×
NEW
90
                PcsSupportEnabled:           config.PCSSupportEnabled,
×
NEW
91
        }
×
92
        ctx, cancel := context.WithCancel(context.Background())
×
93
        kubeConfig, err := configureKubeConfig(config.APIServer, defaultClientGOTimeout, ctx.Done())
×
94
        if err != nil {
×
95
                log.Fatalf("Failed to setup Kubernetes config: %v", err)
×
96
        }
×
97

98
        client, err := clientset.NewForConfig(kubeConfig)
×
99
        if err != nil {
×
100
                log.Fatalf("Failed to initialize Kubernetes client: %v", err)
×
101
        }
×
102

103
        controller, err := controller.NewStackSetController(
×
104
                client,
×
105
                prometheus.DefaultRegisterer,
×
NEW
106
                stackSetConfig,
×
107
        )
×
108
        if err != nil {
×
109
                log.Fatalf("Failed to create Stackset controller: %v", err)
×
110
        }
×
111

112
        go handleSigterm(cancel)
×
113
        go serveMetrics(config.MetricsAddress)
×
114
        err = controller.Run(ctx)
×
115
        if err != nil {
×
116
                cancel()
×
117
                log.Fatalf("Failed to run controller: %v", err)
×
118
        }
×
119
}
120

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

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

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

159
        if apiServerURL != nil {
×
160
                return &rest.Config{
×
161
                        Host:      apiServerURL.String(),
×
162
                        Timeout:   timeout,
×
163
                        Transport: tr,
×
164
                        QPS:       100.0,
×
165
                        Burst:     500,
×
166
                }, nil
×
167
        }
×
168

169
        config, err := rest.InClusterConfig()
×
170
        if err != nil {
×
171
                return nil, err
×
172
        }
×
173

174
        // patch TLS config
175
        restTransportConfig, err := config.TransportConfig()
×
176
        if err != nil {
×
177
                return nil, err
×
178
        }
×
179
        restTLSConfig, err := transport.TLSConfigFor(restTransportConfig)
×
180
        if err != nil {
×
181
                return nil, err
×
182
        }
×
183
        tr.TLSClientConfig = restTLSConfig
×
184

×
185
        config.Timeout = timeout
×
186
        config.Transport = tr
×
187
        config.QPS = 100.0
×
188
        config.Burst = 500
×
189
        // disable TLSClientConfig to make the custom Transport work
×
190
        config.TLSClientConfig = rest.TLSClientConfig{}
×
191
        return config, nil
×
192
}
193

194
// gather go metrics
195
func serveMetrics(address string) {
×
196
        http.Handle("/metrics", promhttp.Handler())
×
197
        log.Fatal(http.ListenAndServe(address, nil))
×
198
}
×
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

© 2026 Coveralls, Inc