Skip to content

Commit 8c12bfc

Browse files
committed
Update main function
- Update main to the new call process and remove the workaround from previous versions. - Update the boilerplate check to handle 2021 as a year. - Remove redundant main update task in Makefile.
1 parent 7e22f3d commit 8c12bfc

File tree

5 files changed

+50
-149
lines changed

5 files changed

+50
-149
lines changed

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,3 @@ cloud-build:
8989
.PHONY: delete_k8s_build
9090
delete_k8s_build:
9191
kubectl delete jobs -l build=cloud-controller-build
92-
93-
main.go:
94-
@./hack/create-main.sh

app/cloudcontroller.go

Lines changed: 0 additions & 136 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/brightbox/gobrightbox v0.5.7
77
github.com/brightbox/k8ssdk v0.6.1
88
github.com/go-test/deep v1.0.7
9-
github.com/spf13/cobra v1.1.1
9+
github.com/spf13/pflag v1.0.5
1010
k8s.io/api v0.21.0
1111
k8s.io/apimachinery v0.21.0
1212
k8s.io/cloud-provider v0.21.0

hack/boilerplate/boilerplate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def get_regexs():
198198
# in the real thing
199199
regexs["year"] = re.compile('YEAR')
200200
# dates can be 2017, 2018; company holder names can be anything
201-
regexs["date"] = re.compile('(201[789]|2020)')
201+
regexs["date"] = re.compile('(201[789]|202[01])')
202202
# strip // +build \n\n build constraints
203203
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n",
204204
re.MULTILINE)

main.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Brightbox Systems Ltd
1+
// Copyright 2021 Brightbox Systems Ltd
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,32 +12,72 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

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-
1815
package main
1916

2017
import (
2118
"math/rand"
2219
"os"
2320
"time"
2421

25-
"github.com/brightbox/brightbox-cloud-controller-manager/app"
2622
_ "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"
2730
"k8s.io/component-base/logs"
28-
2931
_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
3032
_ "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>"
3136
)
3237

3338
func main() {
3439
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()
3555
logs.InitLogs()
3656
defer logs.FlushLogs()
3757

38-
command := app.NewBrightboxCloudControllerManagerCommand()
39-
4058
if err := command.Execute(); err != nil {
4159
os.Exit(1)
4260
}
4361
}
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

Comments
 (0)