Skip to content

Commit

Permalink
fix(cmd/localizer): use bootstrap template (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredallard authored Feb 7, 2022
1 parent 9cf3702 commit 1abc14e
Show file tree
Hide file tree
Showing 4 changed files with 477 additions and 148 deletions.
2 changes: 1 addition & 1 deletion bootstrap.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

268 changes: 141 additions & 127 deletions cmd/localizer/localizer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
// Copyright 2021 Outreach.io
// Copyright 2020 Jared Allard
//
// 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.
// Copyright 2022 Outreach Corporation. All Rights Reserved.

// Description: This file is the entrypoint for the localizer CLI
// command for localizer.
// Managed: true

package main

import (
Expand All @@ -27,147 +19,169 @@ import (
"time"

"github.com/bombsimon/logrusr/v2"
oapp "github.com/getoutreach/gobox/pkg/app"
gcli "github.com/getoutreach/gobox/pkg/cli"
"github.com/getoutreach/localizer/internal/kevents"
"github.com/getoutreach/localizer/internal/kube"
"github.com/getoutreach/localizer/internal/server"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
// Place any extra imports for your startup code here
///Block(imports)
///EndBlock(imports)
)

/// Deviation(unbootstrapped): waiting on OSS bootstrap
// HoneycombTracingKey gets set by the Makefile at compile-time which is pulled
// down by devconfig.sh.
var HoneycombTracingKey = "NOTSET" //nolint:gochecknoglobals // Why: We can't compile in things as a const.

var Version = "v0.0.0-unset"
///Block(honeycombDataset)
const HoneycombDataset = ""

func main() { //nolint:funlen
ctx, cancel := context.WithCancel(context.Background())
log := logrus.New()
log.Formatter = &logrus.TextFormatter{
ForceColors: true,
}
///EndBlock(honeycombDataset)

tmpFilePath := filepath.Join(os.TempDir(), "localizer-"+strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", "-")+".log")
tmpFile, err := os.Create(tmpFilePath)
if err == nil {
defer tmpFile.Close()
///Block(global)
///EndBlock(global)

log.Out = io.MultiWriter(os.Stderr, tmpFile)
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
log := logrus.New()

// this prevents the CLI from clobbering context cancellation
cli.OsExiter = func(code int) {
if code != 0 {
os.Exit(code)
}
}
///Block(init)
///EndBlock(init)

app := cli.App{
Version: Version,
Version: oapp.Version,
Name: "localizer",
///Block(app)
EnableBashCompletion: true,
Name: "localizer",
Flags: []cli.Flag{
// Note: KUBECONFIG is respected, but we don't allow passing a
// CLI argument to reduce the complexity and re-parsing of it.
&cli.StringFlag{
Name: "context",
Usage: "Specify Kubernetes context to use",
EnvVars: []string{"KUBECONTEXT"},
},
&cli.StringFlag{
Name: "log-level",
Usage: "Set the log level",
EnvVars: []string{"LOG_LEVEL"},
Value: "DEBUG",
DefaultText: "DEBUG",
},
&cli.StringFlag{
Name: "log-format",
Usage: "Set the log format",
EnvVars: []string{"LOG_FORMAT"},
DefaultText: "TEXT",
},
&cli.StringFlag{
Name: "cluster-domain",
Usage: "Configure the cluster domain used for service DNS endpoints",
Value: "cluster.local",
},
&cli.StringFlag{
Name: "ip-cidr",
Usage: "Set the IP address CIDR, must include the /",
Value: "127.0.0.1/8",
},
&cli.StringFlag{
Name: "namespace",
Usage: "Restrict forwarding to the given namespace. (default: all namespaces)",
},
///EndBlock(app)
}
app.Flags = []cli.Flag{
///Block(flags)
// Note: KUBECONFIG is respected, but we don't allow passing a
// CLI argument to reduce the complexity and re-parsing of it.
&cli.StringFlag{
Name: "context",
Usage: "Specify Kubernetes context to use",
EnvVars: []string{"KUBECONTEXT"},
},
Commands: []*cli.Command{
NewListCommand(log),
NewExposeCommand(log),
&cli.StringFlag{
Name: "log-level",
Usage: "Set the log level",
EnvVars: []string{"LOG_LEVEL"},
Value: "DEBUG",
DefaultText: "DEBUG",
},
Before: func(c *cli.Context) error {
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-sigC
log.WithField("signal", sig.String()).Info("shutting down")
cancel()
}()

// good for testing shut down issues
// go func() {
// time.Sleep(2 * time.Second)
// sigC <- os.Interrupt
// }()

if strings.EqualFold(c.String("log-level"), "debug") {
log.SetLevel(logrus.DebugLevel)
}

if strings.EqualFold(c.String("log-format"), "JSON") {
log.SetFormatter(&logrus.JSONFormatter{})
}
&cli.StringFlag{
Name: "log-format",
Usage: "Set the log format",
EnvVars: []string{"LOG_FORMAT"},
DefaultText: "TEXT",
},
&cli.StringFlag{
Name: "cluster-domain",
Usage: "Configure the cluster domain used for service DNS endpoints",
Value: "cluster.local",
},
&cli.StringFlag{
Name: "ip-cidr",
Usage: "Set the IP address CIDR, must include the /",
Value: "127.0.0.1/8",
},
&cli.StringFlag{
Name: "namespace",
Usage: "Restrict forwarding to the given namespace. (default: all namespaces)",
},
///EndBlock(flags)
}
app.Commands = []*cli.Command{
///Block(commands)
NewListCommand(log),
NewExposeCommand(log),
///EndBlock(commands)
}

klog.SetLogger(logrusr.New(log))
///Block(postApp)
log.Formatter = &logrus.TextFormatter{
ForceColors: true,
}

// setup the global kubernetes cache interface
config, k, err := kube.GetKubeClient(c.String("context"))
if err != nil {
return err
app.Before = func(c *cli.Context) error {
// Automatic updater is currently disabled
// until consumers have time to pass in
// --skip-update if required.
c.Set("skip-update", "true") //nolint:errcheck // Why: Best effort

sigC := make(chan os.Signal, 1)
signal.Notify(sigC, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-sigC
log.WithField("signal", sig.String()).Info("shutting down")
cancel()
}()

// best attempt don't output on --help or --version
if c.Args().First() != "--version" || c.Args().First() != "--help" {
// write to a logfile
tmpFilePath := filepath.Join(os.TempDir(), "localizer-"+strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", "-")+".log")
tmpFile, err := os.Create(tmpFilePath)
if err == nil {
defer tmpFile.Close()

log.Out = io.MultiWriter(os.Stderr, tmpFile)
}
log.Infof("using apiserver %s", config.Host)
kevents.ConfigureGlobalCache(k, c.String("namespace"))
log.WithField("file.path", tmpFilePath).Info("created logfile")
}

return nil
},
Action: func(c *cli.Context) error {
u, err := user.Current()
if err != nil {
return errors.Wrap(err, "failed to get current user")
}
if strings.EqualFold(c.String("log-level"), "debug") {
log.SetLevel(logrus.DebugLevel)
}

if u.Uid != "0" {
return fmt.Errorf("must be run as root/Administrator")
}
if strings.EqualFold(c.String("log-format"), "JSON") {
log.SetFormatter(&logrus.JSONFormatter{})
}

clusterDomain := c.String("cluster-domain")
ipCidr := c.String("ip-cidr")
klog.SetLogger(logrusr.New(log))

log.Infof("using cluster domain: %v", clusterDomain)
log.Infof("using ip cidr: %v", ipCidr)
// setup the global kubernetes cache interface
config, k, err := kube.GetKubeClient(c.String("context"))
if err != nil {
return err
}
log.Infof("using apiserver %s", config.Host)
kevents.ConfigureGlobalCache(k, c.String("namespace"))

srv := server.NewGRPCService(&server.RunOpts{
ClusterDomain: clusterDomain,
IPCidr: ipCidr,
KubeContext: c.String("context"),
})
return srv.Run(ctx, log)
},
return nil
}

if err := app.Run(os.Args); err != nil {
log.Errorf("failed to run: %v", err)
return
app.Action = func(c *cli.Context) error {
u, err := user.Current()
if err != nil {
return errors.Wrap(err, "failed to get current user")
}

if u.Uid != "0" {
return fmt.Errorf("must be run as root/Administrator")
}

clusterDomain := c.String("cluster-domain")
ipCidr := c.String("ip-cidr")

log.Infof("using cluster domain: %v", clusterDomain)
log.Infof("using ip cidr: %v", ipCidr)

srv := server.NewGRPCService(&server.RunOpts{
ClusterDomain: clusterDomain,
IPCidr: ipCidr,
KubeContext: c.String("context"),
})
return srv.Run(ctx, log)
}
///EndBlock(postApp)

// Insert global flags, tracing, updating and start the application.
gcli.HookInUrfaveCLI(ctx, cancel, &app, log, HoneycombTracingKey, HoneycombDataset)
}
Loading

0 comments on commit 1abc14e

Please sign in to comment.