// Run launches a template router using the provided options. It never exits. func (o *TemplateRouterOptions) Run() error { pluginCfg := templateplugin.TemplatePluginConfig{ WorkingDir: o.WorkingDir, TemplatePath: o.TemplateFile, ReloadScriptPath: o.ReloadScript, DefaultCertificate: o.DefaultCertificate, StatsPort: o.StatsPort, StatsUsername: o.StatsUsername, StatsPassword: o.StatsPassword, PeerService: o.RouterService, IncludeUDP: o.RouterSelection.IncludeUDP, } templatePlugin, err := templateplugin.NewTemplatePlugin(pluginCfg) if err != nil { return err } plugin := controller.NewUniqueHost(templatePlugin, controller.HostForRoute) oc, kc, err := o.Config.Clients() if err != nil { return err } factory := o.RouterSelection.NewFactory(oc, kc) controller := factory.Create(plugin) controller.Run() proc.StartReaper() select {} }
func main() { clientConfig := kubectl_util.DefaultClientConfig(flags) flags.Parse(os.Args) cfg := parseCfg(*config, *lbDefAlgorithm) if len(*tcpServices) == 0 { glog.Infof("All tcp/https services will be ignored.") } var kubeClient *unversioned.Client var err error defErrorPage := newStaticPageHandler(*errorPage, defaultErrorPage) if defErrorPage == nil { glog.Fatalf("Failed to load the default error page") } go registerHandlers(defErrorPage) proc.StartReaper() if *startSyslog { cfg.startSyslog = true _, err = newSyslogServer("/var/run/haproxy.log.socket") if err != nil { glog.Fatalf("Failed to start syslog server: %v", err) } } if *cluster { if kubeClient, err = unversioned.NewInCluster(); err != nil { glog.Fatalf("Failed to create client: %v", err) } } else { config, err := clientConfig.ClientConfig() if err != nil { glog.Fatalf("error connecting to the client: %v", err) } kubeClient, err = unversioned.New(config) } namespace, specified, err := clientConfig.Namespace() if err != nil { glog.Fatalf("unexpected error: %v", err) } if !specified { namespace = api.NamespaceDefault } // TODO: Handle multiple namespaces lbc := newLoadBalancerController(cfg, kubeClient, namespace) go lbc.epController.Run(util.NeverStop) go lbc.svcController.Run(util.NeverStop) if *dry { dryRun(lbc) } else { lbc.cfg.reload() util.Until(lbc.worker, time.Second, util.NeverStop) } }
func main() { clientConfig := kubectl_util.DefaultClientConfig(flags) flags.Parse(os.Args) var err error var kubeClient *unversioned.Client if *cluster { if kubeClient, err = unversioned.NewInCluster(); err != nil { glog.Fatalf("Failed to create client: %v", err) } } else { config, err := clientConfig.ClientConfig() if err != nil { glog.Fatalf("error connecting to the client: %v", err) } kubeClient, err = unversioned.New(config) } namespace, specified, err := clientConfig.Namespace() if err != nil { glog.Fatalf("unexpected error: %v", err) } if !specified { namespace = "" } err = loadIPVModule() if err != nil { glog.Fatalf("Terminating execution: %v", err) } err = changeSysctl() if err != nil { glog.Fatalf("Terminating execution: %v", err) } proc.StartReaper() glog.Info("starting LVS configuration") if *useUnicast { glog.Info("keepalived will use unicast to sync the nodes") } ipvsc := newIPVSController(kubeClient, namespace, *useUnicast) go ipvsc.epController.Run(util.NeverStop) go ipvsc.svcController.Run(util.NeverStop) go util.Until(ipvsc.worker, time.Second, util.NeverStop) time.Sleep(5 * time.Second) glog.Info("starting keepalived to announce VIPs") ipvsc.keepalived.Start() }
// start launches the router. func start(cfg *clientcmd.Config, plugin router.Plugin) error { osClient, kubeClient, err := cfg.Clients() if err != nil { return err } proc.StartReaper() factory := controllerfactory.RouterControllerFactory{KClient: kubeClient, OSClient: osClient} controller := factory.Create(plugin) controller.Run() select {} }
// Run launches a template router using the provided options. It never exits. func (o *TemplateRouterOptions) Run() error { pluginCfg := templateplugin.TemplatePluginConfig{ WorkingDir: o.WorkingDir, TemplatePath: o.TemplateFile, ReloadScriptPath: o.ReloadScript, ReloadInterval: o.ReloadInterval, DefaultCertificate: o.DefaultCertificate, DefaultCertificatePath: o.DefaultCertificatePath, DefaultCertificateDir: o.DefaultCertificateDir, StatsPort: o.StatsPort, StatsUsername: o.StatsUsername, StatsPassword: o.StatsPassword, PeerService: o.RouterService, BindPortsAfterSync: o.BindPortsAfterSync, IncludeUDP: o.RouterSelection.IncludeUDP, AllowWildcardRoutes: o.RouterSelection.AllowWildcardRoutes, } oc, kc, err := o.Config.Clients() if err != nil { return err } svcFetcher := templateplugin.NewListWatchServiceLookup(kc.Core(), 10*time.Minute) templatePlugin, err := templateplugin.NewTemplatePlugin(pluginCfg, svcFetcher) if err != nil { return err } statusPlugin := controller.NewStatusAdmitter(templatePlugin, oc, o.RouterName) var nextPlugin router.Plugin = statusPlugin if o.ExtendedValidation { nextPlugin = controller.NewExtendedValidator(nextPlugin, controller.RejectionRecorder(statusPlugin)) } uniqueHostPlugin := controller.NewUniqueHost(nextPlugin, o.RouteSelectionFunc(), controller.RejectionRecorder(statusPlugin)) plugin := controller.NewHostAdmitter(uniqueHostPlugin, o.RouteAdmissionFunc(), o.RestrictSubdomainOwnership, controller.RejectionRecorder(statusPlugin)) factory := o.RouterSelection.NewFactory(oc, kc) controller := factory.Create(plugin, false) controller.Run() proc.StartReaper() select {} }
func main() { flags.Parse(os.Args) cfg := parseCfg(*config) if len(*tcpServices) == 0 { glog.Infof("All tcp/https services will be ignored.") } go healthzServer() proc.StartReaper() var kubeClient *unversioned.Client var err error clientConfig := kubectl_util.DefaultClientConfig(flags) if *cluster { if kubeClient, err = unversioned.NewInCluster(); err != nil { glog.Fatalf("Failed to create client: %v", err) } } else { config, err := clientConfig.ClientConfig() if err != nil { glog.Fatalf("error connecting to the client: %v", err) } kubeClient, err = unversioned.New(config) } namespace, specified, err := clientConfig.Namespace() if err != nil { glog.Fatalf("unexpected error: %v", err) } if !specified { namespace = "default" } // TODO: Handle multiple namespaces lbc := newLoadBalancerController(cfg, kubeClient, namespace) go lbc.epController.Run(util.NeverStop) go lbc.svcController.Run(util.NeverStop) if *dry { dryRun(lbc) } else { util.Until(lbc.worker, time.Second, util.NeverStop) } }
// Run launches a template router using the provided options. It never exits. func (o *TemplateRouterOptions) Run() error { pluginCfg := templateplugin.TemplatePluginConfig{ WorkingDir: o.WorkingDir, TemplatePath: o.TemplateFile, ReloadScriptPath: o.ReloadScript, ReloadInterval: o.ReloadInterval, DefaultCertificate: o.DefaultCertificate, DefaultCertificatePath: o.DefaultCertificatePath, StatsPort: o.StatsPort, StatsUsername: o.StatsUsername, StatsPassword: o.StatsPassword, PeerService: o.RouterService, IncludeUDP: o.RouterSelection.IncludeUDP, } templatePlugin, err := templateplugin.NewTemplatePlugin(pluginCfg) if err != nil { return err } oc, kc, err := o.Config.Clients() if err != nil { return err } statusPlugin := controller.NewStatusAdmitter(templatePlugin, oc, o.RouterName) var nextPlugin router.Plugin = statusPlugin if o.ExtendedValidation { nextPlugin = controller.NewExtendedValidator(nextPlugin, controller.RejectionRecorder(statusPlugin)) } plugin := controller.NewUniqueHost(nextPlugin, o.RouteSelectionFunc(), controller.RejectionRecorder(statusPlugin)) factory := o.RouterSelection.NewFactory(oc, kc) controller := factory.Create(plugin) controller.Run() proc.StartReaper() select {} }
func main() { flags.Parse(os.Args) proc.StartReaper() var kubeClient *client.Client if *cluster { clusterClient, err := client.NewInCluster() if err != nil { glog.Fatalf("Failed to create client: %v", err) } kubeClient = clusterClient } else { config := &client.Config{ Host: *master, } confClient, err := client.New(config) if err != nil { glog.Fatalf("Could not create api client %v", err) } kubeClient = confClient } err := haproxy.StartSyslogServer("/var/run/haproxy.log.sock") if err != nil { glog.Fatalf("Failed to start syslog server: %v", err) } lbc := newLoadBalancerController(kubeClient, "default", *domain, strings.Split(*nodes, ",")) go healthzServer() go lbc.epController.Run(util.NeverStop) go lbc.svcController.Run(util.NeverStop) util.Until(lbc.worker, time.Second, util.NeverStop) }
func (o *ObserveOptions) Run() error { if len(o.deleteCommand) > 0 && len(o.nameSyncCommand) == 0 { fmt.Fprintf(o.errOut, "warning: If you are modifying resources outside of %q, you should use the --names command to ensure you don't miss deletions that occur while the command is not running.\n", o.mapping.Resource) } // watch the given resource for changes store := cache.NewDeltaFIFO(objectArgumentsKeyFunc, nil, o.knownObjects) lw := restListWatcher{Helper: resource.NewHelper(o.client, o.mapping)} if !o.allNamespaces { lw.namespace = o.namespace } // ensure any child processes are reaped if we are running as PID 1 proc.StartReaper() // listen on the provided address for metrics if len(o.listenAddr) > 0 { prometheus.MustRegister(observeCounts) prometheus.MustRegister(execDurations) prometheus.MustRegister(nameExecDurations) errWaitingForSync := fmt.Errorf("waiting for initial sync") healthz.InstallHandler(http.DefaultServeMux, healthz.NamedCheck("ready", func(r *http.Request) error { if !store.HasSynced() { return errWaitingForSync } return nil })) http.Handle("/metrics", prometheus.Handler()) go func() { glog.Fatalf("Unable to listen on %q: %v", o.listenAddr, http.ListenAndServe(o.listenAddr, nil)) }() glog.V(2).Infof("Listening on %s at /metrics and /healthz", o.listenAddr) } // exit cleanly after a certain period // lock guards the loop to ensure no child processes are running var lock sync.Mutex if o.exitAfterPeriod > 0 { go func() { <-time.After(o.exitAfterPeriod) lock.Lock() o.dumpMetrics() fmt.Fprintf(o.errOut, "Shutting down after %s ...\n", o.exitAfterPeriod) os.Exit(0) }() } defer o.dumpMetrics() // start the reflector reflector := cache.NewNamedReflector("observer", lw, nil, store, o.resyncPeriod) reflector.Run() if o.once { // wait until the reflector reports it has completed the initial list and the // fifo has been populated for len(reflector.LastSyncResourceVersion()) == 0 { time.Sleep(50 * time.Millisecond) } // if the store is empty, there is nothing to sync if store.HasSynced() && len(store.ListKeys()) == 0 { fmt.Fprintf(o.errOut, "Nothing to sync, exiting immediately\n") return nil } } // process all changes that occur in the resource syncing := false for { _, err := store.Pop(func(obj interface{}) error { // if we failed to retrieve the list of keys, exit if err := o.argumentStore.ListKeysError(); err != nil { return fmt.Errorf("unable to list known keys: %v", err) } deltas := obj.(cache.Deltas) for _, delta := range deltas { lock.Lock() // handle before and after observe notification switch { case !syncing && delta.Type == cache.Sync: if err := o.startSync(); err != nil { return err } syncing = true case syncing && delta.Type != cache.Sync: if err := o.finishSync(); err != nil { return err } syncing = false } // require the user to provide a name function in order to get events beyond added / updated if !syncing && o.knownObjects == nil && !(delta.Type == cache.Added || delta.Type == cache.Updated) { continue } observeCounts.WithLabelValues(string(delta.Type)).Inc() // calculate the arguments for the delta and then invoke any command object, arguments, output, err := o.calculateArguments(delta) if err != nil { return err } if err := o.next(delta.Type, object, output, arguments); err != nil { return err } lock.Unlock() } return nil }) if err != nil { return err } // if we only want to run once, exit here if o.once && store.HasSynced() { if syncing { if err := o.finishSync(); err != nil { return err } } return nil } } }