-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial implementation of serializers and forwarders (#34)
- Loading branch information
Showing
26 changed files
with
523 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ linters: | |
- godot | ||
- gofmt | ||
- goimports | ||
- golint | ||
- revive | ||
- gomnd | ||
- gosec | ||
- govet | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package forwarders | ||
|
||
type discardForwarder struct{} | ||
|
||
// NewDiscardForwarder creates a new discardForwarder which discards all received | ||
// data. | ||
func NewDiscardForwarder() *discardForwarder { | ||
return &discardForwarder{} | ||
} | ||
|
||
func (df *discardForwarder) Name() string { | ||
return "DiscardForwarder" | ||
} | ||
|
||
func (df *discardForwarder) Forward(payload []byte) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package forwarders | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
type logForwarder struct { | ||
log logr.Logger | ||
} | ||
|
||
// NewLogForwarder creates new logForwarded which uses the provided logger to | ||
// print all the received data. | ||
func NewLogForwarder(log logr.Logger) *logForwarder { | ||
return &logForwarder{ | ||
log: log, | ||
} | ||
} | ||
|
||
func (lf *logForwarder) Name() string { | ||
return "LogForwarder" | ||
} | ||
|
||
func (lf *logForwarder) Forward(payload []byte) error { | ||
lf.log.Info("got a report", "report", payload) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package forwarders | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
defaultTimeout = time.Second * 30 | ||
defaultDeadline = time.Minute | ||
) | ||
|
||
var tlsConf = tls.Config{ | ||
MinVersion: tls.VersionTLS13, | ||
MaxVersion: tls.VersionTLS13, | ||
} | ||
|
||
// TODO: Address logging levels and library to be used. | ||
// See: https://github.com/Kong/kubernetes-ingress-controller/issues/1893 | ||
const ( | ||
logrusrDiff = 4 | ||
|
||
// InfoLevel is the converted logging level from logrus to go-logr for | ||
// information level logging. Note that the logrusr middleware technically | ||
// flattens all levels prior to this level into this level as well. | ||
InfoLevel = int(logrus.InfoLevel) - logrusrDiff | ||
|
||
// DebugLevel is the converted logging level from logrus to go-logr for | ||
// debug level logging. | ||
DebugLevel = int(logrus.DebugLevel) - logrusrDiff | ||
|
||
// WarnLevel is the converted logrus level to go-logr for warnings. | ||
WarnLevel = int(logrus.WarnLevel) - logrusrDiff | ||
) | ||
|
||
type tlsForwarder struct { | ||
log logr.Logger | ||
conn *tls.Conn | ||
} | ||
|
||
// NewTLSForwarder creates a TLS forwarder which forwards received serialized reports | ||
// to a TLS endpoint specified by the provided address. | ||
func NewTLSForwarder(address string, log logr.Logger) *tlsForwarder { | ||
conn, err := tls.DialWithDialer( | ||
&net.Dialer{ | ||
Timeout: defaultTimeout, | ||
}, | ||
"tcp", | ||
address, | ||
&tlsConf, | ||
) | ||
if err != nil { | ||
log.V(DebugLevel).Info("failed to connect to reporting server", "error", err) | ||
return nil | ||
} | ||
|
||
return &tlsForwarder{ | ||
log: log, | ||
conn: conn, | ||
} | ||
} | ||
|
||
func (tf *tlsForwarder) Name() string { | ||
return "TLSForwarder" | ||
} | ||
|
||
func (tf *tlsForwarder) Forward(payload []byte) error { | ||
err := tf.conn.SetDeadline(time.Now().Add(defaultDeadline)) | ||
if err != nil { | ||
return fmt.Errorf("failed to set report connection deadline: %w", err) | ||
} | ||
|
||
_, err = tf.conn.Write(payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to send report: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.