Repository navigation
Expand file tree
/
Copy pathvlan.go
More file actions
192 lines (163 loc) · 5.09 KB
/
Copy pathvlan.go
File metadata and controls
192 lines (163 loc) · 5.09 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package network
//docgen:jsonschema
import (
"errors"
"net/netip"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/config/internal/registry"
"github.com/siderolabs/talos/pkg/machinery/config/types/meta"
"github.com/siderolabs/talos/pkg/machinery/config/validation"
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
)
// VLANKind is a VLAN config document kind.
const VLANKind = "VLANConfig"
func init() {
registry.Register(VLANKind, func(version string) config.Document {
switch version {
case "v1alpha1": //nolint:goconst
return &VLANConfigV1Alpha1{}
default:
return nil
}
})
}
// Check interfaces.
var (
_ config.NetworkVLANConfig = &VLANConfigV1Alpha1{}
_ config.ConflictingDocument = &VLANConfigV1Alpha1{}
_ config.NamedDocument = &VLANConfigV1Alpha1{}
_ config.Validator = &VLANConfigV1Alpha1{}
)
// VLANConfigV1Alpha1 is a config document to create a VLAN (virtual LAN) over a parent link.
//
// examples:
// - value: exampleVLANConfigV1Alpha1()
// alias: VLANConfig
// schemaRoot: true
// schemaMeta: v1alpha1/VLANConfig
type VLANConfigV1Alpha1 struct {
meta.Meta `yaml:",inline"`
// description: |
// Name of the VLAN link (interface) to be created.
//
// examples:
// - value: >
// "enp0s3.34"
// schemaRequired: true
MetaName string `yaml:"name"`
// description: |
// VLAN ID to be used for the VLAN link.
//
// examples:
// - value: >
// 34
// schemaRequired: true
VLANIDConfig uint16 `yaml:"vlanID,omitempty"`
// description: |
// Set the VLAN mode to use.
// If not set, defaults to '802.1q'.
//
// examples:
// - value: >
// "802.1q"
// values:
// - "802.1q"
// - "802.1ad"
VLANModeConfig *nethelpers.VLANProtocol `yaml:"vlanMode,omitempty"`
// description: |
// Name of the parent link (interface) on which the VLAN link will be created.
// Link aliases can be used here as well.
//
// examples:
// - value: >
// "enp0s3"
// schemaRequired: true
ParentLinkConfig string `yaml:"parent,omitempty"`
//nolint:embeddedstructfieldcheck
CommonLinkConfig `yaml:",inline"`
}
// NewVLANConfigV1Alpha1 creates a new VLANConfig config document.
func NewVLANConfigV1Alpha1(name string) *VLANConfigV1Alpha1 {
return &VLANConfigV1Alpha1{
Meta: meta.Meta{
MetaKind: VLANKind,
MetaAPIVersion: "v1alpha1",
},
MetaName: name,
}
}
func exampleVLANConfigV1Alpha1() *VLANConfigV1Alpha1 {
cfg := NewVLANConfigV1Alpha1("enp0s3.34")
cfg.VLANIDConfig = 34
cfg.ParentLinkConfig = "enp0s3"
cfg.LinkAddresses = []AddressConfig{
{
AddressAddress: netip.MustParsePrefix("192.168.1.100/24"),
},
}
cfg.LinkRoutes = []RouteConfig{
{
RouteDestination: meta.Prefix{Prefix: netip.MustParsePrefix("192.168.0.0/16")},
RouteGateway: meta.Addr{Addr: netip.MustParseAddr("192.168.1.1")},
},
}
return cfg
}
// Clone implements config.Document interface.
func (s *VLANConfigV1Alpha1) Clone() config.Document {
return s.DeepCopy()
}
// Name implements config.NamedDocument interface.
func (s *VLANConfigV1Alpha1) Name() string {
return s.MetaName
}
// VLANConfig implements NetworkVLANConfig interface.
func (s *VLANConfigV1Alpha1) VLANConfig() {}
// VLANID returns the VLAN ID.
func (s *VLANConfigV1Alpha1) VLANID() uint16 {
return s.VLANIDConfig
}
// ParentLink returns the parent link name.
func (s *VLANConfigV1Alpha1) ParentLink() string {
return s.ParentLinkConfig
}
// VLANMode returns the VLAN mode.
func (s *VLANConfigV1Alpha1) VLANMode() optional.Optional[nethelpers.VLANProtocol] {
if s.VLANModeConfig == nil {
return optional.None[nethelpers.VLANProtocol]()
}
return optional.Some(*s.VLANModeConfig)
}
// ConflictsWithKinds implements config.ConflictingDocument interface.
func (s *VLANConfigV1Alpha1) ConflictsWithKinds() []string {
return conflictingLinkKinds(VLANKind)
}
// Validate implements config.Validator interface.
func (s *VLANConfigV1Alpha1) Validate(validation.RuntimeMode, ...validation.Option) ([]string, error) {
var (
errs error
warnings []string //nolint:prealloc
)
if s.MetaName == "" {
errs = errors.Join(errs, errors.New("name must be specified"))
}
if s.VLANIDConfig == 0 || s.VLANIDConfig > 4094 {
errs = errors.Join(errs, errors.New("vlanID must be specified and between 1 and 4094"))
}
if s.ParentLinkConfig == "" {
errs = errors.Join(errs, errors.New("parent must be specified"))
}
switch {
case s.VLANModeConfig == nil:
// default is 802.1q
case *s.VLANModeConfig != nethelpers.VLANProtocol8021Q && *s.VLANModeConfig != nethelpers.VLANProtocol8021AD:
errs = errors.Join(errs, errors.New("vlanMode must be either '802.1q' or '802.1ad'"))
}
extraWarnings, extraErrs := s.CommonLinkConfig.Validate()
errs, warnings = errors.Join(errs, extraErrs), append(warnings, extraWarnings...)
return warnings, errs
}