forked from k3s-io/k3s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
70 lines (60 loc) · 2.04 KB
/
context.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
package server
import (
"context"
"github.com/rancher/helm-controller/pkg/generated/controllers/helm.cattle.io"
"github.com/rancher/k3s/pkg/generated/controllers/k3s.cattle.io"
"github.com/rancher/wrangler-api/pkg/generated/controllers/apps"
"github.com/rancher/wrangler-api/pkg/generated/controllers/batch"
"github.com/rancher/wrangler-api/pkg/generated/controllers/core"
"github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/crd"
"github.com/rancher/wrangler/pkg/start"
"k8s.io/client-go/rest"
"k8s.io/kubernetes/staging/src/k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/clientcmd"
)
type Context struct {
K3s *k3s.Factory
Helm *helm.Factory
Batch *batch.Factory
Apps *apps.Factory
Auth *rbac.Factory
Core *core.Factory
K8s kubernetes.Interface
Apply apply.Apply
}
func (c *Context) Start(ctx context.Context) error {
return start.All(ctx, 5, c.K3s, c.Helm, c.Apps, c.Auth, c.Batch, c.Core)
}
func newContext(ctx context.Context, cfg string) (*Context, error) {
restConfig, err := clientcmd.BuildConfigFromFlags("", cfg)
if err != nil {
return nil, err
}
if err := crds(ctx, restConfig); err != nil {
return nil, err
}
k8s := kubernetes.NewForConfigOrDie(restConfig)
return &Context{
K3s: k3s.NewFactoryFromConfigOrDie(restConfig),
Helm: helm.NewFactoryFromConfigOrDie(restConfig),
K8s: k8s,
Auth: rbac.NewFactoryFromConfigOrDie(restConfig),
Apps: apps.NewFactoryFromConfigOrDie(restConfig),
Batch: batch.NewFactoryFromConfigOrDie(restConfig),
Core: core.NewFactoryFromConfigOrDie(restConfig),
Apply: apply.New(k8s, apply.NewClientFactory(restConfig)),
}, nil
}
func crds(ctx context.Context, config *rest.Config) error {
factory, err := crd.NewFactoryFromClient(config)
if err != nil {
return err
}
factory.BatchCreateCRDs(ctx, crd.NamespacedTypes(
"ListenerConfig.k3s.cattle.io/v1",
"Addon.k3s.cattle.io/v1",
"HelmChart.helm.cattle.io/v1")...)
return factory.BatchWait()
}