File: provider.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (91 lines) | stat: -rw-r--r-- 2,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cnmallocator

import (
	"strings"

	"github.com/docker/docker/libnetwork/driverapi"
	"github.com/docker/docker/libnetwork/drivers/overlay/overlayutils"
	"github.com/docker/docker/libnetwork/ipamapi"
	"github.com/docker/docker/pkg/plugingetter"
	"github.com/moby/swarmkit/v2/api"
	"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type Provider struct {
	pg plugingetter.PluginGetter
}

var _ networkallocator.Provider = &Provider{}

// NewProvider returns a new cnmallocator provider.
func NewProvider(pg plugingetter.PluginGetter) *Provider {
	return &Provider{pg: pg}
}

// ValidateIPAMDriver implements networkallocator.NetworkProvider.
func (p *Provider) ValidateIPAMDriver(driver *api.Driver) error {
	if driver == nil {
		// It is ok to not specify the driver. We will choose
		// a default driver.
		return nil
	}

	if driver.Name == "" {
		return status.Errorf(codes.InvalidArgument, "driver name: if driver is specified name is required")
	}
	if strings.ToLower(driver.Name) == ipamapi.DefaultIPAM {
		return nil
	}
	return p.validatePluginDriver(driver, ipamapi.PluginEndpointType)
}

// ValidateIngressNetworkDriver implements networkallocator.NetworkProvider.
func (p *Provider) ValidateIngressNetworkDriver(driver *api.Driver) error {
	if driver != nil && driver.Name != "overlay" {
		return status.Errorf(codes.Unimplemented, "only overlay driver is currently supported for ingress network")
	}
	return p.ValidateNetworkDriver(driver)
}

// ValidateNetworkDriver implements networkallocator.NetworkProvider.
func (p *Provider) ValidateNetworkDriver(driver *api.Driver) error {
	if driver == nil {
		// It is ok to not specify the driver. We will choose
		// a default driver.
		return nil
	}

	if driver.Name == "" {
		return status.Errorf(codes.InvalidArgument, "driver name: if driver is specified name is required")
	}

	// First check against the known drivers
	if IsBuiltInDriver(driver.Name) {
		return nil
	}

	return p.validatePluginDriver(driver, driverapi.NetworkPluginEndpointType)
}

func (p *Provider) validatePluginDriver(driver *api.Driver, pluginType string) error {
	if p.pg == nil {
		return status.Errorf(codes.InvalidArgument, "plugin %s not supported", driver.Name)
	}

	plug, err := p.pg.Get(driver.Name, pluginType, plugingetter.Lookup)
	if err != nil {
		return status.Errorf(codes.InvalidArgument, "error during lookup of plugin %s", driver.Name)
	}

	if plug.IsV1() {
		return status.Errorf(codes.InvalidArgument, "legacy plugin %s of type %s is not supported in swarm mode", driver.Name, pluginType)
	}

	return nil
}

func (p *Provider) SetDefaultVXLANUDPPort(port uint32) error {
	return overlayutils.ConfigVXLANUDPPort(port)
}