From 99e2bde6a18a262a3bde82df40b68d0b1a0ffa52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Wed, 27 Nov 2024 15:25:47 +0000 Subject: [PATCH] bug: adapter duration conversion --- pkg/apis/duration.go | 12 ++++------- pkg/apis/sources/v1alpha1/az_types.go | 2 +- .../v1alpha1/ocimetricssource_types.go | 4 ++-- pkg/flow/reconciler/synchronizer/adapter.go | 3 ++- .../adapter/webhooksource/webhook_test.go | 21 ++++++++----------- .../flow/reconciler/synchronizer/adapter.go | 3 ++- .../reconciler/httppollersource/adapter.go | 3 ++- .../adapter/servicenowtarget/adapter.go | 5 +++-- 8 files changed, 25 insertions(+), 28 deletions(-) diff --git a/pkg/apis/duration.go b/pkg/apis/duration.go index 129f674d..b1395249 100644 --- a/pkg/apis/duration.go +++ b/pkg/apis/duration.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "time" + + "github.com/zeiss/pkg/conv" ) // Duration extends time.Duration with additional methods for (de-)serialization to/from @@ -11,16 +13,10 @@ import ( type Duration time.Duration var ( - _ fmt.Stringer = (*Duration)(nil) _ json.Marshaler = (*Duration)(nil) _ json.Unmarshaler = (*Duration)(nil) ) -// String implements the fmt.Stringer interface. -func (d Duration) String() string { - return time.Duration(d).String() -} - // UnmarshalJSON implements json.Unmarshaler. func (d *Duration) UnmarshalJSON(data []byte) error { var dataStr string @@ -39,6 +35,6 @@ func (d *Duration) UnmarshalJSON(data []byte) error { } // MarshalJSON implements json.Marshaler. -func (d Duration) MarshalJSON() ([]byte, error) { - return []byte(`"` + time.Duration(d).String() + `"`), nil +func (d *Duration) MarshalJSON() ([]byte, error) { + return conv.Bytes(fmt.Sprintf(`"%d"`, d)), nil } diff --git a/pkg/apis/sources/v1alpha1/az_types.go b/pkg/apis/sources/v1alpha1/az_types.go index 8a0cad5c..617618f3 100644 --- a/pkg/apis/sources/v1alpha1/az_types.go +++ b/pkg/apis/sources/v1alpha1/az_types.go @@ -187,7 +187,7 @@ func (rID *AzureResourceID) UnmarshalJSON(data []byte) error { // MarshalJSON implements json.Marshaler // nolint:gocyclo -func (rID AzureResourceID) MarshalJSON() ([]byte, error) { +func (rID *AzureResourceID) MarshalJSON() ([]byte, error) { if rID.SubscriptionID == "" { return nil, errAzureResourceIDEmptyAttrs } diff --git a/pkg/apis/sources/v1alpha1/ocimetricssource_types.go b/pkg/apis/sources/v1alpha1/ocimetricssource_types.go index 8fe48bdd..870b1689 100644 --- a/pkg/apis/sources/v1alpha1/ocimetricssource_types.go +++ b/pkg/apis/sources/v1alpha1/ocimetricssource_types.go @@ -89,8 +89,8 @@ type OCIMetrics struct { type OCIMetricsDecodedList []OCIMetrics // Decode deserializes a list of OCI metrics. -func (o *OCIMetricsDecodedList) Decode(value string) error { - err := json.Unmarshal([]byte(value), o) +func (o OCIMetricsDecodedList) Decode(value string) error { + err := json.Unmarshal([]byte(value), &o) if err != nil { return fmt.Errorf("unable to deserialize metrics: %w", err) } diff --git a/pkg/flow/reconciler/synchronizer/adapter.go b/pkg/flow/reconciler/synchronizer/adapter.go index 70d55925..2ccbb151 100644 --- a/pkg/flow/reconciler/synchronizer/adapter.go +++ b/pkg/flow/reconciler/synchronizer/adapter.go @@ -9,6 +9,7 @@ import ( "knative.dev/pkg/apis" servingv1 "knative.dev/serving/pkg/apis/serving/v1" + "github.com/zeiss/pkg/conv" commonv1alpha1 "github.com/zeiss/typhoon/pkg/apis/common/v1alpha1" "github.com/zeiss/typhoon/pkg/apis/flow/v1alpha1" common "github.com/zeiss/typhoon/pkg/reconciler" @@ -52,7 +53,7 @@ func MakeAppEnv(o *v1alpha1.Synchronizer) []corev1.EnvVar { }, { Name: "RESPONSE_WAIT_TIMEOUT", - Value: o.Spec.Response.Timeout.String(), + Value: conv.String(o.Spec.Response.Timeout), }, } diff --git a/pkg/sources/adapter/webhooksource/webhook_test.go b/pkg/sources/adapter/webhooksource/webhook_test.go index b1bc125b..f02db19b 100644 --- a/pkg/sources/adapter/webhooksource/webhook_test.go +++ b/pkg/sources/adapter/webhooksource/webhook_test.go @@ -28,16 +28,13 @@ const ( tResponseEventSource = "testRespSource" ) -var ( - expectedExtensionsBase = map[string]interface{}{ - "host": tHost, - "method": http.MethodGet, - "path": "/", - } -) +var expectedExtensionsBase = map[string]interface{}{ + "host": tHost, + "method": http.MethodGet, + "path": "/", +} func TestWebhookEvent(t *testing.T) { - logger := zapt.NewLogger(t).Sugar() tc := map[string]struct { @@ -194,15 +191,15 @@ func TestWebhookEvent(t *testing.T) { }, } - req, _ := http.NewRequest(http.MethodGet, "/"+c.query, c.body) + ctx := context.Background() + + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/"+c.query, c.body) for k, v := range c.headers { req.Header.Add(k, v) } req.Host = tHost - ctx := context.Background() - - th := http.HandlerFunc(handler.handleAll(ctx)) + th := handler.handleAll(ctx) rr := httptest.NewRecorder() diff --git a/pkg/sources/flow/reconciler/synchronizer/adapter.go b/pkg/sources/flow/reconciler/synchronizer/adapter.go index 70d55925..2ccbb151 100644 --- a/pkg/sources/flow/reconciler/synchronizer/adapter.go +++ b/pkg/sources/flow/reconciler/synchronizer/adapter.go @@ -9,6 +9,7 @@ import ( "knative.dev/pkg/apis" servingv1 "knative.dev/serving/pkg/apis/serving/v1" + "github.com/zeiss/pkg/conv" commonv1alpha1 "github.com/zeiss/typhoon/pkg/apis/common/v1alpha1" "github.com/zeiss/typhoon/pkg/apis/flow/v1alpha1" common "github.com/zeiss/typhoon/pkg/reconciler" @@ -52,7 +53,7 @@ func MakeAppEnv(o *v1alpha1.Synchronizer) []corev1.EnvVar { }, { Name: "RESPONSE_WAIT_TIMEOUT", - Value: o.Spec.Response.Timeout.String(), + Value: conv.String(o.Spec.Response.Timeout), }, } diff --git a/pkg/sources/reconciler/httppollersource/adapter.go b/pkg/sources/reconciler/httppollersource/adapter.go index 4be7bb2a..68bce433 100644 --- a/pkg/sources/reconciler/httppollersource/adapter.go +++ b/pkg/sources/reconciler/httppollersource/adapter.go @@ -11,6 +11,7 @@ import ( "knative.dev/eventing/pkg/reconciler/source" "knative.dev/pkg/apis" + "github.com/zeiss/pkg/conv" commonv1alpha1 "github.com/zeiss/typhoon/pkg/apis/common/v1alpha1" "github.com/zeiss/typhoon/pkg/apis/sources/v1alpha1" common "github.com/zeiss/typhoon/pkg/reconciler" @@ -77,7 +78,7 @@ func MakeAppEnv(src *v1alpha1.HTTPPollerSource) []corev1.EnvVar { Value: strconv.FormatBool(skipVerify), }, { Name: envHTTPPollerInterval, - Value: src.Spec.Interval.String(), + Value: conv.String(src.Spec.Interval), }} if src.Spec.Headers != nil { diff --git a/pkg/targets/adapter/servicenowtarget/adapter.go b/pkg/targets/adapter/servicenowtarget/adapter.go index ffc087c0..ca2d07e5 100644 --- a/pkg/targets/adapter/servicenowtarget/adapter.go +++ b/pkg/targets/adapter/servicenowtarget/adapter.go @@ -22,7 +22,7 @@ func EnvAccessorCtor() pkgadapter.EnvConfigAccessor { type envAccessor struct { pkgadapter.EnvConfig - instance string `envconfig:"SERVICENOW_INSTANCE"` + instance string `envconfig:"SERVICENOW_INSTANCE" required:"true"` user string `envconfig:"SERVICENOW_BASICAUTH_USER"` password string `envconfig:"SERVICENOW_BASICAUTH_PASSWORD"` } @@ -43,7 +43,7 @@ func NewTarget(ctx context.Context, envAcc pkgadapter.EnvConfigAccessor, client logger := logging.FromContext(ctx) mt := &pkgadapter.MetricTag{ - ResourceGroup: targets.KafkaTargetResource.String(), + ResourceGroup: targets.ServiceNowTargetResource.String(), Namespace: envAcc.GetNamespace(), Name: envAcc.GetName(), } @@ -54,6 +54,7 @@ func NewTarget(ctx context.Context, envAcc pkgadapter.EnvConfigAccessor, client if err != nil { logger.Panic("failed to create authenticator", zap.Error(err)) } + sc := snowgo.New(env.instance, snowgo.WithRequestEditorFn(basicAuth.Intercept)) return &snowAdapter{