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 14, 2024
1 parent 7bab5a8 commit ed0bc4a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
3 changes: 2 additions & 1 deletion controller/internal/controller/output/mcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (srv *mcpServer) initSubscriberResource(sub *subscriber) {
srv.resourceLock.Lock()
defer srv.resourceLock.Unlock()

srv.logger.Info("sending initial conf to subscriber", "id", sub.id)
srv.logger.Info("sending initial conf to subscriber", "id", sub.id,
"EnvoyFilter", len(srv.envoyFilters), "ServiceEntry", len(srv.serviceEntries))
if len(srv.envoyFilters) > 0 {
typeUrl := "networking.istio.io/v1alpha3/EnvoyFilter"
srv.send(sub, typeUrl, srv.envoyFilters)
Expand Down
2 changes: 2 additions & 0 deletions controller/internal/registry/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (store *serviceEntryStore) Update(service string, se *pkgRegistry.ServiceEn
store.lock.Lock()
defer store.lock.Unlock()

logger.Info("service entry store update", "service", service, "entry", &se.ServiceEntry)
ctx := context.Background()
store.entries[service] = &se.ServiceEntry

Expand All @@ -58,6 +59,7 @@ func (store *serviceEntryStore) Delete(service string) {
return
}

logger.Info("service entry store delete", "service", service)
delete(store.entries, service)
store.output.WriteServiceEntries(context.Background(), procession.ConfigSourceServiceRegistry, store.entries)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ package controller

import (
"context"
"encoding/json"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
istiov1b1 "istio.io/client-go/pkg/apis/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

mosniov1 "mosn.io/htnn/controller/api/v1"
"mosn.io/htnn/controller/internal/model"
"mosn.io/htnn/controller/tests/integration/helper"
"mosn.io/htnn/controller/tests/pkg"
)
Expand All @@ -34,6 +40,54 @@ func mustReadServiceRegistry(fn string, out *[]map[string]interface{}) {
helper.MustReadInput(fn, out)
}

func listServiceEntries() []*istiov1b1.ServiceEntry {
var entries istiov1b1.ServiceEntryList
Expect(k8sClient.List(ctx, &entries, client.MatchingLabels{model.LabelCreatedBy: "ServiceRegistry"})).Should(Succeed())
return entries.Items
}

func registerNacosInstance(nacosPort string, name string, ip string, port string, metadata map[string]any) {
nacosServerURL := "http://0.0.0.0:" + nacosPort

params := url.Values{}
params.Set("serviceName", name)
params.Set("ip", ip)
params.Set("port", port)

if metadata != nil {
b, err := json.Marshal(metadata)
Expect(err).To(BeNil())
params.Set("metadata", string(b))
}

fullURL := nacosServerURL + "/nacos/v1/ns/instance?" + params.Encode()

req, err := http.NewRequest("POST", fullURL, strings.NewReader(""))
Expect(err).To(BeNil())
client := &http.Client{}
resp, err := client.Do(req)
Expect(err).To(BeNil())
Expect(resp.StatusCode).To(Equal(200))
}

func deregisterNacosInstance(nacosPort string, name string, ip string, port string) {
nacosServerURL := "http://0.0.0.0:" + nacosPort

params := url.Values{}
params.Set("serviceName", name)
params.Set("ip", ip)
params.Set("port", port)

fullURL := nacosServerURL + "/nacos/v1/ns/instance?" + params.Encode()

req, err := http.NewRequest("DELETE", fullURL, nil)
Expect(err).To(BeNil())
client := &http.Client{}
resp, err := client.Do(req)
Expect(err).To(BeNil())
Expect(resp.StatusCode).To(Equal(200))
}

var _ = Describe("ServiceRegistry controller", func() {

const (
Expand Down Expand Up @@ -112,6 +166,19 @@ var _ = Describe("ServiceRegistry controller", func() {
Expect(cs[0].Type).To(Equal(string(mosniov1.ConditionAccepted)))
Expect(cs[0].Reason).To(Equal(string(mosniov1.ReasonAccepted)))

// This part of code is a little repeated with the one in registries integration tests.
// We add this code to ensure the basic feature is working.
registerNacosInstance("8848", "test", "1.2.3.4", "8080", nil)

var entries []*istiov1b1.ServiceEntry
Eventually(func() bool {
entries = listServiceEntries()
return len(entries) == 1
}, timeout, interval).Should(BeTrue())

Expect(entries[0].Name).To(Equal("test.default-group.public.earth.nacos"))
Expect(entries[0].Spec.GetHosts()).To(Equal([]string{"test.default-group.public.earth.nacos"}))

// to invalid
base := client.MergeFrom(r.DeepCopy())
r.Spec.Config.Raw = []byte(`{}`)
Expand All @@ -129,6 +196,17 @@ var _ = Describe("ServiceRegistry controller", func() {
}
return false
}, timeout, interval).Should(BeTrue())

deregisterNacosInstance("8848", "test", "1.2.3.4", "8080")
sr := &mosniov1.ServiceRegistry{}
sr.SetName("earth")
sr.SetNamespace("default")
Expect(k8sClient.Delete(context.Background(), sr)).Should(Succeed())

Eventually(func() bool {
entries = listServiceEntries()
return len(entries) == 0
}, timeout, interval).Should(BeTrue())
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
spec:
type: nacos
config:
serverUrl: http://0.0.0.0:8848
serverUrl: http://127.0.0.1:8848
serviceRefreshInterval: 1s
3 changes: 3 additions & 0 deletions controller/tests/integration/helper/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package helper

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -43,6 +44,8 @@ func (o *OutputSuite) Get(ctx context.Context, c client.Client) procession.Outpu

mc := NewMcpClient(c)
defer mc.Close()
// add a little sleep to simulate a client starts after server gets new conf
time.Sleep(5 * time.Second)
mc.Init()
mc.Handle()
}()
Expand Down

0 comments on commit ed0bc4a

Please sign in to comment.