mirror of
https://github.com/alibaba/higress.git
synced 2026-03-09 03:00:54 +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
|
||||
|
||||
// nolint
|
||||
import (
|
||||
"path"
|
||||
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/golang/protobuf/ptypes/any"
|
||||
extensions "istio.io/api/extensions/v1alpha1"
|
||||
mcp "istio.io/api/mcp/v1alpha1"
|
||||
networking "istio.io/api/networking/v1alpha3"
|
||||
"istio.io/istio/pilot/pkg/model"
|
||||
"istio.io/istio/pilot/pkg/xds"
|
||||
cfg "istio.io/istio/pkg/config"
|
||||
)
|
||||
|
||||
type ServiceEntryGenerator struct {
|
||||
@@ -33,31 +33,7 @@ type ServiceEntryGenerator struct {
|
||||
|
||||
func (c ServiceEntryGenerator) Generate(proxy *model.Proxy, push *model.PushContext, w *model.WatchedResource,
|
||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||
resources := make([]*any.Any, 0)
|
||||
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
|
||||
return generate(proxy, push.AllServiceEntries, w, updates)
|
||||
}
|
||||
|
||||
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,
|
||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||
resources := make([]*any.Any, 0)
|
||||
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
|
||||
return generate(proxy, push.AllVirtualServices, w, updates)
|
||||
}
|
||||
|
||||
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,
|
||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||
resources := make([]*any.Any, 0)
|
||||
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
|
||||
return generate(proxy, push.AllDestinationRules, w, updates)
|
||||
}
|
||||
|
||||
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,
|
||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||
resources := make([]*any.Any, 0)
|
||||
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
|
||||
return generate(proxy, push.AllEnvoyFilters, w, updates)
|
||||
}
|
||||
|
||||
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,
|
||||
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
|
||||
resources := make([]*any.Any, 0)
|
||||
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
|
||||
return generate(proxy, push.AllGateways, w, updates)
|
||||
}
|
||||
|
||||
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,
|
||||
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) {
|
||||
resources := make([]*any.Any, 0)
|
||||
configs := push.AllWasmplugins
|
||||
for _, config := range configs {
|
||||
body, err := types.MarshalAny(config.Spec.(*extensions.WasmPlugin))
|
||||
body, err := cfg.ToProtoGogo(config.Spec)
|
||||
if err != nil {
|
||||
return nil, model.DefaultXdsLogDetails, err
|
||||
}
|
||||
@@ -246,6 +136,7 @@ func (c WasmpluginGenerator) Generate(proxy *model.Proxy, push *model.PushContex
|
||||
CreateTime: createTime,
|
||||
},
|
||||
}
|
||||
// nolint
|
||||
mcpAny, err := ptypes.MarshalAny(resource)
|
||||
if err != nil {
|
||||
return nil, model.DefaultXdsLogDetails, err
|
||||
@@ -254,9 +145,3 @@ func (c WasmpluginGenerator) Generate(proxy *model.Proxy, push *model.PushContex
|
||||
}
|
||||
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{},
|
||||
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",
|
||||
fn: func() (*model.PushContext, any) {
|
||||
|
||||
Reference in New Issue
Block a user