This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
65 lines (56 loc) · 1.83 KB
/
services.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
package services
import (
_ "embed"
"fmt"
"github.com/acorn-io/assistant-runtime/pkg/scheme"
"github.com/acorn-io/baaah/pkg/randomtoken"
"github.com/acorn-io/baaah/pkg/ratelimit"
"github.com/acorn-io/baaah/pkg/restconfig"
"github.com/acorn-io/mink/pkg/db"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/client-go/rest"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)
type Config struct {
HTTPListenPort int `usage:"HTTP port to listen on" default:"8080"`
HTTPSListenPort int `usage:"HTTPS port to listen on"`
AdminToken string `usage:"Token for admin access, will be generated if not passed"`
AuditLogPath string `usage:"Location of where to store audit logs"`
AuditLogPolicyFile string `usage:"Location of audit log policy file"`
DSN string `usage:"Database dsn in driver://connection_string format" default:"sqlite://file:assistant.db?_journal=WAL&cache=shared&_busy_timeout=30000"`
}
func New(config Config) (_ *Services, err error) {
if config.AdminToken == "" {
config.AdminToken, err = randomtoken.Generate()
if err != nil {
return nil, err
}
}
downstreamConfig := restconfig.SetScheme(&rest.Config{
Host: fmt.Sprintf("http://127.0.0.1:%d", config.HTTPListenPort),
//BearerToken: config.AdminToken,
RateLimiter: ratelimit.None,
}, scheme.Scheme)
downstreamClient, err := kclient.NewWithWatch(downstreamConfig, kclient.Options{
Scheme: scheme.Scheme,
})
if err != nil {
return nil, err
}
dbClient, err := db.NewFactory(scheme.Scheme, config.DSN)
if err != nil {
return nil, err
}
services := &Services{
Client: downstreamClient,
RESTConfig: downstreamConfig,
DB: dbClient,
}
return services, nil
}
type Services struct {
Client kclient.Client
RESTConfig *rest.Config
DB *db.Factory
Authn authenticator.Request
}