Fix panic on startup when misconfigured (#154)

Fixes #153
This commit is contained in:
Yusuke Kuoka 2020-11-10 17:03:33 +09:00 committed by GitHub
parent f2a2ab7ede
commit 3f335ca628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 7 deletions

View File

@ -9,14 +9,12 @@ import (
"time"
"github.com/bradleyfalzon/ghinstallation"
"github.com/go-logr/logr"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
// Config contains configuration for Github client
type Config struct {
Log logr.Logger
EnterpriseURL string `split_words:"true"`
AppID int64 `split_words:"true"`
AppInstallationID int64 `split_words:"true"`
@ -46,7 +44,6 @@ func (c *Config) NewClient() (*Client, error) {
} else {
tr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, c.AppID, c.AppInstallationID, c.AppPrivateKey)
if err != nil {
c.Log.Error(err, "Authentication failed")
return nil, fmt.Errorf("authentication failed: %v", err)
}
httpClient = &http.Client{Transport: tr}
@ -56,7 +53,6 @@ func (c *Config) NewClient() (*Client, error) {
var err error
client, err = github.NewEnterpriseClient(c.EnterpriseURL, c.EnterpriseURL, httpClient)
if err != nil {
c.Log.Error(err, "Enterprise client creation failed")
return nil, fmt.Errorf("enterprise client creation failed: %v", err)
}
githubBaseURL = fmt.Sprintf("%s://%s%s", client.BaseURL.Scheme, client.BaseURL.Host, strings.TrimSuffix(client.BaseURL.Path, "api/v3/"))

View File

@ -82,15 +82,17 @@ func main() {
flag.DurationVar(&syncPeriod, "sync-period", 10*time.Minute, "Determines the minimum frequency at which K8s resources managed by this controller are reconciled. When you use autoscaling, set to a lower value like 10 minute, because this corresponds to the minimum time to react on demand change")
flag.Parse()
logger := zap.New(func(o *zap.Options) {
o.Development = true
})
ghClient, err = c.NewClient()
if err != nil {
fmt.Fprintln(os.Stderr, "Error: Client creation failed.", err)
os.Exit(1)
}
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
}))
ctrl.SetLogger(logger)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,