Skip to content

Commit

Permalink
fix: interview revision (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 authored Sep 14, 2024
1 parent 61f7a55 commit 29a52d5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 60 deletions.
76 changes: 41 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
export PATH := $(abspath bin/):${PATH}

# Project variables
PROJECT_NAME = kube-pod-autocomplete
CONTAINER_IMAGE_REF = ghcr.io/csatib02/${PROJECT_NAME}:dev
CONTAINER_IMAGE_REF = ghcr.io/csatib02/$(PROJECT_NAME):dev

# Dependency versions
GOLANGCI_LINT_VERSION = 1.60.3
KIND_VERSION = 0.24.0

# Dependency binaries
BIN_DIR := bin
GOLANGCI_LINT_BIN := golangci-lint
KIND_BIN := kind
HELM_BIN := helm

##@ General

Expand All @@ -17,17 +28,17 @@ help: ## Display this help

.PHONY: up
up: ## Start development environment
${KIND_BIN} create cluster --name ${PROJECT_NAME}
$(BIN_DIR)/$(KIND_BIN) create cluster --name $(PROJECT_NAME)

.PHONY: down
down: ## Stop development environment
${KIND_BIN} delete cluster --name ${PROJECT_NAME}
$(BIN_DIR)/$(KIND_BIN) delete cluster --name $(PROJECT_NAME)

.PHONY: deploy
deploy: container-image ## Deploy kube-pod-autocomplete to the development environment
kind load docker-image ${CONTAINER_IMAGE_REF} --name ${PROJECT_NAME}
kubectl create ns ${PROJECT_NAME}
${HELM_BIN} upgrade --install ${PROJECT_NAME} deploy/charts/${PROJECT_NAME} --namespace ${PROJECT_NAME} --set image.tag=dev
$(BIN_DIR)/$(KIND_BIN) load docker-image $(CONTAINER_IMAGE_REF) --name $(PROJECT_NAME)
kubectl create ns $(PROJECT_NAME)
$(BIN_DIR)/$(HELM_BIN) upgrade --install $(PROJECT_NAME) deploy/charts/$(PROJECT_NAME) --namespace $(PROJECT_NAME) --set image.tag=dev

.PHONY: deploy-testdata
deploy-testdata: ## Deploy testdata to the development environment
Expand All @@ -40,20 +51,19 @@ deploy-testdata: ## Deploy testdata to the development environment
.PHONY: build
build: ## Build binary
@mkdir -p build
go build -race -o build/${PROJECT_NAME} .
go build -race -o build/$(PROJECT_NAME) .

.PHONY: artifacts
artifacts: container-image helm-chart
artifacts: ## Build artifacts
artifacts: container-image helm-chart ## Build artifacts

.PHONY: container-image
container-image: ## Build container image
docker build -t ${CONTAINER_IMAGE_REF} .
docker build -t $(CONTAINER_IMAGE_REF) .

.PHONY: helm-chart
helm-chart: ## Build Helm chart
@mkdir -p build
$(HELM_BIN) package -d build/ deploy/charts/${PROJECT_NAME}
$(if $(CI),$(HELM_BIN),$(BIN_DIR)/$(HELM_BIN)) package -d build/ deploy/charts/$(PROJECT_NAME)

##@ Checks

Expand All @@ -70,47 +80,43 @@ test-e2e: ## Run end-to-end tests

.PHONY: test-e2e-local
test-e2e-local: container-image ## Run e2e tests locally
LOAD_IMAGE=${CONTAINER_IMAGE_REF} VERSION=dev ${MAKE} test-e2e
LOAD_IMAGE=$(CONTAINER_IMAGE_REF) VERSION=dev ${MAKE} test-e2e

.PHONY: lint
lint: lint-go lint-helm ## Run linters

.PHONY: lint-go
lint-go:
$(GOLANGCI_LINT_BIN) run $(if ${CI},--out-format github-actions,)
$(BIN_DIR)/$(GOLANGCI_LINT_BIN) run $(if ${CI},--out-format github-actions,)

.PHONY: lint-helm
lint-helm:
$(HELM_BIN) lint deploy/charts/${PROJECT_NAME}
$(if $(CI),$(HELM_BIN),$(BIN_DIR)/$(HELM_BIN)) lint deploy/charts/$(PROJECT_NAME)

.PHONY: fmt
fmt: ## Format code
$(GOLANGCI_LINT_BIN) run --fix
$(BIN_DIR)/$(GOLANGCI_LINT_BIN) run --fix

##@ Dependencies

deps: bin/golangci-lint bin/kind
deps: bin/golangci-lint-$(GOLANGCI_LINT_VERSION) bin/kind-$(KIND_VERSION) bin/helm
deps: ## Install dependencies
@ln -sf golangci-lint-$(GOLANGCI_LINT_VERSION) $(BIN_DIR)/golangci-lint
@ln -sf kind-$(KIND_VERSION) $(BIN_DIR)/kind

# Dependency versions
GOLANGCI_LINT_VERSION = 1.60.3
KIND_VERSION = 0.24.0

# Dependency binaries
GOLANGCI_LINT_BIN := golangci-lint
KIND_BIN := kind
HELM_BIN := helm

bin/golangci-lint:
@mkdir -p bin
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v${GOLANGCI_LINT_VERSION}
bin/golangci-lint-$(GOLANGCI_LINT_VERSION):
@mkdir -p $(BIN_DIR)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v$(GOLANGCI_LINT_VERSION)
@mv $(BIN_DIR)/golangci-lint $@

bin/kind:
@mkdir -p bin
curl -Lo bin/kind https://kind.sigs.k8s.io/dl/v${KIND_VERSION}/kind-$(shell uname -s | tr '[:upper:]' '[:lower:]')-$(shell uname -m | sed -e "s/aarch64/arm64/; s/x86_64/amd64/")
@chmod +x bin/kind
bin/kind-$(KIND_VERSION):
@mkdir -p $(BIN_DIR)
curl -Lo $(BIN_DIR)/kind https://kind.sigs.k8s.io/dl/v$(KIND_VERSION)/kind-$(shell uname -s | tr '[:upper:]' '[:lower:]')-$(shell uname -m | sed -e "s/aarch64/arm64/; s/x86_64/amd64/")
@mv $(BIN_DIR)/kind $@
@chmod +x $@

bin/helm:
@mkdir -p bin
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | USE_SUDO=false HELM_INSTALL_DIR=bin bash
@chmod +x bin/helm
@mkdir -p $(BIN_DIR)
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | USE_SUDO=false HELM_INSTALL_DIR=$(BIN_DIR) bash
@mv $(BIN_DIR)/helm $@
@chmod +x $@
17 changes: 10 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ const (
)

type Config struct {
ListenAddress string `mapstructure:"listen_address"`
TrustedProxies []string `mapstructure:"trusted_proxies"`
Mode string `mapstructure:"mode"`
LogLevel string `mapstructure:"log_level"`
JSONLog bool `mapstructure:"json_log"`
LogServerAddress string `mapstructure:"log_server"`
ListenAddress string `mapstructure:"listen_address"`
TrustedProxies []string `mapstructure:"trusted_proxies"`
Mode string `mapstructure:"mode"`
LogLevel string `mapstructure:"log_level"`
JSONLog bool `mapstructure:"json_log"`
LogServerAddress string `mapstructure:"log_server_address"`
LogServerTransport string `mapstructure:"log_server_transport"`
}

func LoadConfig() (*Config, error) {
Expand All @@ -47,7 +48,9 @@ func LoadConfig() (*Config, error) {

_ = v.BindEnv("json_log")

_ = v.BindEnv("log_server")
_ = v.BindEnv("log_server_address")

_ = v.BindEnv("log_server_transport")

config := &Config{}
if err := v.Unmarshal(config); err != nil {
Expand Down
26 changes: 14 additions & 12 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ func TestLoadConfig(t *testing.T) {
{
name: "Custom values",
envVars: map[string]string{
"KPA_LISTEN_ADDRESS": "127.0.0.1:9090",
"KPA_TRUSTED_PROXIES": "192.168.1.1,192.168.1.2",
"KPA_MODE": gin.ReleaseMode,
"KPA_LOG_LEVEL": "debug",
"KPA_JSON_LOG": "true",
"KPA_LOG_SERVER": "logserver.local",
"KPA_LISTEN_ADDRESS": "127.0.0.1:9090",
"KPA_TRUSTED_PROXIES": "192.168.1.1,192.168.1.2",
"KPA_MODE": gin.ReleaseMode,
"KPA_LOG_LEVEL": "debug",
"KPA_JSON_LOG": "true",
"KPA_LOG_SERVER_ADDRESS": "logserver.local",
"KPA_LOG_SERVER_TRANSPORT": "tcp",
},
wantConfig: &Config{
ListenAddress: "127.0.0.1:9090",
TrustedProxies: []string{"192.168.1.1", "192.168.1.2"},
Mode: gin.ReleaseMode,
LogLevel: "debug",
JSONLog: true,
LogServerAddress: "logserver.local",
ListenAddress: "127.0.0.1:9090",
TrustedProxies: []string{"192.168.1.1", "192.168.1.2"},
Mode: gin.ReleaseMode,
LogLevel: "debug",
JSONLog: true,
LogServerAddress: "logserver.local",
LogServerTransport: "tcp",
},
wantError: nil,
},
Expand Down
8 changes: 4 additions & 4 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func NewClient() (*Client, error) {
return &Client{clientset: clientset}, nil
}

func (c *Client) ListResource(ctx context.Context, resource common.Resources) (common.Resources, error) {
switch resource.(type) {
case common.ResourceType:
func (c *Client) ListResource(ctx context.Context, resource common.ResourceType) (common.Resources, error) {
switch resource {
case common.PodResourceType:
return c.listPods(ctx)
default:
return nil, fmt.Errorf("unsupported resource type: %T", resource)
return nil, fmt.Errorf("unsupported resource type: %s", resource)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New(config *config.Config) (*Server, error) {
gin.SetMode(config.Mode)

if config.LogServerAddress != "" {
writer, err := net.Dial("udp", config.LogServerAddress)
writer, err := net.Dial(config.LogServerTransport, config.LogServerAddress)
if err != nil {
return nil, fmt.Errorf("failed to connect to log server: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func InitLogger(config *config.Config) {
}

if config.LogServerAddress != "" {
writer, err := net.Dial("udp", config.LogServerAddress)
writer, err := net.Dial(config.LogServerTransport, config.LogServerAddress)
if err != nil {
slog.Error(fmt.Errorf("failed to connect to log server: %w", err).Error())
os.Exit(1)
Expand Down

0 comments on commit 29a52d5

Please sign in to comment.