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

Make route domain error specific #15082

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions pkg/apis/serving/v1/route_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func (rs *RouteStatus) MarkUnknownTrafficError(msg string) {
routeCondSet.Manage(rs).MarkUnknown(RouteConditionAllTrafficAssigned, "Unknown", msg)
}

// MarkRevisionTargetTrafficError marks the RouteConditionAllTrafficAssigned condition
// to indicate an error has occurred wrt a revision target.
func (rs *RouteStatus) MarkRevisionTargetTrafficError(reason, msg string) {
routeCondSet.Manage(rs).MarkFalse(RouteConditionAllTrafficAssigned, reason, msg)
}

// MarkConfigurationNotReady marks the RouteConditionAllTrafficAssigned
// condition to indiciate the Revision is not yet ready.
func (rs *RouteStatus) MarkConfigurationNotReady(name string) {
Expand Down
16 changes: 13 additions & 3 deletions pkg/reconciler/route/domains/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ import (
// HTTPScheme is the string representation of http.
const HTTPScheme string = "http"

var _ error = (*DomainNameError)(nil)

type DomainNameError struct {
msg string
}

func (e DomainNameError) Error() string {
return e.msg
}

// GetAllDomainsAndTags returns all of the domains and tags(including subdomains) associated with a Route
func GetAllDomainsAndTags(ctx context.Context, r *v1.Route, names []string, visibility map[string]netv1alpha1.IngressVisibility) (map[string]string, error) {
domainTagMap := make(map[string]string)
Expand Down Expand Up @@ -119,12 +129,12 @@ func DomainNameFromTemplate(ctx context.Context, r metav1.ObjectMeta, name strin
}

if err := templ.Execute(&buf, data); err != nil {
return "", fmt.Errorf("error executing the DomainTemplate: %w", err)
return "", DomainNameError{msg: fmt.Sprintf("error executing the DomainTemplate: %q", err.Error())}
}

urlErrs := validation.IsFullyQualifiedDomainName(field.NewPath("url"), buf.String())
if urlErrs != nil {
return "", fmt.Errorf("invalid domain name %q: %w", buf.String(), urlErrs.ToAggregate())
return "", DomainNameError{msg: fmt.Sprintf("invalid domain name %q: %s", buf.String(), urlErrs.ToAggregate())}
}

return buf.String(), nil
Expand All @@ -147,7 +157,7 @@ func HostnameFromTemplate(ctx context.Context, name, tag string) (string, error)
networkConfig := config.FromContext(ctx).Network
buf := bytes.Buffer{}
if err := networkConfig.GetTagTemplate().Execute(&buf, data); err != nil {
return "", fmt.Errorf("error executing the TagTemplate: %w", err)
return "", DomainNameError{fmt.Errorf("error executing the TagTemplate: %w", err).Error()}
}
return buf.String(), nil
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/reconciler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Reconciler struct {
enqueueAfter func(interface{}, time.Duration)
}

const errorConfigMsg = "ErrorConfig"

// Check that our Reconciler implements routereconciler.Interface
var _ routereconciler.Interface = (*Reconciler)(nil)

Expand Down Expand Up @@ -118,7 +120,11 @@ func (c *Reconciler) ReconcileKind(ctx context.Context, r *v1.Route) pkgreconcil
traffic, err := c.configureTraffic(ctx, r)
if traffic == nil || err != nil {
if err != nil {
r.Status.MarkUnknownTrafficError(err.Error())
if errors.As(err, &domains.DomainNameError{}) {
Copy link
Member

Choose a reason for hiding this comment

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

I think errors.Is is more apt since you're not using special fields in DomainNameError

Copy link
Contributor Author

@skonto skonto Apr 9, 2024

Choose a reason for hiding this comment

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

error.Is will not work in this case (custom error) as it does not check the type. It returns false. In its code it has:

		if isComparable && err == target {
			return true
		}

This will never be equal, unless we have a ref of some global error with a specific msg eg. created with errors.New().

errors.As does this check:

		if reflectlite.TypeOf(err).AssignableTo(targetType) {
			val.Elem().Set(reflectlite.ValueOf(err))
			return true
		}

that matches the error.
See also
https://stackoverflow.com/questions/39121172/how-to-compare-go-errors.
https://medium.com/@reetas/exploring-the-appropriate-use-cases-for-errors-is-and-errors-as-in-comparing-errors-in-golang-3584c6bc5417

Anyway I will use some predefined error to mark the tree, I can avoid the custom error if that is the goal (since it has no extra fields).

r.Status.MarkRevisionTargetTrafficError(errorConfigMsg, err.Error())
} else {
r.Status.MarkUnknownTrafficError(err.Error())
}
}
// Traffic targets aren't ready, no need to configure child resources.
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/route/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ func TestReconcile(t *testing.T) {
Object: Route("default", "tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-long",
WithConfigTarget("config"), WithRouteObservedGeneration,
WithRouteFinalizer, WithInitRouteConditions,
MarkUnknownTrafficError(`invalid domain name "tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-long.default.example.com": url: Invalid value: "tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-long": must be no more than 63 characters`),
MarkRevisionTargetTrafficError(errorConfigMsg, `invalid domain name "tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-long.default.example.com": url: Invalid value: "tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-long": must be no more than 63 characters`),
WithHost("tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-long.default.svc.cluster.local"),
),
}},
Expand Down
6 changes: 3 additions & 3 deletions pkg/testing/v1/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ func MarkTrafficAssigned(r *v1.Route) {
r.Status.MarkTrafficAssigned()
}

// MarkUnknownTrafficError calls the method of the same name on .Status
func MarkUnknownTrafficError(msg string) RouteOption {
// MarkRevisionTargetTrafficError calls the method of the same name on .Status
func MarkRevisionTargetTrafficError(reason, msg string) RouteOption {
return func(r *v1.Route) {
r.Status.MarkUnknownTrafficError(msg)
r.Status.MarkRevisionTargetTrafficError(reason, msg)
}
}

Expand Down
Loading