-
Notifications
You must be signed in to change notification settings - Fork 112
Don't autodetect CPLB IP as host private address #1101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e177f8a
Don't autodetect CPLB IP as host private address
byDimasik 7dcacec
fix: correctly parse vrrpInstances as a YAML list
kke 1d982f0
refactor: consolidate CPLB VIP parsing into cluster package
kke add96b6
test: drop redundant configurer.Linux embed from mocks
kke dbe32dc
refactor: precompute CPLB VIP set in GatherFacts.Prepare
kke 0f87724
fix: guard against nil Spec in GatherFacts.Prepare
kke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package phase | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/k0sproject/dig" | ||
| "github.com/k0sproject/k0sctl/configurer/linux" | ||
| "github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1" | ||
| "github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster" | ||
| rigOS "github.com/k0sproject/rig/os" | ||
| "github.com/k0sproject/version" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // linux.Ubuntu already embeds configurer.Linux (via linux.Debian) and satisfies | ||
| // configurer.Configurer, so embedding it alone gives the mock all the no-op | ||
| // configurer methods. | ||
| type privateAddressMock struct { | ||
| linux.Ubuntu | ||
| addr string | ||
| err error | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
|
|
||
| func (m *privateAddressMock) PrivateAddress(_ rigOS.Host, _, _ string) (string, error) { | ||
| return m.addr, m.err | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
|
|
||
| // makeCPLBConfig builds a dig.Mapping representing a k0s config with CPLB settings. | ||
| func makeCPLBConfig(enabled bool, cplbType string, vrrpVIPs []string, virtualServerIPs []string) dig.Mapping { | ||
| vsEntries := make([]any, len(virtualServerIPs)) | ||
| for i, ip := range virtualServerIPs { | ||
| vsEntries[i] = dig.Mapping{"ipAddress": ip} | ||
| } | ||
| vrrpEntries := make([]any, len(vrrpVIPs)) | ||
| for i, ip := range vrrpVIPs { | ||
| vrrpEntries[i] = ip | ||
| } | ||
| return dig.Mapping{ | ||
| "spec": dig.Mapping{ | ||
| "network": dig.Mapping{ | ||
| "controlPlaneLoadBalancing": dig.Mapping{ | ||
| "enabled": enabled, | ||
| "type": cplbType, | ||
| "keepalived": dig.Mapping{ | ||
| "vrrpInstances": []any{dig.Mapping{"virtualIPs": vrrpEntries}}, | ||
| "virtualServers": vsEntries, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestInvestigateHostPrivateAddress(t *testing.T) { | ||
| const iface = "eth0" | ||
|
|
||
| makePhase := func(k0sConfig dig.Mapping) *GatherFacts { | ||
| config := &v1beta1.Cluster{ | ||
| Spec: &cluster.Spec{ | ||
| K0s: &cluster.K0s{ | ||
| Version: version.MustParse("v1.33.0"), | ||
| Config: k0sConfig, | ||
| }, | ||
| }, | ||
| } | ||
| p := &GatherFacts{SkipMachineIDs: true} | ||
| // Prepare() precomputes the CPLB VIP set the same way the manager would | ||
| // before Run/investigateHost run. | ||
| require.NoError(t, p.Prepare(config)) | ||
| return p | ||
| } | ||
|
|
||
| makeHost := func(addr string, addrErr error) *cluster.Host { | ||
| return &cluster.Host{ | ||
| HostnameOverride: "test-host", | ||
| PrivateInterface: iface, | ||
| Metadata: cluster.HostMetadata{Arch: "amd64"}, | ||
| Configurer: &privateAddressMock{addr: addr, err: addrErr}, | ||
| } | ||
| } | ||
|
|
||
| const cplbVIP = "10.0.0.1" | ||
| const normalIP = "192.168.1.5" | ||
| cplbCfg := makeCPLBConfig(true, "Keepalived", nil, []string{cplbVIP}) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| phase *GatherFacts | ||
| host *cluster.Host | ||
| wantPrivate string | ||
| }{ | ||
| { | ||
| name: "CPLB IP returned: private address not set", | ||
| phase: makePhase(cplbCfg), | ||
| host: makeHost(cplbVIP, nil), | ||
| wantPrivate: "", | ||
| }, | ||
|
kke marked this conversation as resolved.
|
||
| { | ||
| name: "non-CPLB IP returned: private address set", | ||
| phase: makePhase(cplbCfg), | ||
| host: makeHost(normalIP, nil), | ||
| wantPrivate: normalIP, | ||
| }, | ||
| { | ||
| name: "configurer error: private address not set", | ||
| phase: makePhase(cplbCfg), | ||
| host: makeHost("", errors.New("lookup failed")), | ||
| wantPrivate: "", | ||
| }, | ||
| { | ||
| name: "nil k0s config: non-CPLB check skipped, address set", | ||
| phase: makePhase(nil), | ||
| host: makeHost(normalIP, nil), | ||
| wantPrivate: normalIP, | ||
| }, | ||
| { | ||
| name: "CPLB disabled: configured VIP not treated as CPLB, address set", | ||
| phase: makePhase(makeCPLBConfig(false, "Keepalived", nil, []string{cplbVIP})), | ||
| host: makeHost(cplbVIP, nil), | ||
| wantPrivate: cplbVIP, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| require.NoError(t, tc.phase.investigateHost(context.Background(), tc.host)) | ||
| require.Equal(t, tc.wantPrivate, tc.host.PrivateAddress) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.