|
1 |
| -// Copyright 2019 Brightbox Systems Ltd |
| 1 | +// Copyright 2021 Brightbox Systems Ltd |
2 | 2 | //
|
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | // you may not use this file except in compliance with the License.
|
|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -// The external controller manager is responsible for running controller loops that |
16 |
| -// are cloud provider dependent. It uses the API to listen to new events on resources. |
17 |
| - |
18 | 15 | package main
|
19 | 16 |
|
20 | 17 | import (
|
21 | 18 | "math/rand"
|
22 | 19 | "os"
|
23 | 20 | "time"
|
24 | 21 |
|
25 |
| - "github.com/brightbox/brightbox-cloud-controller-manager/app" |
26 | 22 | _ "github.com/brightbox/brightbox-cloud-controller-manager/brightbox"
|
| 23 | + "github.com/spf13/pflag" |
| 24 | + "k8s.io/apimachinery/pkg/util/wait" |
| 25 | + cloudprovider "k8s.io/cloud-provider" |
| 26 | + "k8s.io/cloud-provider/app" |
| 27 | + "k8s.io/cloud-provider/app/config" |
| 28 | + "k8s.io/cloud-provider/options" |
| 29 | + cliflag "k8s.io/component-base/cli/flag" |
27 | 30 | "k8s.io/component-base/logs"
|
28 |
| - |
29 | 31 | _ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
|
30 | 32 | _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
|
| 33 | + "k8s.io/klog/v2" |
| 34 | + // For existing cloud providers, the option to import legacy providers is still available. |
| 35 | + // e.g. _"k8s.io/legacy-cloud-providers/<provider>" |
31 | 36 | )
|
32 | 37 |
|
33 | 38 | func main() {
|
34 | 39 | rand.Seed(time.Now().UnixNano())
|
| 40 | + |
| 41 | + ccmOptions, err := options.NewCloudControllerManagerOptions() |
| 42 | + if err != nil { |
| 43 | + klog.Fatalf("unable to initialize command options: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + fss := cliflag.NamedFlagSets{} |
| 47 | + command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors, fss, wait.NeverStop) |
| 48 | + |
| 49 | + // TODO: once we switch everything over to Cobra commands, we can go back to calling |
| 50 | + // utilflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the |
| 51 | + // normalize func and add the go flag set by hand. |
| 52 | + // Here is an sample |
| 53 | + pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) |
| 54 | + // utilflag.InitFlags() |
35 | 55 | logs.InitLogs()
|
36 | 56 | defer logs.FlushLogs()
|
37 | 57 |
|
38 |
| - command := app.NewBrightboxCloudControllerManagerCommand() |
39 |
| - |
40 | 58 | if err := command.Execute(); err != nil {
|
41 | 59 | os.Exit(1)
|
42 | 60 | }
|
43 | 61 | }
|
| 62 | + |
| 63 | +func cloudInitializer(config *config.CompletedConfig) cloudprovider.Interface { |
| 64 | + cloudConfig := config.ComponentConfig.KubeCloudShared.CloudProvider |
| 65 | + |
| 66 | + // initialize cloud provider with the cloud provider name and config file provided |
| 67 | + cloud, err := cloudprovider.InitCloudProvider(cloudConfig.Name, cloudConfig.CloudConfigFile) |
| 68 | + if err != nil { |
| 69 | + klog.Fatalf("Cloud provider could not be initialized: %v", err) |
| 70 | + } |
| 71 | + if cloud == nil { |
| 72 | + klog.Fatalf("Cloud provider is nil") |
| 73 | + } |
| 74 | + |
| 75 | + if !cloud.HasClusterID() { |
| 76 | + if config.ComponentConfig.KubeCloudShared.AllowUntaggedCloud { |
| 77 | + 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") |
| 78 | + } else { |
| 79 | + 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") |
| 80 | + } |
| 81 | + } |
| 82 | + return cloud |
| 83 | +} |
0 commit comments