Skip to content

Commit

Permalink
Enable support for CNI mode networks for Consul Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
sundbry committed Jan 17, 2024
1 parent a7cfff3 commit 7217e7f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions client/allocrunner/consul_grpc_sock_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func (*consulGRPCSocketHook) Name() string {
func (h *consulGRPCSocketHook) shouldRun() bool {
tg := h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup)

// we must be in bridge networking and at least one connect sidecar task
if !tgFirstNetworkIsBridge(tg) {
// we must be in bridge or CNI networking, with at least one connect sidecar task.
if !tgFirstNetworkIsBridgeOrCNI(tg) {
return false
}

Expand Down
13 changes: 7 additions & 6 deletions client/allocrunner/consul_http_sock_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand All @@ -20,11 +21,12 @@ import (
"github.com/hashicorp/nomad/nomad/structs/config"
)

func tgFirstNetworkIsBridge(tg *structs.TaskGroup) bool {
if len(tg.Networks) < 1 || tg.Networks[0].Mode != "bridge" {
func tgFirstNetworkIsBridgeOrCNI(tg *structs.TaskGroup) bool {
if len(tg.Networks) < 1 {
return false
}
return true
mode := tg.Networks[0].Mode
return mode == "bridge" || strings.HasPrefix(mode, "cni/")
}

const (
Expand Down Expand Up @@ -56,12 +58,11 @@ func (*consulHTTPSockHook) Name() string {
// shouldRun returns true if the alloc contains at least one connect native
// task and has a network configured in bridge mode
//
// todo(shoenig): what about CNI networks?
func (h *consulHTTPSockHook) shouldRun() bool {
tg := h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup)

// we must be in bridge networking and at least one connect native task
if !tgFirstNetworkIsBridge(tg) {
// we must be in bridge or CNI networking, with at least one connect native task.
if !tgFirstNetworkIsBridgeOrCNI(tg) {
return false
}

Expand Down
6 changes: 4 additions & 2 deletions nomad/job_endpoint_hook_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,10 @@ func groupConnectSidecarValidate(g *structs.TaskGroup, s *structs.Service) error
return fmt.Errorf("Consul Connect sidecars require exactly 1 network, found %d in group %q", n, g.Name)
}

if g.Networks[0].Mode != "bridge" {
return fmt.Errorf("Consul Connect sidecar requires bridge network, found %q in group %q", g.Networks[0].Mode, g.Name)
mode := g.Networks[0].Mode

if mode != "bridge" && !strings.HasPrefix(mode, "cni/") {
return fmt.Errorf("Consul Connect sidecar requires a bridge or CNI network, found %q in group %q", g.Networks[0].Mode, g.Name)
}

// We must enforce lowercase characters on group and service names for connect
Expand Down

0 comments on commit 7217e7f

Please sign in to comment.