diff --git a/cmd/cloud-controller-manager/BUILD b/cmd/cloud-controller-manager/BUILD index eaf49c7aab2..ef66d9441f4 100644 --- a/cmd/cloud-controller-manager/BUILD +++ b/cmd/cloud-controller-manager/BUILD @@ -17,7 +17,7 @@ go_binary( go_library( name = "go_default_library", srcs = [ - "controller-manager.go", + "main.go", "nodeipamcontroller.go", "providers.go", ], diff --git a/cmd/cloud-controller-manager/controller-manager.go b/cmd/cloud-controller-manager/main.go similarity index 92% rename from cmd/cloud-controller-manager/controller-manager.go rename to cmd/cloud-controller-manager/main.go index 67aa06da9c7..62843f55a2b 100644 --- a/cmd/cloud-controller-manager/controller-manager.go +++ b/cmd/cloud-controller-manager/main.go @@ -18,6 +18,9 @@ limitations under the License. // are cloud provider dependent. It uses the API to listen to new events on resources. // This file should be written by each cloud provider. +// For an minimal working example, please refer to k8s.io/cloud-provider/sample/basic_main.go +// For an advanced example, please refer to k8s.io/cloud-provider/sample/advanced_main.go +// For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/main.go // The current file demonstrate how other cloud provider should leverage CCM and it uses fake parameters. Please modify for your own use. package main @@ -41,16 +44,13 @@ import ( genericcontrollermanager "k8s.io/controller-manager/app" "k8s.io/klog/v2" nodeipamconfig "k8s.io/kubernetes/pkg/controller/nodeipam/config" + // For existing cloud providers, the option to import legacy providers is still available. + // e.g. _"k8s.io/legacy-cloud-providers/" ) const ( - // cloudProviderName shows an sample of using hard coded parameter + // cloudProviderName shows an sample of using hard coded parameter, please edit the value for your case. cloudProviderName = "SampleCloudProviderName" - - // defaultNodeMaskCIDRIPv4 is default mask size for IPv4 node cidr - defaultNodeMaskCIDRIPv4 = 24 - // defaultNodeMaskCIDRIPv6 is default mask size for IPv6 node cidr - defaultNodeMaskCIDRIPv6 = 64 ) func main() { diff --git a/cmd/cloud-controller-manager/nodeipamcontroller.go b/cmd/cloud-controller-manager/nodeipamcontroller.go index 9a8beae2c97..105b1b4f809 100644 --- a/cmd/cloud-controller-manager/nodeipamcontroller.go +++ b/cmd/cloud-controller-manager/nodeipamcontroller.go @@ -38,6 +38,13 @@ import ( netutils "k8s.io/utils/net" ) +const ( + // defaultNodeMaskCIDRIPv4 is default mask size for IPv4 node cidr + defaultNodeMaskCIDRIPv4 = 24 + // defaultNodeMaskCIDRIPv6 is default mask size for IPv6 node cidr + defaultNodeMaskCIDRIPv6 = 64 +) + func startNodeIpamController(ccmconfig *cloudcontrollerconfig.CompletedConfig, nodeipamconfig nodeipamconfig.NodeIPAMControllerConfiguration, ctx genericcontrollermanager.ControllerContext, cloud cloudprovider.Interface) (http.Handler, bool, error) { var serviceCIDR *net.IPNet var secondaryServiceCIDR *net.IPNet diff --git a/staging/src/k8s.io/cloud-provider/sample/BUILD b/staging/src/k8s.io/cloud-provider/sample/BUILD index ceb72b3cd98..2088b0faace 100644 --- a/staging/src/k8s.io/cloud-provider/sample/BUILD +++ b/staging/src/k8s.io/cloud-provider/sample/BUILD @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = ["main.go"], + srcs = [ + "advanced_main.go", + "basic_main.go", + ], importmap = "k8s.io/kubernetes/vendor/k8s.io/cloud-provider/sample", importpath = "k8s.io/cloud-provider/sample", visibility = ["//visibility:public"], diff --git a/staging/src/k8s.io/cloud-provider/sample/README.md b/staging/src/k8s.io/cloud-provider/sample/README.md new file mode 100644 index 00000000000..04ce9d0da5f --- /dev/null +++ b/staging/src/k8s.io/cloud-provider/sample/README.md @@ -0,0 +1,20 @@ +# cloud-provider/sample + +This directory provides sample code about how all cloud providers should leverage CCM begin at 1.20. + +## Purpose + +Begin with 1.20, all cloud providers should not copy over or vender in `k8s.io/kubernetes/cmd/cloud-controller-manager`. Inside this directory, some sample code will be provided to demonstrate how cloud providers should leverage cloud-controller-manager. + +## Steps cloud providers shoud follow + +1. Have your external repo under k8s.io. e.g. `k8s.io/cloud-provider-` +2. Create `main.go` file under your external repo CCM directory. Please refer to `basic_main.go` for a minial working sample and `advanced_main.go` for advanced configuration samples. +Note: If you have a requirement of adding/deleting controllers within CCM, please refer to `k8s.io/kubernetes/cmd/cloud-controller-manager/main.go` for detailed samples. +3. Build/release CCM from your external repo. For existing cloud providers, the option to import legacy providers from `k8s.io/legacy-cloud-provider/` is still available. + +## Things you should NOT do + + 1. Vendor in `k8s.io/cmd/cloud-controller-manager`. + 2. Directly modify anything under `vendor/k8s.io/cloud-provider/sample` in this repo. Those are driven from `k8s.io/kubernetes/staging/src/k8s.io/cloud-provider/sample`. + 3. Make specific cloud provider changes in sample files. diff --git a/staging/src/k8s.io/cloud-provider/sample/main.go b/staging/src/k8s.io/cloud-provider/sample/advanced_main.go similarity index 88% rename from staging/src/k8s.io/cloud-provider/sample/main.go rename to staging/src/k8s.io/cloud-provider/sample/advanced_main.go index 6006ea1f1dd..49b7198509c 100644 --- a/staging/src/k8s.io/cloud-provider/sample/main.go +++ b/staging/src/k8s.io/cloud-provider/sample/advanced_main.go @@ -15,7 +15,9 @@ limitations under the License. */ // This file should be written by each cloud provider. -// This is an minimal example. For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/controller-manager.go +// For an minimal working example, please refer to k8s.io/cloud-provider/sample/basic_main.go +// For an advanced example, please refer to k8s.io/cloud-provider/sample/advanced_main.go +// For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/main.go package sample @@ -34,14 +36,18 @@ import ( _ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration "k8s.io/klog/v2" + // For existing cloud providers, the option to import legacy providers is still available. + // e.g. _"k8s.io/legacy-cloud-providers/" ) const ( + // The variables below are samples, please edit the value for your case. + // cloudProviderName shows an sample of using hard coded parameter cloudProviderName = "SampleCloudProviderName" ) -func main() { +func advancedMain() { rand.Seed(time.Now().UnixNano()) // cloudProviderConfigFile shows an sample of parse config file from flag option diff --git a/staging/src/k8s.io/cloud-provider/sample/basic_main.go b/staging/src/k8s.io/cloud-provider/sample/basic_main.go new file mode 100644 index 00000000000..f00873f3ac7 --- /dev/null +++ b/staging/src/k8s.io/cloud-provider/sample/basic_main.go @@ -0,0 +1,111 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// This file should be written by each cloud provider. +// For an minimal working example, please refer to k8s.io/cloud-provider/sample/basic_main.go +// For an advanced example, please refer to k8s.io/cloud-provider/sample/advanced_main.go +// For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/main.go + +package sample + +import ( + "fmt" + "math/rand" + "os" + "time" + + "github.com/spf13/pflag" + "k8s.io/cloud-provider" + "k8s.io/cloud-provider/app" + "k8s.io/cloud-provider/options" + "k8s.io/component-base/cli/flag" + "k8s.io/component-base/logs" + _ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins + _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration + "k8s.io/klog/v2" + // For existing cloud providers, the option to import legacy providers is still available. + // e.g. _"k8s.io/legacy-cloud-providers/" +) + +const ( + // The variables below are samples, please edit the value for your case. + + // sampleCloudProviderName shows an sample of using hard coded parameter for CloudProviderName + sampleCloudProviderName = "SampleCloudProviderName" + // sampleCloudProviderConfigFile shows an sample of using hard coded parameter for CloudProviderConfigFile + sampleCloudProviderConfigFile = "sampleCloudProviderConfigFile" +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + s, err := options.NewCloudControllerManagerOptions() + if err != nil { + klog.Fatalf("unable to initialize command options: %v", err) + } + c, err := s.Config([]string{}, app.ControllersDisabledByDefault.List()) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + + // initialize cloud provider with the cloud provider name and config file provided + cloud, err := cloudprovider.InitCloudProvider(sampleCloudProviderName, sampleCloudProviderConfigFile) + 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 c.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") + } + } + + // Initialize the cloud provider with a reference to the clientBuilder + cloud.Initialize(c.ClientBuilder, make(chan struct{})) + // Set the informer on the user cloud object + if informerUserCloud, ok := cloud.(cloudprovider.InformerUser); ok { + informerUserCloud.SetInformers(c.SharedInformers) + } + + controllerInitializers := app.DefaultControllerInitializers(c.Complete(), cloud) + command := app.NewCloudControllerManagerCommand(s, c, controllerInitializers) + + // TODO: once we switch everything over to Cobra commands, we can go back to calling + // utilflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the + // normalize func and add the go flag set by hand. + // Here is an sample + pflag.CommandLine.SetNormalizeFunc(flag.WordSepNormalizeFunc) + // utilflag.InitFlags() + logs.InitLogs() + defer logs.FlushLogs() + + // the flags could be set before execute + command.Flags().VisitAll(func(flag *pflag.Flag) { + if flag.Name == "cloud-provider" { + flag.Value.Set("SampleCloudProviderFlagValue") + return + } + }) + if err := command.Execute(); err != nil { + os.Exit(1) + } +}