-
Notifications
You must be signed in to change notification settings - Fork 9
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
Implement Distro detection #84
Closed
Closed
Changes from all commits
Commits
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
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,61 @@ | ||
/* | ||
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, | ||
// 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 |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/spinkube/runtime-class-manager/internal/containerd" | ||
"github.com/spinkube/runtime-class-manager/internal/preset" | ||
"github.com/spinkube/runtime-class-manager/internal/shim" | ||
) | ||
|
||
|
@@ -36,9 +37,16 @@ var installCmd = &cobra.Command{ | |
Run: func(_ *cobra.Command, _ []string) { | ||
rootFs := afero.NewOsFs() | ||
hostFs := afero.NewBasePathFs(rootFs, config.Host.RootPath) | ||
restarter := containerd.NewRestarter() | ||
|
||
if err := RunInstall(config, rootFs, hostFs, restarter); err != nil { | ||
distro, err := DetectDistro(config, hostFs) | ||
if err != nil { | ||
slog.Error("failed to detect containerd config", "error", err) | ||
os.Exit(1) | ||
} | ||
|
||
config.Runtime.ConfigPath = distro.ConfigPath | ||
|
||
if err := RunInstall(config, rootFs, hostFs, distro.Restarter(preset.Env{ConfigPath: distro.ConfigPath, HostFs: hostFs})); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not happy yet with how parts of the returned |
||
slog.Error("failed to install", "error", err) | ||
os.Exit(1) | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package preset | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spinkube/runtime-class-manager/internal/containerd" | ||
) | ||
|
||
type Settings struct { | ||
ConfigPath string | ||
Setup func(Env) error | ||
Restarter func(Env) containerd.Restarter | ||
} | ||
|
||
type Env struct { | ||
HostFs afero.Fs | ||
ConfigPath string | ||
} | ||
|
||
var Default = Settings{ | ||
ConfigPath: "/etc/containerd/config.toml", | ||
Setup: func(_ Env) error { return nil }, | ||
Restarter: func(_ Env) containerd.Restarter { return containerd.NewRestarter() }, | ||
} | ||
|
||
func (s Settings) WithConfigPath(path string) Settings { | ||
s.ConfigPath = path | ||
return s | ||
} | ||
|
||
func (s Settings) WithSetup(setup func(env Env) error) Settings { | ||
s.Setup = setup | ||
return s | ||
} | ||
|
||
var MicroK8s = Default.WithConfigPath("/var/snap/microk8s/current/args/containerd-template.toml") | ||
|
||
var RKE2 = Default.WithConfigPath("/var/lib/rancher/rke2/agent/etc/containerd/config.toml.tmpl"). | ||
WithSetup(func(env Env) error { | ||
_, err := env.HostFs.Stat(env.ConfigPath) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if errors.Is(err, os.ErrNotExist) { | ||
// TODO: Copy file from original file to new config file | ||
return nil | ||
} | ||
|
||
return err | ||
}) | ||
|
||
var K3s = RKE2.WithConfigPath("/var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not happy with this being duplicated in
uninstall.go