This repository has been archived by the owner on May 23, 2023. It is now read-only.
generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (123 loc) · 3.45 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
package main
import (
"context"
"flag"
"log"
"net"
"os"
rt "runtime"
"time"
sgv1 "github.com/vshn/appcat-comp-functions/apis/stackgres/v1"
vp "github.com/vshn/appcat-comp-functions/functions/vshn-postgres-func"
"github.com/vshn/appcat-comp-functions/runtime"
pb "github.com/crossplane/crossplane/apis/apiextensions/fn/proto/v1alpha1"
"github.com/go-logr/logr"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var AI = runtime.AppInfo{
Version: "unknown",
Commit: "-dirty-",
Date: time.Now().Format("2006-01-02"),
AppName: "functionio-vshn",
AppLongName: "A crossplane composition function gRPC server",
}
var postgresFunctions = []runtime.Transform{
{
Name: "url-connection-detail",
TransformFunc: vp.AddUrlToConnectionDetails,
},
{
Name: "user-alerting",
TransformFunc: vp.AddUserAlerting,
},
{
Name: "restart",
TransformFunc: vp.TransformRestart,
},
{
Name: "random-default-schedule",
TransformFunc: vp.TransformSchedule,
},
{
Name: "encrypted-pvc-secret",
TransformFunc: vp.AddPvcSecret,
},
}
var (
Network = "unix"
AddressFlag = "@crossplane/fn/default.sock"
LogLevel = 1
)
type server struct {
pb.UnimplementedContainerizedFunctionRunnerServiceServer
logger logr.Logger
}
func (s *server) RunFunction(ctx context.Context, in *pb.RunFunctionRequest) (*pb.RunFunctionResponse, error) {
ctx = logr.NewContext(ctx, s.logger)
switch in.Image {
case "postgresql":
fnio, err := runtime.RunCommand(ctx, in.Input, postgresFunctions)
if err != nil {
return &pb.RunFunctionResponse{
Output: fnio,
}, status.Errorf(codes.Aborted, "Can't process request for PostgreSQL")
}
return &pb.RunFunctionResponse{
Output: fnio,
}, nil
case "redis":
return &pb.RunFunctionResponse{
// return what was sent as it's currently not supported
Output: in.Input,
}, status.Error(codes.Unimplemented, "Redis is not yet implemented")
default:
return &pb.RunFunctionResponse{
Output: []byte("Bad configuration"),
}, status.Error(codes.NotFound, "Unknown request")
}
}
func main() {
flag.StringVar(&AddressFlag, "socket", "@crossplane/fn/default.sock", "optional -> set where socket should be located")
flag.IntVar(&LogLevel, "loglevel", 0, "optional -> set log level [0,1]")
flag.Parse()
logger, err := runtime.NewZapLogger(AI.AppName, AI.Version, LogLevel, true)
if err != nil {
log.Fatal("logging broke, exiting")
}
logger.WithValues(
"version", AI.Version,
"date", AI.Date,
"go_os", rt.GOOS,
"go_arch", rt.GOARCH,
"go_version", rt.Version(),
"uid", os.Getuid(),
"gid", os.Getgid(),
).Info("Starting up " + AI.AppName)
if err := cleanStart(AddressFlag); err != nil {
log.Fatal(err)
}
lis, err := net.Listen(Network, AddressFlag)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
err = runtime.AddToScheme(sgv1.SchemeBuilder.SchemeBuilder)
if err != nil {
log.Fatalf("failed register stackgres CRDs: %v", err)
}
s := grpc.NewServer()
pb.RegisterContainerizedFunctionRunnerServiceServer(s, &server{logger: logger})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
// socket isn't removed after server stop listening and blocks another starts
func cleanStart(socketName string) error {
if _, err := os.Stat(socketName); err == nil {
err := os.RemoveAll(socketName)
return err
}
return nil
}