|
| 1 | +/* |
| 2 | + Copyright The SpinKube Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "log/slog" |
| 23 | + |
| 24 | + "github.com/spf13/afero" |
| 25 | + "github.com/spinkube/runtime-class-manager/internal/preset" |
| 26 | +) |
| 27 | + |
| 28 | +var containerdConfigLocations = map[string]preset.Settings{ |
| 29 | + // Microk8s |
| 30 | + "/var/snap/microk8s/current/args/containerd-template.toml": preset.MicroK8s, |
| 31 | + // RKE2 |
| 32 | + "/var/lib/rancher/rke2/agent/etc/containerd/config.toml": preset.RKE2, |
| 33 | + // K3s |
| 34 | + "/var/lib/rancher/k3s/agent/etc/containerd/config.toml": preset.K3s, |
| 35 | + // default |
| 36 | + "/etc/containerd/config.toml": preset.Default, |
| 37 | +} |
| 38 | + |
| 39 | +func DetectDistro(config Config, hostFs afero.Fs) (preset.Settings, error) { |
| 40 | + if config.Runtime.ConfigPath != "" { |
| 41 | + // containerd config path has been set explicitly |
| 42 | + if distro, ok := containerdConfigLocations[config.Runtime.ConfigPath]; ok { |
| 43 | + return distro, nil |
| 44 | + } |
| 45 | + slog.Warn("could not determine distro from containerd config, falling back to defaults", "config", config.Runtime.ConfigPath) |
| 46 | + return preset.Default.WithConfigPath(config.Runtime.ConfigPath), nil |
| 47 | + } |
| 48 | + |
| 49 | + var errs []error |
| 50 | + |
| 51 | + for loc, distro := range containerdConfigLocations { |
| 52 | + _, err := hostFs.Stat(loc) |
| 53 | + if err == nil { |
| 54 | + // config file found, return corresponding distro settings |
| 55 | + return distro, nil |
| 56 | + } |
| 57 | + errs = append(errs, err) |
| 58 | + } |
| 59 | + |
| 60 | + return preset.Settings{}, fmt.Errorf("failed to detect containerd config path: %w", errors.Join(errs...)) |
| 61 | +} |
0 commit comments