diff --git a/Makefile b/Makefile index 449d082..25440ea 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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 @@ -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 $@ diff --git a/internal/config/config.go b/internal/config/config.go index c7b3718..e1512cb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,7 +20,8 @@ type Config struct { Mode string `mapstructure:"mode"` LogLevel string `mapstructure:"log_level"` JSONLog bool `mapstructure:"json_log"` - LogServerAddress string `mapstructure:"log_server"` + LogServerAddress string `mapstructure:"log_server_address"` + LogServerNetwork string `mapstructure:"log_server_network"` } func LoadConfig() (*Config, error) { @@ -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_network") config := &Config{} if err := v.Unmarshal(config); err != nil { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 35ce29f..5dc3798 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -31,12 +31,12 @@ 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", }, wantConfig: &Config{ ListenAddress: "127.0.0.1:9090", diff --git a/internal/k8s/client.go b/internal/k8s/client.go index ca29ae1..3183df4 100644 --- a/internal/k8s/client.go +++ b/internal/k8s/client.go @@ -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) } } diff --git a/internal/server/server.go b/internal/server/server.go index 0a87ad4..36e09e7 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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.LogServerNetwork, config.LogServerAddress) if err != nil { return nil, fmt.Errorf("failed to connect to log server: %w", err) } diff --git a/pkg/log/logger.go b/pkg/log/logger.go index e12ee08..9bdc154 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -56,7 +56,7 @@ func InitLogger(config *config.Config) { } if config.LogServerAddress != "" { - writer, err := net.Dial("udp", config.LogServerAddress) + writer, err := net.Dial(config.LogServerNetwork, config.LogServerAddress) if err != nil { slog.Error(fmt.Errorf("failed to connect to log server: %w", err).Error()) os.Exit(1)