-
Notifications
You must be signed in to change notification settings - Fork 50
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
Improve error handling #916
base: main
Are you sure you want to change the base?
Conversation
Skipping CI for Draft Pull Request. |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: mrkisaolamb The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
9fb1cee
to
05ccd22
Compare
05ccd22
to
eeae165
Compare
eeae165
to
d8398dc
Compare
Build failed (check pipeline). Post https://softwarefactory-project.io/zuul/t/rdoproject.org/buildset/3d86a4af47ab4c45957184d8e9c14dde ✔️ openstack-meta-content-provider SUCCESS in 2h 40m 52s |
instance.Status.Conditions.Set(condition.FalseCondition( | ||
condition.NetworkAttachmentsReadyCondition, | ||
condition.ErrorReason, | ||
condition.SeverityWarning, | ||
condition.NetworkAttachmentsReadyErrorMessage, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used in common.go ensureNetworkAttachments() as well, but you did not changed it there. Is it because there the message using an error coming from a call instead of defined locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes exactly that was the reasone
@@ -56,6 +56,9 @@ const ( | |||
|
|||
// Common Messages used by API objects. | |||
const ( | |||
//NovaNetworkAttachmentsReadyInitMessage | |||
NovaNetworkAttachmentsReadyErrorMessage = "NetworkAttachments error occurred not all pods have interfaces with ips as configured in NetworkAttachments: %s" | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this eventually move to lib-common to be used across the operators? E.g. in https://github.com/openstack-k8s-operators/placement-operator/blob/5c39b092c37feddb1d94539b759ee8cebab371e2/controllers/placementapi_controller.go#L1159-L1160
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes eventually we can move to lib-common all common errors
controllers/common.go
Outdated
@@ -225,7 +223,10 @@ func ensureSecret( | |||
for _, field := range expectedFields { | |||
val, ok := secret.Data[field] | |||
if !ok { | |||
err := fmt.Errorf("field '%s' not found in secret/%s", field, secretName.Name) | |||
// Usage in the code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
controllers/common.go
Outdated
// ensureSecret - ensures that the Secret object exists and the expected fields | ||
// are in the Secret. It returns a hash of the values of the expected fields. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This belongs to the function definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
controllers/common.go
Outdated
@@ -579,7 +580,7 @@ func getNovaClient( | |||
return nil, err | |||
} | |||
if (ctrlResult != ctrl.Result{}) { | |||
return nil, fmt.Errorf("the CABundleSecret %s not found", auth.GetCABundleSecretName()) | |||
return nil, fmt.Errorf("the CABundleSecret %s not found: %w", auth.GetCABundleSecretName(), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as I see err will be always nil
here as if not then the previous condition is true and the function returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ou true so I create originall error that we lost during geting secret
controllers/common.go
Outdated
@@ -225,7 +223,10 @@ func ensureSecret( | |||
for _, field := range expectedFields { | |||
val, ok := secret.Data[field] | |||
if !ok { | |||
err := fmt.Errorf("field '%s' not found in secret/%s", field, secretName.Name) | |||
// Usage in the code | |||
err := k8s_errors.NewBadRequest(fmt.Sprintf("field '%s' not found in secret/%s", field, secretName.Name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we call ensureSecret from the validation webhook? As far as I remember we don't. So it is a bit strange to me to use HTTP response codes as errors here. I.e. the NewBadRequest creates a StatusError where the description says:
StatusError is an error intended for consumption by a REST API server; it can also be reconstructed by clients from a REST response. Public to allow easy type switches.
https://pkg.go.dev/k8s.io/apimachinery/pkg/api/errors#StatusError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used error that I already created ErrFieldNotFound
@@ -641,7 +642,7 @@ func ensureMemcached( | |||
condition.RequestedReason, | |||
condition.SeverityInfo, | |||
condition.MemcachedReadyWaitingMessage)) | |||
return nil, fmt.Errorf("memcached %s not found", memcachedName) | |||
return nil, fmt.Errorf("%w: memcached %s not found", err, memcachedName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yepp this is a good improvement. Thanks.
controllers/nova_controller.go
Outdated
@@ -1631,8 +1631,8 @@ func (r *NovaReconciler) ensureMQ( | |||
|
|||
url, ok := secret.Data[TransportURLSelector] | |||
if !ok { | |||
return "", nova.MQFailed, fmt.Errorf( | |||
"the TransportURL secret %s does not have 'transport_url' field", transportURL.Status.SecretName) | |||
return "", nova.MQFailed, k8s_errors.NewBadRequest(fmt.Sprintf( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto REST errors feels strange to me inside the reconciler loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
d8398dc
to
f7d3ecc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for review!
@@ -56,6 +56,9 @@ const ( | |||
|
|||
// Common Messages used by API objects. | |||
const ( | |||
//NovaNetworkAttachmentsReadyInitMessage | |||
NovaNetworkAttachmentsReadyErrorMessage = "NetworkAttachments error occurred not all pods have interfaces with ips as configured in NetworkAttachments: %s" | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes eventually we can move to lib-common all common errors
controllers/common.go
Outdated
@@ -225,7 +223,10 @@ func ensureSecret( | |||
for _, field := range expectedFields { | |||
val, ok := secret.Data[field] | |||
if !ok { | |||
err := fmt.Errorf("field '%s' not found in secret/%s", field, secretName.Name) | |||
// Usage in the code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
controllers/common.go
Outdated
@@ -225,7 +223,10 @@ func ensureSecret( | |||
for _, field := range expectedFields { | |||
val, ok := secret.Data[field] | |||
if !ok { | |||
err := fmt.Errorf("field '%s' not found in secret/%s", field, secretName.Name) | |||
// Usage in the code | |||
err := k8s_errors.NewBadRequest(fmt.Sprintf("field '%s' not found in secret/%s", field, secretName.Name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used error that I already created ErrFieldNotFound
controllers/common.go
Outdated
// ensureSecret - ensures that the Secret object exists and the expected fields | ||
// are in the Secret. It returns a hash of the values of the expected fields. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
controllers/common.go
Outdated
@@ -579,7 +580,7 @@ func getNovaClient( | |||
return nil, err | |||
} | |||
if (ctrlResult != ctrl.Result{}) { | |||
return nil, fmt.Errorf("the CABundleSecret %s not found", auth.GetCABundleSecretName()) | |||
return nil, fmt.Errorf("the CABundleSecret %s not found: %w", auth.GetCABundleSecretName(), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ou true so I create originall error that we lost during geting secret
controllers/nova_controller.go
Outdated
@@ -1631,8 +1631,8 @@ func (r *NovaReconciler) ensureMQ( | |||
|
|||
url, ok := secret.Data[TransportURLSelector] | |||
if !ok { | |||
return "", nova.MQFailed, fmt.Errorf( | |||
"the TransportURL secret %s does not have 'transport_url' field", transportURL.Status.SecretName) | |||
return "", nova.MQFailed, k8s_errors.NewBadRequest(fmt.Sprintf( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
instance.Status.Conditions.Set(condition.FalseCondition( | ||
condition.NetworkAttachmentsReadyCondition, | ||
condition.ErrorReason, | ||
condition.SeverityWarning, | ||
condition.NetworkAttachmentsReadyErrorMessage, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes exactly that was the reasone
To follow best practices for error and exception handling in Go and to prevent regressions, err113 and gosec checks in golang-ci-linter are now enabled. Errors now use API errors from k8s.io/apimachinery and predefined messages for status updates.
f7d3ecc
to
10f87cd
Compare
Build failed (check pipeline). Post https://softwarefactory-project.io/zuul/t/rdoproject.org/buildset/d46f9fd903354941affead375c45cedb ✔️ openstack-meta-content-provider SUCCESS in 5h 42m 59s |
To follow best practices for error and exception handling in Go and to prevent regressions,
err113 and gosec checks in golang-ci-linter are now enabled.
Errors now use API errors from k8s.io/apimachinery and predefined messages for status updates.
Resolve: https://issues.redhat.com/browse/OSPRH-9046