References: [1] #2193 [2] https://fissionio.slack.com/archives/C3LUX6BBP/p1631706812069300 Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
/*
|
|
Copyright 2016 The Fission Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
"go.uber.org/zap"
|
|
"golang.org/x/net/context/ctxhttp"
|
|
|
|
"github.com/fission/fission/pkg/builder"
|
|
ferror "github.com/fission/fission/pkg/error"
|
|
otelUtils "github.com/fission/fission/pkg/utils/otel"
|
|
)
|
|
|
|
type (
|
|
Client struct {
|
|
logger *zap.Logger
|
|
url string
|
|
httpClient *http.Client
|
|
}
|
|
)
|
|
|
|
func MakeClient(logger *zap.Logger, builderUrl string) *Client {
|
|
hc := &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
|
|
return &Client{
|
|
logger: logger.Named("builder_client"),
|
|
url: strings.TrimSuffix(builderUrl, "/"),
|
|
httpClient: hc,
|
|
}
|
|
}
|
|
|
|
func (c *Client) Build(ctx context.Context, req *builder.PackageBuildRequest) (*builder.PackageBuildResponse, error) {
|
|
logger := otelUtils.LoggerWithTraceID(ctx, c.logger)
|
|
|
|
body, err := json.Marshal(req)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error marshaling json")
|
|
}
|
|
|
|
maxRetries := 20
|
|
var resp *http.Response
|
|
|
|
for i := 0; i < maxRetries; i++ {
|
|
resp, err = ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", bytes.NewReader(body))
|
|
if err == nil {
|
|
if resp.StatusCode == 200 {
|
|
break
|
|
}
|
|
err = ferror.MakeErrorFromHTTP(resp)
|
|
}
|
|
|
|
if i < maxRetries-1 {
|
|
time.Sleep(50 * time.Duration(2*i) * time.Millisecond)
|
|
logger.Error("error building package, retrying", zap.Error(err))
|
|
continue
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
rBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
logger.Error("error reading resp body", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
pkgBuildResp := builder.PackageBuildResponse{}
|
|
err = json.Unmarshal(rBody, &pkgBuildResp)
|
|
if err != nil {
|
|
logger.Error("error parsing resp body", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return &pkgBuildResp, ferror.MakeErrorFromHTTP(resp)
|
|
}
|