Skip to content

Commit

Permalink
chore(worker): switch to slog
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
  • Loading branch information
fabriziosestito committed Nov 22, 2024
1 parent 4635a67 commit fc6c3c3
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 107 deletions.
25 changes: 12 additions & 13 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package main

import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"

"go.uber.org/zap"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -23,32 +21,33 @@ import (

func main() {
// TODO: add CLI flags for log level
logger, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("failed to create logger: %v", err))
opts := slog.HandlerOptions{
Level: slog.LevelDebug,
}
defer logger.Sync() //nolint: errcheck // flushes buffer, ignore error

logger := slog.New(slog.NewJSONHandler(os.Stdout, &opts)).With("component", "worker")
logger.Info("Starting worker")

// TODO: add CLI flags for NATS server address
sub, err := messaging.NewSubscription("nats://controller-nats.sbombastic.svc.cluster.local",
"worker")
if err != nil {
logger.Fatal("Error creating subscription", zap.Error(err))
logger.Error("Error creating subscription", "error", err)
os.Exit(1)
}

config := ctrl.GetConfigOrDie()
scheme := scheme.Scheme
if err := v1alpha1.AddToScheme(scheme); err != nil {
logger.Fatal("Error adding v1alpha1 to scheme", zap.Error(err))
logger.Error("Error adding v1alpha1 to scheme", "error", err)
os.Exit(1)
}
if err := storagev1alpha1.AddToScheme(scheme); err != nil {
logger.Fatal("Error adding storagev1alpha1 to scheme", zap.Error(err))
logger.Error("Error adding storagev1alpha1 to scheme", "error", err)
}
k8sClient, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
logger.Fatal("Error creating k8s client", zap.Error(err))
logger.Error("Error creating k8s client", "error", err)
os.Exit(1)
}
registryClientFactory := func(transport http.RoundTripper) registry.Client {
return registry.NewClient(transport, logger)
Expand All @@ -72,6 +71,6 @@ func main() {

err = subscriber.Run(ctx)
if err != nil {
logger.Fatal("Error running worker subscriber", zap.Error(err))
logger.Error("Error running worker subscriber", "error", err)
}
}
48 changes: 18 additions & 30 deletions internal/handlers/create_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"log/slog"
"net/http"
"path"

"go.uber.org/zap"

"github.com/google/go-containerregistry/pkg/name"
cranev1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
Expand All @@ -34,15 +33,15 @@ type CreateCatalogHandler struct {
registryClientFactory registryclient.ClientFactory
k8sClient client.Client
scheme *runtime.Scheme
logger *zap.Logger
logger *slog.Logger
}

func NewCreateCatalogHandler(registryClientFactory registryclient.ClientFactory, k8sClient client.Client, scheme *runtime.Scheme, logger *zap.Logger) *CreateCatalogHandler {
func NewCreateCatalogHandler(registryClientFactory registryclient.ClientFactory, k8sClient client.Client, scheme *runtime.Scheme, logger *slog.Logger) *CreateCatalogHandler {
return &CreateCatalogHandler{
registryClientFactory: registryClientFactory,
k8sClient: k8sClient,
scheme: scheme,
logger: logger.Named("create_catalog_handler"),
logger: logger.With("handler", "create_catalog_handler"),
}
}

Expand All @@ -53,8 +52,8 @@ func (h *CreateCatalogHandler) Handle(message messaging.Message) error {
}

h.logger.Debug("Catalog creation requested",
zap.String("registry", createCatalogMessage.RegistryName),
zap.String("namespace", createCatalogMessage.RegistryNamespace),
"registry", createCatalogMessage.RegistryName,
"namespace", createCatalogMessage.RegistryNamespace,
)

ctx := context.Background()
Expand All @@ -68,9 +67,7 @@ func (h *CreateCatalogHandler) Handle(message messaging.Message) error {
return fmt.Errorf("cannot get registry %s/%s: %w", createCatalogMessage.RegistryNamespace, createCatalogMessage.RegistryName, err)
}

h.logger.Debug("Registry found",
zap.Any("registry", registry),
)
h.logger.Debug("Registry found", "registry", registry)

transport := h.transportFromRegistry(registry)
registryClient := h.registryClientFactory(transport)
Expand All @@ -84,19 +81,14 @@ func (h *CreateCatalogHandler) Handle(message messaging.Message) error {
for _, repository := range repositories {
repoImages, err := h.discoverImages(ctx, registryClient, repository)
if err != nil {
h.logger.Error(
"cannot discover images",
zap.String("repository", repository),
zap.Error(err),
)
continue
return fmt.Errorf("cannot discover images in registry %s: %w", registry.Name, err)
}
discoveredImageNames.Insert(repoImages...)
}

existingImageList := &storagev1alpha1.ImageList{}
if err := h.k8sClient.List(ctx, existingImageList, client.InNamespace(registry.Namespace), client.MatchingLabels{"registry": registry.Name}); err != nil {
return fmt.Errorf("cannot list existing images: %w", err)
return fmt.Errorf("cannot list existing images in registry %s: %w", registry.Name, err)
}
existingImageNames := sets.Set[string]{}
for _, existingImage := range existingImageList.Items {
Expand Down Expand Up @@ -126,7 +118,7 @@ func (h *CreateCatalogHandler) Handle(message messaging.Message) error {
}

if err := h.deleteObsoleteImages(ctx, existingImageNames, discoveredImageNames, registry.Namespace); err != nil {
return fmt.Errorf("cannot delete obsolete images: %w", err)
return fmt.Errorf("cannot delete obsolete images in registry %s: %w", registry.Name, err)
}

return nil
Expand Down Expand Up @@ -199,18 +191,12 @@ func (h *CreateCatalogHandler) refToImages(registryClient registryclient.Client,
platformStr = platform.String()
}

h.logger.Error(
"cannot get image details",
zap.String("image", ref.Name()),
zap.String("platform", platformStr),
zap.Error(err))
continue
return nil, fmt.Errorf("cannot get image details for %s %s: %w", ref, platformStr, err)
}

image, err := imageDetailsToImage(ref, imageDetails, registry)
if err != nil {
h.logger.Error("cannot convert image details to image", zap.Error(err))
continue
return nil, fmt.Errorf("cannot convert image details to image: %w", err)
}

if err := controllerutil.SetControllerReference(registry, &image, h.scheme); err != nil {
Expand All @@ -230,8 +216,8 @@ func (h *CreateCatalogHandler) refToPlatforms(registryClient registryclient.Clie
if err != nil {
h.logger.Debug(
"image doesn't seem to be multi-architecture",
zap.String("image", ref.Name()),
zap.Error(err))
"image", ref.Name(),
"error", err)
return []*cranev1.Platform(nil), nil
}

Expand All @@ -258,15 +244,17 @@ func (h *CreateCatalogHandler) transportFromRegistry(registry *v1alpha1.Registry
if len(registry.Spec.CABundle) > 0 {
rootCAs, err := x509.SystemCertPool()
if err != nil {
h.logger.Error("cannot load system cert pool, using empty pool", zap.Error(err))
h.logger.Error("cannot load system cert pool, using empty pool", "error", err)
rootCAs = x509.NewCertPool()
}

ok := rootCAs.AppendCertsFromPEM([]byte(registry.Spec.CABundle))
if ok {
transport.TLSClientConfig.RootCAs = rootCAs
} else {
h.logger.Info("cannot load the given CA bundle")
h.logger.Info("cannot load the given CA bundle",
"registry", registry.Name,
"namespace", registry.Namespace)
}
}

Expand Down
7 changes: 3 additions & 4 deletions internal/handlers/create_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"fmt"
"log/slog"
"net/http"
"path"
"strconv"
Expand All @@ -15,8 +16,6 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"go.uber.org/zap"

"github.com/google/go-containerregistry/pkg/name"
cranev1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/types"
Expand Down Expand Up @@ -124,7 +123,7 @@ func TestCreateCatalogHandler_Handle(t *testing.T) {
WithRuntimeObjects(registry).
Build()

handler := NewCreateCatalogHandler(mockRegistryClientFactory, k8sClient, scheme, zap.NewNop())
handler := NewCreateCatalogHandler(mockRegistryClientFactory, k8sClient, scheme, slog.Default())
err = handler.Handle(&messaging.CreateCatalog{
RegistryName: registry.Name,
RegistryNamespace: registry.Namespace,
Expand Down Expand Up @@ -231,7 +230,7 @@ func TestCataloghandler_DeleteObsoleteImages(t *testing.T) {

handler := &CreateCatalogHandler{
k8sClient: k8sClient,
logger: zap.NewNop(),
logger: slog.Default(),
}

ctx := context.Background()
Expand Down
23 changes: 11 additions & 12 deletions internal/handlers/generate_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"fmt"
"io"
"log/slog"
"os"

"go.uber.org/zap"

trivyCommands "github.com/aquasecurity/trivy/pkg/commands"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -23,15 +22,15 @@ type GenerateSBOMHandler struct {
k8sClient client.Client
scheme *runtime.Scheme
workDir string
logger *zap.Logger
logger *slog.Logger
}

func NewGenerateSBOMHandler(k8sClient client.Client, scheme *runtime.Scheme, workDir string, logger *zap.Logger) *GenerateSBOMHandler {
func NewGenerateSBOMHandler(k8sClient client.Client, scheme *runtime.Scheme, workDir string, logger *slog.Logger) *GenerateSBOMHandler {
return &GenerateSBOMHandler{
k8sClient: k8sClient,
scheme: scheme,
workDir: workDir,
logger: logger.Named("generate_sbom_handler"),
logger: logger.With("handler", "generate_sbom_handler"),
}
}

Expand All @@ -42,8 +41,8 @@ func (h *GenerateSBOMHandler) Handle(message messaging.Message) error {
}

h.logger.Debug("SBOM generation requested",
zap.String("image", generateSBOMMessage.ImageName),
zap.String("namespace", generateSBOMMessage.ImageNamespace),
"image", generateSBOMMessage.ImageName,
"namespace", generateSBOMMessage.ImageNamespace,
)

ctx := context.Background()
Expand All @@ -58,7 +57,7 @@ func (h *GenerateSBOMHandler) Handle(message messaging.Message) error {
}

h.logger.Debug("Image found",
zap.Any("image", image),
"image", image,
)

sbomFile, err := os.CreateTemp(h.workDir, "trivy.sbom.*.json")
Expand All @@ -67,11 +66,11 @@ func (h *GenerateSBOMHandler) Handle(message messaging.Message) error {
}
defer func() {
if err := sbomFile.Close(); err != nil {
h.logger.Error("failed to close temporary SBOM file", zap.Error(err))
h.logger.Error("failed to close temporary SBOM file", "error", err)
}

if err := os.Remove(sbomFile.Name()); err != nil {
h.logger.Error("failed to remove temporary SBOM file", zap.Error(err))
h.logger.Error("failed to remove temporary SBOM file", "error", err)
}
}()

Expand All @@ -89,8 +88,8 @@ func (h *GenerateSBOMHandler) Handle(message messaging.Message) error {
}

h.logger.Debug("SBOM generated",
zap.String("image", image.Name),
zap.String("namespace", image.Namespace),
"image", image.Name,
"namespace", image.Namespace,
)

spdxBytes, err := io.ReadAll(sbomFile)
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/generate_sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"
"encoding/json"
"log/slog"
"os"
"path/filepath"
"testing"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/spdx/tools-golang/spdx"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestGenerateSBOMHandler_Handle(t *testing.T) {
err = json.Unmarshal(spdxData, expectedSPDX)
require.NoError(t, err)

handler := NewGenerateSBOMHandler(k8sClient, scheme, "/tmp", zap.NewNop())
handler := NewGenerateSBOMHandler(k8sClient, scheme, "/tmp", slog.Default())

err = handler.Handle(&messaging.GenerateSBOM{
ImageName: image.Name,
Expand Down
Loading

0 comments on commit fc6c3c3

Please sign in to comment.