-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
122 lines (107 loc) · 3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"golang.org/x/sync/errgroup"
"github.com/alexflint/go-arg"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/afero"
"github.com/xenitab/pkg/kubernetes"
"go.uber.org/zap"
"github.com/xenitab/git-auth-proxy/pkg/auth"
"github.com/xenitab/git-auth-proxy/pkg/config"
"github.com/xenitab/git-auth-proxy/pkg/server"
"github.com/xenitab/git-auth-proxy/pkg/token"
)
type Arguments struct {
Addr string `arg:"--addr" default:":8080"`
MetricsAddr string `arg:"--metrics-addr" default:":9090"`
CfgPath string `arg:"--config,required"`
KubeconfigPath string `arg:"--kubeconfig"`
}
func main() {
args := &Arguments{}
arg.MustParse(args)
zapLog, err := zap.NewProduction()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log := zapr.NewLogger(zapLog)
ctx := logr.NewContext(context.Background(), log)
if err := run(ctx, args.Addr, args.MetricsAddr, args.CfgPath, args.KubeconfigPath); err != nil {
log.Error(err, "")
os.Exit(1)
}
log.Info("gracefully shutdown")
}
func run(ctx context.Context, addr, metricsAddr, cfgPath, kubeconfigPath string) error {
authz, err := getAutorization(cfgPath)
if err != nil {
return err
}
client, err := kubernetes.GetKubernetesClientset(kubeconfigPath)
if err != nil {
return err
}
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
metricsSrv := &http.Server{ReadTimeout: 5 * time.Second, Addr: metricsAddr, Handler: promhttp.Handler()}
g.Go(func() error {
if err := metricsSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
g.Go(func() error {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return metricsSrv.Shutdown(shutdownCtx)
})
tokenWriter := token.NewTokenWriter(client, authz)
g.Go(func() error {
if err := tokenWriter.Start(ctx); err != nil {
return err
}
return nil
})
gp := server.NewGitProxy(authz)
proxySrv := gp.Server(ctx, addr)
g.Go(func() error {
if err := proxySrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
g.Go(func() error {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return proxySrv.Shutdown(shutdownCtx)
})
logr.FromContextOrDiscard(ctx).Info("running git-auth-proxy")
if err := g.Wait(); err != nil {
return err
}
return nil
}
func getAutorization(path string) (*auth.Authorizer, error) {
cfg, err := config.LoadConfiguration(afero.NewOsFs(), path)
if err != nil {
return nil, fmt.Errorf("could not load configuration: %w", err)
}
authz, err := auth.NewAuthorizer(cfg)
if err != nil {
return nil, fmt.Errorf("could not generate authorization: %w", err)
}
return authz, nil
}