diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c58c252..45508e0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/.golangci.yml b/.golangci.yml index 8992db4..8bc35d9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,7 +8,6 @@ linters: disable-all: true enable: - bodyclose - - depguard - errcheck - dupl - exhaustive diff --git a/Makefile b/Makefile index 2774a45..5b6acee 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -18,4 +18,7 @@ test: go test ./... -bench . compose: - docker compose up --wait --build --force-recreate --remove-orphans \ No newline at end of file + docker compose up --wait --build --force-recreate --remove-orphans + +docker-build: + docker build --progress=plain -t docker.io/trendyoltech/dcp . \ No newline at end of file diff --git a/helpers/utils_test.go b/helpers/utils_test.go index 867d680..62f340f 100644 --- a/helpers/utils_test.go +++ b/helpers/utils_test.go @@ -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) { @@ -24,7 +26,7 @@ func TestIsMetadata_ReturnsTrue_WhenKeyHasTxnPrefix(t *testing.T) { } testData := ts{ - Key: []byte(TxnPrefix + "test"), + Key: []byte(TxnPrefix + key), } if !IsMetadata(testData) { @@ -38,7 +40,7 @@ func TestIsMetadata_ReturnsFalse_WhenKeyHasNoPrefix(t *testing.T) { } testData := ts{ - Key: []byte("test"), + Key: []byte(key), } if IsMetadata(testData) { diff --git a/kubernetes/client.go b/kubernetes/client.go index abec44d..818718b 100644 --- a/kubernetes/client.go +++ b/kubernetes/client.go @@ -2,6 +2,7 @@ package kubernetes import ( "context" + "errors" "os" "strings" "time" @@ -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(), } }