Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more metadata about the target if possible #447

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion control-plane/catalog/to-consul/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const (

// ConsulK8SNS is the key used in the meta to record the namespace
// of the service/node registration.
ConsulK8SNS = "external-k8s-ns"
ConsulK8SNS = "external-k8s-ns"
ConsulK8SRefKind = "external-k8s-ref-kind"
ConsulK8SRefValue = "external-k8s-ref-name"
ConsulK8SNodeName = "external-k8s-node-name"
)

type NodePortSyncType string
Expand Down Expand Up @@ -656,6 +659,19 @@ func (t *ServiceResource) registerServiceInstance(
r.Service.ID = serviceID(r.Service.Service, addr)
r.Service.Address = addr
r.Service.Port = epPort
r.Service.Meta = make(map[string]string)
// Deepcopy baseService.Meta into r.Service.Meta as baseService is shared
// between all nodes of a service
for k, v := range baseService.Meta {
r.Service.Meta[k] = v
}
if subsetAddr.TargetRef != nil {
r.Service.Meta[ConsulK8SRefValue] = subsetAddr.TargetRef.Name
r.Service.Meta[ConsulK8SRefKind] = subsetAddr.TargetRef.Kind
}
if subsetAddr.NodeName != nil {
r.Service.Meta[ConsulK8SNodeName] = *subsetAddr.NodeName
}

t.consulMap[key] = append(t.consulMap[key], &r)
}
Expand Down
40 changes: 39 additions & 1 deletion control-plane/catalog/to-consul/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,43 @@ func TestServiceResource_clusterIPTargetPortNamed(t *testing.T) {
})
}

// Test target information are set in service meta.
func TestServiceResource_targetRefInMeta(t *testing.T) {
t.Parallel()
client := fake.NewSimpleClientset()
syncer := newTestSyncer()
serviceResource := defaultServiceResource(client, syncer)
serviceResource.ClusterIPSync = true

// Start the controller
closer := controller.TestControllerRun(&serviceResource)
defer closer()

// Insert the service
svc := clusterIPService("foo", metav1.NamespaceDefault)
_, err := client.CoreV1().Services(metav1.NamespaceDefault).Create(context.Background(), svc, metav1.CreateOptions{})
require.NoError(t, err)

// Insert the endpoints
createEndpoints(t, client, "foo", metav1.NamespaceDefault)

// Verify what we got
retry.Run(t, func(r *retry.R) {
syncer.Lock()
defer syncer.Unlock()
actual := syncer.Registrations
require.Len(r, actual, 2)
require.Equal(r, "foobar", actual[0].Service.Meta[ConsulK8SRefValue])
require.Equal(r, "pod", actual[0].Service.Meta[ConsulK8SRefKind])
require.Equal(r, nodeName1, actual[0].Service.Meta[ConsulK8SNodeName])

// For second service, there is a node but no target refs
require.Equal(r, nodeName2, actual[1].Service.Meta[ConsulK8SNodeName])
require.NotContains(r, actual[1].Service.Meta, ConsulK8SRefValue)
require.NotContains(r, actual[1].Service.Meta, ConsulK8SRefKind)
})
lkysow marked this conversation as resolved.
Show resolved Hide resolved
}

// Test allow/deny namespace lists.
func TestServiceResource_AllowDenyNamespaces(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -1601,6 +1638,7 @@ func createNodes(t *testing.T, client *fake.Clientset) (*apiv1.Node, *apiv1.Node
func createEndpoints(t *testing.T, client *fake.Clientset, serviceName string, namespace string) {
node1 := nodeName1
node2 := nodeName2
targetRef := apiv1.ObjectReference{Kind: "pod", Name: "foobar"}
_, err := client.CoreV1().Endpoints(namespace).Create(
context.Background(),
&apiv1.Endpoints{
Expand All @@ -1612,7 +1650,7 @@ func createEndpoints(t *testing.T, client *fake.Clientset, serviceName string, n
Subsets: []apiv1.EndpointSubset{
{
Addresses: []apiv1.EndpointAddress{
{NodeName: &node1, IP: "1.1.1.1"},
{NodeName: &node1, IP: "1.1.1.1", TargetRef: &targetRef},
},
Ports: []apiv1.EndpointPort{
{Name: "http", Port: 8080},
Expand Down