forked from manifoldco/kubernetes-credentials
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
100 lines (80 loc) · 2.44 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/manifoldco/go-manifold"
"github.com/manifoldco/go-manifold/integrations"
"github.com/manifoldco/kubernetes-credentials/controller"
"github.com/manifoldco/kubernetes-credentials/crd"
"github.com/manifoldco/kubernetes-credentials/crd/projects"
"github.com/manifoldco/kubernetes-credentials/crd/resources"
"github.com/manifoldco/kubernetes-credentials/primitives"
)
func main() {
log.Printf("Starting the controller...")
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
cfg, err := rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
kc, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatal(err)
}
cs, err := clientset.NewForConfig(cfg)
if err != nil {
log.Fatal(err)
}
if err := crd.CreateCRD(cs, primitives.CRDProjectsName, primitives.CRDProjectsPlural, primitives.CRDGroup, primitives.CRDVersion); err != nil {
log.Fatal(err)
}
if err := crd.CreateCRD(cs, primitives.CRDResourcesName, primitives.CRDResourcesPlural, primitives.CRDGroup, primitives.CRDVersion); err != nil {
log.Fatal(err)
}
rc, err := newClient(cfg)
if err != nil {
log.Fatal(err)
}
ptr := func(str string) *string {
return &str
}
manifoldClient := manifold.New(
manifold.WithAPIToken(os.Getenv("MANIFOLD_API_TOKEN")),
manifold.WithUserAgent(fmt.Sprintf("kubernetes-%s", Version)),
)
wrapper, err := integrations.NewClient(manifoldClient, ptr(os.Getenv("MANIFOLD_TEAM")))
if err != nil {
log.Fatal(err)
}
ctrl := controller.New(kc, rc, wrapper)
go ctrl.Run(ctx)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Printf("Shutting down...")
}
func newClient(cfg *rest.Config) (*rest.RESTClient, error) {
scheme := runtime.NewScheme()
if err := projects.AddToScheme(scheme); err != nil {
return nil, err
}
if err := resources.AddToScheme(scheme); err != nil {
return nil, err
}
config := *cfg
config.GroupVersion = &crd.SchemeGroupVersion
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(scheme)}
return rest.RESTClientFor(&config)
}