Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions admission/plugin/machinevolumedevices/admission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2022 OnMetal 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 machinevolumedevices

import (
"context"
"fmt"
"io"

"github.com/onmetal/onmetal-api/admission/plugin/machinevolumedevices/device"
"github.com/onmetal/onmetal-api/apis/compute"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apiserver/pkg/admission"
)

// PluginName indicates name of admission plugin.
const PluginName = "MachineVolumeDevices"

// Register registers a plugin
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
return NewMachineVolumeDevices(), nil
})
}

type MachineVolumeDevices struct {
*admission.Handler
}

func NewMachineVolumeDevices() *MachineVolumeDevices {
return &MachineVolumeDevices{
Handler: admission.NewHandler(admission.Create, admission.Update),
}
}

func (d *MachineVolumeDevices) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {
if shouldIgnore(a) {
return nil
}

machine, ok := a.GetObject().(*compute.Machine)
if !ok {
return apierrors.NewBadRequest("Resource was marked with kind Machine but was unable to be converted")
}

namer, err := deviceNamerFromMachineVolumes(machine)
if err != nil {
return apierrors.NewBadRequest("Machine has conflicting volume device names")
}

for i := range machine.Spec.Volumes {
volume := &machine.Spec.Volumes[i]
if volume.Device != "" {
continue
}

dev, err := namer.Generate(device.VirtioPrefix) // TODO: We should have a better way for a device prefix.
if err != nil {
return apierrors.NewBadRequest("No device names left for machine")
}

volume.Device = dev
}

return nil
}

func shouldIgnore(a admission.Attributes) bool {
if a.GetKind().GroupKind() != compute.Kind("Machine") {
return true
}

machine, ok := a.GetObject().(*compute.Machine)
if !ok {
return true
}

return !machineHasAnyVolumeWithoutDevice(machine)
}

func machineHasAnyVolumeWithoutDevice(machine *compute.Machine) bool {
for _, volume := range machine.Spec.Volumes {
if volume.Device == "" {
return true
}
}
return false
}

func deviceNamerFromMachineVolumes(machine *compute.Machine) (*device.Namer, error) {
namer := device.NewNamer()

// Observe reserved names.
if err := namer.Observe(device.Name(device.VirtioPrefix, 0)); err != nil {
return nil, err
}

for _, volume := range machine.Spec.Volumes {
if dev := volume.Device; dev != "" {
if err := namer.Observe(dev); err != nil {
return nil, fmt.Errorf("error observing device %s: %w", dev, err)
}
}
}
return namer, nil
}
119 changes: 119 additions & 0 deletions admission/plugin/machinevolumedevices/admission_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2022 OnMetal 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 machinevolumedevices_test

import (
"context"

. "github.com/onmetal/onmetal-api/admission/plugin/machinevolumedevices"
"github.com/onmetal/onmetal-api/apis/compute"
"github.com/onmetal/onmetal-api/apis/storage"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/admission"
)

var _ = Describe("Admission", func() {
var (
plugin *MachineVolumeDevices
)
BeforeEach(func() {
plugin = NewMachineVolumeDevices()
})

It("should ignore non-machine objects", func() {
volume := &storage.Volume{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
}
origVolume := volume.DeepCopy()
Expect(plugin.Admit(
context.TODO(),
admission.NewAttributesRecord(
volume,
nil,
storage.Kind("Volume").WithVersion("version"),
volume.Namespace,
volume.Name,
storage.Resource("volumes").WithVersion("version"),
"",
admission.Create,
&metav1.CreateOptions{},
false,
nil,
),
nil,
)).NotTo(HaveOccurred())
Expect(volume).To(Equal(origVolume))
})

It("should add volume device names when unset", func() {
machine := &compute.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
Spec: compute.MachineSpec{
Volumes: []compute.Volume{
{
Name: "foo",
Device: "vdb",
},
{
Name: "bar",
},
{
Name: "baz",
},
},
},
}
Expect(plugin.Admit(
context.TODO(),
admission.NewAttributesRecord(
machine,
nil,
compute.Kind("Machine").WithVersion("version"),
machine.Namespace,
machine.Name,
compute.Resource("machines").WithVersion("version"),
"",
admission.Create,
&metav1.CreateOptions{},
false,
nil,
),
nil,
)).NotTo(HaveOccurred())

Expect(machine.Spec.Volumes).To(Equal([]compute.Volume{
{
Name: "foo",
Device: "vdb",
},
{
Name: "bar",
Device: "vdc",
},
{
Name: "baz",
Device: "vdd",
},
}))
})
})
Loading