Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (53 loc) · 2.22 KB
/
Copy pathmain.go
File metadata and controls
60 lines (53 loc) · 2.22 KB
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
// Package main implements a Composition Function.
package main
import (
"fmt"
"os"
"github.com/alecthomas/kong"
"github.com/crossplane/function-sdk-go"
)
// CLI of this Function.
type CLI struct {
Debug bool `short:"d" help:"Emit debug logs in addition to info logs."`
Network string `help:"Network on which to listen for gRPC connections." default:"tcp"`
Address string `help:"Address at which to listen for gRPC connections." default:":9443"`
TLSCertsDir string `help:"Directory containing server certs (tls.key, tls.crt) and the CA used to verify client certificates (ca.crt)" env:"TLS_SERVER_CERTS_DIR"`
Dependencies string `help:"File containing dependencies to add to all functions."`
Insecure bool `help:"Run without mTLS credentials. If you supply this flag --tls-server-certs-dir will be ignored."`
MaxRecvMessageSize int `help:"Maximum size of received messages in MB." default:"4"`
}
// Run this Function.
func (c *CLI) Run() error {
dependencies := ""
if c.Dependencies != "" {
if bytes, err := os.ReadFile(c.Dependencies); err != nil {
return fmt.Errorf("reading %q: %w", c.Dependencies, err)
} else {
dependencies = string(bytes)
}
}
log, err := function.NewLogger(c.Debug)
if err != nil {
return err
}
// Watchdog that recycles the process before the KCL native memory leak can
// OOMKill it mid-reconcile. Configured via FUNCTION_KCL_MAX_* env vars;
// no-op when no trigger is enabled.
rec := newRecycler(log, recycleConfigFromEnv())
rec.run()
// Optional render cache: memoise KCL output for byte-identical reconciles to
// skip recompilation (CPU + leak). Enabled via FUNCTION_KCL_RENDER_CACHE_SIZE.
cache := newRenderCacheFromEnv()
if cache.enabled() {
log.Info("render cache enabled", "maxEntries", cache.max, "ttl", cache.ttl.String())
}
return function.Serve(&Function{dependencies: dependencies, log: log, recycler: rec, cache: cache},
function.Listen(c.Network, c.Address),
function.MTLSCertificates(c.TLSCertsDir),
function.Insecure(c.Insecure),
function.MaxRecvMessageSize(c.MaxRecvMessageSize*1024*1024))
}
func main() {
ctx := kong.Parse(&CLI{}, kong.Description("A Crossplane Composition Function using KCL."))
ctx.FatalIfErrorf(ctx.Run())
}