@@ -23,115 +23,46 @@ import (
23
23
goflag "flag"
24
24
"fmt"
25
25
"math/rand"
26
- "net/http"
27
26
"os"
28
27
"time"
29
28
29
+ "github.com/spf13/pflag"
30
30
"k8s.io/apimachinery/pkg/util/wait"
31
- "k8s.io/apiserver/pkg/server/healthz"
32
31
cloudprovider "k8s.io/cloud-provider"
33
- "k8s.io/cloud-provider-openstack/pkg/openstack"
34
- "k8s.io/cloud-provider-openstack/pkg/version"
35
32
"k8s.io/cloud-provider/app"
33
+ "k8s.io/cloud-provider/app/config"
36
34
"k8s.io/cloud-provider/options"
37
- "k8s.io/component-base/cli/flag"
35
+ cliflag "k8s.io/component-base/cli/flag"
38
36
"k8s.io/component-base/logs"
39
37
_ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
40
38
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
41
39
"k8s.io/klog/v2"
42
40
_ "k8s.io/kubernetes/pkg/features" // add the kubernetes feature gates
43
41
44
- "github.com/spf13/cobra"
45
- "github.com/spf13/pflag"
46
- )
47
-
48
- func init () {
49
- mux := http .NewServeMux ()
50
- healthz .InstallHandler (mux )
51
- }
52
-
53
- var (
54
- versionFlag bool
42
+ "k8s.io/cloud-provider-openstack/pkg/openstack"
43
+ "k8s.io/cloud-provider-openstack/pkg/version"
55
44
)
56
45
57
46
func main () {
58
- rand .Seed (time .Now ().UTC ().UnixNano ())
59
-
60
- goflag .CommandLine .Parse ([]string {})
61
- controllerList := []string {"cloud-node" , "cloud-node-lifecycle" , "service" , "route" }
47
+ rand .Seed (time .Now ().UnixNano ())
62
48
63
- s , err := options .NewCloudControllerManagerOptions ()
49
+ ccmOptions , err := options .NewCloudControllerManagerOptions ()
64
50
if err != nil {
65
51
klog .Fatalf ("unable to initialize command options: %v" , err )
66
52
}
67
- s .KubeCloudShared .CloudProvider .Name = openstack .ProviderName
68
-
69
- command := & cobra.Command {
70
- Use : "openstack-cloud-controller-manager" ,
71
- Long : `The Cloud controller manager is a daemon that embeds
72
- the cloud specific control loops shipped with Kubernetes.` ,
73
- PersistentPreRun : func (cmd * cobra.Command , args []string ) {
74
- // Glog requires this otherwise it complains.
75
- goflag .CommandLine .Parse (nil )
76
-
77
- // This is a temporary hack to enable proper logging until upstream dependencies
78
- // are migrated to fully utilize klog instead of glog.
79
- klogFlags := goflag .NewFlagSet ("klog" , goflag .ExitOnError )
80
- klog .InitFlags (klogFlags )
81
-
82
- // Sync the glog and klog flags.
83
- cmd .Flags ().VisitAll (func (f1 * pflag.Flag ) {
84
- f2 := klogFlags .Lookup (f1 .Name )
85
- if f2 != nil {
86
- value := f1 .Value .String ()
87
- f2 .Value .Set (value )
88
- }
89
- })
90
- },
91
- Run : func (cmd * cobra.Command , args []string ) {
92
- if versionFlag {
93
- version .PrintVersionAndExit ()
94
- }
95
-
96
- flag .PrintFlags (cmd .Flags ())
97
-
98
- c , err := s .Config (controllerList , app .ControllersDisabledByDefault .List ())
99
- if err != nil {
100
- fmt .Fprintf (os .Stderr , "%v\n " , err )
101
- os .Exit (1 )
102
- }
103
-
104
- cloudconfig := c .Complete ().ComponentConfig .KubeCloudShared .CloudProvider
105
- cloud , err := cloudprovider .InitCloudProvider (cloudconfig .Name , cloudconfig .CloudConfigFile )
106
- if err != nil {
107
- klog .Fatalf ("Cloud provider could not be initialized: %v" , err )
108
- }
109
- if cloud == nil {
110
- klog .Fatalf ("cloud provider is nil" )
111
- }
112
-
113
- if err := app .Run (c .Complete (), app .DefaultControllerInitializers (c .Complete (), cloud ), wait .NeverStop ); err != nil {
114
- fmt .Fprintf (os .Stderr , "%v\n " , err )
115
- os .Exit (1 )
116
- }
117
- },
118
- }
119
53
120
- fs := command .Flags ()
121
- namedFlagSets := s .Flags (controllerList , app .ControllersDisabledByDefault .List ())
122
- for _ , f := range namedFlagSets .FlagSets {
123
- fs .AddFlagSet (f )
124
- }
125
-
126
- fs .BoolVar (& versionFlag , "version" , false , "Print version and exit" )
54
+ fss := cliflag.NamedFlagSets {}
55
+ command := app .NewCloudControllerManagerCommand (ccmOptions , cloudInitializer , app .DefaultInitFuncConstructors , fss , wait .NeverStop )
127
56
128
57
openstack .AddExtraFlags (pflag .CommandLine )
129
58
130
59
// TODO: once we switch everything over to Cobra commands, we can go back to calling
131
60
// utilflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
132
61
// normalize func and add the go flag set by hand.
133
- pflag .CommandLine .SetNormalizeFunc (flag .WordSepNormalizeFunc )
62
+ // Here is an sample
63
+ pflag .CommandLine .SetNormalizeFunc (cliflag .WordSepNormalizeFunc )
134
64
pflag .CommandLine .AddGoFlagSet (goflag .CommandLine )
65
+
135
66
// utilflag.InitFlags()
136
67
logs .InitLogs ()
137
68
defer logs .FlushLogs ()
@@ -143,3 +74,25 @@ the cloud specific control loops shipped with Kubernetes.`,
143
74
os .Exit (1 )
144
75
}
145
76
}
77
+
78
+ func cloudInitializer (config * config.CompletedConfig ) cloudprovider.Interface {
79
+ cloudConfig := config .ComponentConfig .KubeCloudShared .CloudProvider
80
+
81
+ // initialize cloud provider with the cloud provider name and config file provided
82
+ cloud , err := cloudprovider .InitCloudProvider (cloudConfig .Name , cloudConfig .CloudConfigFile )
83
+ if err != nil {
84
+ klog .Fatalf ("Cloud provider could not be initialized: %v" , err )
85
+ }
86
+ if cloud == nil {
87
+ klog .Fatalf ("Cloud provider is nil" )
88
+ }
89
+
90
+ if ! cloud .HasClusterID () {
91
+ if config .ComponentConfig .KubeCloudShared .AllowUntaggedCloud {
92
+ 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" )
93
+ } else {
94
+ 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" )
95
+ }
96
+ }
97
+ return cloud
98
+ }
0 commit comments