Support dynamically updating Softnet policy (#477)

* Support dynamically updating Softnet policy

* Use cmpopts.EquateEmpty() when comparing two VMSpecs
This commit is contained in:
edi-oai
2026-09-02 14:19:29 +01:00
committed by GitHub
parent f0bdf3a151
commit 62acf3c265
21 changed files with 544 additions and 35 deletions
+10
View File
@@ -5,6 +5,7 @@ import (
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
// TestVM ensures that v1.VM and its embedded structs can be compared
@@ -12,3 +13,12 @@ import (
func TestVM(t *testing.T) {
cmp.Equal(v1.VM{}, v1.VM{})
}
func TestVMSpecSemanticallyEqualEquatesEmptySlices(t *testing.T) {
nilSlicesSpec := v1.VMSpec{}
emptySlicesSpec := v1.VMSpec{
NetSoftnetAllow: []string{},
NetSoftnetBlock: []string{},
}
require.True(t, nilSlicesSpec.SemanticallyEqual(emptySlicesSpec))
}
+19
View File
@@ -3,7 +3,11 @@ package v1
import (
"encoding/json"
"fmt"
"slices"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// Meta is a common set of fields that apply to all resources managed by the Controller.
@@ -193,6 +197,21 @@ type VMSpec struct {
PowerState PowerState `json:"powerState,omitempty"`
}
func (vm VMSpec) SemanticallyEqual(other VMSpec) bool {
// Treat omitted and explicitly empty collections as the same VM specification
return cmp.Equal(vm, other, cmpopts.EquateEmpty())
}
func (vm VMSpec) SoftnetEnabled() bool {
return vm.NetSoftnetDeprecated || vm.NetSoftnet ||
len(vm.NetSoftnetAllow) != 0 || len(vm.NetSoftnetBlock) != 0
}
func (vm VMSpec) SoftnetPolicyChanged(other VMSpec) bool {
return !slices.Equal(vm.NetSoftnetAllow, other.NetSoftnetAllow) ||
!slices.Equal(vm.NetSoftnetBlock, other.NetSoftnetBlock)
}
type VMSpecReadOnly struct {
LocalName string `json:"localName,omitempty"`