Skip to content

Commit

Permalink
feat: add explicit port routing & go tools bump (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
nandor-magyar committed Sep 6, 2023
1 parent a014619 commit 88e6fbf
Show file tree
Hide file tree
Showing 53 changed files with 598 additions and 445 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/builder_image_golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ permissions:
env:
GITHUB_REGISTRY: ghcr.io
BUILDER_IMAGE_NAME: dyrector-io/dyrectorio/builder-images/golang
VERSION: 1
VERSION: 2
jobs:
build:
runs-on: ubuntu-22.04
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/product_builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
runs-on: ubuntu-22.04
container:
# yamlfmt resides here because alpine doesn't provide yamlfmt package
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
runs-on: ubuntu-22.04
needs: gather_changes
container:
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
defaults:
run:
working-directory: ${{ env.GOLANG_WORKING_DIRECTORY }}
Expand All @@ -139,7 +139,7 @@ jobs:
runs-on: ubuntu-22.04
needs: gather_changes
container:
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
defaults:
run:
working-directory: ${{ env.GOLANG_WORKING_DIRECTORY }}
Expand All @@ -163,7 +163,7 @@ jobs:
runs-on: ubuntu-22.04
needs: gather_changes
container:
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
defaults:
run:
working-directory: ${{ env.GOLANG_WORKING_DIRECTORY }}
Expand All @@ -189,7 +189,7 @@ jobs:
runs-on: ubuntu-22.04
needs: gather_changes
container:
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
defaults:
run:
working-directory: ${{ env.GOLANG_WORKING_DIRECTORY }}
Expand Down Expand Up @@ -220,7 +220,7 @@ jobs:
- go_integration
- gather_changes
container:
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
defaults:
run:
working-directory: ${{ env.GOLANG_WORKING_DIRECTORY }}
Expand Down Expand Up @@ -615,7 +615,7 @@ jobs:
packages: write
runs-on: ubuntu-22.04
container:
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:1
image: ghcr.io/dyrector-io/dyrectorio/builder-images/golang:2
defaults:
run:
working-directory: ${{ env.GOLANG_WORKING_DIRECTORY }}
Expand Down
14 changes: 6 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ run:
- internal/cache
linters-settings:
depguard:
list-type: blacklist
packages:
# logging is allowed zerolog only
# is allowed to use only package
- log
packages-with-error-message:
- log: "logging is allowed only by github.com/rs/zerolog"
rules:
logblock:
deny:
- pkg: "log"
desc: "logging is allowed only by github.com/rs/zerolog"
dupl:
threshold: 100
forbidigo:
exclude_godoc_examples: false
exclude-godoc-examples: false
forbid:
- ^print.*$
- 'fmt\.Print.*'
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/iancoleman/strcase v0.3.0
github.com/imdario/mergo v0.3.15 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -127,6 +128,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ilyakaznacheev/cleanenv v1.4.2 h1:nRqiriLMAC7tz7GzjzUTBHfzdzw6SQ7XvTagkFqe/zU=
github.com/ilyakaznacheev/cleanenv v1.4.2/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
Expand Down
8 changes: 4 additions & 4 deletions golang/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ LDFLAGS := -ldflags "-X '${PACKAGE}/internal/version.BuildTimestamp=${BUILD_TIME
# default tag is latest for building
image_version ?= latest

GOAIR=v1.42.0
GOSEC=v2.15.0
GOLANGCI=v1.51.2
GOFUMPT=v0.4.0
GOAIR=v1.45.0
GOSEC=v2.17.0
GOLANGCI=v1.54.2
GOFUMPT=v0.5.0
YAMLFMT=v0.9.0

# support for: linux darwin windows
Expand Down
4 changes: 3 additions & 1 deletion golang/api/v1/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ type ContainerConfig struct {
IngressPath string `json:"ingressPath"`
// ingress path for path based routing
IngressStripPath bool `json:"ingressPathStrip"`
// ingress port to target
IngressPort uint16 `json:"ingressPort"`
// for docker hosts, this is needs to be bytes: 1000000 ~1m
IngressUploadLimit string `json:"ingressUploadLimit"`
// if put together with another instances consume their shared configs eg. -common config map, generated from here
Expand Down Expand Up @@ -208,7 +210,7 @@ type ContainerConfig struct {

// k8s-only-section
// Deployments strategy, on deployment how to restart underlying pods
// Values: Recreate (all-at-once), Rolling(one-by-one only if succeeds)
// Values: Recreate (all-at-once), RollingUpdate(one-by-one only if succeeds)
DeploymentStrategy string `json:"deploymentStrategy"`
// health check configuration
HealthCheckConfig HealthCheckConfig `json:"healthCheck"`
Expand Down
4 changes: 2 additions & 2 deletions golang/cmd/crane/crane.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func loadConfiguration() (*config.Configuration, *k8s.Secret) {
return cfg, secretHandler
}

func serve(cCtx *cli.Context) error {
func serve(_ *cli.Context) error {
cfg, secretHandler := loadConfiguration()

crane.Serve(cfg, secretHandler)
Expand Down Expand Up @@ -98,7 +98,7 @@ func initKey(cCtx *cli.Context) error {
return nil
}

func getHealth(cCtx *cli.Context) error {
func getHealth(_ *cli.Context) error {
healthy, err := health.GetHealthy()
if err != nil {
log.Error().Err(err).Send()
Expand Down
1 change: 0 additions & 1 deletion golang/cmd/dagent/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ DEFAULT_TAG=latest
DEFAULT_TIMEOUT=5s
GRPC_KEEPALIVE=60s
HOST_DOCKER_SOCK_PATH=/var/run/docker.sock
HOST_MOUNT_PATH=/srv/dagent
INTERNAL_MOUNT_PATH=/srv/dagent
LOG_DEFAULT_SKIP=0
LOG_DEFAULT_TAKE=100
Expand Down
4 changes: 2 additions & 2 deletions golang/cmd/dagent/dagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
cli "github.com/urfave/cli/v2"
)

func serve(cCtx *cli.Context) error {
func serve(_ *cli.Context) error {
cfg := config.Configuration{}

err := util.ReadConfig(&cfg)
Expand All @@ -39,7 +39,7 @@ func serve(cCtx *cli.Context) error {
return nil
}

func getHealth(cCtx *cli.Context) error {
func getHealth(_ *cli.Context) error {
healthy, err := health.GetHealthy()
if err != nil {
log.Error().Err(err).Send()
Expand Down
10 changes: 6 additions & 4 deletions golang/internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ type ContainerWatchContext struct {
Error chan error
}

// UpdateOptions options to increase update testability
type UpdateOptions struct {
UpdateAlways bool
// always execute the update, regardless of the actual version
UpdateAlways bool
// if false will not restart by itself, only a message printed about the token events
UseContainers bool
}

Expand Down Expand Up @@ -312,10 +315,9 @@ func (cl *ClientLoop) grpcLoop(connParams *ConnectionParams) {
time.Sleep(time.Second)
grpcConn.Client = nil
continue
} else {
log.Info().Msg("Stream connection is up")
health.SetHealthGRPCStatus(true)
}
log.Info().Msg("Stream connection is up")
health.SetHealthGRPCStatus(true)
}

command := new(agent.AgentCommand)
Expand Down
7 changes: 6 additions & 1 deletion golang/internal/mapper/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/iancoleman/strcase"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -126,6 +127,7 @@ func mapContainerConfig(in *agent.DeployRequest) v1.ContainerConfig {
containerConfig.IngressUploadLimit = pointer.GetString(cc.Routing.UploadLimit)
containerConfig.IngressPath = pointer.GetString(cc.Routing.Path)
containerConfig.IngressStripPath = pointer.GetBool(cc.Routing.StripPath)
containerConfig.IngressPort = uint16(pointer.GetUint32(cc.Routing.Port))
}

if cc.ConfigContainer != nil {
Expand Down Expand Up @@ -169,7 +171,7 @@ func mapDagentConfig(dagent *agent.DagentContainerConfig, containerConfig *v1.Co
}

func mapCraneConfig(crane *agent.CraneContainerConfig, containerConfig *v1.ContainerConfig) {
containerConfig.DeploymentStrategy = crane.DeploymentStatregy.String()
containerConfig.DeploymentStrategy = strcase.ToCamel(crane.DeploymentStrategy.String())

if crane.ProxyHeaders != nil {
containerConfig.ProxyHeaders = *crane.ProxyHeaders
Expand Down Expand Up @@ -390,6 +392,9 @@ func mapVolumeLinks(in []*agent.VolumeLink) []v1.VolumeLink {
}

func MapContainerState(it *dockerTypes.Container, prefix string) *common.ContainerStateItem {
if it == nil {
return nil
}
name := ""
if len(it.Names) > 0 {
name = strings.TrimPrefix(it.Names[0], "/")
Expand Down
59 changes: 44 additions & 15 deletions golang/internal/mapper/grpc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build unit
// +build unit

package mapper_test
package mapper

import (
"testing"
Expand All @@ -11,7 +11,6 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/dyrector-io/dyrectorio/golang/internal/config"
"github.com/dyrector-io/dyrectorio/golang/internal/helper/image"
"github.com/dyrector-io/dyrectorio/golang/internal/mapper"
builder "github.com/dyrector-io/dyrectorio/golang/pkg/builder/container"
"github.com/dyrector-io/dyrectorio/protobuf/go/agent"
"github.com/dyrector-io/dyrectorio/protobuf/go/common"
Expand All @@ -25,7 +24,7 @@ func TestMapDeployImageRequest(t *testing.T) {
req := testDeployRequest()
cfg := testAppConfig()

res := mapper.MapDeployImage(req, cfg)
res := MapDeployImage(req, cfg)
expected := testExpectedCommon(req)

assert.Equal(t, expected, res)
Expand Down Expand Up @@ -64,6 +63,7 @@ func testExpectedCommon(req *agent.DeployRequest) *v1.DeployImageRequest {
ExposeTLS: true,
IngressHost: "test-domain",
IngressName: "",
IngressPort: 12345,
IngressUploadLimit: "5Mi",
Shared: false,
ConfigContainer: &v1.ConfigContainer{
Expand Down Expand Up @@ -106,7 +106,7 @@ func testExpectedCommon(req *agent.DeployRequest) *v1.DeployImageRequest {
Service: map[string]string{"annot2": "value2"},
Ingress: map[string]string{"annot3": "value3"},
},
DeploymentStrategy: "RECREATE",
DeploymentStrategy: "Recreate",
Labels: v1.Markers{
Deployment: map[string]string{"label1": "value1"},
Service: map[string]string{"label2": "value2"},
Expand Down Expand Up @@ -157,14 +157,14 @@ func TestMapPorts(t *testing.T) {
},
}

bindings := mapper.MapPorts(ps)
bindings := MapPorts(ps)
assert.Equal(t, expected, bindings)
}

func TestMapSecrets(t *testing.T) {
kvl := testKeyValueList()

m := mapper.MapSecrets(kvl)
m := MapSecrets(kvl)
expected := map[string]string{
"testKey-1": "testID-1",
"testKey-2": "testID-2",
Expand All @@ -174,13 +174,13 @@ func TestMapSecrets(t *testing.T) {
}

func TestMapDockerContainerEventToContainerState(t *testing.T) {
assert.Equal(t, common.ContainerState_WAITING, mapper.MapDockerContainerEventToContainerState("create"))
assert.Equal(t, common.ContainerState_REMOVED, mapper.MapDockerContainerEventToContainerState("destroy"))
assert.Equal(t, common.ContainerState_WAITING, mapper.MapDockerContainerEventToContainerState("pause"))
assert.Equal(t, common.ContainerState_RUNNING, mapper.MapDockerContainerEventToContainerState("restart"))
assert.Equal(t, common.ContainerState_RUNNING, mapper.MapDockerContainerEventToContainerState("start"))
assert.Equal(t, common.ContainerState_EXITED, mapper.MapDockerContainerEventToContainerState("stop"))
assert.Equal(t, common.ContainerState_EXITED, mapper.MapDockerContainerEventToContainerState("die"))
assert.Equal(t, common.ContainerState_WAITING, MapDockerContainerEventToContainerState("create"))
assert.Equal(t, common.ContainerState_REMOVED, MapDockerContainerEventToContainerState("destroy"))
assert.Equal(t, common.ContainerState_WAITING, MapDockerContainerEventToContainerState("pause"))
assert.Equal(t, common.ContainerState_RUNNING, MapDockerContainerEventToContainerState("restart"))
assert.Equal(t, common.ContainerState_RUNNING, MapDockerContainerEventToContainerState("start"))
assert.Equal(t, common.ContainerState_EXITED, MapDockerContainerEventToContainerState("stop"))
assert.Equal(t, common.ContainerState_EXITED, MapDockerContainerEventToContainerState("die"))
}

func testDeployRequest() *agent.DeployRequest {
Expand Down Expand Up @@ -227,6 +227,7 @@ func testDeployRequest() *agent.DeployRequest {
Routing: &common.Routing{
Domain: pointer.ToString("test-domain"),
UploadLimit: &upLimit,
Port: pointer.ToUint32(12345),
},
ConfigContainer: &common.ConfigContainer{
Image: "test-image",
Expand Down Expand Up @@ -354,7 +355,7 @@ func testCraneConfig() *agent.CraneContainerConfig {
ReadinessProbe: &rProbe,
StartupProbe: &sProbe,
},
DeploymentStatregy: common.DeploymentStrategy_RECREATE.Enum(),
DeploymentStrategy: common.DeploymentStrategy_RECREATE.Enum(),
}
}

Expand Down Expand Up @@ -480,10 +481,38 @@ func TestMapDeployImageLogConfig(t *testing.T) {
req := testDeployRequestWithLogDriver(tC.driver)
cfg := testAppConfig()

res := mapper.MapDeployImage(req, cfg)
res := MapDeployImage(req, cfg)
expected := testExpectedCommonWithLogConfigType(req, tC.want)

assert.Equal(t, expected, res)
})
}
}

func TestCraneDeploymentStrategyMapping(t *testing.T) {
craneConfig := testCraneConfig()
testCases := []struct {
strat *common.DeploymentStrategy
exp string
desc string
}{
{
desc: "expected Recreate",
exp: "Recreate",
strat: common.DeploymentStrategy_RECREATE.Enum(),
},
{
desc: "expected RollingUpdate",
exp: "RollingUpdate",
strat: common.DeploymentStrategy_ROLLING_UPDATE.Enum(),
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
craneConfig.DeploymentStrategy = tC.strat
resultConfig := v1.ContainerConfig{}
mapCraneConfig(craneConfig, &resultConfig)
assert.Equalf(t, tC.exp, resultConfig.DeploymentStrategy, "%s got: %s", tC.desc, resultConfig.DeploymentStrategy)
})
}
}
Loading

0 comments on commit 88e6fbf

Please sign in to comment.