-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to start FLP directly from the flow logs producer (#538)
* Allow to start FLP directly from the flow logs producer - New "InProcess" ingest stage - New entry point to start the whole FLP in-process: pipeline.StartFLPInProcess - Add some tests * Add builder functions ToConfig / IntoConfig * Allow disabling operational metrics * Add log when prom disabled * InProcess ingest now takes in channel as argument, directly with the GenericMap
- Loading branch information
Showing
12 changed files
with
282 additions
and
108 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ingest | ||
|
||
import ( | ||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/utils" | ||
) | ||
|
||
// InProcess ingester is meant to be imported and used from another program | ||
// via pipeline.StartFLPInProcess | ||
type InProcess struct { | ||
in chan config.GenericMap | ||
} | ||
|
||
func NewInProcess(in chan config.GenericMap) *InProcess { | ||
return &InProcess{in: in} | ||
} | ||
|
||
func (d *InProcess) Ingest(out chan<- config.GenericMap) { | ||
go func() { | ||
<-utils.ExitChannel() | ||
d.Close() | ||
}() | ||
for rec := range d.in { | ||
out <- rec | ||
} | ||
} | ||
|
||
func (d *InProcess) Close() { | ||
} |
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,32 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/ingest" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/prometheus" | ||
) | ||
|
||
// StartFLPInProcess is an entry point to start the whole FLP / pipeline processing from imported code | ||
func StartFLPInProcess(cfg *config.ConfigFileStruct, in chan config.GenericMap) error { | ||
promServer := prometheus.InitializePrometheus(&cfg.MetricsSettings) | ||
|
||
// Create new flows pipeline | ||
ingester := ingest.NewInProcess(in) | ||
flp, err := newPipelineFromIngester(cfg, ingester) | ||
if err != nil { | ||
return fmt.Errorf("failed to initialize pipeline %w", err) | ||
} | ||
|
||
// Starts the flows pipeline; blocking call | ||
go func() { | ||
flp.Run() | ||
if promServer != nil { | ||
_ = promServer.Shutdown(context.Background()) | ||
} | ||
}() | ||
|
||
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,55 @@ | ||
package pipeline | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/netobserv/flowlogs-pipeline/pkg/api" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInProcessFLP(t *testing.T) { | ||
pipeline := config.NewPresetIngesterPipeline() | ||
pipeline = pipeline.WriteStdout("writer", api.WriteStdout{Format: "json"}) | ||
in := make(chan config.GenericMap, 100) | ||
defer close(in) | ||
err := StartFLPInProcess(pipeline.ToConfigFileStruct(), in) | ||
require.NoError(t, err) | ||
|
||
capturedOut, w, _ := os.Pipe() | ||
old := os.Stdout | ||
os.Stdout = w | ||
defer func() { | ||
os.Stdout = old | ||
}() | ||
|
||
// yield thread to allow pipe services correctly start | ||
time.Sleep(10 * time.Millisecond) | ||
|
||
in <- config.GenericMap{ | ||
"SrcAddr": "1.2.3.4", | ||
"DstAddr": "5.6.7.8", | ||
"Dscp": float64(1), | ||
"DstMac": "11:22:33:44:55:66", | ||
"SrcMac": "01:02:03:04:05:06", | ||
} | ||
|
||
scanner := bufio.NewScanner(capturedOut) | ||
require.True(t, scanner.Scan()) | ||
capturedRecord := map[string]interface{}{} | ||
bytes := scanner.Bytes() | ||
require.NoError(t, json.Unmarshal(bytes, &capturedRecord), string(bytes)) | ||
|
||
assert.EqualValues(t, map[string]interface{}{ | ||
"SrcAddr": "1.2.3.4", | ||
"DstAddr": "5.6.7.8", | ||
"Dscp": float64(1), | ||
"DstMac": "11:22:33:44:55:66", | ||
"SrcMac": "01:02:03:04:05:06", | ||
}, capturedRecord) | ||
} |
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.