-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
59 lines (45 loc) · 1.5 KB
/
server.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
"github.com/x95castle1/convention-server-framework/pkg/handler"
"github.com/x95castle1/multi-purpose-convention-server/pkg/conventions"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/vmware-tanzu/cartographer-conventions/webhook"
"go.uber.org/zap"
)
const (
logComponentKey = "component"
logComponentName = "multi-purpose-convention"
)
func main() {
ctx := context.Background()
annotationPrefix := os.Getenv("ANNOTATION_PREFIX")
port := os.Getenv("PORT")
if port == "" {
port = "9000"
}
// Setup logger
config := zap.NewProductionConfig()
config.EncoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
l, err := config.Build()
if err != nil {
os.Exit(1)
}
logger := l.Sugar().With(logComponentKey, logComponentName)
ctx = logr.NewContext(ctx, zapr.NewLogger(l))
logger.Info("Convention server starting on port: ", port, " with prefix: ", annotationPrefix)
// Setting an Anonymous Function to call the handler.AddConventions
c := func(template *corev1.PodTemplateSpec, images []webhook.ImageConfig) ([]string, error) {
return handler.AddConventions(logger, template, images, conventions.Conventions)
}
// Create a listener on the / root to call. This is the real HTTPServer.
// This is why you have port defined above.
http.HandleFunc("/", webhook.ConventionHandler(ctx, c))
logger.Fatal(webhook.NewConventionServer(ctx, fmt.Sprintf(":%s", port)))
}