Skip to content

Commit

Permalink
chore: return object in client.Update method
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers committed Jan 6, 2021
1 parent e5849c5 commit 44d6323
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 30 deletions.
8 changes: 4 additions & 4 deletions pkg/apisix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Route interface {
List(context.Context, string) ([]*v1.Route, error)
Create(context.Context, *v1.Route) (*v1.Route, error)
Delete(context.Context, *v1.Route) error
Update(context.Context, *v1.Route) error
Update(context.Context, *v1.Route) (*v1.Route, error)
}

// SSL is the specific client interface to take over the create, update,
Expand All @@ -58,7 +58,7 @@ type SSL interface {
List(context.Context, string) ([]*v1.Ssl, error)
Create(context.Context, *v1.Ssl) (*v1.Ssl, error)
Delete(context.Context, *v1.Ssl) error
Update(context.Context, *v1.Ssl) error
Update(context.Context, *v1.Ssl) (*v1.Ssl, error)
}

// Upstream is the specific client interface to take over the create, update,
Expand All @@ -67,7 +67,7 @@ type Upstream interface {
List(context.Context, string) ([]*v1.Upstream, error)
Create(context.Context, *v1.Upstream) (*v1.Upstream, error)
Delete(context.Context, *v1.Upstream) error
Update(context.Context, *v1.Upstream) error
Update(context.Context, *v1.Upstream) (*v1.Upstream, error)
}

// Service is the specific client interface to take over the create, update,
Expand All @@ -76,7 +76,7 @@ type Service interface {
List(context.Context, string) ([]*v1.Service, error)
Create(context.Context, *v1.Service) (*v1.Service, error)
Delete(context.Context, *v1.Service) error
Update(context.Context, *v1.Service) error
Update(context.Context, *v1.Service) (*v1.Service, error)
}

type client struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apisix/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type createResponse struct {
Item item `json:"node"`
}

type updateResponse = createResponse

type node struct {
Key string `json:"key"`
Items items `json:"nodes"`
Expand Down
14 changes: 11 additions & 3 deletions pkg/apisix/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *routeClient) Delete(ctx context.Context, obj *v1.Route) error {
return r.stub.deleteResource(ctx, url)
}

func (r *routeClient) Update(ctx context.Context, obj *v1.Route) error {
func (r *routeClient) Update(ctx context.Context, obj *v1.Route) (*v1.Route, error) {
log.Infof("update route, id:%s", *obj.ID)
body, err := json.Marshal(routeReqBody{
Desc: obj.Name,
Expand All @@ -117,8 +117,16 @@ func (r *routeClient) Update(ctx context.Context, obj *v1.Route) error {
Plugins: obj.Plugins,
})
if err != nil {
return err
return nil, err
}
url := r.url + "/" + *obj.ID
return r.stub.updateResource(ctx, url, bytes.NewReader(body))
resp, err := r.stub.updateResource(ctx, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
var group string
if obj.Group != nil {
group = *obj.Group
}
return resp.Item.route(group)
}
4 changes: 3 additions & 1 deletion pkg/apisix/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func (srv *fakeAPISIXRouteSrv) ServeHTTP(w http.ResponseWriter, r *http.Request)
srv.route[id] = data

w.WriteHeader(http.StatusOK)
output := fmt.Sprintf(`{"action": "compareAndSwap", "node": {"key": "%s", "value": %s}}`, id, string(data))
_, _ = w.Write([]byte(output))
return
}
}
Expand Down Expand Up @@ -216,7 +218,7 @@ func TestRouteClient(t *testing.T) {
// Patch then List
id = "112"
objId := "2"
err = cli.Update(context.Background(), &v1.Route{
_, err = cli.Update(context.Background(), &v1.Route{
ID: &objId,
Host: &host,
Path: &uri,
Expand Down
14 changes: 11 additions & 3 deletions pkg/apisix/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *serviceClient) Delete(ctx context.Context, obj *v1.Service) error {
return s.stub.deleteResource(ctx, url)
}

func (s *serviceClient) Update(ctx context.Context, obj *v1.Service) error {
func (s *serviceClient) Update(ctx context.Context, obj *v1.Service) (*v1.Service, error) {
log.Infof("update service, id:%s", *obj.ID)

body, err := json.Marshal(serviceItem{
Expand All @@ -108,9 +108,17 @@ func (s *serviceClient) Update(ctx context.Context, obj *v1.Service) error {
Desc: obj.Name,
})
if err != nil {
return err
return nil, err
}

url := s.url + "/" + *obj.ID
return s.stub.updateResource(ctx, url, bytes.NewReader(body))
resp, err := s.stub.updateResource(ctx, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
var group string
if obj.Group != nil {
group = *obj.Group
}
return resp.Item.service(group)
}
4 changes: 3 additions & 1 deletion pkg/apisix/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (srv *fakeAPISIXServiceSrv) ServeHTTP(w http.ResponseWriter, r *http.Reques
srv.service[id] = data

w.WriteHeader(http.StatusOK)
output := fmt.Sprintf(`{"action": "compareAndSwap", "node": {"key": "%s", "value": %s}}`, id, string(data))
_, _ = w.Write([]byte(output))
return
}
}
Expand Down Expand Up @@ -192,7 +194,7 @@ func TestServiceClient(t *testing.T) {
// Patch then List
upsId = "14"
objId := "2"
err = cli.Update(context.Background(), &v1.Service{
_, err = cli.Update(context.Background(), &v1.Service{
ID: &objId,
FullName: &fullName,
Group: &group,
Expand Down
14 changes: 11 additions & 3 deletions pkg/apisix/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (t *sslClient) Delete(ctx context.Context, obj *v1.Ssl) error {
return t.stub.deleteResource(ctx, url)
}

func (t *sslClient) Update(ctx context.Context, obj *v1.Ssl) error {
func (t *sslClient) Update(ctx context.Context, obj *v1.Ssl) (*v1.Ssl, error) {
log.Infof("update ssl, id:%s", *obj.ID)
url := t.url + "/" + *obj.ID
data, err := json.Marshal(v1.Ssl{
Expand All @@ -110,7 +110,15 @@ func (t *sslClient) Update(ctx context.Context, obj *v1.Ssl) error {
Status: obj.Status,
})
if err != nil {
return err
return nil, err
}
return t.stub.updateResource(ctx, url, bytes.NewReader(data))
resp, err := t.stub.updateResource(ctx, url, bytes.NewReader(data))
if err != nil {
return nil, err
}
var group string
if obj.Group != nil {
group = *obj.Group
}
return resp.Item.ssl(group)
}
4 changes: 3 additions & 1 deletion pkg/apisix/ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (srv *fakeAPISIXSSLSrv) ServeHTTP(w http.ResponseWriter, r *http.Request) {
srv.ssl[id] = data

w.WriteHeader(http.StatusOK)
output := fmt.Sprintf(`{"action": "compareAndSwap", "node": {"key": "%s", "value": %s}}`, id, string(data))
_, _ = w.Write([]byte(output))
return
}
}
Expand Down Expand Up @@ -185,7 +187,7 @@ func TestSSLClient(t *testing.T) {
// Patch then List
objId := "2"
sni = "foo.com"
err = cli.Update(context.Background(), &v1.Ssl{
_, err = cli.Update(context.Background(), &v1.Ssl{
ID: &objId,
Snis: []*string{&sni},
})
Expand Down
15 changes: 10 additions & 5 deletions pkg/apisix/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,26 @@ func (s *stub) createResource(ctx context.Context, url string, body io.Reader) (
return &cr, nil
}

func (s *stub) updateResource(ctx context.Context, url string, body io.Reader) error {
func (s *stub) updateResource(ctx context.Context, url string, body io.Reader) (*updateResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, body)
if err != nil {
return err
return nil, err
}
resp, err := s.do(req)
if err != nil {
return err
return nil, err
}
defer drainBody(resp.Body, url)

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
var ur updateResponse
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&ur); err != nil {
return nil, err
}
return &ur, nil
}

func (s *stub) deleteResource(ctx context.Context, url string) error {
Expand Down
14 changes: 11 additions & 3 deletions pkg/apisix/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (u *upstreamClient) Delete(ctx context.Context, obj *v1.Upstream) error {
return u.stub.deleteResource(ctx, url)
}

func (u *upstreamClient) Update(ctx context.Context, obj *v1.Upstream) error {
func (u *upstreamClient) Update(ctx context.Context, obj *v1.Upstream) (*v1.Upstream, error) {
log.Infof("update upstream, id:%s", *obj.ID)

// TODO Just pass the node array.
Expand All @@ -138,9 +138,17 @@ func (u *upstreamClient) Update(ctx context.Context, obj *v1.Upstream) error {
Desc: obj.Name,
})
if err != nil {
return err
return nil, err
}

url := u.url + "/" + *obj.ID
return u.stub.updateResource(ctx, url, bytes.NewReader(body))
resp, err := u.stub.updateResource(ctx, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
var group string
if obj.Group != nil {
group = *obj.Group
}
return resp.Item.upstream(group)
}
4 changes: 3 additions & 1 deletion pkg/apisix/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func (srv *fakeAPISIXUpstreamSrv) ServeHTTP(w http.ResponseWriter, r *http.Reque
srv.upstream[id] = data

w.WriteHeader(http.StatusOK)
output := fmt.Sprintf(`{"action": "compareAndSwap", "node": {"key": "%s", "value": %s}}`, id, string(data))
_, _ = w.Write([]byte(output))
return
}
}
Expand Down Expand Up @@ -208,7 +210,7 @@ func TestUpstreamClient(t *testing.T) {
// Patch then List
lbType = "chash"
objId := "2"
err = cli.Update(context.Background(), &v1.Upstream{
_, err = cli.Update(context.Background(), &v1.Upstream{
ID: &objId,
FullName: &fullName,
Group: &group,
Expand Down
3 changes: 2 additions & 1 deletion pkg/seven/apisix/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ func PatchNodes(upstream *v1.Upstream, nodes []*v1.Node) error {
// Restore it
upstream.Nodes = oldNodes
}()
return conf.Client.Upstream().Update(context.TODO(), upstream)
_, err := conf.Client.Upstream().Update(context.TODO(), upstream)
return err
}
4 changes: 2 additions & 2 deletions pkg/seven/state/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (r *routeWorker) sync() {
return
}
// 2. sync apisix
if err := conf.Client.Route().Update(context.TODO(), r.Route); err != nil {
if _, err := conf.Client.Route().Update(context.TODO(), r.Route); err != nil {
log.Errorf("failed to update route %s: %s, ", *r.Name, err)
}
} else {
Expand Down Expand Up @@ -196,7 +196,7 @@ func SolverUpstream(upstreams []*v1.Upstream, swg ServiceWorkerGroup) {
return
}
// 2.sync apisix
if err = conf.Client.Upstream().Update(context.TODO(), u); err != nil {
if _, err = conf.Client.Upstream().Update(context.TODO(), u); err != nil {
glog.Errorf("solver upstream failed, update upstream to etcd failed, err: %+v", err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/seven/state/service_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func SolverService(services []*v1.Service, rwg RouteWorkerGroup) error {
// todo log error
}
// 2. sync apisix
if err := conf.Client.Service().Update(context.TODO(), svc); err != nil {
if _, err := conf.Client.Service().Update(context.TODO(), svc); err != nil {
log.Errorf("failed to update service: %s, id:%s", err, *svc.ID)
} else {
log.Infof("updated service, id:%s, upstream_id:%s", *svc.ID, *svc.UpstreamId)
Expand Down
3 changes: 2 additions & 1 deletion pkg/seven/state/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func SyncSsl(ssl *v1.Ssl, method string) error {
_, err := conf.Client.SSL().Create(context.TODO(), ssl)
return err
case Update:
return conf.Client.SSL().Update(context.TODO(), ssl)
_, err := conf.Client.SSL().Update(context.TODO(), ssl)
return err
case Delete:
return conf.Client.SSL().Delete(context.TODO(), ssl)
}
Expand Down

0 comments on commit 44d6323

Please sign in to comment.