Skip to content
Draft
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
67 changes: 35 additions & 32 deletions images/hooks/pkg/hooks/install-vmclass-generic/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"hooks/pkg/settings"

"github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha3"

"github.com/deckhouse/module-sdk/pkg"
"github.com/deckhouse/module-sdk/pkg/registry"
Expand Down Expand Up @@ -71,7 +71,7 @@ var config = &pkg.HookConfig{
},
{
Name: vmClassGenericSnapshot,
Kind: v1alpha2.VirtualMachineClassKind,
Kind: v1alpha3.VirtualMachineClassKind,
JqFilter: `{apiVersion, kind, "metadata": ( .metadata | {name, labels, annotations, creationTimestamp} ) }`,
NameSelector: &pkg.NameSelector{
MatchNames: []string{vmClassGenericName},
Expand Down Expand Up @@ -167,13 +167,13 @@ func parseVMClassInstallationStateFromSnapshot(input *pkg.HookInput) (*vmClassIn
}

// parseVMClassGenericFromSnapshot unmarshal ModuleConfig from jqFilter result.
func parseVMClassGenericFromSnapshot(input *pkg.HookInput) (*v1alpha2.VirtualMachineClass, error) {
func parseVMClassGenericFromSnapshot(input *pkg.HookInput) (*v1alpha3.VirtualMachineClass, error) {
snap := input.Snapshots.Get(vmClassGenericSnapshot)
if len(snap) < 1 {
return nil, nil
}

var vmclass v1alpha2.VirtualMachineClass
var vmclass v1alpha3.VirtualMachineClass
err := snap[0].UnmarshalTo(&vmclass)
if err != nil {
return nil, err
Expand All @@ -183,11 +183,11 @@ func parseVMClassGenericFromSnapshot(input *pkg.HookInput) (*v1alpha2.VirtualMac

// vmClassGenericManifest returns a manifest for 'generic' vmclass
// that should work for VM on every Node in cluster.
func vmClassGenericManifest() *v1alpha2.VirtualMachineClass {
return &v1alpha2.VirtualMachineClass{
func vmClassGenericManifest() *v1alpha3.VirtualMachineClass {
return &v1alpha3.VirtualMachineClass{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: v1alpha2.VirtualMachineClassKind,
APIVersion: v1alpha3.SchemeGroupVersion.String(),
Kind: v1alpha3.VirtualMachineClassKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: vmClassGenericName,
Expand All @@ -196,51 +196,51 @@ func vmClassGenericManifest() *v1alpha2.VirtualMachineClass {
"module": settings.ModuleName,
},
},
Spec: v1alpha2.VirtualMachineClassSpec{
CPU: v1alpha2.CPU{
Type: v1alpha2.CPUTypeModel,
Spec: v1alpha3.VirtualMachineClassSpec{
CPU: v1alpha3.CPU{
Type: v1alpha3.CPUTypeModel,
Model: "Nehalem",
},
SizingPolicies: []v1alpha2.SizingPolicy{
SizingPolicies: []v1alpha3.SizingPolicy{
{
Cores: &v1alpha2.SizingPolicyCores{
Cores: &v1alpha3.SizingPolicyCores{
Min: 1,
Max: 4,
},
DedicatedCores: []bool{false},
CoreFractions: []v1alpha2.CoreFractionValue{5, 10, 20, 50, 100},
CoreFractions: []v1alpha3.CoreFractionValue{"5%", "10%", "20%", "50%", "100%"},
},
{
Cores: &v1alpha2.SizingPolicyCores{
Cores: &v1alpha3.SizingPolicyCores{
Min: 5,
Max: 8,
},
DedicatedCores: []bool{false},
CoreFractions: []v1alpha2.CoreFractionValue{20, 50, 100},
CoreFractions: []v1alpha3.CoreFractionValue{"20%", "50%", "100%"},
},
{
Cores: &v1alpha2.SizingPolicyCores{
Cores: &v1alpha3.SizingPolicyCores{
Min: 9,
Max: 16,
},
DedicatedCores: []bool{true, false},
CoreFractions: []v1alpha2.CoreFractionValue{50, 100},
CoreFractions: []v1alpha3.CoreFractionValue{"50%", "100%"},
},
{
Cores: &v1alpha2.SizingPolicyCores{
Cores: &v1alpha3.SizingPolicyCores{
Min: 17,
Max: 1024,
},
DedicatedCores: []bool{true, false},
CoreFractions: []v1alpha2.CoreFractionValue{100},
CoreFractions: []v1alpha3.CoreFractionValue{"100%"},
},
},
},
}
}

// isManagedByModule checks if vmclass has all labels that module set when installing vmclass.
func isManagedByModule(vmClass *v1alpha2.VirtualMachineClass) bool {
func isManagedByModule(vmClass *v1alpha3.VirtualMachineClass) bool {
if vmClass == nil {
return false
}
Expand All @@ -266,7 +266,7 @@ const (

// addPatchesToCleanupMetadata fills patch collector with patches if vmclass metadata
// should be cleaned.
func addPatchesToCleanupMetadata(input *pkg.HookInput, vmClass *v1alpha2.VirtualMachineClass) {
func addPatchesToCleanupMetadata(input *pkg.HookInput, vmClass *v1alpha3.VirtualMachineClass) {
var patches []map[string]interface{}

labelNames := []string{
Expand All @@ -283,30 +283,33 @@ func addPatchesToCleanupMetadata(input *pkg.HookInput, vmClass *v1alpha2.Virtual
}
}

// Ensure "keep resource" annotation on vmclass/generic, so Helm will keep it
// in the cluster even that we've deleted its manifest from templates.
if _, exists := vmClass.Annotations[helmKeepResourceAnno]; !exists {
patches = append(patches, map[string]interface{}{
"op": "add",
"path": fmt.Sprintf("/metadata/annotations/%s", jsonPatchEscape(helmKeepResourceAnno)),
"value": nil,
})
}

annoNames := []string{
helmReleaseNameAnno,
helmReleaseNamespaceAnno,
}
hasHelmAnnotations := false
for _, annoName := range annoNames {
if _, exists := vmClass.Annotations[annoName]; exists {
patches = append(patches, map[string]interface{}{
"op": "remove",
"path": fmt.Sprintf("/metadata/annotations/%s", jsonPatchEscape(annoName)),
"value": nil,
})
hasHelmAnnotations = true
}
}

// Ensure "keep resource" annotation on vmclass/generic, so Helm will keep resource
// in the cluster even that we've deleted its manifest from templates.
_, hasKeepResourceAnno := vmClass.Annotations[helmKeepResourceAnno]
if hasHelmAnnotations && !hasKeepResourceAnno {
patches = append(patches, map[string]interface{}{
"op": "add",
"path": fmt.Sprintf("/metadata/annotations/%s", jsonPatchEscape(helmKeepResourceAnno)),
"value": nil,
})
}

if len(patches) == 0 {
return
}
Expand Down
20 changes: 10 additions & 10 deletions images/hooks/pkg/hooks/install-vmclass-generic/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/module-sdk/pkg"
"github.com/deckhouse/module-sdk/testing/mock"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha3"
)

func Test_InstallVMClassGeneric(t *testing.T) {
Expand Down Expand Up @@ -121,7 +121,7 @@ var _ = Describe("Install VMClass Generic hook", func() {
}
snapshots.GetMock.When(vmClassGenericSnapshot).Then([]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
vmClassInSnapshot, ok := v.(*v1alpha2.VirtualMachineClass)
vmClassInSnapshot, ok := v.(*v1alpha3.VirtualMachineClass)
Expect(ok).To(BeTrue())
*vmClassInSnapshot = *vmClass
return nil
Expand All @@ -133,7 +133,7 @@ var _ = Describe("Install VMClass Generic hook", func() {
vmClass := vmClassGenericManifest().DeepCopy()
snapshots.GetMock.When(vmClassGenericSnapshot).Then([]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
vmClassInSnapshot, ok := v.(*v1alpha2.VirtualMachineClass)
vmClassInSnapshot, ok := v.(*v1alpha3.VirtualMachineClass)
Expect(ok).To(BeTrue())
*vmClassInSnapshot = *vmClass
return nil
Expand All @@ -149,7 +149,7 @@ var _ = Describe("Install VMClass Generic hook", func() {
vmClass.Annotations = nil
snapshots.GetMock.When(vmClassGenericSnapshot).Then([]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
vmClassInSnapshot, ok := v.(*v1alpha2.VirtualMachineClass)
vmClassInSnapshot, ok := v.(*v1alpha3.VirtualMachineClass)
Expect(ok).To(BeTrue())
*vmClassInSnapshot = *vmClass
return nil
Expand All @@ -167,7 +167,7 @@ var _ = Describe("Install VMClass Generic hook", func() {
}
snapshots.GetMock.When(vmClassGenericSnapshot).Then([]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
vmClassInSnapshot, ok := v.(*v1alpha2.VirtualMachineClass)
vmClassInSnapshot, ok := v.(*v1alpha3.VirtualMachineClass)
Expect(ok).To(BeTrue())
*vmClassInSnapshot = *vmClass
return nil
Expand All @@ -187,7 +187,7 @@ var _ = Describe("Install VMClass Generic hook", func() {
}
snapshots.GetMock.When(vmClassGenericSnapshot).Then([]pkg.Snapshot{
mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) error {
vmClassInSnapshot, ok := v.(*v1alpha2.VirtualMachineClass)
vmClassInSnapshot, ok := v.(*v1alpha3.VirtualMachineClass)
Expect(ok).To(BeTrue())
*vmClassInSnapshot = *vmClass
return nil
Expand All @@ -197,7 +197,7 @@ var _ = Describe("Install VMClass Generic hook", func() {

expectVMClassGeneric := func(obj interface{}) {
GinkgoHelper()
vmClass, ok := obj.(*v1alpha2.VirtualMachineClass)
vmClass, ok := obj.(*v1alpha3.VirtualMachineClass)
Expect(ok).To(BeTrue())
Expect(vmClass.Name).To(Equal("generic"))
Expect(vmClass.Labels).To(Equal(map[string]string{
Expand Down Expand Up @@ -318,17 +318,17 @@ var _ = Describe("Install VMClass Generic hook", func() {
})
})

When("vmclass/generic without keep-resource annotation is present", func() {
When("vmclass/generic without keep-resource annotation", func() {
It("should not change vmclass/generic and set values", func() {
prepareVMClassSnapshotGenericWithoutKeepResource()

values.SetMock.Return()
patchCollector.CreateMock.Optional()
patchCollector.PatchWithJSONMock.Return()
patchCollector.PatchWithJSONMock.Optional()

Expect(Reconcile(context.Background(), newInput())).To(Succeed())
Expect(patchCollector.CreateMock.Calls()).To(HaveLen(0))
Expect(patchCollector.PatchWithJSONMock.Calls()).To(HaveLen(1))
Expect(patchCollector.PatchWithJSONMock.Calls()).To(HaveLen(0))
Expect(values.SetMock.Calls()).To(HaveLen(1))
})
})
Expand Down
Loading