mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-09-30 03:51:43 +02:00
Support dynamically updating Softnet policy (#477)
* Support dynamically updating Softnet policy * Use cmpopts.EquateEmpty() when comparing two VMSpecs
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user