Skip to content
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

Add ngrok enriched errors #555

Merged
merged 2 commits into from
Jan 23, 2025
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
1 change: 0 additions & 1 deletion api/bindings/v1alpha1/boundendpoint_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ type BindingEndpoint struct {
// ErrorCode is the ngrok API error code if the status is error
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Pattern=`^ERR_NGROK_\d+$`
// TODO(hkatz) Define error codes and implement in the API
ErrorCode string `json:"errorCode,omitempty"`

// ErrorMessage is a free-form error message if the status is error
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/controller/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"github.com/go-logr/logr"
"github.com/ngrok/ngrok-api-go/v7"
"github.com/ngrok/ngrok-operator/internal/ngrokapi"
"github.com/ngrok/ngrok-operator/internal/util"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -163,6 +164,12 @@
return ctrl.Result{RequeueAfter: time.Minute}, nil
case nerr.StatusCode == http.StatusNotFound:
return ctrl.Result{}, err
case ngrok.IsErrorCode(nerr, ngrokapi.NgrokOpErrFailedToCreateCSR.Code):
return ctrl.Result{RequeueAfter: 30 * time.Second}, nerr
case ngrok.IsErrorCode(nerr, ngrokapi.NgrokOpErrFailedToCreateUpstreamService.Code, ngrokapi.NgrokOpErrFailedToCreateTargetService.Code):
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nerr
case ngrok.IsErrorCode(nerr, ngrokapi.NgrokOpErrEndpointDenied.Code):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't be relevant anymore, but we can remove it after we remove the enriched error on the other side

return ctrl.Result{}, nil // do not retry, endpoint poller will take care of this

Check warning on line 172 in internal/controller/base_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/base_controller.go#L167-L172

Added lines #L167 - L172 were not covered by tests
default:
// the rest are client errors, we don't retry by default
return ctrl.Result{}, nil
Expand Down
43 changes: 28 additions & 15 deletions internal/controller/bindings/boundendpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/go-logr/logr"
"github.com/ngrok/ngrok-api-go/v7"
bindingsv1alpha1 "github.com/ngrok/ngrok-operator/api/bindings/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/controller"
"github.com/ngrok/ngrok-operator/internal/ngrokapi"
"github.com/ngrok/ngrok-operator/internal/util"
)

Expand All @@ -61,12 +63,6 @@
// Used for indexing BoundEndpoints by their target namespace. Not an actual
// field on the BoundEndpoint object.
BoundEndpointTargetNamespacePath = ".spec.targetNamespace"

// TODO(hkatz) ngrok-error-codes
NgrokErrorUpstreamServiceCreateFailed = "ERR_NGROK_0001"
NgrokErrorTargetServiceCreateFailed = "ERR_NGROK_0002"
NgrokErrorFailedToBind = "ERR_NGROK_003"
NgrokErrorNotAllowed = "ERR_NGROK_004"
)

var (
Expand Down Expand Up @@ -223,13 +219,15 @@
r.Recorder.Event(owner, v1.EventTypeWarning, "Created", "Failed to create Target Service")
log.Error(err, "Failed to create Target Service")

ngrokErr := ngrokapi.NewNgrokError(err, ngrokapi.NgrokOpErrFailedToCreateTargetService, "Failed to create Target Service")

Check warning on line 223 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L222-L223

Added lines #L222 - L223 were not covered by tests
setEndpointsStatus(owner, &bindingsv1alpha1.BindingEndpoint{
Status: bindingsv1alpha1.StatusError,
ErrorCode: NgrokErrorTargetServiceCreateFailed,
ErrorMessage: fmt.Sprintf("Failed to create Target Service: %s", err),
ErrorCode: ngrokErr.ErrorCode,
ErrorMessage: ngrokErr.Error(),

Check warning on line 227 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L226-L227

Added lines #L226 - L227 were not covered by tests
})

return err
return ngrokErr

Check warning on line 230 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L230

Added line #L230 was not covered by tests
}

r.Recorder.Event(service, v1.EventTypeNormal, "Created", "Created Target Service")
Expand All @@ -245,13 +243,15 @@
r.Recorder.Event(owner, v1.EventTypeWarning, "Created", "Failed to create Upstream Service")
log.Error(err, "Failed to create Upstream Service")

ngrokErr := ngrokapi.NewNgrokError(err, ngrokapi.NgrokOpErrFailedToCreateUpstreamService, "Failed to create Upstream Service")

Check warning on line 247 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L246-L247

Added lines #L246 - L247 were not covered by tests
setEndpointsStatus(owner, &bindingsv1alpha1.BindingEndpoint{
Status: bindingsv1alpha1.StatusError,
ErrorCode: NgrokErrorUpstreamServiceCreateFailed,
ErrorMessage: fmt.Sprintf("Failed to create Upstream Service: %s", err),
ErrorCode: ngrokErr.ErrorCode,
ErrorMessage: ngrokErr.Error(),

Check warning on line 251 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L250-L251

Added lines #L250 - L251 were not covered by tests
})

return err
return ngrokErr

Check warning on line 254 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L254

Added line #L254 was not covered by tests
}

r.Recorder.Event(service, v1.EventTypeNormal, "Created", "Created Upstream Service")
Expand Down Expand Up @@ -589,13 +589,15 @@

// update statuses
var desired *bindingsv1alpha1.BindingEndpoint
var ngrokErr *ngrok.Error

Check warning on line 592 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L592

Added line #L592 was not covered by tests
if bindErr != nil {
// error
log.Error(bindErr, "Failed to bind BoundEndpoint, moving to error")
ngrokErr = ngrokapi.NewNgrokError(bindErr, ngrokapi.NgrokOpErrFailedToConnectServices, "Failed to bind BoundEndpoint")

Check warning on line 596 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L596

Added line #L596 was not covered by tests
desired = &bindingsv1alpha1.BindingEndpoint{
Status: bindingsv1alpha1.StatusError,
ErrorCode: NgrokErrorFailedToBind,
ErrorMessage: fmt.Sprintf("Failed to bind BoundEndpoint: %s", bindErr),
ErrorCode: ngrokErr.ErrorCode,
ErrorMessage: ngrokErr.Error(),

Check warning on line 600 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L599-L600

Added lines #L599 - L600 were not covered by tests
}
} else {
// success
Expand All @@ -610,5 +612,16 @@

// set status
setEndpointsStatus(boundEndpoint, desired)
return bindErr

// Why do we check for nil here and return nil explicitly instead of just `return ngrokErr`?
// Because *ngrok.Error meets the interface of error means that error is actually (T=*ngrok.Error, V=nil)
// so outer function signature is (error) and not (*ngrok.Error)
// and an interface _pointing_ to nil is != to nil itself
// therefore *ngrok.Error.(error) is _always_ != nil
//
// See: https://go.dev/doc/faq#nil_error
if ngrokErr != nil {
return ngrokErr
}
return nil

Check warning on line 626 in internal/controller/bindings/boundendpoint_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/bindings/boundendpoint_controller.go#L615-L626

Added lines #L615 - L626 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func Test_setEndpointsStatus(t *testing.T) {
desired: &bindingsv1alpha1.BindingEndpoint{
Status: bindingsv1alpha1.StatusError,
ErrorCode: "ERR_NGROK_1234",
ErrorMessage: "Exampl Error Message",
ErrorMessage: "Example Error Message",
},
},
}
Expand Down
16 changes: 8 additions & 8 deletions internal/controller/bindings/boundendpoint_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/go-logr/logr"
v6 "github.com/ngrok/ngrok-api-go/v7"
"github.com/ngrok/ngrok-api-go/v7"
bindingsv1alpha1 "github.com/ngrok/ngrok-operator/api/bindings/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/ngrokapi"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -184,7 +184,7 @@ func Test_BoundEndpointPoller_boundEndpointNeedsUpdate(t *testing.T) {
HashedName: hashURI(uriExample1),
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{
Ref: v6.Ref{ID: "ep_abc123", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_abc123", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
Expand Down Expand Up @@ -218,7 +218,7 @@ func Test_BoundEndpointPoller_boundEndpointNeedsUpdate(t *testing.T) {
HashedName: hashURI(uriExample1),
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{
Ref: v6.Ref{ID: "ep_abc123", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_abc123", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
Expand Down Expand Up @@ -248,7 +248,7 @@ func Test_BoundEndpointPoller_boundEndpointNeedsUpdate(t *testing.T) {
HashedName: hashURI(uriExample1),
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{
Ref: v6.Ref{ID: "ep_abc123", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_abc123", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
Expand All @@ -275,7 +275,7 @@ func Test_BoundEndpointPoller_boundEndpointNeedsUpdate(t *testing.T) {
HashedName: hashURI(uriExample2),
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{
Ref: v6.Ref{ID: "ep_def456", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_def456", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
Expand All @@ -301,19 +301,19 @@ func Test_BoundEndpointPoller_boundEndpointNeedsUpdate(t *testing.T) {
HashedName: hashURI(uriExample2),
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{
Ref: v6.Ref{ID: "ep_def456", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_def456", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
},
{
Ref: v6.Ref{ID: "ep_xyz999", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_xyz999", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
},
{
Ref: v6.Ref{ID: "ep_www000", URI: "example-uri"},
Ref: ngrok.Ref{ID: "ep_www000", URI: "example-uri"},
Status: bindingsv1alpha1.StatusProvisioning,
ErrorCode: "",
ErrorMessage: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
ngrokv1alpha1.KubernetesOperatorFeatureGateway: "Gateway",
}

const (
NgrokErrorFailedToCreateCSR = "ERR_NGROK_20006"
)

// KubernetesOperatorReconciler reconciles a KubernetesOperator object
type KubernetesOperatorReconciler struct {
client.Client
Expand Down Expand Up @@ -149,7 +153,7 @@
if bindingsEnabled {
tlsSecret, err := r.findOrCreateTLSSecret(ctx, ko)
if err != nil {
return err
return ngrokapi.NewNgrokError(err, ngrokapi.NgrokOpErrFailedToCreateCSR, "failed to create TLS secret for CSR")

Check warning on line 156 in internal/controller/ngrok/kubernetesoperator_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/ngrok/kubernetesoperator_controller.go#L156

Added line #L156 was not covered by tests
}

createParams.Binding = &ngrok.KubernetesOperatorBindingCreate{
Expand Down
45 changes: 45 additions & 0 deletions internal/ngrokapi/enriched_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ngrokapi

import (
"fmt"
"net/http"

"github.com/ngrok/ngrok-api-go/v7"
)

// Enriched Errors utilities to help interact with ngrok.Error and ngrok.IsErrorCode() returned by the ngrok API
// For right now these are all manually defined

type EnrichedError struct {
name string
Code int
statusCode int32
}

// list of supported ngrok error codes
var (
NgrokOpErrInternalServerError = EnrichedError{"ERR_NGROK_20000", 20000, http.StatusInternalServerError}
NgrokOpErrConfigurationError = EnrichedError{"ERR_NGROK_20001", 20001, http.StatusBadRequest}
NgrokOpErrFailedToCreateUpstreamService = EnrichedError{"ERR_NGROK_20002", 20002, http.StatusServiceUnavailable}
NgrokOpErrFailedToCreateTargetService = EnrichedError{"ERR_NGROK_20003", 20003, http.StatusServiceUnavailable}
NgrokOpErrFailedToConnectServices = EnrichedError{"ERR_NGROK_20004", 20004, http.StatusServiceUnavailable}
NgrokOpErrEndpointDenied = EnrichedError{"ERR_NGROK_20005", 20005, http.StatusForbidden}
NgrokOpErrFailedToCreateCSR = EnrichedError{"ERR_NGROK_20006", 20006, http.StatusInternalServerError}
)

func NewNgrokError(origErr error, ee EnrichedError, msg string) *ngrok.Error {
if ngrokErr, ok := origErr.(*ngrok.Error); ok {
// already have an ngrok.Error
// overwrite the message and return
return &ngrok.Error{
Msg: ngrokErr.Msg,
Details: ngrokErr.Details,
}
}

Check warning on line 38 in internal/ngrokapi/enriched_errors.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/enriched_errors.go#L30-L38

Added lines #L30 - L38 were not covered by tests

return &ngrok.Error{
Msg: fmt.Sprintf("%s: %s", msg, origErr),
ErrorCode: ee.name,
StatusCode: ee.statusCode,
}

Check warning on line 44 in internal/ngrokapi/enriched_errors.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/enriched_errors.go#L40-L44

Added lines #L40 - L44 were not covered by tests
}
Loading