Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/step_tests-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v7
with:
version: v2.1.1
version: v2.4.0
args: --timeout=5m
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ linters:
- testpackage
- varnamelen
- wrapcheck
- wsl
settings:
wsl_v5:
allow-first-in-block: true
allow-whole-block: false
branch-max-lines: 2
errcheck:
exclude-functions:
# Used in HTTP handlers, any error is handled by the server itself.
Expand Down
3 changes: 2 additions & 1 deletion cmd/ceems_api_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func main() {
}

// Main entrypoint of the app
if err := CEEMSServer.Main(); err != nil {
err = CEEMSServer.Main()
if err != nil {
log.Println(err)
os.Exit(1)
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/ceems_api_server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (
)

func TestBatchjobStatsExecutable(t *testing.T) {
if _, err := os.Stat(binary); err != nil {
_, err := os.Stat(binary)
if err != nil {
t.Skipf("ceems_api_server binary not available, try to run `make build` first: %s", err)
}

Expand All @@ -31,7 +32,8 @@ func TestBatchjobStatsExecutable(t *testing.T) {
err = os.Link(sacctPath, tmpSacctPath)
require.NoError(t, err)

usagestats := exec.Command(
usagestats := exec.CommandContext(
t.Context(),
binary,
"--web.listen-address", address,
"--no-security.drop-privileges",
Expand All @@ -40,7 +42,8 @@ func TestBatchjobStatsExecutable(t *testing.T) {
}

func runCommandAndTests(cmd *exec.Cmd) error {
if err := cmd.Start(); err != nil {
err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to start command: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/ceems_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func main() {
}

// Main entrypoint of the app
if err := ceemsExporterApp.Main(); err != nil {
err = ceemsExporterApp.Main()
if err != nil {
log.Println(err)
os.Exit(1)
}
Expand Down
27 changes: 18 additions & 9 deletions cmd/ceems_exporter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const (
)

func TestFileDescriptorLeak(t *testing.T) {
if _, err := os.Stat(binary); err != nil {
_, err := os.Stat(binary)
if err != nil {
t.Skipf("ceems_exporter binary not available, try to run `make build` first: %s", err)
}

Expand All @@ -33,7 +34,8 @@ func TestFileDescriptorLeak(t *testing.T) {
)
}

if _, err := fs.Stat(); err != nil {
_, err = fs.Stat()
if err != nil {
t.Errorf("unable to read process stats: %s", err)
}

Expand All @@ -43,7 +45,8 @@ func TestFileDescriptorLeak(t *testing.T) {
procfsPath, err := filepath.Abs("../../pkg/collector/testdata/proc")
require.NoError(t, err)

exporter := exec.Command(
exporter := exec.CommandContext(
t.Context(),
binary,
"--web.listen-address", address,
"--path.cgroupfs", sysfsPath,
Expand All @@ -52,7 +55,8 @@ func TestFileDescriptorLeak(t *testing.T) {
// "--no-security.drop-privileges",
)
test := func(pid int) error {
if err := queryExporter(address); err != nil {
err := queryExporter(address)
if err != nil {
return err
}

Expand All @@ -67,7 +71,8 @@ func TestFileDescriptorLeak(t *testing.T) {
}

for range 5 {
if err := queryExporter(address); err != nil {
err := queryExporter(address)
if err != nil {
return err
}
}
Expand Down Expand Up @@ -102,7 +107,8 @@ func queryExporter(address string) error {
return err
}

if err := resp.Body.Close(); err != nil {
err = resp.Body.Close()
if err != nil {
return err
}

Expand All @@ -114,14 +120,16 @@ func queryExporter(address string) error {
}

func runCommandAndTests(cmd *exec.Cmd, address string, fn func(pid int) error) error {
if err := cmd.Start(); err != nil {
err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to start command: %w", err)
}

time.Sleep(50 * time.Millisecond)

for i := range 10 {
if err := queryExporter(address); err == nil {
err := queryExporter(address)
if err == nil {
break
}

Expand All @@ -133,11 +141,12 @@ func runCommandAndTests(cmd *exec.Cmd, address string, fn func(pid int) error) e
}

errc := make(chan error)

go func(pid int) {
errc <- fn(pid)
}(cmd.Process.Pid)

err := <-errc
err = <-errc

if cmd.Process != nil {
cmd.Process.Kill()
Expand Down
15 changes: 10 additions & 5 deletions cmd/ceems_k8s_admission_controller/base/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ func NewRuntimeScheme() (*runtime.Scheme, error) {
runtimeScheme := runtime.NewScheme()

// Add resources to runtime scheme
if err := corev1.AddToScheme(runtimeScheme); err != nil {
err := corev1.AddToScheme(runtimeScheme)
if err != nil {
return nil, fmt.Errorf("failed to add core resources to runtime scheme: %w", err)
}

if err := appsv1.AddToScheme(runtimeScheme); err != nil {
err = appsv1.AddToScheme(runtimeScheme)
if err != nil {
return nil, fmt.Errorf("failed to add apps resources to runtime scheme: %w", err)
}

if err := batchv1.AddToScheme(runtimeScheme); err != nil {
err = batchv1.AddToScheme(runtimeScheme)
if err != nil {
return nil, fmt.Errorf("failed to add batch resources to runtime scheme: %w", err)
}

if err := admissionv1beta1.AddToScheme(runtimeScheme); err != nil {
err = admissionv1beta1.AddToScheme(runtimeScheme)
if err != nil {
return nil, fmt.Errorf("failed to add v1beta1 admission resources to runtime scheme: %w", err)
}

if err := admissionv1.AddToScheme(runtimeScheme); err != nil {
err = admissionv1.AddToScheme(runtimeScheme)
if err != nil {
return nil, fmt.Errorf("failed to add v1 admission resources to runtime scheme: %w", err)
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/ceems_k8s_admission_controller/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (h *admissionHandler) Serve(hook base.Hook) http.HandlerFunc {

var obj runtime.Object

if obj, gvk, err = h.decoder.Decode(body, nil, nil); err != nil {
obj, gvk, err = h.decoder.Decode(body, nil, nil)
if err != nil {
h.logger.Error("Failed to decode body into admission review", "err", err)

http.Error(w, fmt.Sprintf("could not deserialize request: %v", err), http.StatusBadRequest)
Expand Down Expand Up @@ -229,7 +230,8 @@ func (h *admissionHandler) Serve(hook base.Hook) http.HandlerFunc {
// Write response
w.WriteHeader(http.StatusOK)

if err = json.NewEncoder(w).Encode(&responseObj); err != nil {
err = json.NewEncoder(w).Encode(&responseObj)
if err != nil {
h.logger.Error("Failed to encode response", "path", r.URL.Path, "version", version, "uid", uid, "err", err)
http.Error(w, fmt.Sprintf("could not marshal JSON patch: %v", err), http.StatusInternalServerError)
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/ceems_k8s_admission_controller/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func NewAdmissionControllerServer(c *base.Config) (*AdmissionControllerServer, e
func (s *AdmissionControllerServer) Start() error {
s.logger.Info("Starting " + base.AppName)

if err := web.ListenAndServe(s.server, s.webConfig, s.logger); err != nil && !errors.Is(err, http.ErrServerClosed) {
err := web.ListenAndServe(s.server, s.webConfig, s.logger)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
s.logger.Error("Failed to Listen and Serve HTTP server", "err", err)

return err
Expand All @@ -91,7 +92,8 @@ func (s *AdmissionControllerServer) Shutdown(ctx context.Context) error {

// First shutdown HTTP server to avoid accepting any incoming
// connections
if err := s.server.Shutdown(ctx); err != nil {
err := s.server.Shutdown(ctx)
if err != nil {
s.logger.Error("Failed to stop exporter's HTTP server")

return err
Expand Down
1 change: 1 addition & 0 deletions cmd/ceems_k8s_admission_controller/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestNewAdmissionControllerServer(t *testing.T) {
require.NoError(t, err, test.name)

defer resp.Body.Close()

assert.Equal(t, test.code, resp.StatusCode, test.name)
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/ceems_k8s_admission_controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func main() {
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below.
go func() {
if err := server.Start(); err != nil {
err := server.Start()
if err != nil {
logger.Error("Failed to start server", "err", err)
}
}()
Expand All @@ -117,7 +118,8 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := server.Shutdown(ctx); err != nil {
err = server.Shutdown(ctx)
if err != nil {
logger.Error("Failed to gracefully shutdown server", "err", err)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/ceems_lb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func main() {
}

// Main entrypoint of the app
if err := CEEMSLoadBalancer.Main(); err != nil {
err = CEEMSLoadBalancer.Main()
if err != nil {
log.Println(err)
os.Exit(1)
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/ceems_lb/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (
)

func TestCEEMSLBExecutable(t *testing.T) {
if _, err := os.Stat(binary); err != nil {
_, err := os.Stat(binary)
if err != nil {
t.Skipf("ceems_lb binary not available, try to run `make build` first: %s", err)
}

Expand All @@ -31,7 +32,8 @@ func TestCEEMSLBExecutable(t *testing.T) {
err = os.Link(configPath, tmpConfigPath)
require.NoError(t, err)

lb := exec.Command(
lb := exec.CommandContext(
t.Context(),
binary, "--path.data", tmpDir,
"--config.path", tmpConfigPath,
"--web.listen-address", address,
Expand All @@ -41,7 +43,8 @@ func TestCEEMSLBExecutable(t *testing.T) {
}

func runCommandAndTests(cmd *exec.Cmd) error {
if err := cmd.Start(); err != nil {
err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to start command: %w", err)
}

Expand Down
26 changes: 18 additions & 8 deletions cmd/ceems_tool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ func GenerateWebConfig(basicAuth bool, tls bool, hosts []string, validity time.D

// Encode to YAML with indent set to 2
var b bytes.Buffer

yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(2)

if err := yamlEncoder.Encode(&config); err != nil {
err = yamlEncoder.Encode(&config)
if err != nil {
fmt.Fprintln(os.Stderr, "error encoding web config", err)

return err
}

// Write to disk
configFile := filepath.Join(outDir, "web-config.yml")
if err := os.WriteFile(configFile, b.Bytes(), 0o600); err != nil {

err = os.WriteFile(configFile, b.Bytes(), 0o600)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to write web config file:", err)

return err
Expand All @@ -108,15 +112,17 @@ func GenerateWebConfig(basicAuth bool, tls bool, hosts []string, validity time.D
// tlsConfig returns a TLS config based on self signed TLS certificates.
func tlsConfig(hosts []string, validity time.Duration, outDir string) (TLSConfig, error) {
// Make directory to store certificate files
if err := os.MkdirAll(outDir, 0o700); err != nil {
err := os.MkdirAll(outDir, 0o700)
if err != nil {
fmt.Fprintln(os.Stderr, "error creating output directory:", err)

return TLSConfig{}, err
}

// Generate self signed certificates
// Nicked from https://go.dev/src/crypto/tls/generate_cert.go
if err := selfSignedTLS(hosts, validity, outDir); err != nil {
err = selfSignedTLS(hosts, validity, outDir)
if err != nil {
fmt.Fprintln(os.Stderr, "error generating self signed TLS certificate", err)

return TLSConfig{}, err
Expand Down Expand Up @@ -209,11 +215,13 @@ func selfSignedTLS(hosts []string, validity time.Duration, outDir string) error
return err
}

if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return err
}

if err := certOut.Close(); err != nil {
err = certOut.Close()
if err != nil {
return err
}

Expand All @@ -227,11 +235,13 @@ func selfSignedTLS(hosts []string, validity time.Duration, outDir string) error
return err
}

if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
err = pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
if err != nil {
return err
}

if err := keyOut.Close(); err != nil {
err = keyOut.Close()
if err != nil {
return err
}

Expand Down
Loading