Skip to content

Commit

Permalink
update structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan committed Jun 6, 2022
1 parent e48eafd commit a8e7e86
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 66 deletions.
4 changes: 2 additions & 2 deletions pkg/apisix/apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ type PluginConfig interface {
type UpstreamServiceRelation interface {
Get(context.Context, string) (*v1.UpstreamServiceRelation, error)
List(context.Context) ([]*v1.UpstreamServiceRelation, error)
Delete(context.Context, string) error
Create(context.Context, string) error
Delete(context.Context, *v1.UpstreamServiceRelation) error
Create(context.Context, *v1.UpstreamServiceRelation) error
}

type apisix struct {
Expand Down
16 changes: 4 additions & 12 deletions pkg/apisix/cache/memdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,7 @@ func TestMemDBCacheUpstreamServiceRelation(t *testing.T) {
assert.Nil(t, err, "NewMemDBCache")

us1 := &v1.UpstreamServiceRelation{
Metadata: v1.Metadata{
ID: "1",
Name: "abc",
},
ServiceName: "1",
}
assert.Nil(t, c.InsertUpstreamServiceRelation(us1), "inserting route 1")

Expand All @@ -480,10 +477,7 @@ func TestMemDBCacheUpstreamServiceRelation(t *testing.T) {
assert.Equal(t, us1, us)

us2 := &v1.UpstreamServiceRelation{
Metadata: v1.Metadata{
ID: "2",
Name: "abc",
},
ServiceName: "2",
}
assert.Nil(t, c.InsertUpstreamServiceRelation(us2), "inserting route 1")

Expand All @@ -496,10 +490,8 @@ func TestMemDBCacheUpstreamServiceRelation(t *testing.T) {
assert.Len(t, uss, 2)

us3 := &v1.UpstreamServiceRelation{
Metadata: v1.Metadata{
ID: "httpbin",
},
UpstreamId: "upstream",
ServiceName: "httpbin",
UpstreamName: "upstream",
}
assert.Nil(t, c.InsertUpstreamServiceRelation(us3), "inserting route 1")

Expand Down
2 changes: 1 addition & 1 deletion pkg/apisix/cache/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var (
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "ID"},
Indexer: &memdb.StringFieldIndex{Field: "ServiceName"},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/apisix/nonexistentclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ type dummyUpstreamServiceRelation struct {
func (f *dummyUpstreamServiceRelation) Get(_ context.Context, _ string) (*v1.UpstreamServiceRelation, error) {
return nil, ErrClusterNotExist
}
func (f *dummyUpstreamServiceRelation) Create(_ context.Context, _ string) error {
func (f *dummyUpstreamServiceRelation) Create(_ context.Context, _ *v1.UpstreamServiceRelation) error {
return ErrClusterNotExist
}
func (f *dummyUpstreamServiceRelation) List(_ context.Context) ([]*v1.UpstreamServiceRelation, error) {
return nil, ErrClusterNotExist
}
func (f *dummyUpstreamServiceRelation) Delete(_ context.Context, _ string) error {
func (f *dummyUpstreamServiceRelation) Delete(_ context.Context, _ *v1.UpstreamServiceRelation) error {
return ErrClusterNotExist
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/apisix/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (u *upstreamClient) Create(ctx context.Context, obj *v1.Upstream) (*v1.Upst
zap.String("cluster", "default"),
)

if err := u.cluster.upstreamServiceRelation.Create(ctx, obj.Name); err != nil {
if err := u.cluster.upstreamServiceRelation.Create(ctx, &v1.UpstreamServiceRelation{UpstreamName: obj.Name}); err != nil {
log.Errorf("failed to reflect upstreamService create to cache: %s", err)
}
if err := u.cluster.HasSynced(ctx); err != nil {
Expand Down Expand Up @@ -178,7 +178,7 @@ func (u *upstreamClient) Delete(ctx context.Context, obj *v1.Upstream) error {
zap.String("url", u.url),
)

if err := u.cluster.upstreamServiceRelation.Delete(ctx, obj.Name); err != nil {
if err := u.cluster.upstreamServiceRelation.Delete(ctx, &v1.UpstreamServiceRelation{UpstreamName: obj.Name}); err != nil {
log.Errorf("failed to delete upstreamService in cache: %s", err)
}
if err := u.cluster.HasSynced(ctx); err != nil {
Expand Down Expand Up @@ -207,7 +207,7 @@ func (u *upstreamClient) Update(ctx context.Context, obj *v1.Upstream) (*v1.Upst
zap.String("url", u.url),
)

if err := u.cluster.upstreamServiceRelation.Create(ctx, obj.Name); err != nil {
if err := u.cluster.upstreamServiceRelation.Create(ctx, &v1.UpstreamServiceRelation{UpstreamName: obj.Name}); err != nil {
log.Errorf("failed to reflect upstreamService create to cache: %s", err)
}
if err := u.cluster.HasSynced(ctx); err != nil {
Expand Down
87 changes: 45 additions & 42 deletions pkg/apisix/upstreamservicerelation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package apisix

import (
"context"
"fmt"
"strings"

"go.uber.org/zap"
Expand Down Expand Up @@ -55,55 +56,58 @@ func (u *upstreamService) Get(ctx context.Context, svcId string) (*v1.UpstreamSe
return nil, err
}

func (u *upstreamService) Delete(ctx context.Context, svcId string) error {
func (u *upstreamService) Delete(ctx context.Context, relation *v1.UpstreamServiceRelation) error {
log.Debugw("try to delete upstreamService in cache",
zap.String("svcId", svcId),
zap.String("cluster", "default"),
)
us, err := u.cluster.cache.GetUpstreamServiceRelation(svcId)
if err != nil {
return err
u.initUpstreamServiceRelation(relation)
if relation == nil || relation.ServiceName == "" && relation.UpstreamName == "" {
return fmt.Errorf("UpstreamServiceRelation is empty object")
}
ups, err := u.cluster.upstream.Get(ctx, us.UpstreamId)
if err != nil {
log.Errorf("failed to get upstream in cache: %s", err)
return err
}
ups.Nodes = make(v1.UpstreamNodes, 0)
_, err = u.cluster.upstream.Update(ctx, ups)
if err != nil {
return err
if relation.UpstreamName != "" {
err := u.cluster.cache.DeleteUpstreamServiceRelation(relation)
if err != nil {
return err
}
} else {
usr, err := u.cluster.cache.GetUpstreamServiceRelation(relation.ServiceName)
if err != nil {
return err
}
ups, err := u.cluster.upstream.Get(ctx, usr.UpstreamName)
if err != nil {
return err
}
ups.Nodes = make(v1.UpstreamNodes, 0)
_, err = u.cluster.upstream.Update(ctx, ups)
if err != nil {
return err
}
err = u.cluster.cache.DeleteUpstreamServiceRelation(usr)
if err != nil {
return err
}
}
err = u.cluster.cache.DeleteUpstreamServiceRelation(us)
return err
return nil
}

func (u *upstreamService) Create(ctx context.Context, upstreamName string) error {
func (u *upstreamService) Create(ctx context.Context, relation *v1.UpstreamServiceRelation) error {
log.Debugw("try to create upstreamService in cache",
zap.String("upstreamName", upstreamName),
zap.String("cluster", "default"),
)
upsId, svcId := u.parseUpstreamName(upstreamName)
if upsId == "" || svcId == "" {
log.Error("failed to parse upstreamName",
zap.String("upstreamName", upstreamName),
)
u.initUpstreamServiceRelation(relation)
if relation == nil || relation.ServiceName == "" || relation.UpstreamName == "" {
log.Error("UpstreamServiceRelation object ")
return nil
}
us, err := u.cluster.cache.GetUpstreamServiceRelation(svcId)
if err != nil {
if err != cache.ErrNotFound {
return err
}
us = &v1.UpstreamServiceRelation{
Metadata: v1.Metadata{
ID: svcId,
},
UpstreamId: upsId,
}
us, err := u.cluster.cache.GetUpstreamServiceRelation(relation.ServiceName)
if err != nil && err != cache.ErrNotFound {
return err
}
if us != nil {
us.UpstreamId = upsId
us.UpstreamName = relation.UpstreamName
} else {
us = relation
}
if err := u.cluster.cache.InsertUpstreamServiceRelation(us); err != nil {
log.Errorf("failed to reflect upstreamService create to cache: %s", err)
Expand All @@ -126,15 +130,14 @@ func (u *upstreamService) List(ctx context.Context) ([]*v1.UpstreamServiceRelati
return usrs, nil
}

func (u *upstreamService) parseUpstreamName(upsName string) (upsId string, svcId string) {
log.Debugw("try to parse upstreamName",
zap.String("upstreamName", upsName),
zap.String("cluster", "default"),
)
args := strings.Split(upsName, "_")
func (u *upstreamService) initUpstreamServiceRelation(us *v1.UpstreamServiceRelation) {
if us.UpstreamName == "" || us.ServiceName != "" {
return
}
args := strings.Split(us.UpstreamName, "_")
// namespace_service_subcret_port
if len(args) < 2 {
return
}
return upsName, args[0] + "_" + args[1]
us.ServiceName = args[0] + "_" + args[1]
}
5 changes: 4 additions & 1 deletion pkg/ingress/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/apache/apisix-ingress-controller/pkg/kube"
"github.com/apache/apisix-ingress-controller/pkg/log"
"github.com/apache/apisix-ingress-controller/pkg/types"
v1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
)

type endpointsController struct {
Expand Down Expand Up @@ -94,7 +95,9 @@ func (c *endpointsController) sync(ctx context.Context, ev *types.Event) error {
}
if ev.Type == types.EventDelete {
clusterName := c.controller.cfg.APISIX.DefaultClusterName
err := c.controller.apisix.Cluster(clusterName).UpstreamServiceRelation().Delete(ctx, ep.Namespace()+"_"+ep.ServiceName())
err := c.controller.apisix.Cluster(clusterName).UpstreamServiceRelation().Delete(ctx, &v1.UpstreamServiceRelation{
ServiceName: ep.Namespace() + "_" + ep.ServiceName(),
})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/apisix/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ type PluginConfig struct {
// UpstreamServiceRelation Upstream association object
// +k8s:deepcopy-gen=true
type UpstreamServiceRelation struct {
Metadata `json:",inline" yaml:",inline"`
UpstreamId string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"`
ServiceName string `json:"service_name" yaml:"service_name"`
UpstreamName string `json:"upstream_name,omitempty" yaml:"upstream_name,omitempty"`
}

// NewDefaultUpstream returns an empty Upstream with default values.
Expand Down
1 change: 0 additions & 1 deletion pkg/types/apisix/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8e7e86

Please sign in to comment.