### Community Note
* Please vote on this issue by adding a 👍 [reaction](https…://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request. Searching for pre-existing feature requests helps us consolidate datapoints for identical requirements into a single place, thank you!
* Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
* If you are interested in working on this issue or have submitted a pull request, please leave a comment.
### Overview of the Issue
Unable to establish connectivity through either HTTP or gRPC with Consul Service Mesh.
I was following [kubernetes-pods-with-multiple-ports](https://www.consul.io/docs/k8s/connect#kubernetes-pods-with-multiple-ports) docs, and after deployment, consul-connect will panic. Below is 6 times it restarted and panic before hitting the threshold.
```
pod/consul-connect-injector-85b7dc57dd-g4g98 0/1 CrashLoopBackOff 6 (3m22s ago) 12m
pod/consul-connect-injector-85b7dc57dd-xcsrb 0/1 CrashLoopBackOff 6 (3m42s ago) 12m
```
### Reproduction Steps
1. Deploy Consul Service Mesh
```yaml
global:
name: consul
connectInject:
enabled: true
controller:
enabled: true
ui:
enabled: true
service:
type: LoadBalancer
```
2. Deploy multiport service: `kubectl apply -n greeter-server -f manifest.yaml`:
```yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: greeter-server
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: greeter-server-grpc
---
apiVersion: v1
kind: Service
metadata:
name: greeter-server
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: greeter-server
---
apiVersion: v1
kind: Service
metadata:
name: greeter-server-grpc
spec:
ports:
- name: grpc
port: 9080
protocol: TCP
targetPort: 9080
selector:
app: greeter-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: greeter-server
spec:
replicas: 1
selector:
matchLabels:
app: greeter-server
template:
metadata:
annotations:
consul.hashicorp.com/connect-inject: "true"
consul.hashicorp.com/connect-service: http,grpc
consul.hashicorp.com/connect-service-port: 8080,9080
consul.hashicorp.com/transparent-proxy: "false"
labels:
app: greeter-server
name: greeter-server
spec:
containers:
- image: darknerd/greeter-server
name: greeter-server
ports:
- containerPort: 9080
name: grpc
- containerPort: 8080
name: http
serviceAccountName: greeter-server
```
3. `kubectl get pods --namespace consul`
### Logs
<details>
<summary>kubectl logs pod/consul-connect-injector-85b7dc57dd-jtmmm -n consul</summary>
```
2022-10-04T09:44:59.817Z INFO controller-runtime.metrics metrics server is starting to listen {"addr": "0.0.0.0:9444"}
2022-10-04T09:44:59.818Z INFO controller-runtime.webhook registering webhook {"path": "/mutate"}
2022-10-04T09:44:59.818Z INFO attempting to acquire leader lease consul/consul-controller-lock...
2022-10-04T09:44:59.818Z INFO starting metrics server {"path": "/metrics"}
2022-10-04T09:44:59.818Z INFO controller-runtime.webhook.webhooks starting webhook server
2022-10-04T09:44:59.819Z INFO controller-runtime.certwatcher Updated current TLS certificate
2022-10-04T09:44:59.819Z INFO controller-runtime.webhook serving webhook server {"host": "", "port": 8080}
2022-10-04T09:44:59.856Z INFO controller-runtime.certwatcher Starting certificate watcher
2022-10-04T09:45:15.058Z INFO successfully acquired lease consul/consul-controller-lock
2022-10-04T09:45:15.059Z INFO controller.endpoints Starting EventSource {"reconciler group": "", "reconciler kind": "Endpoints", "source": "kind source: /, Kind="}
2022-10-04T09:45:15.059Z INFO controller.endpoints Starting EventSource {"reconciler group": "", "reconciler kind": "Endpoints", "source": "kind source: /, Kind="}
2022-10-04T09:45:15.059Z INFO controller.endpoints Starting Controller {"reconciler group": "", "reconciler kind": "Endpoints"}
2022-10-04T09:45:15.360Z INFO controller.endpoints Starting workers {"reconciler group": "", "reconciler kind": "Endpoints", "worker count": 1}
2022-10-04T09:45:15.360Z INFO controller.endpoints received update for Consul client pod {"name": "consul-client-ss79v"}
2022-10-04T09:45:15.361Z INFO controller.endpoints retrieved {"name": "consul-connect-injector", "ns": "consul"}
2022-10-04T09:45:15.362Z INFO controller.endpoints received update for Consul client pod {"name": "consul-client-xbjkp"}
2022-10-04T09:45:15.362Z INFO controller.endpoints received update for Consul client pod {"name": "consul-client-879jg"}
2022-10-04T09:45:15.561Z INFO controller.endpoints retrieved {"name": "greeter-client", "ns": "greeter-client"}
2022-10-04T09:45:15.765Z INFO controller.endpoints registering service with Consul {"name": "greeter-client", "id": "greeter-client-55fc8944ff-2smkz-greeter-client", "agentIP": "10.128.0.18"}
2022-10-04T09:45:15.811Z INFO controller.endpoints registering proxy service with Consul {"name": "greeter-client-sidecar-proxy"}
2022-10-04T09:45:15.852Z INFO controller.endpoints updating health check status for service {"name": "greeter-client", "reason": "Kubernetes health checks passing", "status": "passing"}
2022-10-04T09:45:15.853Z INFO controller.endpoints updating health check {"id": "greeter-client/greeter-client-55fc8944ff-2smkz-greeter-client/kubernetes-health-check"}
2022-10-04T09:45:16.034Z INFO controller.endpoints retrieved {"name": "greeter-server-grpc", "ns": "greeter-server"}
panic: runtime error: index out of range [-1]
goroutine 258 [running]:
github.com/hashicorp/consul-k8s/control-plane/connect-inject.(*EndpointsController).createServiceRegistrations(_, {{{0x1b293d8, 0x3}, {0x22c9093, 0x2}}, {{0xc0003d3760, 0x1f}, {0xc0003d3780, 0x1a}, {0xc000623b20, ...}, ...}, ...}, ...)
/home/runner/work/consul-k8s/consul-k8s/control-plane/connect-inject/endpoints_controller.go:417 +0x2374
github.com/hashicorp/consul-k8s/control-plane/connect-inject.(*EndpointsController).registerServicesAndHealthCheck(_, {{{0x1b293d8, 0x3}, {0x22c9093, 0x2}}, {{0xc0003d3760, 0x1f}, {0xc0003d3780, 0x1a}, {0xc000623b20, ...}, ...}, ...}, ...)
/home/runner/work/consul-k8s/consul-k8s/control-plane/connect-inject/endpoints_controller.go:251 +0x2e8
github.com/hashicorp/consul-k8s/control-plane/connect-inject.(*EndpointsController).Reconcile(0xc000305e00, {0x2689b60, 0xc00059f800}, {{{0xc0007da990?, 0x20f4f80?}, {0xc0008ca4b0?, 0xc0005be000?}}})
/home/runner/work/consul-k8s/consul-k8s/control-plane/connect-inject/endpoints_controller.go:195 +0x10db
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile(0xc0005d7cc0, {0x2689b60, 0xc00059f770}, {{{0xc0007da990?, 0x20f4f80?}, {0xc0008ca4b0?, 0xc000791c80?}}})
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:114 +0x222
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc0005d7cc0, {0x2689ab8, 0xc0005be380}, {0x1f9d360?, 0xc000446e80?})
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:311 +0x2e9
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc0005d7cc0, {0x2689ab8, 0xc0005be380})
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:266 +0x1d9
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2()
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:227 +0x85
created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:223 +0x307
```
</details>
<details>
<summary>kubectl logs consul-connect-injector-85b7dc57dd-dxz5b -n consul</summary>
```
2022-10-04T09:47:04.245Z INFO controller-runtime.metrics metrics server is starting to listen {"addr": "0.0.0.0:9444"}
2022-10-04T09:47:04.343Z INFO controller-runtime.webhook registering webhook {"path": "/mutate"}
2022-10-04T09:47:04.344Z INFO attempting to acquire leader lease consul/consul-controller-lock...
2022-10-04T09:47:04.344Z INFO starting metrics server {"path": "/metrics"}
2022-10-04T09:47:04.344Z INFO controller-runtime.webhook.webhooks starting webhook server
2022-10-04T09:47:04.345Z INFO controller-runtime.certwatcher Updated current TLS certificate
2022-10-04T09:47:04.345Z INFO controller-runtime.webhook serving webhook server {"host": "", "port": 8080}
2022-10-04T09:47:04.443Z INFO controller-runtime.certwatcher Starting certificate watcher
2022-10-04T09:47:20.343Z INFO successfully acquired lease consul/consul-controller-lock
2022-10-04T09:47:20.344Z INFO controller.endpoints Starting EventSource {"reconciler group": "", "reconciler kind": "Endpoints", "source": "kind source: /, Kind="}
2022-10-04T09:47:20.345Z INFO controller.endpoints Starting EventSource {"reconciler group": "", "reconciler kind": "Endpoints", "source": "kind source: /, Kind="}
2022-10-04T09:47:20.345Z INFO controller.endpoints Starting Controller {"reconciler group": "", "reconciler kind": "Endpoints"}
2022-10-04T09:47:20.645Z INFO controller.endpoints received update for Consul client pod {"name": "consul-client-xbjkp"}
2022-10-04T09:47:20.646Z INFO controller.endpoints Starting workers {"reconciler group": "", "reconciler kind": "Endpoints", "worker count": 1}
2022-10-04T09:47:20.646Z INFO controller.endpoints retrieved {"name": "consul-connect-injector", "ns": "consul"}
2022-10-04T09:47:20.649Z INFO controller.endpoints received update for Consul client pod {"name": "consul-client-879jg"}
2022-10-04T09:47:20.649Z INFO controller.endpoints received update for Consul client pod {"name": "consul-client-ss79v"}
2022-10-04T09:47:20.660Z INFO controller.endpoints retrieved {"name": "greeter-server", "ns": "greeter-server"}
panic: runtime error: index out of range [-1]
goroutine 254 [running]:
github.com/hashicorp/consul-k8s/control-plane/connect-inject.(*EndpointsController).createServiceRegistrations(_, {{{0x1b293d8, 0x3}, {0x22c9093, 0x2}}, {{0xc00077b1c0, 0x1f}, {0xc00077b1e0, 0x1a}, {0xc0007ed1f0, ...}, ...}, ...}, ...)
/home/runner/work/consul-k8s/consul-k8s/control-plane/connect-inject/endpoints_controller.go:417 +0x2374
github.com/hashicorp/consul-k8s/control-plane/connect-inject.(*EndpointsController).registerServicesAndHealthCheck(_, {{{0x1b293d8, 0x3}, {0x22c9093, 0x2}}, {{0xc00077b1c0, 0x1f}, {0xc00077b1e0, 0x1a}, {0xc0007ed1f0, ...}, ...}, ...}, ...)
/home/runner/work/consul-k8s/consul-k8s/control-plane/connect-inject/endpoints_controller.go:251 +0x2e8
github.com/hashicorp/consul-k8s/control-plane/connect-inject.(*EndpointsController).Reconcile(0xc0005d4140, {0x2689b60, 0xc000a5be90}, {{{0xc00012a680?, 0x20f4f80?}, {0xc00012a630?, 0xc0000c4100?}}})
/home/runner/work/consul-k8s/consul-k8s/control-plane/connect-inject/endpoints_controller.go:195 +0x10db
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile(0xc0008bc1e0, {0x2689b60, 0xc000a5be00}, {{{0xc00012a680?, 0x20f4f80?}, {0xc00012a630?, 0xc0000c56c0?}}})
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:114 +0x222
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc0008bc1e0, {0x2689ab8, 0xc0000c4540}, {0x1f9d360?, 0xc00045e7a0?})
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:311 +0x2e9
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc0008bc1e0, {0x2689ab8, 0xc0000c4540})
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:266 +0x1d9
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2()
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:227 +0x85
created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2
/home/runner/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.2/pkg/internal/controller/controller.go:223 +0x307
```
</details>
### Expected behavior
I expected that the side-car proxy containers would be injected, and that the connect-injector container would not continuously panic.
### Environment details
Additionally, please provide details regarding the Kubernetes Infrastructure, as shown below:
- Kubernetes version: **v1.22** (`v1.22.12-gke.300`)
- Cloud Provider: **GKE**
- Networking CNI plugin in use: default whatever that is
### Additional Context
* Related: https://github.com/hashicorp/consul/issues/5388