/
main.go
102 lines (82 loc) · 3.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
// Licensed under the Apache License 2.0
package main
import (
"flag"
"net/http"
"os"
"time"
"github.com/spf13/pflag"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client/config"
proxyconfig "github.com/stolostron/multicluster-observability-operator/proxy/pkg/config"
"github.com/stolostron/multicluster-observability-operator/proxy/pkg/proxy"
"github.com/stolostron/multicluster-observability-operator/proxy/pkg/util"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
)
const (
defaultListenAddress = "0.0.0.0:3002"
)
type proxyConf struct {
listenAddress string
metricServer string
kubeconfigLocation string
}
func main() {
cfg := proxyConf{}
klogFlags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
klog.InitFlags(klogFlags)
flagset := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
flagset.AddGoFlagSet(klogFlags)
flagset.StringVar(&cfg.listenAddress, "listen-address",
defaultListenAddress, "The address HTTP server should listen on.")
flagset.StringVar(&cfg.metricServer, "metrics-server", "",
"The address the metrics server should run on.")
_ = flagset.Parse(os.Args[1:])
if err := os.Setenv("METRICS_SERVER", cfg.metricServer); err != nil {
klog.Fatalf("failed to Setenv: %v", err)
}
//Kubeconfig flag
flagset.StringVar(&cfg.kubeconfigLocation, "kubeconfig", "",
"Path to a kubeconfig file. If unset, in-cluster configuration will be used")
klog.Infof("proxy server will running on: %s", cfg.listenAddress)
klog.Infof("metrics server is: %s", cfg.metricServer)
klog.Infof("kubeconfig is: %s", cfg.kubeconfigLocation)
clusterClient, err := clusterclientset.NewForConfig(config.GetConfigOrDie())
if err != nil {
klog.Fatalf("failed to initialize new cluster clientset: %v", err)
}
kubeClient, err := kubernetes.NewForConfig(config.GetConfigOrDie())
if err != nil {
klog.Fatalf("failed to initialize new kubernetes client: %v", err)
}
_, err = proxyconfig.GetManagedClusterLabelAllowListConfigmap(kubeClient,
proxyconfig.ManagedClusterLabelAllowListNamespace)
if err != nil {
if k8serrors.IsNotFound(err) {
_ = proxyconfig.CreateManagedClusterLabelAllowListCM(
proxyconfig.GetManagedClusterLabelAllowListConfigMapKey(),
)
}
}
// watch all managed clusters
go util.WatchManagedCluster(clusterClient, kubeClient)
go util.WatchManagedClusterLabelAllowList(kubeClient)
go util.ScheduleManagedClusterLabelAllowlistResync(kubeClient)
go util.CleanExpiredProjectInfoJob(24 * 60 * 60)
handlers := http.NewServeMux()
handlers.HandleFunc("/", proxy.HandleRequestAndRedirect)
s := http.Server{
Addr: cfg.listenAddress,
Handler: handlers,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 12 * time.Minute,
}
if err := s.ListenAndServe(); err != nil {
klog.Fatalf("failed to ListenAndServe: %v", err)
}
}