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

Per container ingress network override #480

Merged
merged 8 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions generator/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ func (g *CaddyfileGenerator) getContainerCaddyfile(container *types.Container, l
func (g *CaddyfileGenerator) getContainerIPAddresses(container *types.Container, logger *zap.Logger, ingress bool) ([]string, error) {
ips := []string{}

for _, network := range container.NetworkSettings.Networks {
if !ingress || g.ingressNetworks[network.NetworkID] {
ingressNetworkFromLabel, overrideNetwork := container.Labels[IngressNetworkLabel]

for networkName, network := range container.NetworkSettings.Networks {
include := false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a simpler way to express this logic, but I believe the caddy_ingress_network label needs to be a complete override of the allowed networks. Otherwise, we could still return IPs from another network?

Copy link
Owner

@lucaslorentz lucaslorentz Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense, can you please let param ingress overrule everything else? Basically, there is a place that calls this function to get all IPs, not only IPs for ingress.

Maybe doing an if !ingress before if overrideNetwork

Feel free to rename the ingress param as well to something more clear, maybe forIngress or onlyIngressIps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will go with onlyIngressIps


if overrideNetwork {
include = networkName == ingressNetworkFromLabel
} else {
include = !ingress || g.ingressNetworks[network.NetworkID]
}

if include {
ips = append(ips, network.IPAddress)
}
}
Expand Down
2 changes: 2 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
// DefaultLabelPrefix for caddy labels in docker
const DefaultLabelPrefix = "caddy"

const IngressNetworkLabel = "caddy_ingress_network"

const swarmAvailabilityCacheInterval = 1 * time.Minute

// CaddyfileGenerator generates caddyfile from docker configuration
Expand Down
12 changes: 11 additions & 1 deletion generator/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ func (g *CaddyfileGenerator) getServiceTasksIps(service *swarm.Service, logger *
for _, task := range tasks {
if task.Status.State == swarm.TaskStateRunning {
hasRunningTasks = true
ingressNetworkFromLabel, overrideNetwork := task.Labels[IngressNetworkLabel]
kiwiz marked this conversation as resolved.
Show resolved Hide resolved

for _, networkAttachment := range task.NetworksAttachments {
if !ingress || g.ingressNetworks[networkAttachment.Network.ID] {
include := false

if overrideNetwork {
include = networkAttachment.Network.Spec.Name == ingressNetworkFromLabel
} else {
include = !ingress || g.ingressNetworks[networkAttachment.Network.ID]
}

if include {
for _, address := range networkAttachment.Addresses {
ipAddress, _, _ := net.ParseCIDR(address)
tasksIps = append(tasksIps, ipAddress.String())
Expand Down