Skip to content

Commit c7ee4e1

Browse files
Merge pull request #18 from Tech-Preta/copilot/fix-c6eafeec-7290-41c0-9a60-89272a15e9ba
Fix PR #17 review comments - refactor os.Exit usage, add context propagation, and improve tooling
2 parents df30867 + 61d15a3 commit c7ee4e1

8 files changed

Lines changed: 104 additions & 62 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/vscode/devcontainers/base:ubuntu-22.04
1+
FROM mcr.microsoft.com/vscode/devcontainers/base:1.0-ubuntu-22.04
22

33
ENV DEBIAN_FRONTEND=noninteractive
44

.devcontainer/post-create.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,20 @@ install_project_deps() {
9191
echo "Compilando o projeto..."
9292
make build || {
9393
echo "Falha no build com Makefile, tentando build direto..."
94+
mkdir -p bin
9495
go build -o bin/kubeprobes ./cmd/kubeprobes
9596
}
9697

9798
echo "Executando testes..."
98-
go test -v ./... || { echo "Tests failed"; exit 1; }
99+
if ! go test -v ./...; then
100+
echo "ERRO: Testes falharam durante a configuração do ambiente."
101+
return 1
102+
fi
99103

100104
echo "Projeto Go configurado com sucesso!"
101105
else
102106
echo "go.mod não encontrado no diretório do projeto"
107+
return 1
103108
fi
104109
}
105110

Dockerfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build stage
2-
FROM cgr.dev/chainguard/go:latest AS builder
2+
FROM cgr.dev/chainguard/go:1.24 AS builder
33

44
WORKDIR /app
55

@@ -19,14 +19,18 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o /app/kubeprobes ./cmd/kubeprobes
1919
# Final stage
2020
FROM cgr.dev/chainguard/static:latest
2121

22+
# Create a non-root user
23+
# Note: chainguard/static already includes a non-root user 'nonroot' with UID 65532
24+
USER 65532:65532
25+
2226
# Copy the binary from builder
2327
COPY --from=builder /app/kubeprobes /usr/local/bin/kubeprobes
2428

2529
# Set the entrypoint
2630
ENTRYPOINT ["kubeprobes"]
2731

28-
# Adiciona HEALTHCHECK inline usando exec form
29-
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 CMD ["sh", "-c", "kubeprobes --help > /dev/null 2>&1 || exit 1"]
32+
# Simple healthcheck using the binary itself
33+
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 CMD ["kubeprobes", "--help"]
3034

3135
# Default command
3236
CMD ["--help"]

Makefile

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
# Makefile for kubeprobes
22

3+
# Default target
4+
.PHONY: all
5+
all: build
6+
7+
# Variables
38
BINARY_NAME=kubeprobes
49
BUILD_DIR=./bin
510
CMD_DIR=./cmd/kubeprobes
11+
INSTALL_DIR?=/usr/local/bin
12+
ARGS?=
613

7-
# Build for current platform
14+
# Build the binary for current platform
815
.PHONY: build
916
build:
1017
@echo "Building $(BINARY_NAME)..."
1118
@mkdir -p $(BUILD_DIR)
1219
@go build -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_DIR)
1320

14-
# Build for multiple platforms
21+
# Build for multiple platforms
1522
.PHONY: build-all
1623
build-all:
1724
@echo "Building for multiple platforms..."
@@ -21,18 +28,30 @@ build-all:
2128
@GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(CMD_DIR)
2229
@GOOS=windows GOARCH=amd64 go build -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(CMD_DIR)
2330

24-
# Run tests
25-
.PHONY: test
26-
test:
27-
@echo "Running tests..."
28-
@go test -v ./...
31+
# Install the binary to system
32+
.PHONY: install
33+
install: build
34+
@echo "Installing $(BINARY_NAME) to $(INSTALL_DIR)..."
35+
@sudo cp $(BUILD_DIR)/$(BINARY_NAME) $(INSTALL_DIR)/$(BINARY_NAME)
36+
37+
# Run the application with optional arguments
38+
# Usage: make run ARGS="scan --help"
39+
.PHONY: run
40+
run:
41+
@go run $(CMD_DIR) $(ARGS)
2942

3043
# Clean build artifacts
3144
.PHONY: clean
3245
clean:
3346
@echo "Cleaning..."
3447
@rm -rf $(BUILD_DIR)
3548

49+
# Run tests
50+
.PHONY: test
51+
test:
52+
@echo "Running tests..."
53+
@go test -v ./...
54+
3655
# Format code
3756
.PHONY: fmt
3857
fmt:
@@ -43,37 +62,31 @@ fmt:
4362
.PHONY: lint
4463
lint:
4564
@echo "Linting code..."
46-
@golangci-lint run
65+
@go vet ./...
4766

48-
# Install dependencies
67+
# Download dependencies
4968
.PHONY: deps
5069
deps:
5170
@echo "Installing dependencies..."
5271
@go mod download
5372
@go mod tidy
5473

55-
# Install binary to system
56-
.PHONY: install
57-
install: build
58-
@echo "Installing $(BINARY_NAME) to /usr/local/bin..."
59-
@sudo cp $(BUILD_DIR)/$(BINARY_NAME) /usr/local/bin/
60-
61-
# Run the application
62-
.PHONY: run
63-
run:
64-
@go run $(CMD_DIR) $(ARGS)
65-
66-
# Show help
74+
# Help target
6775
.PHONY: help
6876
help:
6977
@echo "Available targets:"
78+
@echo " all - Default target, builds the binary"
7079
@echo " build - Build the binary for current platform"
7180
@echo " build-all - Build binaries for multiple platforms"
72-
@echo " test - Run tests"
81+
@echo " install - Install binary to $(INSTALL_DIR) (configurable with INSTALL_DIR)"
82+
@echo " run - Run the application (use ARGS to pass parameters)"
7383
@echo " clean - Clean build artifacts"
84+
@echo " test - Run tests"
7485
@echo " fmt - Format code"
7586
@echo " lint - Lint code"
76-
@echo " deps - Install dependencies"
77-
@echo " install - Install binary to system"
78-
@echo " run - Run the application (use ARGS=... for arguments)"
79-
@echo " help - Show this help message"
87+
@echo " deps - Download and tidy dependencies"
88+
@echo " help - Show this help"
89+
@echo ""
90+
@echo "Examples:"
91+
@echo " make run ARGS=\"scan --help\""
92+
@echo " make install INSTALL_DIR=/opt/bin"

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,3 @@ sequenceDiagram
130130
end
131131
end
132132
```
133-
134-
## Requisitos
135-
136-
- Go 1.13 ou superior
137-
- kubectl
138-
- Um cluster Kubernetes acessível
139-
140-
## Contribuições
141-
142-
Contribuições são bem-vindas! Sinta-se à vontade para abrir um issue ou enviar um pull request.

cmd/kubeprobes/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
"kubeprobes/internal/cli"
8+
"kubeprobes/internal/scanner"
9+
)
10+
11+
func main() {
12+
if err := cli.Execute(); err != nil {
13+
// Check if it's our custom error indicating probe issues found
14+
if _, ok := err.(*scanner.ProbeIssuesFoundError); ok {
15+
os.Exit(1)
16+
}
17+
log.Fatalf("Error executing command: %s", err.Error())
18+
}
19+
}

internal/scanner/cmd.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package scanner
22

33
import (
4-
"log"
4+
"context"
5+
"fmt"
56

67
"github.com/spf13/cobra"
78
)
@@ -17,40 +18,44 @@ Exit codes:
1718
0: Nenhum problema encontrado
1819
1: Problemas de probe encontrados
1920
`,
20-
Run: func(cmd *cobra.Command, args []string) {
21+
RunE: func(cmd *cobra.Command, args []string) error {
2122
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
2223
if err != nil {
23-
log.Fatalf("Error getting kubeconfig flag: %s", err.Error())
24+
return fmt.Errorf("error getting kubeconfig flag: %w", err)
2425
}
2526

2627
kubeContext, err := cmd.Flags().GetString("kubeContext")
2728
if err != nil {
28-
log.Fatalf("Error getting kubeContext flag: %s", err.Error())
29+
return fmt.Errorf("error getting kubeContext flag: %w", err)
2930
}
3031

3132
namespace, err := cmd.Flags().GetString("namespace")
3233
if err != nil {
33-
log.Fatalf("Error getting namespace flag: %s", err.Error())
34+
return fmt.Errorf("error getting namespace flag: %w", err)
3435
}
3536

3637
probeType, err := cmd.Flags().GetString("probe-type")
3738
if err != nil {
38-
log.Fatalf("Error getting probe-type flag: %s", err.Error())
39+
return fmt.Errorf("error getting probe-type flag: %w", err)
3940
}
4041

4142
recommendation, err := cmd.Flags().GetBool("recommendation")
4243
if err != nil {
43-
log.Fatalf("Error getting recommendation flag: %s", err.Error())
44+
return fmt.Errorf("error getting recommendation flag: %w", err)
45+
}
46+
47+
// Use context with timeout instead of context.TODO()
48+
ctx := cmd.Context()
49+
if ctx == nil {
50+
ctx = context.Background()
4451
}
4552

4653
scanner, err := NewProbeScanner(kubeconfig, kubeContext, namespace, probeType, recommendation)
4754
if err != nil {
48-
log.Fatalf("Error creating scanner: %s", err.Error())
55+
return fmt.Errorf("error creating scanner: %w", err)
4956
}
5057

51-
if err := scanner.Scan(); err != nil {
52-
log.Fatalf("Error during scan: %s", err.Error())
53-
}
58+
return scanner.Scan(ctx)
5459
},
5560
}
5661

internal/scanner/scanner.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package scanner
22

33
import (
4+
"context"
45
"fmt"
5-
"os"
66
"strings"
77

88
"kubeprobes/pkg/kubernetes"
9-
corev1 "k8s.io/api/core/v1"
109
)
1110

1211
var validProbeTypes = map[string]bool{
@@ -16,6 +15,15 @@ var validProbeTypes = map[string]bool{
1615
"": true, // empty string means all types
1716
}
1817

18+
// ProbeIssuesFoundError indicates that probe issues were found during scanning
19+
type ProbeIssuesFoundError struct {
20+
Message string
21+
}
22+
23+
func (e *ProbeIssuesFoundError) Error() string {
24+
return e.Message
25+
}
26+
1927
// ProbeScanner handles the scanning logic
2028
type ProbeScanner struct {
2129
kubeClient *kubernetes.Client
@@ -49,8 +57,8 @@ func NewProbeScanner(kubeconfig, kubeContext, namespace, probeType string, recom
4957
}
5058

5159
// Scan performs the probe scanning
52-
func (ps *ProbeScanner) Scan() error {
53-
pods, err := ps.kubeClient.GetPods(ps.namespace)
60+
func (ps *ProbeScanner) Scan(ctx context.Context) error {
61+
pods, err := ps.kubeClient.GetPods(ctx, ps.namespace)
5462
if err != nil {
5563
return fmt.Errorf("error listing pods: %w", err)
5664
}
@@ -97,12 +105,10 @@ func (ps *ProbeScanner) Scan() error {
97105
}
98106

99107
if !issuesFound {
100-
fmt.Printf("No probe issues found in namespace %s\n", ps.namespace)
101-
return nil
102-
} else {
103-
fmt.Println("Issues found. Exiting with status code 1.")
104-
return fmt.Errorf("probe issues found")
108+
fmt.Printf("No probe issues found in namespace %s\n", ps.namespace)
109+
return nil
105110
}
106111

107-
return nil
112+
fmt.Println("Issues found. Exiting with status code 1.")
113+
return &ProbeIssuesFoundError{Message: "probe issues found"}
108114
}

0 commit comments

Comments
 (0)