Skip to content

Commit

Permalink
Preparation for Otterize Cloud - Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
orishoshan committed Jan 15, 2023
2 parents 475004b + a75c841 commit 1025f5c
Show file tree
Hide file tree
Showing 37 changed files with 1,273 additions and 200 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
push:
branches:
- main
- develop

env:
REGISTRY: 353146681200.dkr.ecr.us-east-1.amazonaws.com/otterize
Expand All @@ -31,6 +32,7 @@ jobs:
uses: actions/checkout@v2
with:
submodules: recursive
token: ${{ secrets.OTTERIZEBOT_GITHUB_TOKEN }} # required for checking out submodules

- name: Set up Docker Buildx
id: buildx
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '>=1.19.1'
- uses: actions/checkout@v3
- name: Install dependencies
run: sudo apt update && sudo apt install libpcap-dev # required for the linter to be able to lint github.com/google/gopacket
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.50.1

# Optional: working directory, useful for monorepos
working-directory: src

# Optional: golangci-lint command line arguments.
args: --timeout 5m

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/cloudgraphql"]
path = src/cloudgraphql
url = https://github.com/otterize/graphql
1 change: 1 addition & 0 deletions src/cloudgraphql
Submodule cloudgraphql added at 4d044b
2 changes: 1 addition & 1 deletion src/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/mapper.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/amd64 golang:1.18-alpine as buildenv
FROM --platform=linux/amd64 golang:1.19-alpine as buildenv
RUN apk add --no-cache ca-certificates git protoc
RUN apk add build-base libpcap-dev
WORKDIR /src
Expand Down
15 changes: 15 additions & 0 deletions src/mapper/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/otterize/intents-operator/src/shared/serviceidresolver"
"github.com/otterize/network-mapper/src/mapper/pkg/cloudclient"
"github.com/otterize/network-mapper/src/mapper/pkg/clouduploader"
"github.com/otterize/network-mapper/src/mapper/pkg/config"
"github.com/otterize/network-mapper/src/mapper/pkg/kubefinder"
"github.com/otterize/network-mapper/src/mapper/pkg/resolvers"
Expand All @@ -13,9 +15,11 @@ import (
"github.com/spf13/viper"
"net/http"
"os"
"os/signal"
clientconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"syscall"
"time"
)

Expand Down Expand Up @@ -79,11 +83,22 @@ func main() {
logrus.Error(intentHolderCfg)
os.Exit(1)
}

cloudConfig := clouduploader.ConfigFromViper()
intentsHolder := resolvers.NewIntentsHolder(mgr.GetClient(), intentHolderCfg)
cloudClient := clouduploader.NewCloudUploader(intentsHolder, cloudConfig, cloudclient.NewClient)
resolver := resolvers.NewResolver(kubeFinder, serviceidresolver.NewResolver(mgr.GetClient()), intentsHolder)
_ = resolver.LoadStore(initCtx) // loads the store from the previous run
resolver.Register(e)

if cloudConfig.IsCloudUploadEnabled() {
go func() {
cloudClientCtx, cloudClientCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cloudClientCancel()
cloudClient.PeriodicIntentsUpload(cloudClientCtx)
}()
}

logrus.Info("Starting api server")
err = e.Start("0.0.0.0:9090")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/mapper/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Where are all the schema files located? globs are supported eg src/**/*.graphqls
schema:
- '../graphql/*'
- '../mappergraphql/*.graphql'

# Where should the generated server code go?
exec:
Expand Down
52 changes: 52 additions & 0 deletions src/mapper/pkg/cloudclient/cloud_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cloudclient

import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)

type FactoryFunction func(ctx context.Context, apiAddress string, tokenSource oauth2.TokenSource) CloudClient

type CloudClient interface {
ReportDiscoveredIntents(intents []*DiscoveredIntentInput) bool
ReportComponentStatus(component ComponentType)
}

type CloudClientImpl struct {
ctx context.Context
client graphql.Client
}

func NewClient(ctx context.Context, apiAddress string, tokenSource oauth2.TokenSource) CloudClient {
url := fmt.Sprintf("%s/graphql/v1", apiAddress)
client := graphql.NewClient(url, oauth2.NewClient(ctx, tokenSource))

return &CloudClientImpl{
client: client,
ctx: ctx,
}
}

func (c *CloudClientImpl) ReportDiscoveredIntents(intents []*DiscoveredIntentInput) bool {
logrus.Info("Uploading intents to cloud, count: ", len(intents))

_, err := ReportDiscoveredIntents(c.ctx, c.client, intents)
if err != nil {
logrus.Error("Failed to upload intents to cloud ", err)
return false
}

return true
}

func (c *CloudClientImpl) ReportComponentStatus(component ComponentType) {
logrus.Info("Uploading component to cloud")

_, err := ReportComponentStatus(c.ctx, c.client, component)
if err != nil {
logrus.Error("Failed to upload component to cloud ", err)
}
}
4 changes: 4 additions & 0 deletions src/mapper/pkg/cloudclient/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cloudclient

//go:generate go run github.com/Khan/genqlient ./genqlient.yaml
//go:generate go run github.com/golang/mock/[email protected] -destination=./mocks/mocks.go -package=cloudclientmocks -source=./cloud_client.go CloudClient
Loading

0 comments on commit 1025f5c

Please sign in to comment.