-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distro detection parity with containerd-shim-spin's installer (#240)
* implement some basic k8s distro detection * feat(distros): add rke2/k3s setup; call setup func in install Signed-off-by: Vaughn Dice <[email protected]> * feat(node-installer): add k0s support Signed-off-by: Vaughn Dice <[email protected]> * ref(node-installer): rm unused Env param from Restarter Signed-off-by: Vaughn Dice <[email protected]> * feat(node-installer): add DetectDistro tests, testdata Signed-off-by: Vaughn Dice <[email protected]> * ref(node-installer): simplify Restarter to be containerd.Restarter type Signed-off-by: Vaughn Dice <[email protected]> * feat(preset): add preset_test.go Signed-off-by: Vaughn Dice <[email protected]> --------- Signed-off-by: Vaughn Dice <[email protected]> Co-authored-by: Tom Gehrke <[email protected]>
- Loading branch information
Showing
15 changed files
with
474 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,63 @@ | ||
/* | ||
Copyright The SpinKube Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spinkube/runtime-class-manager/internal/preset" | ||
) | ||
|
||
var containerdConfigLocations = map[string]preset.Settings{ | ||
// Microk8s | ||
"/var/snap/microk8s/current/args/containerd-template.toml": preset.MicroK8s, | ||
// RKE2 | ||
"/var/lib/rancher/rke2/agent/etc/containerd/config.toml": preset.RKE2, | ||
// K3s | ||
"/var/lib/rancher/k3s/agent/etc/containerd/config.toml": preset.K3s, | ||
// K0s | ||
"/etc/k0s/containerd.toml": preset.K0s, | ||
// default | ||
"/etc/containerd/config.toml": preset.Default, | ||
} | ||
|
||
func DetectDistro(config Config, hostFs afero.Fs) (preset.Settings, error) { | ||
if config.Runtime.ConfigPath != "" { | ||
// containerd config path has been set explicitly | ||
if distro, ok := containerdConfigLocations[config.Runtime.ConfigPath]; ok { | ||
return distro, nil | ||
} | ||
slog.Warn("could not determine distro from containerd config, falling back to defaults", "config", config.Runtime.ConfigPath) | ||
return preset.Default.WithConfigPath(config.Runtime.ConfigPath), nil | ||
} | ||
|
||
var errs []error | ||
|
||
for loc, distro := range containerdConfigLocations { | ||
_, err := hostFs.Stat(loc) | ||
if err == nil { | ||
// config file found, return corresponding distro settings | ||
return distro, nil | ||
} | ||
errs = append(errs, err) | ||
} | ||
|
||
return preset.Settings{}, fmt.Errorf("failed to detect containerd config path: %w", errors.Join(errs...)) | ||
} |
This file contains 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,188 @@ | ||
/* | ||
Copyright The SpinKube Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
main "github.com/spinkube/runtime-class-manager/cmd/node-installer" | ||
"github.com/spinkube/runtime-class-manager/internal/preset" | ||
tests "github.com/spinkube/runtime-class-manager/tests/node-installer" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_DetectDistro(t *testing.T) { | ||
type args struct { | ||
config main.Config | ||
hostFs afero.Fs | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
wantPreset preset.Settings | ||
}{ | ||
{ | ||
"config_override", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", preset.MicroK8s.ConfigPath}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/default"), | ||
}, | ||
false, | ||
preset.MicroK8s, | ||
}, | ||
{ | ||
"config_not_found_fallback_default", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", "/etc/containerd/not_found.toml"}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/default"), | ||
}, | ||
false, | ||
preset.Default.WithConfigPath("/etc/containerd/not_found.toml"), | ||
}, | ||
{ | ||
"unsupported", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", ""}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/unsupported"), | ||
}, | ||
true, | ||
preset.Default, | ||
}, | ||
{ | ||
"microk8s", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", ""}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/microk8s"), | ||
}, | ||
false, | ||
preset.MicroK8s, | ||
}, | ||
{ | ||
"k0s", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", ""}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/k0s"), | ||
}, | ||
false, | ||
preset.K0s, | ||
}, | ||
{ | ||
"k3s", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", ""}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/k3s"), | ||
}, | ||
false, | ||
preset.K3s, | ||
}, | ||
{ | ||
"rke2", | ||
args{ | ||
main.Config{ | ||
struct { | ||
Name string | ||
ConfigPath string | ||
}{"containerd", ""}, | ||
struct { | ||
Path string | ||
AssetPath string | ||
}{"/opt/kwasm", "/assets"}, | ||
struct{ RootPath string }{""}, | ||
}, | ||
tests.FixtureFs("../../testdata/node-installer/distros/rke2"), | ||
}, | ||
false, | ||
preset.RKE2, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
preset, err := main.DetectDistro(tt.args.config, tt.args.hostFs) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.wantPreset.ConfigPath, preset.ConfigPath) | ||
require.Equal(t, reflect.ValueOf(tt.wantPreset.Setup), reflect.ValueOf(preset.Setup)) | ||
require.Equal(t, reflect.ValueOf(tt.wantPreset.Restarter), reflect.ValueOf(preset.Restarter)) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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.