Skip to content

Commit

Permalink
Add kubevirt platform ID
Browse files Browse the repository at this point in the history
The KubeVirt platform provides config drive files compatible to the
openstack config drive like nutanix does. It provides a disk with a
`config-2` label (lower case).

Signed-off-by: Roman Mohr <rmohr@redhat.com>
  • Loading branch information
rmohr committed Mar 28, 2022
1 parent d1f7f70 commit 13c86e7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/coreos/ignition/v2/internal/providers/file"
"github.com/coreos/ignition/v2/internal/providers/gcp"
"github.com/coreos/ignition/v2/internal/providers/ibmcloud"
"github.com/coreos/ignition/v2/internal/providers/kubevirt"
"github.com/coreos/ignition/v2/internal/providers/noop"
"github.com/coreos/ignition/v2/internal/providers/nutanix"
"github.com/coreos/ignition/v2/internal/providers/openstack"
Expand Down Expand Up @@ -141,6 +142,10 @@ func init() {
name: "ibmcloud",
fetch: ibmcloud.FetchConfig,
})
configs.Register(Config{
name: "kubevirt",
fetch: kubevirt.FetchConfig,
})
configs.Register(Config{
name: "metal",
fetch: noop.FetchConfig,
Expand Down
91 changes: 91 additions & 0 deletions internal/providers/kubevirt/kubevirt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2021 Red Hat.
//
// 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.

// The KubeVirt (https://kubevirt.io) provider fetches
// configuration from the userdata available in the config-drive. It is similar
// to Openstack and uses the same APIs.

package kubevirt

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/coreos/ignition/v2/config/v3_4_experimental/types"
"github.com/coreos/ignition/v2/internal/distro"
"github.com/coreos/ignition/v2/internal/log"
"github.com/coreos/ignition/v2/internal/providers/util"
"github.com/coreos/ignition/v2/internal/resource"
ut "github.com/coreos/ignition/v2/internal/util"

"github.com/coreos/vcontext/report"
)

const (
configDriveUserdataPath = "/openstack/latest/user_data"
)

func FetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) {
data, err := fetchConfigFromDevice(f.Logger, filepath.Join(distro.DiskByLabelDir(), "config-2"))
if err != nil {
return types.Config{}, report.Report{}, err
}

return util.ParseConfig(f.Logger, data)
}

func fileExists(path string) bool {
_, err := os.Stat(path)
return (err == nil)
}

func fetchConfigFromDevice(logger *log.Logger, path string) ([]byte, error) {
// There is not always a config drive in kubevirt, but we can limit rhcos usage
// to VMs with config drives. Block forever if there is none.
for !fileExists(path) {
logger.Debug("config drive (%q) not found. Waiting...", path)
time.Sleep(time.Second)
}

logger.Debug("creating temporary mount point")
mnt, err := ioutil.TempDir("", "ignition-configdrive")
if err != nil {
return nil, fmt.Errorf("failed to create temp directory: %v", err)
}
defer os.Remove(mnt)

cmd := exec.Command(distro.MountCmd(), "-o", "ro", "-t", "auto", path, mnt)
if _, err := logger.LogCmd(cmd, "mounting config drive"); err != nil {
return nil, err
}
defer func() {
_ = logger.LogOp(
func() error {
return ut.UmountPath(mnt)
},
"unmounting %q at %q", path, mnt,
)
}()

mntConfigDriveUserdataPath := filepath.Join(mnt, configDriveUserdataPath)
if !fileExists(mntConfigDriveUserdataPath) {
return nil, nil
}

return ioutil.ReadFile(mntConfigDriveUserdataPath)
}

0 comments on commit 13c86e7

Please sign in to comment.