-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,069 additions
and
430 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/flowlogs-pipeline | ||
/confgenerator | ||
/k8s-cache | ||
/bin/ | ||
cover.out |
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,78 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/netobserv/flowlogs-pipeline/pkg/api" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/kubernetes" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/utils" | ||
"github.com/sirupsen/logrus" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var ( | ||
buildVersion = "unknown" | ||
buildDate = "unknown" | ||
app = "flp-cache" | ||
configPath = flag.String("config", "", "path to a config file") | ||
versionFlag = flag.Bool("v", false, "print version") | ||
log = logrus.WithField("module", "main") | ||
) | ||
|
||
type Config struct { | ||
KubeConfigPath string `yaml:"kubeConfigPath"` | ||
KafkaConfig api.EncodeKafka `yaml:"kafkaConfig"` | ||
PProfPort int32 `yaml:"pprofPort"` // TODO: manage pprof | ||
LogLevel string `yaml:"logLevel"` | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
appVersion := fmt.Sprintf("%s [build version: %s, build date: %s]", app, buildVersion, buildDate) | ||
if *versionFlag { | ||
fmt.Println(appVersion) | ||
os.Exit(0) | ||
} | ||
|
||
cfg, err := readConfig(*configPath) | ||
if err != nil { | ||
log.WithError(err).Fatal("error reading config file") | ||
} | ||
|
||
lvl, err := logrus.ParseLevel(cfg.LogLevel) | ||
if err != nil { | ||
log.Errorf("Log level %s not recognized, using info", cfg.LogLevel) | ||
lvl = logrus.InfoLevel | ||
} | ||
logrus.SetLevel(lvl) | ||
log.Infof("Starting %s at log level %s", appVersion, lvl) | ||
log.Infof("Configuration: %#v", cfg) | ||
|
||
err = kubernetes.InitInformerDatasource(cfg.KubeConfigPath, &cfg.KafkaConfig) | ||
if err != nil { | ||
log.WithError(err).Fatal("error initializing Kubernetes & informers") | ||
} | ||
|
||
stopCh := utils.SetupElegantExit() | ||
<-stopCh | ||
} | ||
|
||
func readConfig(path string) (*Config, error) { | ||
var cfg Config | ||
if len(path) == 0 { | ||
return &cfg, nil | ||
} | ||
yamlFile, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = yaml.Unmarshal(yamlFile, &cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cfg, err | ||
} |
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,72 @@ | ||
/* | ||
* Copyright (C) 2021 IBM, Inc. | ||
* | ||
* 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 | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/kubernetes" | ||
) | ||
|
||
func TestTheMain(t *testing.T) { | ||
if os.Getenv("BE_CRASHER") == "1" { | ||
main() | ||
return | ||
} | ||
cmd := exec.Command(os.Args[0], "-test.run=TestTheMain") | ||
cmd.Env = append(os.Environ(), "BE_CRASHER=1") | ||
err := cmd.Run() | ||
var castErr *exec.ExitError | ||
if errors.As(err, &castErr) && !castErr.Success() { | ||
return | ||
} | ||
t.Fatalf("process ran with err %v, want exit status 1", err) | ||
} | ||
|
||
func TestPipelineConfigSetup(t *testing.T) { | ||
// Kube init mock | ||
kubernetes.MockInformers() | ||
|
||
js := `{ | ||
"PipeLine": "[{\"name\":\"grpc\"},{\"follows\":\"grpc\",\"name\":\"enrich\"},{\"follows\":\"enrich\",\"name\":\"loki\"},{\"follows\":\"enrich\",\"name\":\"prometheus\"}]", | ||
"Parameters": "[{\"ingest\":{\"grpc\":{\"port\":2055},\"type\":\"grpc\"},\"name\":\"grpc\"},{\"name\":\"enrich\",\"transform\":{\"network\":{\"rules\":[{\"kubernetes\":{\"input\":\"SrcAddr\",\"output\":\"SrcK8S\"},\"type\":\"add_kubernetes\"},{\"kubernetes\":{\"input\":\"DstAddr\",\"output\":\"DstK8S\"},\"type\":\"add_kubernetes\"},{\"add_service\":{\"input\":\"DstPort\",\"output\":\"Service\",\"protocol\":\"Proto\"},\"type\":\"add_service\"},{\"add_subnet\":{\"input\":\"SrcAddr\",\"output\":\"SrcSubnet\",\"subnet_mask\":\"/16\"},\"type\":\"add_subnet\"}]},\"type\":\"network\"}},{\"name\":\"loki\",\"write\":{\"loki\":{\"batchSize\":102400,\"batchWait\":\"1s\",\"clientConfig\":{\"follow_redirects\":false,\"proxy_url\":null,\"tls_config\":{\"insecure_skip_verify\":false}},\"labels\":[\"SrcK8S_Namespace\",\"SrcK8S_OwnerName\",\"DstK8S_Namespace\",\"DstK8S_OwnerName\",\"FlowDirection\"],\"maxBackoff\":\"5m0s\",\"maxRetries\":10,\"minBackoff\":\"1s\",\"staticLabels\":{\"app\":\"netobserv-flowcollector\"},\"tenantID\":\"netobserv\",\"timeout\":\"10s\",\"timestampLabel\":\"TimeFlowEndMs\",\"timestampScale\":\"1ms\",\"url\":\"http://loki.netobserv.svc:3100/\"},\"type\":\"loki\"}},{\"encode\":{\"prom\":{\"metrics\":[{\"buckets\":null,\"labels\":[\"Service\",\"SrcK8S_Namespace\"],\"name\":\"bandwidth_per_network_service_per_namespace\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"labels\":[\"SrcSubnet\"],\"name\":\"bandwidth_per_source_subnet\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"labels\":[\"Service\"],\"name\":\"network_service_total\",\"type\":\"counter\",\"valueKey\":\"\"}],\"prefix\":\"netobserv_\"},\"type\":\"prom\"},\"name\":\"prometheus\"}]", | ||
"Health": { | ||
"Port": "8080" | ||
}, | ||
"Profile": { | ||
"Port": 0 | ||
} | ||
}` | ||
var opts config.Options | ||
err := json.Unmarshal([]byte(js), &opts) | ||
require.NoError(t, err) | ||
cfg, err := config.ParseConfig(&opts) | ||
require.NoError(t, err) | ||
require.NotNil(t, cfg) | ||
mainPipeline, err := pipeline.NewPipeline(&cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, mainPipeline) | ||
} |
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 @@ | ||
# We do not use --platform feature to auto fill this ARG because of incompatibility between podman and docker | ||
ARG TARGETPLATFORM=linux/amd64 | ||
ARG BUILDPLATFORM=linux/amd64 | ||
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22 as builder | ||
|
||
ARG TARGETPLATFORM | ||
ARG TARGETARCH=amd64 | ||
WORKDIR /app | ||
|
||
# Copy source code | ||
COPY go.mod . | ||
COPY go.sum . | ||
COPY Makefile . | ||
COPY .mk/ .mk/ | ||
COPY .bingo/ .bingo/ | ||
COPY vendor/ vendor/ | ||
COPY .git/ .git/ | ||
COPY cmd/ cmd/ | ||
COPY pkg/ pkg/ | ||
|
||
RUN git status --porcelain | ||
RUN GOARCH=$TARGETARCH make build_k8s_cache | ||
|
||
# final stage | ||
FROM --platform=$TARGETPLATFORM registry.access.redhat.com/ubi9/ubi-minimal:9.4 | ||
|
||
COPY --from=builder /app/k8s-cache /app/ | ||
|
||
ENTRYPOINT ["/app/k8s-cache"] |
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.