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

zalando-incubator / stackset-controller / 8738009463

11 Apr 2024 03:07PM UTC coverage: 49.355%. First build
8738009463

push

github

linki
feature flag

3 of 8 new or added lines in 3 files covered. (37.5%)

2833 of 5740 relevant lines covered (49.36%)

0.55 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
                InlineConfigMapEnabled      bool
48
                ConfigMapSupportEnabled     bool
49
                SecretSupportEnabled        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)
×
NEW
70
        kingpin.Flag("enable-inline-configmap-support", "Enable support for inline ConfigMaps on StackSets.").Default("false").BoolVar(&config.InlineConfigMapEnabled)
×
71
        kingpin.Flag("enable-configmap-support", "Enable support for ConfigMaps on StackSets.").Default("false").BoolVar(&config.ConfigMapSupportEnabled)
×
72
        kingpin.Flag("enable-secret-support", "Enable support for Secrets on StackSets.").Default("false").BoolVar(&config.SecretSupportEnabled)
×
73
        kingpin.Parse()
×
74

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

79
        ctx, cancel := context.WithCancel(context.Background())
×
80
        kubeConfig, err := configureKubeConfig(config.APIServer, defaultClientGOTimeout, ctx.Done())
×
81
        if err != nil {
×
82
                log.Fatalf("Failed to setup Kubernetes config: %v", err)
×
83
        }
×
84

85
        client, err := clientset.NewForConfig(kubeConfig)
×
86
        if err != nil {
×
87
                log.Fatalf("Failed to initialize Kubernetes client: %v", err)
×
88
        }
×
89

90
        controller, err := controller.NewStackSetController(
×
91
                client,
×
92
                config.Namespace,
×
93
                config.ControllerID,
×
94
                config.ReconcileWorkers,
×
95
                config.BackendWeightsAnnotationKey,
×
96
                config.ClusterDomains,
×
97
                prometheus.DefaultRegisterer,
×
98
                config.Interval,
×
99
                config.RouteGroupSupportEnabled,
×
100
                config.SyncIngressAnnotations,
×
NEW
101
                config.InlineConfigMapEnabled,
×
102
                config.ConfigMapSupportEnabled,
×
103
                config.SecretSupportEnabled,
×
104
        )
×
105
        if err != nil {
×
106
                log.Fatalf("Failed to create Stackset controller: %v", err)
×
107
        }
×
108

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

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

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

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

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

166
        config, err := rest.InClusterConfig()
×
167
        if err != nil {
×
168
                return nil, err
×
169
        }
×
170

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

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

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