generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (117 loc) · 4.76 KB
/
main.go
File metadata and controls
139 lines (117 loc) · 4.76 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
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
// /*
// Copyright 2025 The Upbound Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// */
// Package main implements a Composition Function.
package main
import (
"io"
"github.com/alecthomas/kong"
"golang.org/x/sync/errgroup"
kruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/function-sdk-go"
"github.com/crossplane/function-sdk-go/errors"
"github.com/upbound/function-analysis-gate/apis/ops"
"github.com/upbound/function-analysis-gate/internal/analysis"
)
func init() {
kruntime.Must(ops.AddToScheme(clientgoscheme.Scheme))
}
const (
name = "function-analysis-gate"
)
type debugFlag bool
// CLI of this Function.
type CLI struct {
Debug debugFlag `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"`
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"`
}
// BeforeApply binds the dev mode logger to the kong context
// when debugFlag is passed.
// This method requires unparam lint exception as Kong expects
// an error value in return from Hook methods but in our case
// there are no error introducing steps.
func (d debugFlag) BeforeApply(ctx *kong.Context) error { //nolint:unparam // BeforeApply requires this signature.
zl := zap.New(zap.UseDevMode(true)).WithName(name)
// BindTo uses reflect.TypeOf to get reflection type of used interface
// A *logging.Logger value here is used to find the reflection type here.
// Please refer: https://golang.org/pkg/reflect/#TypeOf
ctx.BindTo(logging.NewLogrLogger(zl), (*logging.Logger)(nil))
// The controller-runtime runs with a no-op logger by default. It is
// *very* verbose even at info level, so we only provide it a real
// logger when we're running in debug mode.
ctrl.SetLogger(zl)
logging.SetFilteredKlogLogger(zl)
return nil
}
// Run this Function.
func (c *CLI) Run(log logging.Logger) error {
if !c.Debug {
// Setting the controller-runtime logger to a no-op logger by default,
// unless debug mode is enabled. This is because the controller-runtime
// logger is *very* verbose even at info level. This is not really needed,
// but otherwise we get a warning from the controller-runtime.
ctrl.SetLogger(zap.New(zap.WriteTo(io.Discard)))
}
sigCtx := ctrl.SetupSignalHandler()
g, gCtx := errgroup.WithContext(sigCtx)
// Derive config from the associated service account.
cfg := ctrl.GetConfigOrDie()
// Create controller manager for working with Analysis resources.
mgr, err := ctrl.NewManager(cfg, manager.Options{
Metrics: metricsserver.Options{BindAddress: "0"},
})
if err != nil {
return errors.Wrap(err, "failed to create manager")
}
m := analysis.New(mgr.GetClient(), analysis.WithLogger(log))
f := NewFunction(
m,
WithLogger(log),
)
// Start the manager in its own goroutine.
g.Go(func() error {
err := mgr.Start(gCtx)
return errors.Wrap(err, "failed to start controller")
})
// Start function server is a separate goroutine.
g.Go(func() error {
err := function.Serve(f,
function.Listen(c.Network, c.Address),
function.MTLSCertificates(c.TLSCertsDir),
function.Insecure(c.Insecure),
function.MaxRecvMessageSize(c.MaxRecvMessageSize*1024*1024))
return errors.Wrap(err, "failed to start function service")
})
return g.Wait()
}
func main() {
zl := zap.New().WithName(name)
logging.SetFilteredKlogLogger(zl)
ctx := kong.Parse(&CLI{},
kong.Name(name),
kong.Description("The Upbound Analysis Gate Function."),
kong.BindTo(logging.NewLogrLogger(zl), (*logging.Logger)(nil)))
ctx.FatalIfErrorf(ctx.Run())
}