Skip to content

Commit

Permalink
add backwards compatabiilty, additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahalsmiller committed Jan 16, 2024
1 parent 27a0f8b commit 13dea3a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
45 changes: 45 additions & 0 deletions cli/cmd/proxy/list/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,46 @@ func (c *ListCommand) fetchPods() ([]v1.Pod, error) {
}
pods = append(pods, gatewaypods.Items...)

// Fetch API Gateway pods with depricated label and append if they aren't already in the list
//TODO this block can be deleted if and when we decide we are ok with no longer listing pods of people using previous API Gateway
//versions.
// ---
apigatewaypods, err := c.kubernetes.CoreV1().Pods(c.namespace()).List(c.Ctx, metav1.ListOptions{
LabelSelector: "api-gateway.consul.hashicorp.com/managed=true",
})

namespacedName := func(pod v1.Pod) string {
return pod.Namespace + pod.Name
}
if err != nil {
return nil, err
}
if len(apigatewaypods.Items) > 0 {
//Deduplicated pod list
seenPods := map[string]bool{}
for _, pod := range apigatewaypods.Items {
if seenPods[namespacedName(pod)] {
continue
}
found := false
for _, gatewayPod := range gatewaypods.Items {
//note that we already have this pod in the list so we can exit early.
seenPods[namespacedName(gatewayPod)] = true

if (namespacedName(gatewayPod)) == namespacedName(pod) {
found = true
break
}
}
//pod isn't in the list already, we can add it.
if !found {
pods = append(pods, pod)
}

}
}
//---

// Fetch all pods in the namespace with a label indicating they are a service networked by Consul.
sidecarpods, err := c.kubernetes.CoreV1().Pods(c.namespace()).List(c.Ctx, metav1.ListOptions{
LabelSelector: "consul.hashicorp.com/connect-inject-status=injected",
Expand Down Expand Up @@ -271,6 +311,11 @@ func (c *ListCommand) output(pods []v1.Pod) {
default:
// Fallback to "Sidecar" as a default
proxyType = "Sidecar"

// Determine if depreciated API Gateway pod.
if pod.Labels["api-gateway.consul.hashicorp.com/managed"] == "true" {
proxyType = "API Gateway"
}
}

if c.flagAllNamespaces {
Expand Down
45 changes: 44 additions & 1 deletion cli/cmd/proxy/list/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,33 @@ func TestListCommandOutput(t *testing.T) {
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "depricated-api-gateway",
Namespace: "consul",
Labels: map[string]string{
"api-gateway.consul.hashicorp.com/managed": "true",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "api-gateway",
Namespace: "consul",
Labels: map[string]string{
"component": "api-gateway",
"chart": "consul-helm",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "both-labels-api-gateway",
Namespace: "consul",
Labels: map[string]string{
"api-gateway.consul.hashicorp.com/managed": "true",
"component": "api-gateway",
"chart": "consul-helm",
},
},
},
Expand Down Expand Up @@ -321,7 +342,7 @@ func TestListCommandOutput(t *testing.T) {

func TestListCommandOutputInJsonFormat(t *testing.T) {
// These regular expressions must be present in the output.
expected := ".*Name.*mesh-gateway.*\n.*Namespace.*consul.*\n.*Type.*Mesh Gateway.*\n.*\n.*\n.*Name.*terminating-gateway.*\n.*Namespace.*consul.*\n.*Type.*Terminating Gateway.*\n.*\n.*\n.*Name.*ingress-gateway.*\n.*Namespace.*default.*\n.*Type.*Ingress Gateway.*\n.*\n.*\n.*Name.*api-gateway.*\n.*Namespace.*consul.*\n.*Type.*API Gateway.*\n.*\n.*\n.*Name.*pod1.*\n.*Namespace.*default.*\n.*Type.*Sidecar.*"
expected := ".*Name.*api-gateway.*\n.*Namespace.*consul.*\n.*Type.*API Gateway.*\n.*\n.*\n.*Name.*both-labels-api-gateway.*\n.*Namespace.*consul.*\n.*Type.*API Gateway.*\n.*\n.*\n.*Name.*mesh-gateway.*\n.*Namespace.*consul.*\n.*Type.*Mesh Gateway.*\n.*\n.*\n.*Name.*terminating-gateway.*\n.*Namespace.*consul.*\n.*Type.*Terminating Gateway.*\n.*\n.*\n.*Name.*ingress-gateway.*\n.*Namespace.*default.*\n.*Type.*Ingress Gateway.*\n.*\n.*\n.*Name.*depricated-api-gateway.*\n.*Namespace.*consul.*\n.*Type.*API Gateway.*\n.*\n.*\n.*Name.*pod1.*\n.*Namespace.*default.*\n.*Type.*Sidecar.*"
notExpected := "default.*dont-fetch.*Sidecar"

pods := []v1.Pod{
Expand Down Expand Up @@ -359,6 +380,27 @@ func TestListCommandOutputInJsonFormat(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "api-gateway",
Namespace: "consul",
Labels: map[string]string{
"component": "api-gateway",
"chart": "consul-helm",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "both-labels-api-gateway",
Namespace: "consul",
Labels: map[string]string{
"api-gateway.consul.hashicorp.com/managed": "true",
"component": "api-gateway",
"chart": "consul-helm",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "depricated-api-gateway",
Namespace: "consul",
Labels: map[string]string{
"api-gateway.consul.hashicorp.com/managed": "true",
},
Expand Down Expand Up @@ -391,6 +433,7 @@ func TestListCommandOutputInJsonFormat(t *testing.T) {
require.Equal(t, 0, out)

actual := buf.String()
fmt.Println(actual)

require.Regexp(t, expected, actual)
for _, expression := range notExpected {
Expand Down

0 comments on commit 13dea3a

Please sign in to comment.