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

zalando-incubator / stackset-controller / 16118807821

07 Jul 2025 01:32PM UTC coverage: 49.497% (-0.009%) from 49.506%
16118807821

Pull #698

github

Martin Linkhorst
separate public and private domains, allow to choose for stacks
Pull Request #698: separate public and private domains, allow to choose for stacks

6 of 12 new or added lines in 3 files covered. (50.0%)

2608 of 5269 relevant lines covered (49.5%)

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
                ClusterInternalDomains      []string
42
                IgnorePublicDomainsOnStacks bool
43
                NoTrafficScaledownTTL       time.Duration
44
                ControllerID                string
45
                BackendWeightsAnnotationKey string
46
                RouteGroupSupportEnabled    bool
47
                SyncIngressAnnotations      []string
48
                ReconcileWorkers            int
49
                ConfigMapSupportEnabled     bool
50
                SecretSupportEnabled        bool
51
                PCSSupportEnabled           bool
52
        }
53
)
54

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

×
79
        if config.Debug {
×
80
                log.SetLevel(log.DebugLevel)
×
81
        }
×
82

83
        stackSetConfig := controller.StackSetConfig{
×
84
                Namespace:    config.Namespace,
×
85
                ControllerID: config.ControllerID,
×
86

×
87
                ClusterDomains:              config.ClusterDomains,
×
NEW
88
                ClusterInternalDomains:      config.ClusterInternalDomains,
×
NEW
89
                IgnorePublicDomainsOnStacks: config.IgnorePublicDomainsOnStacks,
×
90
                BackendWeightsAnnotationKey: config.BackendWeightsAnnotationKey,
×
91
                SyncIngressAnnotations:      config.SyncIngressAnnotations,
×
92

×
93
                ReconcileWorkers: config.ReconcileWorkers,
×
94
                Interval:         config.Interval,
×
95

×
96
                RouteGroupSupportEnabled: config.RouteGroupSupportEnabled,
×
97
                ConfigMapSupportEnabled:  config.ConfigMapSupportEnabled,
×
98
                SecretSupportEnabled:     config.SecretSupportEnabled,
×
99
                PcsSupportEnabled:        config.PCSSupportEnabled,
×
100
        }
×
101

×
102
        ctx, cancel := context.WithCancel(context.Background())
×
103
        kubeConfig, err := configureKubeConfig(config.APIServer, defaultClientGOTimeout, ctx.Done())
×
104
        if err != nil {
×
105
                log.Fatalf("Failed to setup Kubernetes config: %v", err)
×
106
        }
×
107

108
        client, err := clientset.NewForConfig(kubeConfig)
×
109
        if err != nil {
×
110
                log.Fatalf("Failed to initialize Kubernetes client: %v", err)
×
111
        }
×
112

113
        controller, err := controller.NewStackSetController(
×
114
                client,
×
115
                prometheus.DefaultRegisterer,
×
116
                stackSetConfig,
×
117
        )
×
118
        if err != nil {
×
119
                log.Fatalf("Failed to create Stackset controller: %v", err)
×
120
        }
×
121

122
        go handleSigterm(cancel)
×
123
        go serveMetrics(config.MetricsAddress)
×
124
        err = controller.Run(ctx)
×
125
        if err != nil {
×
126
                cancel()
×
127
                log.Fatalf("Failed to run controller: %v", err)
×
128
        }
×
129
}
130

131
// handleSigterm handles SIGTERM signal sent to the process.
132
func handleSigterm(cancelFunc func()) {
×
133
        signals := make(chan os.Signal, 1)
×
134
        signal.Notify(signals, syscall.SIGTERM)
×
135
        <-signals
×
136
        log.Info("Received Term signal. Terminating...")
×
137
        cancelFunc()
×
138
}
×
139

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

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

169
        if apiServerURL != nil {
×
170
                return &rest.Config{
×
171
                        Host:      apiServerURL.String(),
×
172
                        Timeout:   timeout,
×
173
                        Transport: tr,
×
174
                        QPS:       100.0,
×
175
                        Burst:     500,
×
176
                }, nil
×
177
        }
×
178

179
        config, err := rest.InClusterConfig()
×
180
        if err != nil {
×
181
                return nil, err
×
182
        }
×
183

184
        // patch TLS config
185
        restTransportConfig, err := config.TransportConfig()
×
186
        if err != nil {
×
187
                return nil, err
×
188
        }
×
189
        restTLSConfig, err := transport.TLSConfigFor(restTransportConfig)
×
190
        if err != nil {
×
191
                return nil, err
×
192
        }
×
193
        tr.TLSClientConfig = restTLSConfig
×
194

×
195
        config.Timeout = timeout
×
196
        config.Transport = tr
×
197
        config.QPS = 100.0
×
198
        config.Burst = 500
×
199
        // disable TLSClientConfig to make the custom Transport work
×
200
        config.TLSClientConfig = rest.TLSClientConfig{}
×
201
        return config, nil
×
202
}
203

204
// gather go metrics
205
func serveMetrics(address string) {
×
206
        http.Handle("/metrics", promhttp.Handler())
×
207
        log.Fatal(http.ListenAndServe(address, nil))
×
208
}
×
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