Skip to content

Commit

Permalink
Update consul-k8s CLI to rely on standard component label for api-gat…
Browse files Browse the repository at this point in the history
…eway

This makes api-gateway behave the same as ingress, mesh and terminating gateways; however, it won't pick up pods created by the legacy consul-api-gateway controller.
  • Loading branch information
nathancoleman committed Nov 3, 2023
1 parent 34db0a4 commit 114e92d
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions cli/cmd/proxy/list/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"strings"
"sync"

"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
"github.com/posener/complete"
helmCLI "helm.sh/helm/v3/pkg/cli"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
)

const (
Expand Down Expand Up @@ -200,22 +201,13 @@ func (c *ListCommand) fetchPods() ([]v1.Pod, error) {

// Fetch all pods in the namespace with labels matching the gateway component names.
gatewaypods, err := c.kubernetes.CoreV1().Pods(c.namespace()).List(c.Ctx, metav1.ListOptions{
LabelSelector: "component in (ingress-gateway, mesh-gateway, terminating-gateway), chart=consul-helm",
LabelSelector: "component in (api-gateway, ingress-gateway, mesh-gateway, terminating-gateway), chart=consul-helm",
})
if err != nil {
return nil, err
}
pods = append(pods, gatewaypods.Items...)

// Fetch all pods in the namespace with a label indicating they are an API gateway.
apigatewaypods, err := c.kubernetes.CoreV1().Pods(c.namespace()).List(c.Ctx, metav1.ListOptions{
LabelSelector: "api-gateway.consul.hashicorp.com/managed=true",
})
if err != nil {
return nil, err
}
pods = append(pods, apigatewaypods.Items...)

// 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 @@ -257,21 +249,16 @@ func (c *ListCommand) output(pods []v1.Pod) {

// Get the type for ingress, mesh, and terminating gateways.
switch pod.Labels["component"] {
case "api-gateway":
proxyType = "API Gateway"
case "ingress-gateway":
proxyType = "Ingress Gateway"
case "mesh-gateway":
proxyType = "Mesh Gateway"
case "terminating-gateway":
proxyType = "Terminating Gateway"
}

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

// Fallback to "Sidecar" as a default
if proxyType == "" {
default:
// Fallback to "Sidecar" as a default
proxyType = "Sidecar"
}

Expand Down

0 comments on commit 114e92d

Please sign in to comment.