Skip to content

Commit 2aed07c

Browse files
author
Per Goncalves da Silva
committed
surface error rather than panic in the validatingroundtripper
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent ca4e6d4 commit 2aed07c

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

pkg/controller/operators/catalog/operator.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"sync"
1212
"time"
1313

14-
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
15-
1614
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/labeller"
1715
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/validatingroundtripper"
1816
errorwrap "github.com/pkg/errors"
@@ -151,8 +149,6 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
151149
}
152150

153151
// create a config that validates we're creating objects with labels
154-
_ = apiextensionsv1.AddToScheme(scheme) // required by opClient
155-
_ = apiregistrationv1.AddToScheme(scheme) // required by opClient
156152
validatingConfig := validatingroundtripper.Wrap(config, scheme)
157153

158154
// Create a new client for dynamic types (CRs)

pkg/controller/operators/validatingroundtripper/validating_round_tripper.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,29 @@ func (rt *validatingRoundTripper) decodeProtobuf(body io.Reader) (*unstructured.
4949
return &unstructured.Unstructured{Object: unstructuredObj}, nil
5050
}
5151

52-
func (rt *validatingRoundTripper) decodeRequestBody(req *http.Request) *unstructured.Unstructured {
52+
func (rt *validatingRoundTripper) decodeRequestBody(req *http.Request) (*unstructured.Unstructured, error) {
5353
b, err := req.GetBody()
5454
if err != nil {
5555
panic(fmt.Errorf("failed to get request body: %w", err))
5656
}
5757
defer b.Close()
5858

59-
var unstructuredObject *unstructured.Unstructured
6059
switch req.Header.Get("Content-Type") {
6160
case "application/vnd.kubernetes.protobuf":
62-
unstructuredObject, err = rt.decodeProtobuf(b)
61+
return rt.decodeProtobuf(b)
6362
default:
64-
unstructuredObject, err = rt.decodeYAMLOrJSON(b)
63+
return rt.decodeYAMLOrJSON(b)
6564
}
66-
67-
if err != nil {
68-
panic(fmt.Errorf("failed to decode request body: %w", err))
69-
}
70-
71-
return unstructuredObject
7265
}
7366

7467
func (rt *validatingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
7568
if req.Method == "POST" {
76-
unstructuredObject := rt.decodeRequestBody(req)
69+
unstructuredObject, err := rt.decodeRequestBody(req)
70+
71+
if err != nil {
72+
return nil, err
73+
}
74+
7775
gvk := unstructuredObject.GroupVersionKind()
7876
if gvk.Kind != "Event" {
7977
labels := unstructuredObject.GetLabels()

0 commit comments

Comments
 (0)