-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmain.go
More file actions
193 lines (167 loc) · 9.61 KB
/
Copy pathmain.go
File metadata and controls
193 lines (167 loc) · 9.61 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"context"
"flag"
"fmt"
"net"
"os"
"github.com/linode/linodego/v2"
"github.com/spf13/pflag"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider/app"
"k8s.io/cloud-provider/app/config"
"k8s.io/cloud-provider/names"
"k8s.io/cloud-provider/options"
utilflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
"k8s.io/klog/v2"
"github.com/linode/linode-cloud-controller-manager/cloud/linode"
ccmOptions "github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
"github.com/linode/linode-cloud-controller-manager/sentry"
_ "k8s.io/component-base/metrics/prometheus/clientgo" // for client metric registration
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
)
const (
sentryDSNVariable = "SENTRY_DSN"
sentryEnvironmentVariable = "SENTRY_ENVIRONMENT"
sentryReleaseVariable = "SENTRY_RELEASE"
linodeExternalSubnetVariable = "LINODE_EXTERNAL_SUBNET"
)
func initializeSentry() {
var (
dsn string
environment string
release string
ok bool
)
if dsn, ok = os.LookupEnv(sentryDSNVariable); !ok {
klog.Errorf("%s not set, not initializing Sentry\n", sentryDSNVariable)
return
}
if environment, ok = os.LookupEnv(sentryEnvironmentVariable); !ok {
klog.Errorf("%s not set, not initializing Sentry\n", sentryEnvironmentVariable)
return
}
if release, ok = os.LookupEnv(sentryReleaseVariable); !ok {
klog.Infof("%s not set, defaulting to unknown", sentryReleaseVariable)
release = "unknown"
}
if err := sentry.Initialize(dsn, environment, release); err != nil {
klog.Errorf("error initializing sentry: %s\n", err.Error())
return
}
klog.Infoln("Sentry successfully initialized")
}
func main() {
klog.Infoln("Linode Cloud Controller Manager starting up")
initializeSentry()
ctx := sentry.SetHubOnContext(context.Background())
opts, err := options.NewCloudControllerManagerOptions()
if err != nil {
klog.Fatalf("unable to initialize command options: %v", err)
}
fss := utilflag.NamedFlagSets{}
controllerAliases := names.CCMControllerAliases()
stopCh := make(chan struct{})
command := app.NewCloudControllerManagerCommand(opts, cloudInitializer, app.DefaultInitFuncConstructors, controllerAliases, fss, stopCh)
// Add Linode-specific flags
command.Flags().BoolVar(&ccmOptions.Options.LinodeGoDebug, "linodego-debug", false, "enables debug output for the LinodeAPI wrapper")
command.Flags().BoolVar(&ccmOptions.Options.EnableRouteController, "enable-route-controller", false, "enables route_controller for ccm")
command.Flags().BoolVar(&ccmOptions.Options.EnableTokenHealthChecker, "enable-token-health-checker", false, "enables Linode API token health checker")
command.Flags().StringSliceVar(&ccmOptions.Options.VPCNames, "vpc-names", nil, "comma separated vpc names whose routes will be managed by route-controller")
command.Flags().StringSliceVar(&ccmOptions.Options.SubnetNames, "subnet-names", []string{"default"}, "comma separated subnet names whose routes will be managed by route-controller (requires vpc-names flag to also be set)")
command.Flags().IntSliceVar(&ccmOptions.Options.VPCIDs, "vpc-ids", nil, "comma separated vpc ids whose routes will be managed by route-controller")
command.Flags().IntSliceVar(&ccmOptions.Options.SubnetIDs, "subnet-ids", nil, "comma separated subnet ids whose routes will be managed by route-controller (requires vpc-ids flag to also be set)")
command.Flags().StringVar(&ccmOptions.Options.LoadBalancerType, "load-balancer-type", "nodebalancer", "configures which type of load-balancing to use for LoadBalancer Services (options: nodebalancer, cilium-bgp)")
command.Flags().StringVar(&ccmOptions.Options.BGPNodeSelector, "bgp-node-selector", "", "node selector to use to perform shared IP fail-over with BGP (e.g. cilium-bgp-peering=true")
command.Flags().StringVar(&ccmOptions.Options.IpHolderSuffix, "ip-holder-suffix", "", "suffix to append to the ip holder name when using shared IP fail-over with BGP (e.g. ip-holder-suffix=my-cluster-name")
command.Flags().StringVar(&ccmOptions.Options.DefaultNBType, "default-nodebalancer-type", string(linodego.NBTypeCommon), "default type of NodeBalancer to create (options: common, premium, premium_40GB)")
command.Flags().StringVar(&ccmOptions.Options.NodeBalancerBackendIPv4Subnet, "nodebalancer-backend-ipv4-subnet", "", "ipv4 subnet to use for NodeBalancer backends")
command.Flags().StringSliceVar(&ccmOptions.Options.NodeBalancerTags, "nodebalancer-tags", []string{}, "Linode tags to apply to all NodeBalancers")
command.Flags().BoolVar(&ccmOptions.Options.EnableIPv6ForLoadBalancers, "enable-ipv6-for-loadbalancers", false, "set both IPv4 and IPv6 addresses for all LoadBalancer services (when disabled, only IPv4 is used)")
command.Flags().BoolVar(&ccmOptions.Options.EnableIPv6ForNodeBalancerBackends, "enable-ipv6-for-nodebalancer-backends", false, "use public IPv6 addresses for NodeBalancer service backends, including VPC-backed NodeBalancers (when enabled, may update existing services during reconciliation and all selected backend nodes must have public IPv6)")
command.Flags().IntVar(&ccmOptions.Options.NodeCIDRMaskSizeIPv4, "node-cidr-mask-size-ipv4", 0, "ipv4 cidr mask size for pod cidrs allocated to nodes")
command.Flags().IntVar(&ccmOptions.Options.NodeCIDRMaskSizeIPv6, "node-cidr-mask-size-ipv6", 0, "ipv6 cidr mask size for pod cidrs allocated to nodes")
command.Flags().IntVar(&ccmOptions.Options.NodeBalancerBackendIPv4SubnetID, "nodebalancer-backend-ipv4-subnet-id", 0, "ipv4 subnet id to use for NodeBalancer backends")
command.Flags().StringVar(&ccmOptions.Options.NodeBalancerBackendIPv4SubnetName, "nodebalancer-backend-ipv4-subnet-name", "", "ipv4 subnet name to use for NodeBalancer backends")
command.Flags().BoolVar(&ccmOptions.Options.DisableNodeBalancerVPCBackends, "disable-nodebalancer-vpc-backends", false, "disables nodebalancer backends in VPCs (when enabled, nodebalancers will only have private IPs as backends for backward compatibility)")
command.Flags().StringVar(&ccmOptions.Options.NodeBalancerPrefix, "nodebalancer-prefix", "ccm", fmt.Sprintf("Name prefix for NoadBalancers. (max. %v char.)", linode.NodeBalancerPrefixCharLimit))
command.Flags().BoolVar(&ccmOptions.Options.DisableIPv6NodeCIDRAllocation, "disable-ipv6-node-cidr-allocation", false, "disables IPv6 node cidr allocation by ipam controller (when enabled, IPv6 cidr ranges will be allocated to nodes)")
command.Flags().StringVar(&ccmOptions.Options.LinodeTagFilter, "linode-tag-filter", "", "tag filter for linodes (e.g. cluster name)")
// 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
ccmOptions.Options.KubeconfigFlag = command.Flags().Lookup("kubeconfig")
if ccmOptions.Options.KubeconfigFlag == nil {
msg := "kubeconfig missing from CCM flag set"
sentry.CaptureError(ctx, fmt.Errorf("%s", msg))
fmt.Fprintf(os.Stderr, "kubeconfig missing from CCM flag set"+"\n")
os.Exit(1)
}
if externalSubnet, ok := os.LookupEnv(linodeExternalSubnetVariable); ok && externalSubnet != "" {
_, network, err := net.ParseCIDR(externalSubnet)
if err != nil {
msg := fmt.Sprintf("Unable to parse %s as network subnet: %v", externalSubnet, err)
sentry.CaptureError(ctx, fmt.Errorf("%s", msg))
fmt.Fprintf(os.Stderr, "%v\n", msg)
os.Exit(1)
}
ccmOptions.Options.LinodeExternalNetwork = network
}
// Provide stop channel for linode authenticated client healthchecker
ccmOptions.Options.GlobalStopChannel = stopCh
pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.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
if config.ComponentConfig.KubeCloudShared.AllocateNodeCIDRs {
ccmOptions.Options.AllocateNodeCIDRs = true
if config.ComponentConfig.KubeCloudShared.ClusterCIDR == "" {
fmt.Fprintf(os.Stderr, "--cluster-cidr is not set. This is required if --allocate-node-cidrs is set.\n")
os.Exit(1)
}
ccmOptions.Options.ClusterCIDRIPv4 = config.ComponentConfig.KubeCloudShared.ClusterCIDR
}
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")
} else { // this is in an else to appease nilaway
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
}