|
| 1 | +// Copyright 2024 Red Hat, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// NOTE: This provider is still EXPERIMENTAL. |
| 16 | +// |
| 17 | +// The IONOS Cloud provider fetches the ignition config from the user-data |
| 18 | +// available in an injected file at /var/lib/cloud/seed/nocloud/user-data. |
| 19 | +// This file is created by the IONOS Cloud VM handler before the first boot |
| 20 | +// through the cloud init user data handling. |
| 21 | +// |
| 22 | +// User data with the directive #cloud-config will be ignored |
| 23 | +// See for more: https://docs.ionos.com/cloud/compute-services/compute-engine/how-tos/boot-cloud-init |
| 24 | + |
| 25 | +package ionoscloud |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "fmt" |
| 30 | + "os" |
| 31 | + "os/exec" |
| 32 | + "path/filepath" |
| 33 | + "time" |
| 34 | + |
| 35 | + "github.com/coreos/ignition/v2/config/v3_6_experimental/types" |
| 36 | + "github.com/coreos/ignition/v2/internal/distro" |
| 37 | + "github.com/coreos/ignition/v2/internal/log" |
| 38 | + "github.com/coreos/ignition/v2/internal/platform" |
| 39 | + "github.com/coreos/ignition/v2/internal/providers/util" |
| 40 | + "github.com/coreos/ignition/v2/internal/resource" |
| 41 | + ut "github.com/coreos/ignition/v2/internal/util" |
| 42 | + |
| 43 | + "github.com/coreos/vcontext/report" |
| 44 | +) |
| 45 | + |
| 46 | +const ( |
| 47 | + rootLabelEnvVar = "IGNITION_CONFIG_ROOT_LABEL" |
| 48 | + defaultRootLabel = "ROOT" |
| 49 | + userDataPath = "/var/lib/cloud/seed/nocloud/user-data" |
| 50 | +) |
| 51 | + |
| 52 | +func init() { |
| 53 | + platform.Register(platform.Provider{ |
| 54 | + Name: "ionoscloud", |
| 55 | + Fetch: fetchConfig, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) { |
| 60 | + var data []byte |
| 61 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 62 | + |
| 63 | + dispatch := func(name string, fn func() ([]byte, error)) { |
| 64 | + raw, err := fn() |
| 65 | + if err != nil { |
| 66 | + switch err { |
| 67 | + case context.Canceled: |
| 68 | + case context.DeadlineExceeded: |
| 69 | + f.Logger.Err("timed out while fetching config from %s", name) |
| 70 | + default: |
| 71 | + f.Logger.Err("failed to fetch config from %s: %v", name, err) |
| 72 | + } |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + data = raw |
| 77 | + cancel() |
| 78 | + } |
| 79 | + |
| 80 | + deviceLabel := os.Getenv(rootLabelEnvVar) |
| 81 | + if deviceLabel == "" { |
| 82 | + deviceLabel = defaultRootLabel |
| 83 | + } |
| 84 | + |
| 85 | + go dispatch( |
| 86 | + "load config from root partition", func() ([]byte, error) { |
| 87 | + return fetchConfigFromDevice(f.Logger, ctx, filepath.Join(distro.DiskByLabelDir(), deviceLabel)) |
| 88 | + }, |
| 89 | + ) |
| 90 | + |
| 91 | + <-ctx.Done() |
| 92 | + if ctx.Err() == context.DeadlineExceeded { |
| 93 | + f.Logger.Info("root partition was not available in time. Continuing without a config...") |
| 94 | + } |
| 95 | + |
| 96 | + return util.ParseConfig(f.Logger, data) |
| 97 | +} |
| 98 | + |
| 99 | +func fileExists(path string) bool { |
| 100 | + _, err := os.Stat(path) |
| 101 | + return (err == nil) |
| 102 | +} |
| 103 | + |
| 104 | +func fetchConfigFromDevice(logger *log.Logger, ctx context.Context, device string) ([]byte, error) { |
| 105 | + for !fileExists(device) { |
| 106 | + logger.Debug("root partition (%q) not found. Waiting...", device) |
| 107 | + select { |
| 108 | + case <-time.After(time.Second): |
| 109 | + case <-ctx.Done(): |
| 110 | + return nil, ctx.Err() |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + logger.Debug("creating temporary mount point") |
| 115 | + mnt, err := os.MkdirTemp("", "ignition-config") |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("failed to create temp directory: %v", err) |
| 118 | + } |
| 119 | + defer os.Remove(mnt) |
| 120 | + |
| 121 | + cmd := exec.Command(distro.MountCmd(), "-o", "ro", "-t", "auto", device, mnt) |
| 122 | + if _, err := logger.LogCmd(cmd, "mounting root partition"); err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + defer func() { |
| 126 | + _ = logger.LogOp( |
| 127 | + func() error { |
| 128 | + return ut.UmountPath(mnt) |
| 129 | + }, |
| 130 | + "unmounting %q at %q", device, mnt, |
| 131 | + ) |
| 132 | + }() |
| 133 | + |
| 134 | + if !fileExists(filepath.Join(mnt, userDataPath)) { |
| 135 | + return nil, nil |
| 136 | + } |
| 137 | + |
| 138 | + contents, err := os.ReadFile(filepath.Join(mnt, userDataPath)) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + |
| 143 | + if util.IsCloudConfig(contents) { |
| 144 | + logger.Debug("root partition (%q) contains a cloud-config configuration, ignoring", device) |
| 145 | + return nil, nil |
| 146 | + } |
| 147 | + |
| 148 | + return contents, nil |
| 149 | +} |
0 commit comments