forked from linode/linode-cloud-controller-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (121 loc) · 4.34 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
goflag "flag"
"fmt"
"math/rand"
"os"
"time"
"github.com/linode/linode-cloud-controller-manager/cloud/linode"
"github.com/linode/linode-cloud-controller-manager/sentry"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/wait"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider/app"
"k8s.io/cloud-provider/app/config"
"k8s.io/cloud-provider/options"
utilflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
_ "k8s.io/component-base/metrics/prometheus/clientgo" // for client metric registration
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
"k8s.io/klog"
)
const (
sentryDSNVariable = "SENTRY_DSN"
sentryEnvironmentVariable = "SENTRY_ENVIRONMENT"
sentryReleaseVariable = "SENTRY_RELEASE"
)
func initializeSentry() {
var (
dsn string
environment string
release string
ok bool
)
if dsn, ok = os.LookupEnv(sentryDSNVariable); !ok {
fmt.Printf("%s not set, not initializing Sentry\n", sentryDSNVariable)
return
}
if environment, ok = os.LookupEnv(sentryEnvironmentVariable); !ok {
fmt.Printf("%s not set, not initializing Sentry\n", sentryEnvironmentVariable)
return
}
if release, ok = os.LookupEnv(sentryReleaseVariable); !ok {
fmt.Printf("%s not set, defaulting to unknown", sentryReleaseVariable)
release = "unknown"
}
if err := sentry.Initialize(dsn, environment, release); err != nil {
fmt.Printf("error initializing sentry: %s\n", err.Error())
return
}
fmt.Print("Sentry successfully initialized\n")
}
func main() {
fmt.Printf("Linode Cloud Controller Manager starting up\n")
initializeSentry()
ctx := sentry.SetHubOnContext(context.Background())
rand.Seed(time.Now().UTC().UnixNano())
ccmOptions, err := options.NewCloudControllerManagerOptions()
if err != nil {
klog.Fatalf("unable to initialize command options: %v", err)
}
fss := utilflag.NamedFlagSets{}
command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors, fss, wait.NeverStop)
// Add Linode-specific flags
command.Flags().BoolVar(&linode.Options.LinodeGoDebug, "linodego-debug", false, "enables debug output for the LinodeAPI wrapper")
// Set static flags
command.Flags().VisitAll(func(fl *pflag.Flag) {
var err error
switch fl.Name {
case "cloud-provider":
err = fl.Value.Set(linode.ProviderName)
case
// Prevent reaching out to an authentication-related ConfigMap that
// we do not need, and thus do not intend to create RBAC permissions
// for. See also
// https://github.com/linode/linode-cloud-controller-manager/issues/91
// and https://github.com/kubernetes/cloud-provider/issues/29.
"authentication-skip-lookup":
err = fl.Value.Set("true")
}
if err != nil {
fmt.Fprintf(os.Stderr, "failed to set flag %q: %s\n", fl.Name, err)
os.Exit(1)
}
})
// Make the Linode-specific CCM bits aware of the kubeconfig flag
linode.Options.KubeconfigFlag = command.Flags().Lookup("kubeconfig")
if linode.Options.KubeconfigFlag == nil {
msg := "kubeconfig missing from CCM flag set"
sentry.CaptureError(ctx, fmt.Errorf(msg))
fmt.Fprintf(os.Stderr, "kubeconfig missing from CCM flag set"+"\n")
os.Exit(1)
}
pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
logs.InitLogs()
defer logs.FlushLogs()
if err := command.Execute(); err != nil {
sentry.CaptureError(ctx, err)
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func cloudInitializer(config *config.CompletedConfig) cloudprovider.Interface {
// initialize cloud provider with the cloud provider name and config file provided
cloud, err := cloudprovider.InitCloudProvider(linode.ProviderName, "")
if err != nil {
klog.Fatalf("Cloud provider could not be initialized: %v", err)
}
if cloud == nil {
klog.Fatalf("Cloud provider is nil")
}
if !cloud.HasClusterID() {
if config.ComponentConfig.KubeCloudShared.AllowUntaggedCloud {
klog.Warning("detected a cluster without a ClusterID. A ClusterID will be required in the future. Please tag your cluster to avoid any future issues")
} else {
klog.Fatalf("no ClusterID found. A ClusterID is required for the cloud provider to function properly. This check can be bypassed by setting the allow-untagged-cloud option")
}
}
return cloud
}