Skip to content

Commit

Permalink
bug: adapter duration conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Nov 27, 2024
1 parent b8f6295 commit 99e2bde
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 28 deletions.
12 changes: 4 additions & 8 deletions pkg/apis/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ import (
"encoding/json"
"fmt"
"time"

"github.com/zeiss/pkg/conv"
)

// Duration extends time.Duration with additional methods for (de-)serialization to/from
// JSON, allowing it to be embedded in custom API objects.
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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/apis/sources/v1alpha1/az_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/sources/v1alpha1/ocimetricssource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/flow/reconciler/synchronizer/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
},
}

Expand Down
21 changes: 9 additions & 12 deletions pkg/sources/adapter/webhooksource/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion pkg/sources/flow/reconciler/synchronizer/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
},
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/sources/reconciler/httppollersource/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/targets/adapter/servicenowtarget/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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(),
}
Expand All @@ -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{
Expand Down

0 comments on commit 99e2bde

Please sign in to comment.