-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathgithub_app_ctrl.go
208 lines (187 loc) · 5.98 KB
/
github_app_ctrl.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package githubapp
import (
"encoding/json"
"net/http"
"os"
"sync"
"github.com/hashicorp/go-multierror"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/factory"
"github.com/jenkins-x/go-scm/scm/transport"
"github.com/jenkins-x/lighthouse/pkg/clients"
"github.com/jenkins-x/lighthouse/pkg/config"
"github.com/jenkins-x/lighthouse/pkg/git"
"github.com/jenkins-x/lighthouse/pkg/keeper"
"github.com/jenkins-x/lighthouse/pkg/keeper/history"
"github.com/jenkins-x/lighthouse/pkg/launcher"
"github.com/jenkins-x/lighthouse/pkg/scmprovider"
"github.com/jenkins-x/lighthouse/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type gitHubAppKeeperController struct {
controllers []keeper.Controller
ownerTokenFinder *util.OwnerTokensDir
gitServer string
githubAppSecretDir string
configAgent *config.Agent
botName string
gitKind string
maxRecordsPerPool int
historyURI string
statusURI string
ns string
logger *logrus.Entry
m sync.Mutex
}
// NewGitHubAppKeeperController creates a GitHub App style controller which needs to process each github owner
// using a separate git provider client due to the way GitHub App tokens work
func NewGitHubAppKeeperController(githubAppSecretDir string, configAgent *config.Agent, botName string, gitKind string, maxRecordsPerPool int, historyURI string, statusURI string, ns string) (keeper.Controller, error) {
gitServer := util.GithubServer
return &gitHubAppKeeperController{
ownerTokenFinder: util.NewOwnerTokensDir(gitServer, githubAppSecretDir),
gitServer: gitServer,
configAgent: configAgent,
botName: botName,
gitKind: gitKind,
maxRecordsPerPool: maxRecordsPerPool,
historyURI: historyURI,
statusURI: statusURI,
ns: ns,
logger: logrus.NewEntry(logrus.StandardLogger()),
}, nil
}
func (g *gitHubAppKeeperController) Sync() error {
// lets iterate through the config and create a controller for each
err := g.createOwnerControllers()
if err != nil {
return err
}
// now lets sync them all
var errs *multierror.Error
for _, c := range g.controllers {
err := c.Sync()
if err != nil {
errs = multierror.Append(errs, err)
}
}
return errs.ErrorOrNil()
}
func (g *gitHubAppKeeperController) Shutdown() {
for _, c := range g.controllers {
c.Shutdown()
}
g.controllers = nil
}
func (g *gitHubAppKeeperController) GetPools() []keeper.Pool {
g.m.Lock()
defer g.m.Unlock()
pools := []keeper.Pool{}
for _, c := range g.controllers {
cp := c.GetPools()
pools = append(pools, cp...)
}
return pools
}
func (g *gitHubAppKeeperController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pools := g.GetPools()
b, err := json.Marshal(pools)
if err != nil {
g.logger.WithError(err).Error("Encoding JSON.")
b = []byte("[]")
}
if _, err = w.Write(b); err != nil {
g.logger.WithError(err).Error("Writing JSON response.")
}
}
func (g *gitHubAppKeeperController) GetHistory() *history.History {
answer, err := history.New(g.maxRecordsPerPool, g.historyURI)
if err != nil {
return answer
}
for _, c := range g.controllers {
h := c.GetHistory()
answer.Merge(h)
}
return answer
}
func (g *gitHubAppKeeperController) createOwnerControllers() error {
// lets zap any old controllers
g.Shutdown()
g.controllers = nil
var errs *multierror.Error
cfg := g.configAgent.Config()
if cfg == nil {
return errors.New("no config")
}
oqs := SplitKeeperQueries(cfg.Keeper.Queries)
for owner, queries := range oqs {
// create copy of config with different queries
ocfg := *cfg
ocfg.Keeper.Queries = queries
configGetter := func() *config.Config {
return &ocfg
}
c, err := g.createOwnerController(owner, configGetter)
if err != nil {
errs = multierror.Append(errs, err)
} else {
g.controllers = append(g.controllers, c)
}
}
return errs.ErrorOrNil()
}
func (g *gitHubAppKeeperController) createOwnerController(owner string, configGetter config.Getter) (keeper.Controller, error) {
token, err := g.ownerTokenFinder.FindToken(owner)
if err != nil {
return nil, errors.Wrapf(err, "failed to find GitHub App token for %s", owner)
}
if token == "" {
return nil, errors.Errorf("no GitHub App token for %s", owner)
}
scmClient, err := createKeeperGitHubAppScmClient(g.gitServer, token)
if err != nil {
return nil, errors.Wrap(err, "cannot create SCM client")
}
util.AddAuthToSCMClient(scmClient, token, true)
gitproviderClient := scmprovider.ToClient(scmClient, g.botName)
gitClient, err := git.NewClient(g.gitServer, g.gitKind)
if err != nil {
return nil, errors.Wrap(err, "creating git client")
}
gitClient.SetCredentials(util.GitHubAppGitRemoteUsername, func() []byte {
return []byte(token)
})
tektonClient, _, lhClient, _, err := clients.GetAPIClients()
if err != nil {
return nil, errors.Wrap(err, "Error creating kubernetes resource clients.")
}
launcherClient := launcher.NewLauncher(lhClient, g.ns)
c, err := keeper.NewController(gitproviderClient, gitproviderClient, nil, launcherClient, tektonClient, lhClient, g.ns, configGetter, gitClient, g.maxRecordsPerPool, g.historyURI, g.statusURI, nil)
return c, err
}
func createKeeperGitHubAppScmClient(gitServer string, token string) (*scm.Client, error) {
botName := os.Getenv("GIT_USER")
client, err := factory.NewClient("github", gitServer, "", factory.SetUsername(botName))
defaultScmTransport(client)
auth := &transport.Authorization{
Base: http.DefaultTransport,
Scheme: "token",
Credentials: token,
}
tr := &transport.Custom{Base: auth,
Before: func(r *http.Request) {
r.Header.Set("Accept", "application/vnd.github.machine-man-preview+json")
},
}
client.Client.Transport = tr
return client, err
}
func defaultScmTransport(scmClient *scm.Client) {
if scmClient.Client == nil {
scmClient.Client = http.DefaultClient
}
if scmClient.Client.Transport == nil {
scmClient.Client.Transport = http.DefaultTransport
}
}