Skip to content

Commit ad9f76e

Browse files
authored
Merge pull request #767 from winiciusallan/trustedvif
port: add trustedVIF field
2 parents ee85fd9 + c9a4c1b commit ad9f76e

22 files changed

Lines changed: 255 additions & 9 deletions

File tree

.github/workflows/e2e.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ jobs:
3838
with:
3939
enable_workaround_docker_io: 'false'
4040
branch: ${{ matrix.openstack_version }}
41-
enabled_services: "openstack-cli-server,neutron-trunk"
41+
enabled_services: "openstack-cli-server,neutron-trunk,neutron-port-trusted-vif"
4242
conf_overrides: |
43+
enable_plugin neutron https://github.com/openstack/neutron ${{ matrix.openstack_version }}
44+
4345
[[post-config|/etc/nova/nova.conf]]
4446
[filter_scheduler]
4547
enabled_filters = ComputeFilter,ComputeCapabilitiesFilter,ImagePropertiesFilter,ServerGroupAntiAffinityFilter,ServerGroupAffinityFilter,SameHostFilter,DifferentHostFilter,SimpleCIDRAffinityFilter,JsonFilter

api/v1alpha1/port_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ type PortResourceSpec struct {
210210
// +optional
211211
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="hostID is immutable"
212212
HostID *HostID `json:"hostID,omitempty"` //nolint:kubeapilinter // HostID provides both raw ID and ServerRef options
213+
214+
// trustedVIF indicates whether the VF for the port will become
215+
// trusted by physical function to perform some privileged
216+
// operations. Only admin users can create ports with this field.
217+
// +optional
218+
TrustedVIF *bool `json:"trustedVIF,omitempty"`
213219
}
214220

215221
type PortResourceStatus struct {
@@ -307,6 +313,12 @@ type PortResourceStatus struct {
307313
// +optional
308314
HostID string `json:"hostID,omitempty"`
309315

316+
// trustedVIF indicates whether the VF for the port will become
317+
// trusted by physical function to perform some privileged
318+
// operations.
319+
// +optional
320+
TrustedVIF *bool `json:"trustedVIF,omitempty"`
321+
310322
NeutronStatusMetadata `json:",inline"`
311323
}
312324

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/models-schema/zz_generated.openapi.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/openstack.k-orc.cloud_ports.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ spec:
401401
maxItems: 64
402402
type: array
403403
x-kubernetes-list-type: set
404+
trustedVIF:
405+
description: |-
406+
trustedVIF indicates whether the VF for the port will become
407+
trusted by physical function to perform some privileged
408+
operations. Only admin users can create ports with this field.
409+
type: boolean
404410
vnicType:
405411
description: |-
406412
vnicType specifies the type of vNIC which this port should be
@@ -650,6 +656,12 @@ spec:
650656
maxItems: 64
651657
type: array
652658
x-kubernetes-list-type: atomic
659+
trustedVIF:
660+
description: |-
661+
trustedVIF indicates whether the VF for the port will become
662+
trusted by physical function to perform some privileged
663+
operations.
664+
type: boolean
653665
updatedAt:
654666
description: updatedAt shows the date and time when the resource
655667
was updated. The date and time stamp format is ISO 8601

internal/controllers/port/actuator.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsbinding"
2727
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsecurity"
28+
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portstrustedvif"
2829
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
2930
corev1 "k8s.io/api/core/v1"
3031
"k8s.io/utils/ptr"
@@ -318,7 +319,14 @@ func (actuator portActuator) CreateResource(ctx context.Context, obj *orcv1alpha
318319
orcerrors.Terminal(orcv1alpha1.ConditionReasonInvalidConfiguration, fmt.Sprintf("Invalid value %s", resource.PortSecurity)))
319320
}
320321

321-
osResource, err := actuator.osClient.CreatePort(ctx, &portSecurityOpts)
322+
portTrustedOpts := portstrustedvif.PortCreateOptsExt{
323+
CreateOptsBuilder: portSecurityOpts,
324+
}
325+
if resource.TrustedVIF != nil {
326+
portTrustedOpts.PortTrustedVIF = resource.TrustedVIF
327+
}
328+
329+
osResource, err := actuator.osClient.CreatePort(ctx, &portTrustedOpts)
322330
if err != nil {
323331
// We should require the spec to be updated before retrying a create which returned a conflict
324332
if orcerrors.IsConflict(err) {
@@ -416,6 +424,7 @@ func (actuator portActuator) updateResource(ctx context.Context, obj orcObjectPT
416424

417425
updateOpts = handlePortBindingUpdate(updateOpts, resource, osResource)
418426
updateOpts = handlePortSecurityUpdate(updateOpts, resource, osResource)
427+
updateOpts = handlePortTrustedVIFUpdate(updateOpts, resource, osResource)
419428

420429
needsUpdate, err := needsUpdate(updateOpts)
421430
if err != nil {
@@ -587,6 +596,20 @@ func handleAdminStateUpUpdate(updateOpts *ports.UpdateOpts, resource *resourceSp
587596
}
588597
}
589598

599+
func handlePortTrustedVIFUpdate(updateOpts ports.UpdateOptsBuilder, resource *resourceSpecT, osResource *osResourceT) ports.UpdateOptsBuilder {
600+
trusted := resource.TrustedVIF
601+
if trusted != nil {
602+
if osResource.PortTrustedVIF == nil || *trusted != *osResource.PortTrustedVIF {
603+
updateOpts = portstrustedvif.PortUpdateOptsExt{
604+
UpdateOptsBuilder: updateOpts,
605+
PortTrustedVIF: trusted,
606+
}
607+
}
608+
}
609+
610+
return updateOpts
611+
}
612+
590613
type portHelperFactory struct{}
591614

592615
var _ helperFactory = portHelperFactory{}

internal/controllers/port/actuator_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsbinding"
77
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsecurity"
8+
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portstrustedvif"
89
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
910
orcv1alpha1 "github.com/k-orc/openstack-resource-controller/v2/api/v1alpha1"
1011
osclients "github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
@@ -435,3 +436,37 @@ func TestHandleAdminStateUpUpdate(t *testing.T) {
435436
})
436437
}
437438
}
439+
440+
func TestHandleTrustedVIFUpdate(t *testing.T) {
441+
testCases := []struct {
442+
name string
443+
newValue *bool
444+
existingValue *bool
445+
expectChange bool
446+
}{
447+
{name: "Enabled when the value is not set", newValue: ptr.To(true), existingValue: nil, expectChange: true},
448+
{name: "Enabled when was disabled", newValue: ptr.To(true), existingValue: ptr.To(false), expectChange: true},
449+
{name: "Disabled when was enabled", newValue: ptr.To(false), existingValue: ptr.To(true), expectChange: true},
450+
{name: "Keep the existing value if newValue is not set", newValue: nil, existingValue: ptr.To(true), expectChange: false},
451+
{name: "Keep the existing value when they are the same (true)", newValue: ptr.To(true), existingValue: ptr.To(true), expectChange: false},
452+
{name: "Keep the existing value when they are the same (false)", newValue: ptr.To(false), existingValue: ptr.To(false), expectChange: false},
453+
}
454+
455+
for _, tt := range testCases {
456+
t.Run(tt.name, func(t *testing.T) {
457+
resource := &orcv1alpha1.PortResourceSpec{TrustedVIF: tt.newValue}
458+
osResource := &osclients.PortExt{
459+
PortTrustedVIFExt: portstrustedvif.PortTrustedVIFExt{
460+
PortTrustedVIF: tt.existingValue,
461+
},
462+
}
463+
464+
updateOpts := handlePortTrustedVIFUpdate(&ports.UpdateOpts{}, resource, osResource)
465+
466+
got, _ := needsUpdate(updateOpts)
467+
if got != tt.expectChange {
468+
t.Errorf("expected needsUpdate=%v, got %v", tt.expectChange, got)
469+
}
470+
})
471+
}
472+
}

internal/controllers/port/status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,9 @@ func (portStatusWriter) ApplyResourceStatus(log logr.Logger, osResource *osResou
104104
resourceStatus.WithFixedIPs(fixedIPs...)
105105
}
106106

107+
if osResource.PortTrustedVIF != nil {
108+
resourceStatus.WithTrustedVIF(*osResource.PortTrustedVIF)
109+
}
110+
107111
statusApply.WithResource(resourceStatus)
108112
}

internal/controllers/port/tests/port-create-sriov/00-assert.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ status:
1515
tags:
1616
- tag1
1717
---
18+
apiVersion: openstack.k-orc.cloud/v1alpha1
19+
kind: Port
20+
metadata:
21+
name: port-create-sriov-admin
22+
status:
23+
resource:
24+
name: port-create-sriov-admin
25+
trustedVIF: true
26+
---
1827
apiVersion: kuttl.dev/v1beta1
1928
kind: TestAssert
2029
resourceRefs:
2130
- apiVersion: openstack.k-orc.cloud/v1alpha1
2231
kind: port
2332
name: port-create-sriov
24-
ref: port
33+
ref: port
2534
- apiVersion: openstack.k-orc.cloud/v1alpha1
2635
kind: subnet
2736
name: port-create-sriov
28-
ref: subnet
37+
ref: subnet
2938
assertAll:
3039
- celExpr: "port.status.id != ''"
3140
- celExpr: "port.status.resource.createdAt != ''"
@@ -35,4 +44,4 @@ assertAll:
3544
- celExpr: "port.status.resource.fixedIPs[0].subnetID == subnet.status.id"
3645
- celExpr: "port.status.resource.fixedIPs[0].ip == '192.168.155.122'"
3746
- celExpr: "!has(port.status.resource.allowedAddressPairs)"
38-
- celExpr: "!has(port.status.resource.securityGroups)"
47+
- celExpr: "!has(port.status.resource.securityGroups)"

internal/controllers/port/tests/port-create-sriov/00-create-resource.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,19 @@ spec:
4343
addresses:
4444
- subnetRef: port-create-sriov
4545
ip: 192.168.155.122
46-
vnicType: direct
46+
vnicType: direct
47+
---
48+
# This port is intended to be used to update fields where policies
49+
# enforce its mutability only by admins.
50+
apiVersion: openstack.k-orc.cloud/v1alpha1
51+
kind: Port
52+
metadata:
53+
name: port-create-sriov-admin
54+
spec:
55+
cloudCredentialsRef:
56+
cloudName: openstack-admin
57+
secretName: openstack-clouds
58+
managementPolicy: managed
59+
resource:
60+
networkRef: port-create-sriov
61+
trustedVIF: true

0 commit comments

Comments
 (0)