Skip to content

Commit

Permalink
feat: try to read podIP from env
Browse files Browse the repository at this point in the history
  • Loading branch information
erayarslan committed Nov 15, 2023
1 parent d8dcc3f commit e2e124c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.50
version: v1.55
args: -c .golangci.yml --timeout=5m -v

- name: Install dependencies
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ linters:
disable-all: true
enable:
- bodyclose
- depguard
- errcheck
- dupl
- exhaustive
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ default: init

init:
go mod download
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@v0.8.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@v0.15.0

clean:
rm -rf ./build
Expand All @@ -18,4 +18,7 @@ test:
go test ./... -bench .

compose:
docker compose up --wait --build --force-recreate --remove-orphans
docker compose up --wait --build --force-recreate --remove-orphans

docker-build:
docker build --progress=plain -t docker.io/trendyoltech/dcp .
8 changes: 5 additions & 3 deletions helpers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"testing"
)

const key = "test"

func TestIsMetadata_ReturnsTrue_WhenStructHasKeyPrefix(t *testing.T) {
type ts struct {
Key []byte
}

testData := ts{
Key: []byte(Prefix + "test"),
Key: []byte(Prefix + key),
}

if !IsMetadata(testData) {
Expand All @@ -24,7 +26,7 @@ func TestIsMetadata_ReturnsTrue_WhenKeyHasTxnPrefix(t *testing.T) {
}

testData := ts{
Key: []byte(TxnPrefix + "test"),
Key: []byte(TxnPrefix + key),
}

if !IsMetadata(testData) {
Expand All @@ -38,7 +40,7 @@ func TestIsMetadata_ReturnsFalse_WhenKeyHasNoPrefix(t *testing.T) {
}

testData := ts{
Key: []byte("test"),
Key: []byte(key),
}

if IsMetadata(testData) {
Expand Down
48 changes: 34 additions & 14 deletions kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"context"
"errors"
"os"
"strings"
"time"
Expand Down Expand Up @@ -84,27 +85,46 @@ func (le *client) setIdentity() {
}

var podIP string
for {
pod, err := le.clientSet.CoreV1().Pods(le.namespace).Get(context.Background(), hostname, metaV1.GetOptions{})
if err != nil {
logger.Log.Error("error while getting pod: %v", err)
panic(err)
}

if pod.Status.PodIP != "" {
podIP = pod.Status.PodIP
break
if podIPFromEnv := os.Getenv("POD_IP"); podIPFromEnv != "" {
podIP = podIPFromEnv
} else {
var tries int

for {
pod, err := le.clientSet.CoreV1().Pods(le.namespace).Get(
context.Background(),
hostname,
metaV1.GetOptions{},
)
if err != nil {
logger.Log.Error("error while getting pod: %v", err)
panic(err)
}

if pod.Status.PodIP != "" {
podIP = pod.Status.PodIP
break
}

tries++

if tries > 10 {
err := errors.New("after 10 tries, pod ip is still empty")
logger.Log.Error("failed to get pod ip: %v", err)
panic(err)
} else {
logger.Log.Debug("pod ip is empty, waiting...")
}

time.Sleep(time.Second)
}

time.Sleep(1 * time.Second)
}

now := time.Now().UnixNano()

le.myIdentity = &models.Identity{
IP: podIP,
Name: hostname,
ClusterJoinTime: now,
ClusterJoinTime: time.Now().UnixNano(),
}
}

Expand Down

0 comments on commit e2e124c

Please sign in to comment.