sigs.k8s.io/cluster-api/bootstrap/kubeadm@v0.0.0-20191016155141-23a891785b60/main.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    26  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    27  	"k8s.io/klog"
    28  	"k8s.io/klog/klogr"
    29  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2"
    30  	"sigs.k8s.io/cluster-api/bootstrap/kubeadm/controllers"
    31  	"sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/locking"
    32  	clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
    33  	ctrl "sigs.k8s.io/controller-runtime"
    34  	// +kubebuilder:scaffold:imports
    35  )
    36  
    37  var (
    38  	scheme   = runtime.NewScheme()
    39  	setupLog = ctrl.Log.WithName("setup")
    40  )
    41  
    42  func init() {
    43  	_ = clientgoscheme.AddToScheme(scheme)
    44  	_ = bootstrapv1.AddToScheme(scheme)
    45  	_ = clusterv1.AddToScheme(scheme)
    46  	// +kubebuilder:scaffold:scheme
    47  }
    48  
    49  func main() {
    50  	klog.InitFlags(nil)
    51  
    52  	var (
    53  		metricsAddr          string
    54  		enableLeaderElection bool
    55  		syncPeriod           time.Duration
    56  		watchNamespace       string
    57  	)
    58  
    59  	flag.StringVar(
    60  		&metricsAddr,
    61  		"metrics-addr",
    62  		":8080",
    63  		"The address the metric endpoint binds to.",
    64  	)
    65  
    66  	flag.BoolVar(
    67  		&enableLeaderElection,
    68  		"enable-leader-election",
    69  		false,
    70  		"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.",
    71  	)
    72  
    73  	flag.DurationVar(
    74  		&syncPeriod,
    75  		"sync-period",
    76  		10*time.Minute,
    77  		"The minimum interval at which watched resources are reconciled (e.g. 10m)",
    78  	)
    79  
    80  	flag.DurationVar(
    81  		&controllers.DefaultTokenTTL,
    82  		"bootstrap-token-ttl",
    83  		15*time.Minute,
    84  		"The amount of time the bootstrap token will be valid",
    85  	)
    86  
    87  	flag.StringVar(
    88  		&watchNamespace,
    89  		"namespace",
    90  		"",
    91  		"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.",
    92  	)
    93  
    94  	flag.Parse()
    95  
    96  	ctrl.SetLogger(klogr.New())
    97  
    98  	if controllers.DefaultTokenTTL-syncPeriod < 1*time.Minute {
    99  		setupLog.Info("warning: the sync interval is close to the configured token TTL, tokens may expire temporarily before being refreshed")
   100  	}
   101  
   102  	mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
   103  		Scheme:             scheme,
   104  		MetricsBindAddress: metricsAddr,
   105  		LeaderElection:     enableLeaderElection,
   106  		LeaderElectionID:   "controller-leader-election-cabpk",
   107  		Namespace:          watchNamespace,
   108  		SyncPeriod:         &syncPeriod,
   109  	})
   110  	if err != nil {
   111  		setupLog.Error(err, "unable to start manager")
   112  		os.Exit(1)
   113  	}
   114  
   115  	if err := (&controllers.KubeadmConfigReconciler{
   116  		Client:               mgr.GetClient(),
   117  		SecretsClientFactory: controllers.ClusterSecretsClientFactory{},
   118  		Log:                  ctrl.Log.WithName("KubeadmConfigReconciler"),
   119  		KubeadmInitLock:      locking.NewControlPlaneInitMutex(ctrl.Log.WithName("init-locker"), mgr.GetClient()),
   120  	}).SetupWithManager(mgr); err != nil {
   121  		setupLog.Error(err, "unable to create controller", "controller", "KubeadmConfigReconciler")
   122  		os.Exit(1)
   123  	}
   124  	// +kubebuilder:scaffold:builder
   125  
   126  	setupLog.Info("starting manager")
   127  	if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
   128  		setupLog.Error(err, "problem running manager")
   129  		os.Exit(1)
   130  	}
   131  }