Skip to content

Commit

Permalink
t
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander committed Mar 13, 2024
1 parent f38ea50 commit 761a4aa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
4 changes: 2 additions & 2 deletions controller/internal/controller/output/mcp_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (o *mcpOutput) WriteEnvoyFilters(ctx context.Context, src procession.Config
o.envoyFilters.Range(func(_, value interface{}) bool {
efs := value.(map[string]*istiov1a3.EnvoyFilter)
for name, ef := range efs {
res, err := MarshalToMcpPb(name, &ef.Spec)
res, err := MarshalToMcpPb(name, &ef.ObjectMeta, &ef.Spec)
if err != nil {
o.logger.Error(err, "failed to marshal EnvoyFilter", "name", name)

Check warning on line 97 in controller/internal/controller/output/mcp_output.go

View check run for this annotation

Codecov / codecov/patch

controller/internal/controller/output/mcp_output.go#L97

Added line #L97 was not covered by tests
// do not push partial configuration, this may cause service unavailable
Expand All @@ -113,7 +113,7 @@ func (o *mcpOutput) WriteEnvoyFilters(ctx context.Context, src procession.Config
func (o *mcpOutput) WriteServiceEntries(ctx context.Context, src procession.ConfigSource, serviceEntries map[string]*istioapi.ServiceEntry) {
ress := make([]*anypb.Any, 0, len(serviceEntries))
for name, se := range serviceEntries {
res, err := MarshalToMcpPb(name, se)
res, err := MarshalToMcpPb(name, nil, se)
if err != nil {
o.logger.Error(err, "failed to marshal ServiceEntry", "name", name)

Check warning on line 118 in controller/internal/controller/output/mcp_output.go

View check run for this annotation

Codecov / codecov/patch

controller/internal/controller/output/mcp_output.go#L113-L118

Added lines #L113 - L118 were not covered by tests
// do not push partial configuration, this may cause service unavailable
Expand Down
8 changes: 7 additions & 1 deletion controller/internal/controller/output/mcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
mcpapi "istio.io/api/mcp/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"mosn.io/htnn/controller/internal/config"
)

func MarshalToMcpPb(name string, src proto.Message) (*anypb.Any, error) {
func MarshalToMcpPb(name string, meta *metav1.ObjectMeta, src proto.Message) (*anypb.Any, error) {
body := &anypb.Any{}
if err := anypb.MarshalFrom(body, src, proto.MarshalOptions{}); err != nil {
return nil, fmt.Errorf("failed to marshal mcp body: %w", err)

Check warning on line 42 in controller/internal/controller/output/mcp_server.go

View check run for this annotation

Codecov / codecov/patch

controller/internal/controller/output/mcp_server.go#L42

Added line #L42 was not covered by tests
Expand All @@ -49,6 +50,11 @@ func MarshalToMcpPb(name string, src proto.Message) (*anypb.Any, error) {
Body: body,
}

if meta != nil {
mcpRes.Metadata.Labels = meta.Labels
mcpRes.Metadata.Annotations = meta.Annotations
}

pb := &anypb.Any{}
if err := anypb.MarshalFrom(pb, mcpRes, proto.MarshalOptions{}); err != nil {
return nil, fmt.Errorf("failed to marshal mcp resource: %w", err)

Check warning on line 60 in controller/internal/controller/output/mcp_server.go

View check run for this annotation

Codecov / codecov/patch

controller/internal/controller/output/mcp_server.go#L60

Added line #L60 was not covered by tests
Expand Down
57 changes: 34 additions & 23 deletions controller/tests/integration/controller/consumer_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,42 @@ var _ = Describe("Consumer controller", func() {
Expect(cs[0].Reason).To(Equal(string(mosniov1.ReasonAccepted)))

var envoyfilters istiov1a3.EnvoyFilterList
marshaledCfg := map[string]map[string]map[string]interface{}{}
Eventually(func() bool {
if err := k8sClient.List(ctx, &envoyfilters); err != nil {
return false
}
return len(envoyfilters.Items) == 1 && envoyfilters.Items[0].Namespace == "istio-system"
}, timeout, interval).Should(BeTrue())
if len(envoyfilters.Items) != 1 {
return false
}
ef := envoyfilters.Items[0]
if ef.Namespace != "istio-system" || ef.Name != "htnn-consumer" {
return false
}

ef := envoyfilters.Items[0]
Expect(ef.Namespace).To(Equal("istio-system"))
Expect(ef.Name).To(Equal("htnn-consumer"))
fmt.Printf("AAA %+v\n", ef.Spec.ConfigPatches)
Expect(len(ef.Spec.ConfigPatches)).To(Equal(2))
cp := ef.Spec.ConfigPatches[0]
Expect(cp.ApplyTo).To(Equal(istioapi.EnvoyFilter_EXTENSION_CONFIG))
value := cp.Patch.Value.AsMap()
Expect(value["name"]).To(Equal("htnn-consumer"))
typedCfg := value["typed_config"].(map[string]interface{})
pluginCfg := typedCfg["plugin_config"].(map[string]interface{})
if len(ef.Spec.ConfigPatches) != 2 {
return false
}
cp := ef.Spec.ConfigPatches[0]
if cp.ApplyTo != istioapi.EnvoyFilter_EXTENSION_CONFIG {
return false
}

value := cp.Patch.Value.AsMap()
if value["name"] != "htnn-consumer" {
return false
}
typedCfg := value["typed_config"].(map[string]interface{})
pluginCfg := typedCfg["plugin_config"].(map[string]interface{})

b, _ := json.Marshal(pluginCfg["value"])
json.Unmarshal(b, &marshaledCfg)
// mapping is namespace -> name -> config
fmt.Printf("OO %+v\n", marshaledCfg)
return marshaledCfg["default"]["spacewander"] != nil &&
marshaledCfg["default"]["unchanged"] != nil
}, timeout, interval).Should(BeTrue())

marshaledCfg := map[string]map[string]map[string]interface{}{}
b, _ := json.Marshal(pluginCfg["value"])
json.Unmarshal(b, &marshaledCfg)
// mapping is namespace -> name -> config
Expect(marshaledCfg["default"]["spacewander"]).ToNot(BeNil())
Expect(marshaledCfg["default"]["unchanged"]).ToNot(BeNil())
d := marshaledCfg["default"]["spacewander"]["d"].(string)
cfg := map[string]interface{}{}
err := json.Unmarshal([]byte(d), &cfg)
Expand Down Expand Up @@ -164,12 +175,12 @@ var _ = Describe("Consumer controller", func() {
return len(envoyfilters.Items) == 1
}, timeout, interval).Should(BeTrue())

value = envoyfilters.Items[0].Spec.ConfigPatches[0].Patch.Value.AsMap()
typedCfg = value["typed_config"].(map[string]interface{})
pluginCfg = typedCfg["plugin_config"].(map[string]interface{})
value := envoyfilters.Items[0].Spec.ConfigPatches[0].Patch.Value.AsMap()
typedCfg := value["typed_config"].(map[string]interface{})
pluginCfg := typedCfg["plugin_config"].(map[string]interface{})

marshaledCfg = map[string]map[string]map[string]interface{}{}
b, _ = json.Marshal(pluginCfg["value"])
b, _ := json.Marshal(pluginCfg["value"])
json.Unmarshal(b, &marshaledCfg)
Expect(marshaledCfg["default"]["spacewander"]).To(BeNil())
Expect(marshaledCfg["default"]["unchanged"]).ToNot(BeNil())
Expand Down
8 changes: 7 additions & 1 deletion controller/tests/integration/helper/mcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package helper
import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
Expand Down Expand Up @@ -128,7 +129,9 @@ func (c *mcpClient) Handle() {
}
if _, ok := efs[model.ConsumerEnvoyFilterName]; ok {
c.output.WriteEnvoyFilters(ctx, procession.ConfigSourceConsumer, efs)
} else {
delete(efs, model.ConsumerEnvoyFilterName)
}
if len(efs) > 0 {
c.output.WriteEnvoyFilters(ctx, procession.ConfigSourceHTTPFilterPolicy, efs)
}
case TypeUrlServiceEntry:
Expand All @@ -153,7 +156,10 @@ func (c *mcpClient) convertAnyToEnvoyFilter(res *anypb.Any) *istiov1a3.EnvoyFilt
ss := strings.Split(mcpRes.Metadata.Name, "/")
ef.SetNamespace(ss[0])
ef.SetName(ss[1])
ef.SetAnnotations(mcpRes.Metadata.Annotations)
ef.SetLabels(mcpRes.Metadata.Labels)
err = mcpRes.Body.UnmarshalTo(&ef.Spec)
fmt.Printf("CC %+v\n", ef.Spec.ConfigPatches)
Expect(err).NotTo(HaveOccurred())
return ef
}
Expand Down

0 comments on commit 761a4aa

Please sign in to comment.