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

zalando-incubator / stackset-controller / 8283134271

14 Mar 2024 03:13PM UTC coverage: 52.089% (-0.2%) from 52.337%
8283134271

Pull #608

github

zaklawrencea
Handle informer error

Signed-off-by: Zak Lawrence <zaklawrencea@protonmail.com>
Pull Request #608: Update k8s.io/* dependencies to v0.26.14

0 of 41 new or added lines in 3 files covered. (0.0%)

1 existing line in 1 file now uncovered.

3079 of 5911 relevant lines covered (52.09%)

0.59 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
        "fmt"
6
        "net"
7
        "net/http"
8
        "net/url"
9
        "os"
10
        "os/signal"
11
        "syscall"
12
        "time"
13

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

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

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

56
func main() {
×
57
        kingpin.Flag("debug", "Enable debug logging.").BoolVar(&config.Debug)
×
58
        kingpin.Flag("interval", "Interval between syncing stacksets.").
×
59
                Default(defaultInterval).DurationVar(&config.Interval)
×
60
        kingpin.Flag("apiserver", "API server url.").URLVar(&config.APIServer)
×
61
        kingpin.Flag("namespace", "Limit scope to a particular namespace.").Default(corev1.NamespaceAll).StringVar(&config.Namespace)
×
62
        kingpin.Flag("metrics-address", "defines where to serve metrics").Default(defaultMetricsAddress).StringVar(&config.MetricsAddress)
×
63
        kingpin.Flag("controller-id", "ID of the controller used to determine ownership of StackSet resources").StringVar(&config.ControllerID)
×
64
        kingpin.Flag("reconcile-workers", "The amount of stacksets to reconcile in parallel at a time.").
×
65
                Default(defaultReconcileWorkers).IntVar(&config.ReconcileWorkers)
×
66
        kingpin.Flag("backend-weights-key", "Backend weights annotation key the controller will use to set current traffic values").Default(traffic.DefaultBackendWeightsAnnotationKey).StringVar(&config.BackendWeightsAnnotationKey)
×
67
        kingpin.Flag("cluster-domain", "Main domains of the cluster, used for generating Stack Ingress hostnames").Envar("CLUSTER_DOMAIN").Required().StringsVar(&config.ClusterDomains)
×
68
        kingpin.Flag("enable-routegroup-support", "Enable support for RouteGroups on StackSets.").Default("false").BoolVar(&config.RouteGroupSupportEnabled)
×
69
        kingpin.Flag(
×
70
                "enable-traffic-segments",
×
71
                "Support traffic segments by default. "+
×
72
                        "Ignored when also setting --annotated-traffic-segments.",
×
73
        ).Default("false").BoolVar(&config.TrafficSegmentsEnabled)
×
74
        kingpin.Flag(
×
75
                "annotated-traffic-segments",
×
76
                fmt.Sprintf(
×
77
                        "Support traffic segments annotated with %q.",
×
78
                        controller.TrafficSegmentsAnnotationKey,
×
79
                ),
×
80
        ).Default("false").BoolVar(&config.AnnotatedTrafficSegments)
×
81
        kingpin.Flag(
×
82
                "sync-ingress-annotation",
×
83
                "Ingress/RouteGroup annotation to propagate to all traffic segments.",
×
84
        ).StringsVar(&config.SyncIngressAnnotations)
×
85
        kingpin.Flag("ingress-source-switch-ttl", "The ttl before an ingress source is deleted when replaced with another one e.g. switching from RouteGroup to Ingress or vice versa.").
×
86
                Default(defaultIngressSourceSwitchTTL).DurationVar(&config.IngressSourceSwitchTTL)
×
87
        kingpin.Flag("enable-configmap-support", "Enable support for ConfigMaps on StackSets.").Default("false").BoolVar(&config.ConfigMapSupportEnabled)
×
88
        kingpin.Flag("enable-secret-support", "Enable support for Secrets on StackSets.").Default("false").BoolVar(&config.SecretSupportEnabled)
×
89
        kingpin.Parse()
×
90

×
91
        if config.Debug {
×
92
                log.SetLevel(log.DebugLevel)
×
93
        }
×
94

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

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

106
        controller, err := controller.NewStackSetController(
×
107
                client,
×
108
                config.Namespace,
×
109
                config.ControllerID,
×
110
                config.ReconcileWorkers,
×
111
                config.BackendWeightsAnnotationKey,
×
112
                config.ClusterDomains,
×
113
                prometheus.DefaultRegisterer,
×
114
                config.Interval,
×
115
                config.RouteGroupSupportEnabled,
×
116
                config.TrafficSegmentsEnabled,
×
117
                config.AnnotatedTrafficSegments,
×
118
                config.SyncIngressAnnotations,
×
119
                config.ConfigMapSupportEnabled,
×
120
                config.SecretSupportEnabled,
×
121
                config.IngressSourceSwitchTTL,
×
122
        )
×
123
        if err != nil {
×
124
                log.Fatalf("Failed to create Stackset controller: %v", err)
×
125
        }
×
126

127
        go handleSigterm(cancel)
×
128
        go serveMetrics(config.MetricsAddress)
×
NEW
129
        err = controller.Run(ctx)
×
NEW
130
        if err != nil {
×
NEW
131
                cancel()
×
NEW
132
                log.Fatalf("Failed to run controller: %v", err)
×
NEW
133
        }
×
134
}
135

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

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

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

174
        if apiServerURL != nil {
×
175
                return &rest.Config{
×
176
                        Host:      apiServerURL.String(),
×
177
                        Timeout:   timeout,
×
178
                        Transport: tr,
×
179
                        QPS:       100.0,
×
180
                        Burst:     500,
×
181
                }, nil
×
182
        }
×
183

184
        config, err := rest.InClusterConfig()
×
185
        if err != nil {
×
186
                return nil, err
×
187
        }
×
188

189
        // patch TLS config
190
        restTransportConfig, err := config.TransportConfig()
×
191
        if err != nil {
×
192
                return nil, err
×
193
        }
×
194
        restTLSConfig, err := transport.TLSConfigFor(restTransportConfig)
×
195
        if err != nil {
×
196
                return nil, err
×
197
        }
×
198
        tr.TLSClientConfig = restTLSConfig
×
199

×
200
        config.Timeout = timeout
×
201
        config.Transport = tr
×
202
        config.QPS = 100.0
×
203
        config.Burst = 500
×
204
        // disable TLSClientConfig to make the custom Transport work
×
205
        config.TLSClientConfig = rest.TLSClientConfig{}
×
206
        return config, nil
×
207
}
208

209
// gather go metrics
210
func serveMetrics(address string) {
×
211
        http.Handle("/metrics", promhttp.Handler())
×
212
        log.Fatal(http.ListenAndServe(address, nil))
×
213
}
×
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