|
| 1 | +From 971fc5f34ec13656fe12ae4d698272f8c23a6387 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Jan Larwig <jan.larwig@ionos.com> |
| 3 | +Date: Thu, 17 Oct 2024 15:43:10 +0200 |
| 4 | +Subject: [PATCH] providers: add support for ionos cloud |
| 5 | + |
| 6 | +Add support for IONOS Cloud |
| 7 | + |
| 8 | +Add check to ignore cloud-config |
| 9 | +--- |
| 10 | + docs/release-notes.md | 2 + |
| 11 | + docs/supported-platforms.md | 2 + |
| 12 | + internal/providers/ionoscloud/ionoscloud.go | 149 ++++++++++++++++++++ |
| 13 | + internal/providers/proxmoxve/proxmoxve.go | 4 +- |
| 14 | + internal/providers/util/cloudconfig.go | 13 ++ |
| 15 | + internal/register/providers.go | 1 + |
| 16 | + 6 files changed, 168 insertions(+), 3 deletions(-) |
| 17 | + create mode 100644 internal/providers/ionoscloud/ionoscloud.go |
| 18 | + create mode 100644 internal/providers/util/cloudconfig.go |
| 19 | + |
| 20 | +diff --git a/docs/release-notes.md b/docs/release-notes.md |
| 21 | +index 342fb1aa..2f25b609 100644 |
| 22 | +--- a/docs/release-notes.md |
| 23 | ++++ b/docs/release-notes.md |
| 24 | +@@ -10,6 +10,8 @@ nav_order: 9 |
| 25 | + |
| 26 | + ### Features |
| 27 | + |
| 28 | ++- Support IONOS Cloud |
| 29 | ++ |
| 30 | + ### Changes |
| 31 | + |
| 32 | + ### Bug fixes |
| 33 | +diff --git a/docs/supported-platforms.md b/docs/supported-platforms.md |
| 34 | +index eef319b2..c6846087 100644 |
| 35 | +--- a/docs/supported-platforms.md |
| 36 | ++++ b/docs/supported-platforms.md |
| 37 | +@@ -20,6 +20,7 @@ Ignition is currently supported for the following platforms: |
| 38 | + * [Hetzner Cloud] (`hetzner`) - Ignition will read its configuration from the instance userdata. Cloud SSH keys are handled separately. |
| 39 | + * [Microsoft Hyper-V] (`hyperv`) - Ignition will read its configuration from the `ignition.config` key in pool 0 of the Hyper-V Data Exchange Service (KVP). Values are limited to approximately 1 KiB of text, so Ignition can also read and concatenate multiple keys named `ignition.config.0`, `ignition.config.1`, and so on. |
| 40 | + * [IBM Cloud] (`ibmcloud`) - Ignition will read its configuration from the instance userdata. Cloud SSH keys are handled separately. |
| 41 | ++* [IONOS Cloud] (`ionoscloud`) - Ignition will read its configuration from the instance user-data. Cloud SSH keys are handled separately. Per default the user-data is looked up on the root partition in `/var/lib/cloud/seed/nocloud/user-data`. The root partition is detected by the label `ROOT` which can be customized using the environment variable `IGNITION_CONFIG_ROOT_LABEL`. |
| 42 | + * [KubeVirt] (`kubevirt`) - Ignition will read its configuration from the instance userdata via config drive. Cloud SSH keys are handled separately. |
| 43 | + * Bare Metal (`metal`) - Use the `ignition.config.url` kernel parameter to provide a URL to the configuration. The URL can use the `http://`, `https://`, `tftp://`, `s3://`, `arn:`, or `gs://` schemes to specify a remote config. |
| 44 | + * [Nutanix] (`nutanix`) - Ignition will read its configuration from the instance userdata via config drive. Cloud SSH keys are handled separately. |
| 45 | +@@ -52,6 +53,7 @@ For most cloud providers, cloud SSH keys and custom network configuration are ha |
| 46 | + [Hetzner Cloud]: https://www.hetzner.com/cloud |
| 47 | + [Microsoft Hyper-V]: https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/ |
| 48 | + [IBM Cloud]: https://www.ibm.com/cloud/vpc |
| 49 | ++[IONOS Cloud]: https://cloud.ionos.com/ |
| 50 | + [KubeVirt]: https://kubevirt.io |
| 51 | + [Nutanix]: https://www.nutanix.com/products/ahv |
| 52 | + [OpenStack]: https://www.openstack.org/ |
| 53 | +diff --git a/internal/providers/ionoscloud/ionoscloud.go b/internal/providers/ionoscloud/ionoscloud.go |
| 54 | +new file mode 100644 |
| 55 | +index 00000000..cc660998 |
| 56 | +--- /dev/null |
| 57 | ++++ b/internal/providers/ionoscloud/ionoscloud.go |
| 58 | +@@ -0,0 +1,149 @@ |
| 59 | ++// Copyright 2024 Red Hat, Inc. |
| 60 | ++// |
| 61 | ++// Licensed under the Apache License, Version 2.0 (the "License"); |
| 62 | ++// you may not use this file except in compliance with the License. |
| 63 | ++// You may obtain a copy of the License at |
| 64 | ++// |
| 65 | ++// http://www.apache.org/licenses/LICENSE-2.0 |
| 66 | ++// |
| 67 | ++// Unless required by applicable law or agreed to in writing, software |
| 68 | ++// distributed under the License is distributed on an "AS IS" BASIS, |
| 69 | ++// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 70 | ++// See the License for the specific language governing permissions and |
| 71 | ++// limitations under the License. |
| 72 | ++// |
| 73 | ++// NOTE: This provider is still EXPERIMENTAL. |
| 74 | ++// |
| 75 | ++// The IONOS Cloud provider fetches the ignition config from the user-data |
| 76 | ++// available in an injected file at /var/lib/cloud/seed/nocloud/user-data. |
| 77 | ++// This file is created by the IONOS Cloud VM handler before the first boot |
| 78 | ++// through the cloud init user data handling. |
| 79 | ++// |
| 80 | ++// User data with the directive #cloud-config will be ignored |
| 81 | ++// See for more: https://docs.ionos.com/cloud/compute-services/compute-engine/how-tos/boot-cloud-init |
| 82 | ++ |
| 83 | ++package ionoscloud |
| 84 | ++ |
| 85 | ++import ( |
| 86 | ++ "context" |
| 87 | ++ "fmt" |
| 88 | ++ "os" |
| 89 | ++ "os/exec" |
| 90 | ++ "path/filepath" |
| 91 | ++ "time" |
| 92 | ++ |
| 93 | ++ "github.com/flatcar/ignition/v2/config/v3_6_experimental/types" |
| 94 | ++ "github.com/flatcar/ignition/v2/internal/distro" |
| 95 | ++ "github.com/flatcar/ignition/v2/internal/log" |
| 96 | ++ "github.com/flatcar/ignition/v2/internal/platform" |
| 97 | ++ "github.com/flatcar/ignition/v2/internal/providers/util" |
| 98 | ++ "github.com/flatcar/ignition/v2/internal/resource" |
| 99 | ++ ut "github.com/flatcar/ignition/v2/internal/util" |
| 100 | ++ |
| 101 | ++ "github.com/coreos/vcontext/report" |
| 102 | ++) |
| 103 | ++ |
| 104 | ++const ( |
| 105 | ++ rootLabelEnvVar = "IGNITION_CONFIG_ROOT_LABEL" |
| 106 | ++ defaultRootLabel = "ROOT" |
| 107 | ++ userDataPath = "/var/lib/cloud/seed/nocloud/user-data" |
| 108 | ++) |
| 109 | ++ |
| 110 | ++func init() { |
| 111 | ++ platform.Register(platform.Provider{ |
| 112 | ++ Name: "ionoscloud", |
| 113 | ++ Fetch: fetchConfig, |
| 114 | ++ }) |
| 115 | ++} |
| 116 | ++ |
| 117 | ++func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) { |
| 118 | ++ var data []byte |
| 119 | ++ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 120 | ++ |
| 121 | ++ dispatch := func(name string, fn func() ([]byte, error)) { |
| 122 | ++ raw, err := fn() |
| 123 | ++ if err != nil { |
| 124 | ++ switch err { |
| 125 | ++ case context.Canceled: |
| 126 | ++ case context.DeadlineExceeded: |
| 127 | ++ f.Logger.Err("timed out while fetching config from %s", name) |
| 128 | ++ default: |
| 129 | ++ f.Logger.Err("failed to fetch config from %s: %v", name, err) |
| 130 | ++ } |
| 131 | ++ return |
| 132 | ++ } |
| 133 | ++ |
| 134 | ++ data = raw |
| 135 | ++ cancel() |
| 136 | ++ } |
| 137 | ++ |
| 138 | ++ deviceLabel := os.Getenv(rootLabelEnvVar) |
| 139 | ++ if deviceLabel == "" { |
| 140 | ++ deviceLabel = defaultRootLabel |
| 141 | ++ } |
| 142 | ++ |
| 143 | ++ go dispatch( |
| 144 | ++ "load config from root partition", func() ([]byte, error) { |
| 145 | ++ return fetchConfigFromDevice(f.Logger, ctx, filepath.Join(distro.DiskByLabelDir(), deviceLabel)) |
| 146 | ++ }, |
| 147 | ++ ) |
| 148 | ++ |
| 149 | ++ <-ctx.Done() |
| 150 | ++ if ctx.Err() == context.DeadlineExceeded { |
| 151 | ++ f.Logger.Info("root partition was not available in time. Continuing without a config...") |
| 152 | ++ } |
| 153 | ++ |
| 154 | ++ return util.ParseConfig(f.Logger, data) |
| 155 | ++} |
| 156 | ++ |
| 157 | ++func fileExists(path string) bool { |
| 158 | ++ _, err := os.Stat(path) |
| 159 | ++ return (err == nil) |
| 160 | ++} |
| 161 | ++ |
| 162 | ++func fetchConfigFromDevice(logger *log.Logger, ctx context.Context, device string) ([]byte, error) { |
| 163 | ++ for !fileExists(device) { |
| 164 | ++ logger.Debug("root partition (%q) not found. Waiting...", device) |
| 165 | ++ select { |
| 166 | ++ case <-time.After(time.Second): |
| 167 | ++ case <-ctx.Done(): |
| 168 | ++ return nil, ctx.Err() |
| 169 | ++ } |
| 170 | ++ } |
| 171 | ++ |
| 172 | ++ logger.Debug("creating temporary mount point") |
| 173 | ++ mnt, err := os.MkdirTemp("", "ignition-config") |
| 174 | ++ if err != nil { |
| 175 | ++ return nil, fmt.Errorf("failed to create temp directory: %v", err) |
| 176 | ++ } |
| 177 | ++ defer os.Remove(mnt) |
| 178 | ++ |
| 179 | ++ cmd := exec.Command(distro.MountCmd(), "-o", "ro", "-t", "auto", device, mnt) |
| 180 | ++ if _, err := logger.LogCmd(cmd, "mounting root partition"); err != nil { |
| 181 | ++ return nil, err |
| 182 | ++ } |
| 183 | ++ defer func() { |
| 184 | ++ _ = logger.LogOp( |
| 185 | ++ func() error { |
| 186 | ++ return ut.UmountPath(mnt) |
| 187 | ++ }, |
| 188 | ++ "unmounting %q at %q", device, mnt, |
| 189 | ++ ) |
| 190 | ++ }() |
| 191 | ++ |
| 192 | ++ if !fileExists(filepath.Join(mnt, userDataPath)) { |
| 193 | ++ return nil, nil |
| 194 | ++ } |
| 195 | ++ |
| 196 | ++ contents, err := os.ReadFile(filepath.Join(mnt, userDataPath)) |
| 197 | ++ if err != nil { |
| 198 | ++ return nil, err |
| 199 | ++ } |
| 200 | ++ |
| 201 | ++ if util.IsCloudConfig(contents) { |
| 202 | ++ logger.Debug("root partition (%q) contains a cloud-config configuration, ignoring", device) |
| 203 | ++ return nil, nil |
| 204 | ++ } |
| 205 | ++ |
| 206 | ++ return contents, nil |
| 207 | ++} |
| 208 | +diff --git a/internal/providers/proxmoxve/proxmoxve.go b/internal/providers/proxmoxve/proxmoxve.go |
| 209 | +index 490bfe30..b0dbb481 100644 |
| 210 | +--- a/internal/providers/proxmoxve/proxmoxve.go |
| 211 | ++++ b/internal/providers/proxmoxve/proxmoxve.go |
| 212 | +@@ -20,7 +20,6 @@ |
| 213 | + package proxmoxve |
| 214 | + |
| 215 | + import ( |
| 216 | +- "bytes" |
| 217 | + "context" |
| 218 | + "fmt" |
| 219 | + "os" |
| 220 | +@@ -132,8 +131,7 @@ func fetchConfigFromDevice(logger *log.Logger, ctx context.Context, path string) |
| 221 | + return nil, err |
| 222 | + } |
| 223 | + |
| 224 | +- header := []byte("#cloud-config\n") |
| 225 | +- if bytes.HasPrefix(contents, header) { |
| 226 | ++ if util.IsCloudConfig(contents) { |
| 227 | + logger.Debug("config drive (%q) contains a cloud-config configuration, ignoring", path) |
| 228 | + return nil, nil |
| 229 | + } |
| 230 | +diff --git a/internal/providers/util/cloudconfig.go b/internal/providers/util/cloudconfig.go |
| 231 | +new file mode 100644 |
| 232 | +index 00000000..abe9a2b6 |
| 233 | +--- /dev/null |
| 234 | ++++ b/internal/providers/util/cloudconfig.go |
| 235 | +@@ -0,0 +1,13 @@ |
| 236 | ++package util |
| 237 | ++ |
| 238 | ++import ( |
| 239 | ++ "bytes" |
| 240 | ++) |
| 241 | ++ |
| 242 | ++func IsCloudConfig(contents []byte) bool { |
| 243 | ++ header := []byte("#cloud-config\n") |
| 244 | ++ if bytes.HasPrefix(contents, header) { |
| 245 | ++ return true |
| 246 | ++ } |
| 247 | ++ return false |
| 248 | ++} |
| 249 | +diff --git a/internal/register/providers.go b/internal/register/providers.go |
| 250 | +index bda4b7cf..63249c7d 100644 |
| 251 | +--- a/internal/register/providers.go |
| 252 | ++++ b/internal/register/providers.go |
| 253 | +@@ -29,6 +29,7 @@ import ( |
| 254 | + _ "github.com/flatcar/ignition/v2/internal/providers/hetzner" |
| 255 | + _ "github.com/flatcar/ignition/v2/internal/providers/hyperv" |
| 256 | + _ "github.com/flatcar/ignition/v2/internal/providers/ibmcloud" |
| 257 | ++ _ "github.com/flatcar/ignition/v2/internal/providers/ionoscloud" |
| 258 | + _ "github.com/flatcar/ignition/v2/internal/providers/kubevirt" |
| 259 | + _ "github.com/flatcar/ignition/v2/internal/providers/metal" |
| 260 | + _ "github.com/flatcar/ignition/v2/internal/providers/nutanix" |
| 261 | +-- |
| 262 | +2.43.0 |
| 263 | + |
0 commit comments