forked from hd-Li/application
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
156 lines (138 loc) · 4.21 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
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
package main
import (
"context"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
typesconfig "github.com/hd-Li/types/config"
//"github.com/rancher/norman/leader"
"github.com/rancher/norman/store/crd"
"github.com/rancher/norman/store/proxy"
"github.com/snowzach/rotatefilehook"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
//"github.com/rancher/norman/types"
"github.com/hd-Li/application/controller"
projectschema "github.com/hd-Li/types/apis/project.cattle.io/v3/schema"
projectclient "github.com/hd-Li/types/client/project/v3"
colorable "github.com/mattn/go-colorable"
)
var (
kubeConfig string = "./config142"
)
func init() {
if os.Getenv("REDIS_SERVER") == "" || os.Getenv("AUTHN_ENDPOINT") == "" || os.Getenv("AUTHN_REALM") == "" || os.Getenv("PROXYIMAGE") == "" {
log.Fatalf("Please check env settings (%s %s %s %s)", "REDIS_SERVER", "AUTHN_ENDPOINT", "AUTHN_REALM", "PROXYIMAGE")
}
loglevel := os.Getenv("LOG_LEVEL")
var logLevel log.Level
log.Infof("loglevel env is %s", loglevel)
if loglevel == "debug" {
log.SetLevel(log.DebugLevel)
logLevel = log.DebugLevel
log.Infof("log level is %s", loglevel)
log.SetReportCaller(true)
} else {
log.SetLevel(log.InfoLevel)
logLevel = log.InfoLevel
log.Infoln("log level is normal")
}
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: "logs/console.log",
MaxSize: 50, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Level: logLevel,
Formatter: &log.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
},
})
log.SetOutput(colorable.NewColorableStdout())
if err != nil {
log.Fatalf("Failed to initialize file rotate hook: %v", err)
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
ForceColors: true,
})
log.SetReportCaller(true)
log.AddHook(rotateFileHook)
//todo print log with code line
//log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
// 初始化kubeconfig
restConfig, err := rest.InClusterConfig()
if err != nil {
restConfig, err = clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
log.Fatalf("Get restconfig failed: %s", err.Error())
os.Exit(1)
}
}
ctx := SigTermCancelContext(context.Background())
// 配置userContext 初始化各类资源调用client
userContext, err := typesconfig.NewUserOnlyContext(*restConfig)
if err != nil {
log.Fatalf("create userContext failed, err: %s", err.Error())
os.Exit(1)
}
// 连接apiserver 创建Application CRD资源
err = SetupApplicationCRD(ctx, userContext, *restConfig)
if err != nil {
log.Fatalf("create application crd failed, err: %s ", err.Error())
os.Exit(1)
}
// 注册userContext
controller.Register(ctx, userContext)
// 启动控制器
err = userContext.Start(ctx)
if err != nil {
panic(err)
}
<-ctx.Done()
/*go leader.RunOrDie(ctx, "", "application-controller", userContext.K8sClient, func(ctx context.Context) {
err = SetupApplicationCRD(ctx, userContext, *restConfig)
if err != nil {
log.Fatalf("create application crd failed, err: %s ", err.Error())
os.Exit(1)
}
controller.Register(ctx, userContext)
err = userContext.Start(ctx)
if err != nil {
panic(err)
}
<-ctx.Done()
})
<-ctx.Done()*/
}
// SetupApplicationCRD use for init application crd
func SetupApplicationCRD(ctx context.Context, apiContext *typesconfig.UserOnlyContext, config rest.Config) error {
//schemas := types.Schemas{}
applicationschema := apiContext.Schemas.Schema(&projectschema.Version, projectclient.ApplicationType)
//schemas.AddSchema(applicationschema)
clientGetter, err := proxy.NewClientGetterFromConfig(config)
if err != nil {
log.Fatalf("create clientGetter error: %s", err.Error())
return err
}
factory := &crd.Factory{ClientGetter: clientGetter}
_, err = factory.CreateCRDs(ctx, typesconfig.UserStorageContext, applicationschema)
return err
}
// SigTermCancelContext use for kill process
func SigTermCancelContext(ctx context.Context) context.Context {
term := make(chan os.Signal)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(ctx)
go func() {
select {
case <-term:
cancel()
case <-ctx.Done():
}
}()
return ctx
}