Repository navigation
Expand file tree
/
Copy pathlayer2_vip.go
More file actions
137 lines (115 loc) · 3.95 KB
/
Copy pathlayer2_vip.go
File metadata and controls
137 lines (115 loc) · 3.95 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
// 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"
"fmt"
"net/netip"
"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"
)
// Layer2VIPKind is a Layer2VIP config document kind.
const Layer2VIPKind = "Layer2VIPConfig"
func init() {
registry.Register(Layer2VIPKind, func(version string) config.Document {
switch version {
case "v1alpha1": //nolint:goconst
return &Layer2VIPConfigV1Alpha1{}
default:
return nil
}
})
}
// Check interfaces.
var (
_ config.NetworkLayer2VIPConfig = &Layer2VIPConfigV1Alpha1{}
_ config.ConflictingDocument = &Layer2VIPConfigV1Alpha1{}
_ config.NamedDocument = &Layer2VIPConfigV1Alpha1{}
_ config.Validator = &Layer2VIPConfigV1Alpha1{}
)
// Layer2VIPConfigV1Alpha1 is a config document to configure virtual IP using Layer 2 (Ethernet) advertisement.
//
// description: |
// Virtual IP configuration should be used only on controlplane nodes to provide virtual IP for Kubernetes API server.
// Any other use cases are not supported and may lead to unexpected behavior.
// Virtual IP will be announced from only one node at a time using gratuitous ARP announcements for IPv4.
// examples:
// - value: exampleLayer2VIPConfigV1Alpha1()
// alias: Layer2VIPConfig
// schemaRoot: true
// schemaMeta: v1alpha1/Layer2VIPConfig
type Layer2VIPConfigV1Alpha1 struct {
meta.Meta `yaml:",inline"`
// description: |
// IP address to be advertised as a Layer 2 VIP.
//
// examples:
// - value: >
// "192.168.100.1"
// - value: >
// "fd00::1"
// schemaRequired: true
MetaName string `yaml:"name"`
// description: |
// Name of the link to assign the VIP to.
//
// Selector must match exactly one link, otherwise an error is returned.
// If multiple selectors match the same link, the first one is used.
LinkName string `yaml:"link"`
}
// NewLayer2VIPConfigV1Alpha1 creates a new Layer2VIPConfig config document.
func NewLayer2VIPConfigV1Alpha1(name string) *Layer2VIPConfigV1Alpha1 {
return &Layer2VIPConfigV1Alpha1{
Meta: meta.Meta{
MetaKind: Layer2VIPKind,
MetaAPIVersion: "v1alpha1",
},
MetaName: name,
}
}
func exampleLayer2VIPConfigV1Alpha1() *Layer2VIPConfigV1Alpha1 {
cfg := NewLayer2VIPConfigV1Alpha1("10.3.0.1")
cfg.LinkName = "enp0s2"
return cfg
}
// Clone implements config.Document interface.
func (s *Layer2VIPConfigV1Alpha1) Clone() config.Document {
return s.DeepCopy()
}
// Name implements config.NamedDocument interface.
func (s *Layer2VIPConfigV1Alpha1) Name() string {
return s.MetaName
}
// Validate implements config.Validator interface.
func (s *Layer2VIPConfigV1Alpha1) Validate(validation.RuntimeMode, ...validation.Option) ([]string, error) {
var (
errs error
warnings []string
)
if s.MetaName == "" {
errs = errors.Join(errs, errors.New("name must be specified"))
} else if _, err := netip.ParseAddr(s.MetaName); err != nil {
errs = errors.Join(errs, fmt.Errorf("name must be a valid IP address: %w", err))
}
if s.LinkName == "" {
errs = errors.Join(errs, errors.New("link must be specified"))
}
return warnings, errs
}
// Link implements config.NetworkLayer2VIPConfig interface.
func (s *Layer2VIPConfigV1Alpha1) Link() string {
return s.LinkName
}
// VIP implements config.NetworkLayer2VIPConfig interface.
func (s *Layer2VIPConfigV1Alpha1) VIP() netip.Addr {
addr, _ := netip.ParseAddr(s.MetaName) //nolint:errcheck // already validated
return addr
}
// ConflictsWithKinds implements config.ConflictingDocument interface.
func (s *Layer2VIPConfigV1Alpha1) ConflictsWithKinds() []string {
return []string{HCloudVIPKind}
}