Skip to content

Commit

Permalink
atc: add more context to conversion handler and refactor resource map…
Browse files Browse the repository at this point in the history
…ping resets
  • Loading branch information
davidmdm committed Jan 8, 2025
1 parent 95d5a0f commit b6be425
Showing 3 changed files with 23 additions and 14 deletions.
18 changes: 17 additions & 1 deletion cmd/atc/handler.go
Original file line number Diff line number Diff line change
@@ -62,9 +62,11 @@ func Handler(client *k8s.Client, cache *wasm.ModuleCache, logger *slog.Logger) h
return
}

var desiredAPIVersion string

var review apiextensionsv1.ConversionReview
if err := json.Unmarshal(data, &review); err == nil {
addRequestAttrs(r.Context(), slog.String("desiredAPIVersion", review.Request.DesiredAPIVersion))
desiredAPIVersion = review.Request.DesiredAPIVersion
}

resp, err := wasi.Execute(ctx, wasi.ExecParams{
@@ -78,6 +80,20 @@ func Handler(client *k8s.Client, cache *wasm.ModuleCache, logger *slog.Logger) h
return
}

if err := json.Unmarshal(resp, &review); err == nil {
addRequestAttrs(
r.Context(),
slog.Group(
"converter",
"status", review.Response.Result.Status,
"reason", review.Response.Result.Reason,
"count", len(review.Response.ConvertedObjects),
"uid", review.Response.UID,
"DesiredAPIVersion", desiredAPIVersion,
),
)
}

if _, err := w.Write(resp); err != nil {
logger.Error("unexpected: failed to write response to connection", "error", err)
}
11 changes: 3 additions & 8 deletions internal/atc/atc.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (
"reflect"
"slices"
"strings"
"sync"
"time"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
@@ -274,6 +273,8 @@ func (atc atc) Reconcile(ctx context.Context, event ctrl.Event) (result ctrl.Res
return ctrl.Result{}, fmt.Errorf("airway's template crd failed to become ready: %w", err)
}

ctrl.Client(ctx).Mapper.Reset()

validationWebhook := admissionregistrationv1.ValidatingWebhookConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: admissionregistrationv1.SchemeGroupVersion.Identifier(),
@@ -408,14 +409,7 @@ type FlightReconcilerParams struct {
}

func (atc atc) FlightReconciler(params FlightReconcilerParams) ctrl.HandleFunc {
var once sync.Once

return func(ctx context.Context, event ctrl.Event) (result ctrl.Result, err error) {
once.Do(func() {
// We need to reset to make sure that we are requesting the latest storage version for the resource.
ctrl.Client(ctx).Mapper.Reset()
})

ctx = internal.WithStdio(ctx, io.Discard, io.Discard, os.Stdin)

mapping, err := ctrl.Client(ctx).Mapper.RESTMapping(params.GK, params.Version)
@@ -445,6 +439,7 @@ func (atc atc) FlightReconciler(params FlightReconcilerParams) ctrl.HandleFunc {
if _, err := resourceIntf.Update(ctx, flight, metav1.UpdateOptions{FieldManager: fieldManager}); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to set default namespace on flight: %w", err)
}

return ctrl.Result{}, nil
}

8 changes: 3 additions & 5 deletions internal/k8s/ctrl/controller.go
Original file line number Diff line number Diff line change
@@ -62,13 +62,11 @@ type Params struct {
}

func NewController(ctx context.Context, params Params) (Instance, error) {
params.Client.Mapper.Reset()

mapping, err := params.Client.Mapper.RESTMapping(params.GK)
if err != nil {
params.Client.Mapper.Reset()
mapping, err = params.Client.Mapper.RESTMapping(params.GK)
if err != nil {
return Instance{}, fmt.Errorf("failed to get mapping for %s: %w", params.GK, err)
}
return Instance{}, fmt.Errorf("failed to get mapping for %s: %w", params.GK, err)
}

logger := params.Logger.With(slog.String("groupKind", params.GK.String()))

0 comments on commit b6be425

Please sign in to comment.