mirror of
https://github.com/alibaba/higress.git
synced 2026-05-28 22:57:31 +08:00
opt: optimize redundant code (#103)
Signed-off-by: charlie <qianglin98@qq.com>
This commit is contained in:
@@ -14,17 +14,17 @@
|
|||||||
|
|
||||||
package mcp
|
package mcp
|
||||||
|
|
||||||
|
// nolint
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/gogo/protobuf/types"
|
"github.com/gogo/protobuf/types"
|
||||||
"github.com/golang/protobuf/ptypes"
|
"github.com/golang/protobuf/ptypes"
|
||||||
"github.com/golang/protobuf/ptypes/any"
|
"github.com/golang/protobuf/ptypes/any"
|
||||||
extensions "istio.io/api/extensions/v1alpha1"
|
|
||||||
mcp "istio.io/api/mcp/v1alpha1"
|
mcp "istio.io/api/mcp/v1alpha1"
|
||||||
networking "istio.io/api/networking/v1alpha3"
|
|
||||||
"istio.io/istio/pilot/pkg/model"
|
"istio.io/istio/pilot/pkg/model"
|
||||||
"istio.io/istio/pilot/pkg/xds"
|
"istio.io/istio/pilot/pkg/xds"
|
||||||
|
cfg "istio.io/istio/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceEntryGenerator struct {
|
type ServiceEntryGenerator struct {
|
||||||
@@ -33,31 +33,7 @@ type ServiceEntryGenerator struct {
|
|||||||
|
|
||||||
func (c ServiceEntryGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
func (c ServiceEntryGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
resources := make([]*any.Any, 0)
|
return generate(proxy, push.AllServiceEntries, w, updates)
|
||||||
configs := push.AllServiceEntries
|
|
||||||
for _, config := range configs {
|
|
||||||
body, err := types.MarshalAny(config.Spec.(*networking.ServiceEntry))
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
createTime, err := types.TimestampProto(config.CreationTimestamp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resource := &mcp.Resource{
|
|
||||||
Body: body,
|
|
||||||
Metadata: &mcp.Metadata{
|
|
||||||
Name: path.Join(config.Namespace, config.Name),
|
|
||||||
CreateTime: createTime,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
mcpAny, err := ptypes.MarshalAny(resource)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resources = append(resources, mcpAny)
|
|
||||||
}
|
|
||||||
return resources, model.DefaultXdsLogDetails, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ServiceEntryGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
func (c ServiceEntryGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
||||||
@@ -72,31 +48,7 @@ type VirtualServiceGenerator struct {
|
|||||||
|
|
||||||
func (c VirtualServiceGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
func (c VirtualServiceGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
resources := make([]*any.Any, 0)
|
return generate(proxy, push.AllVirtualServices, w, updates)
|
||||||
configs := push.AllVirtualServices
|
|
||||||
for _, config := range configs {
|
|
||||||
body, err := types.MarshalAny(config.Spec.(*networking.VirtualService))
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
createTime, err := types.TimestampProto(config.CreationTimestamp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resource := &mcp.Resource{
|
|
||||||
Body: body,
|
|
||||||
Metadata: &mcp.Metadata{
|
|
||||||
Name: path.Join(config.Namespace, config.Name),
|
|
||||||
CreateTime: createTime,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
mcpAny, err := ptypes.MarshalAny(resource)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resources = append(resources, mcpAny)
|
|
||||||
}
|
|
||||||
return resources, model.DefaultXdsLogDetails, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c VirtualServiceGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
func (c VirtualServiceGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
||||||
@@ -111,31 +63,7 @@ type DestinationRuleGenerator struct {
|
|||||||
|
|
||||||
func (c DestinationRuleGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
func (c DestinationRuleGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
resources := make([]*any.Any, 0)
|
return generate(proxy, push.AllDestinationRules, w, updates)
|
||||||
configs := push.AllDestinationRules
|
|
||||||
for _, config := range configs {
|
|
||||||
body, err := types.MarshalAny(config.Spec.(*networking.DestinationRule))
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
createTime, err := types.TimestampProto(config.CreationTimestamp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resource := &mcp.Resource{
|
|
||||||
Body: body,
|
|
||||||
Metadata: &mcp.Metadata{
|
|
||||||
Name: path.Join(config.Namespace, config.Name),
|
|
||||||
CreateTime: createTime,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
mcpAny, err := ptypes.MarshalAny(resource)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resources = append(resources, mcpAny)
|
|
||||||
}
|
|
||||||
return resources, model.DefaultXdsLogDetails, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c DestinationRuleGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
func (c DestinationRuleGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
||||||
@@ -150,31 +78,7 @@ type EnvoyFilterGenerator struct {
|
|||||||
|
|
||||||
func (c EnvoyFilterGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
func (c EnvoyFilterGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
resources := make([]*any.Any, 0)
|
return generate(proxy, push.AllEnvoyFilters, w, updates)
|
||||||
configs := push.AllEnvoyFilters
|
|
||||||
for _, config := range configs {
|
|
||||||
body, err := types.MarshalAny(config.Spec.(*networking.EnvoyFilter))
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
createTime, err := types.TimestampProto(config.CreationTimestamp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resource := &mcp.Resource{
|
|
||||||
Body: body,
|
|
||||||
Metadata: &mcp.Metadata{
|
|
||||||
Name: path.Join(config.Namespace, config.Name),
|
|
||||||
CreateTime: createTime,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
mcpAny, err := ptypes.MarshalAny(resource)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resources = append(resources, mcpAny)
|
|
||||||
}
|
|
||||||
return resources, model.DefaultXdsLogDetails, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c EnvoyFilterGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
func (c EnvoyFilterGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
||||||
@@ -189,31 +93,7 @@ type GatewayGenerator struct {
|
|||||||
|
|
||||||
func (c GatewayGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
func (c GatewayGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
resources := make([]*any.Any, 0)
|
return generate(proxy, push.AllGateways, w, updates)
|
||||||
configs := push.AllGateways
|
|
||||||
for _, config := range configs {
|
|
||||||
body, err := types.MarshalAny(config.Spec.(*networking.Gateway))
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
createTime, err := types.TimestampProto(config.CreationTimestamp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resource := &mcp.Resource{
|
|
||||||
Body: body,
|
|
||||||
Metadata: &mcp.Metadata{
|
|
||||||
Name: path.Join(config.Namespace, config.Name),
|
|
||||||
CreateTime: createTime,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
mcpAny, err := ptypes.MarshalAny(resource)
|
|
||||||
if err != nil {
|
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
|
||||||
}
|
|
||||||
resources = append(resources, mcpAny)
|
|
||||||
}
|
|
||||||
return resources, model.DefaultXdsLogDetails, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c GatewayGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
func (c GatewayGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
||||||
@@ -227,11 +107,21 @@ type WasmpluginGenerator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c WasmpluginGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
func (c WasmpluginGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||||
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
|
return generate(proxy, push.AllWasmplugins, w, updates)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c WasmpluginGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
||||||
|
w *model.WatchedResource) ([]*any.Any, []string, model.XdsLogDetails, bool, error) {
|
||||||
|
// TODO: delta implement
|
||||||
|
return nil, nil, model.DefaultXdsLogDetails, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generate(proxy *model.Proxy, configs []cfg.Config, w *model.WatchedResource,
|
||||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||||
resources := make([]*any.Any, 0)
|
resources := make([]*any.Any, 0)
|
||||||
configs := push.AllWasmplugins
|
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
body, err := types.MarshalAny(config.Spec.(*extensions.WasmPlugin))
|
body, err := cfg.ToProtoGogo(config.Spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
return nil, model.DefaultXdsLogDetails, err
|
||||||
}
|
}
|
||||||
@@ -246,6 +136,7 @@ func (c WasmpluginGenerator) Generate(proxy *model.Proxy, push *model.PushContex
|
|||||||
CreateTime: createTime,
|
CreateTime: createTime,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
// nolint
|
||||||
mcpAny, err := ptypes.MarshalAny(resource)
|
mcpAny, err := ptypes.MarshalAny(resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, model.DefaultXdsLogDetails, err
|
return nil, model.DefaultXdsLogDetails, err
|
||||||
@@ -254,9 +145,3 @@ func (c WasmpluginGenerator) Generate(proxy *model.Proxy, push *model.PushContex
|
|||||||
}
|
}
|
||||||
return resources, model.DefaultXdsLogDetails, nil
|
return resources, model.DefaultXdsLogDetails, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c WasmpluginGenerator) GenerateDeltas(proxy *model.Proxy, push *model.PushContext, updates *model.PushRequest,
|
|
||||||
w *model.WatchedResource) ([]*any.Any, []string, model.XdsLogDetails, bool, error) {
|
|
||||||
// TODO: delta implement
|
|
||||||
return nil, nil, model.DefaultXdsLogDetails, false, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -101,6 +101,19 @@ func TestGenerate(t *testing.T) {
|
|||||||
generator: WasmpluginGenerator{},
|
generator: WasmpluginGenerator{},
|
||||||
isErr: false,
|
isErr: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "ServiceEntry",
|
||||||
|
fn: func() (*model.PushContext, any) {
|
||||||
|
ctx := model.NewPushContext()
|
||||||
|
cfg := config.Config{
|
||||||
|
Spec: &networking.ServiceEntry{},
|
||||||
|
}
|
||||||
|
ctx.AllServiceEntries = []config.Config{cfg}
|
||||||
|
return ctx, cfg.Spec
|
||||||
|
},
|
||||||
|
generator: ServiceEntryGenerator{},
|
||||||
|
isErr: false,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "WasmPlugin with wrong config",
|
name: "WasmPlugin with wrong config",
|
||||||
fn: func() (*model.PushContext, any) {
|
fn: func() (*model.PushContext, any) {
|
||||||
|
|||||||
Reference in New Issue
Block a user