Skip to content

Refactor codebase to use inline error checking #1026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Refactor inline error checking for basic cases
  • Loading branch information
bjee19 committed Sep 1, 2023
commit c8a883be10d62158e3906a23bd8757cb8a5db12d
3 changes: 1 addition & 2 deletions internal/framework/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
}

obj := newObject(r.cfg.ObjectType)
err := r.cfg.Getter.Get(ctx, req.NamespacedName, obj)
if err != nil {
if err := r.cfg.Getter.Get(ctx, req.NamespacedName, obj); err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "Failed to get the resource")
return reconcile.Result{}, err
Expand Down
9 changes: 3 additions & 6 deletions internal/framework/controller/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func Register(
}

for field, indexerFunc := range cfg.fieldIndices {
err := addIndex(ctx, mgr.GetFieldIndexer(), objectType, field, indexerFunc)
if err != nil {
if err := addIndex(ctx, mgr.GetFieldIndexer(), objectType, field, indexerFunc); err != nil {
return err
}
}
Expand All @@ -101,8 +100,7 @@ func Register(
NamespacedNameFilter: cfg.namespacedNameFilter,
}

err := builder.Complete(cfg.newReconciler(recCfg))
if err != nil {
if err := builder.Complete(cfg.newReconciler(recCfg)); err != nil {
return fmt.Errorf("cannot build a controller for %T: %w", objectType, err)
}

Expand All @@ -119,8 +117,7 @@ func addIndex(
c, cancel := context.WithTimeout(ctx, addIndexFieldTimeout)
defer cancel()

err := indexer.IndexField(c, objectType, field, indexerFunc)
if err != nil {
if err := indexer.IndexField(c, objectType, field, indexerFunc); err != nil {
return fmt.Errorf("failed to add index for %T for field %s: %w", objectType, field, err)
}

Expand Down
6 changes: 2 additions & 4 deletions internal/framework/events/first_eventbatch_preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func (p *FirstEventBatchPreparerImpl) Prepare(ctx context.Context) (EventBatch,
total := 0

for _, list := range p.objectLists {
err := p.reader.List(ctx, list)
if err != nil {
if err := p.reader.List(ctx, list); err != nil {
return nil, err
}

Expand All @@ -85,8 +84,7 @@ func (p *FirstEventBatchPreparerImpl) Prepare(ctx context.Context) (EventBatch,
for _, obj := range p.objects {
key := types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}

err := p.reader.Get(ctx, key, obj)
if err != nil {
if err := p.reader.Get(ctx, key, obj); err != nil {
if !apierrors.IsNotFound(err) {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/framework/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func PrepareTimeForFakeClient(t metav1.Time) metav1.Time {
panic(fmt.Errorf("failed to marshal time: %w", err))
}

err = t.Unmarshal(bytes)
if err != nil {
if err = t.Unmarshal(bytes); err != nil {
panic(fmt.Errorf("failed to unmarshal time: %w", err))
}

Expand Down
6 changes: 2 additions & 4 deletions internal/framework/status/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ func (upd *updaterImpl) update(
// Otherwise, the Update status API call can fail.
// Note: the default client uses a cache for reads, so we're not making an unnecessary API call here.
// the default is configurable in the Manager options.
err := upd.cfg.Client.Get(ctx, nsname, obj)
if err != nil {
if err := upd.cfg.Client.Get(ctx, nsname, obj); err != nil {
if !apierrors.IsNotFound(err) {
upd.cfg.Logger.Error(
err,
Expand All @@ -161,8 +160,7 @@ func (upd *updaterImpl) update(

statusSetter(obj)

err = upd.cfg.Client.Status().Update(ctx, obj)
if err != nil {
if err := upd.cfg.Client.Status().Update(ctx, obj); err != nil {
upd.cfg.Logger.Error(
err,
"Failed to update status",
Expand Down
3 changes: 1 addition & 2 deletions internal/mode/provisioner/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
// It will configure the Deployment to use the Gateway with the given NamespacedName.
func prepareDeployment(depYAML []byte, id string, gwNsName types.NamespacedName) (*v1.Deployment, error) {
dep := &v1.Deployment{}
err := yaml.Unmarshal(depYAML, dep)
if err != nil {
if err := yaml.Unmarshal(depYAML, dep); err != nil {
return nil, fmt.Errorf("failed to unmarshal deployment: %w", err)
}

Expand Down
6 changes: 2 additions & 4 deletions internal/mode/provisioner/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func (h *eventHandler) ensureDeploymentsMatchGateways(ctx context.Context) {
panic(fmt.Errorf("failed to prepare deployment: %w", err))
}

err = h.k8sClient.Create(ctx, deployment)
if err != nil {
if err = h.k8sClient.Create(ctx, deployment); err != nil {
panic(fmt.Errorf("failed to create deployment: %w", err))
}

Expand All @@ -129,8 +128,7 @@ func (h *eventHandler) ensureDeploymentsMatchGateways(ctx context.Context) {
for _, nsname := range removedGwsWithDeps {
deployment := h.provisions[nsname]

err := h.k8sClient.Delete(ctx, deployment)
if err != nil {
if err := h.k8sClient.Delete(ctx, deployment); err != nil {
panic(fmt.Errorf("failed to delete deployment: %w", err))
}

Expand Down
3 changes: 1 addition & 2 deletions internal/mode/provisioner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func StartManager(cfg Config) error {
firstBatchPreparer,
)

err = mgr.Add(eventLoop)
if err != nil {
if err := mgr.Add(eventLoop); err != nil {
return fmt.Errorf("cannot register event loop: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/mode/static/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ func StartManager(cfg config.Config) error {
eventHandler,
firstBatchPreparer)

err = mgr.Add(eventLoop)
if err != nil {
if err = mgr.Add(eventLoop); err != nil {
return fmt.Errorf("cannot register event loop: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/mode/static/nginx/config/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
func execute(template *template.Template, data interface{}) []byte {
var buf bytes.Buffer

err := template.Execute(&buf, data)
if err != nil {
if err := template.Execute(&buf, data); err != nil {
panic(err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/mode/static/nginx/runtime/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func (m *ManagerImpl) Reload(ctx context.Context) error {

// send HUP signal to the NGINX main process reload configuration
// See https://nginx.org/en/docs/control.html
err = syscall.Kill(pid, syscall.SIGHUP)
if err != nil {
if err := syscall.Kill(pid, syscall.SIGHUP); err != nil {
return fmt.Errorf("failed to send the HUP signal to NGINX main: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/mode/static/state/graph/gateway_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ func validateListenerHostname(listener v1beta1.Listener) []conditions.Condition
return nil
}

err := validateHostname(h)
if err != nil {
if err := validateHostname(h); err != nil {
path := field.NewPath("hostname")
valErr := field.Invalid(path, listener.Hostname, err.Error())
return staticConds.NewListenerUnsupportedValue(valErr.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func createSlice(

func createFakeK8sClient(initObjs ...client.Object) (client.Client, error) {
scheme := runtime.NewScheme()
err := discoveryV1.AddToScheme(scheme)
if err != nil {
if err := discoveryV1.AddToScheme(scheme); err != nil {
return nil, err
}

Expand Down