Skip to content

Commit b8d62d3

Browse files
authored
Merge pull request #617 from winiciusallan/port-asu
Port: add adminStateUp field
2 parents bba52e1 + 50cd644 commit b8d62d3

17 files changed

Lines changed: 132 additions & 14 deletions

File tree

api/v1alpha1/port_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type PortFilter struct {
3636
// +optional
3737
ProjectRef *KubernetesNameRef `json:"projectRef,omitempty"`
3838

39+
// adminStateUp is the administrative state of the port,
40+
// which is up (true) or down (false).
41+
// +optional
42+
AdminStateUp *bool `json:"adminStateUp,omitempty"`
43+
3944
FilterByNeutronTags `json:",inline"`
4045
}
4146

@@ -126,6 +131,12 @@ type PortResourceSpec struct {
126131
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="addresses is immutable"
127132
Addresses []Address `json:"addresses,omitempty"`
128133

134+
// adminStateUp is the administrative state of the port,
135+
// which is up (true) or down (false). The default value is true.
136+
// +kubebuilder:default:=true
137+
// +optional
138+
AdminStateUp *bool `json:"adminStateUp,omitempty"`
139+
129140
// securityGroupRefs are the names of the security groups associated
130141
// with this port.
131142
// +kubebuilder:validation:MaxItems:=64

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ spec:
9595
error state and will not continue to retry.
9696
minProperties: 1
9797
properties:
98+
adminStateUp:
99+
description: |-
100+
adminStateUp is the administrative state of the port,
101+
which is up (true) or down (false).
102+
type: boolean
98103
description:
99104
description: description of the existing resource
100105
maxLength: 255
@@ -251,6 +256,12 @@ spec:
251256
x-kubernetes-validations:
252257
- message: addresses is immutable
253258
rule: self == oldSelf
259+
adminStateUp:
260+
default: true
261+
description: |-
262+
adminStateUp is the administrative state of the port,
263+
which is up (true) or down (false). The default value is true.
264+
type: boolean
254265
allowedAddressPairs:
255266
description: allowedAddressPairs are allowed addresses associated
256267
with this port.

internal/controllers/port/actuator.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ func (actuator portActuator) ListOSResourcesForImport(ctx context.Context, obj o
132132
}
133133

134134
listOpts := ports.ListOpts{
135-
Name: string(ptr.Deref(filter.Name, "")),
136-
Description: string(ptr.Deref(filter.Description, "")),
137-
NetworkID: ptr.Deref(network.Status.ID, ""),
138-
ProjectID: ptr.Deref(project.Status.ID, ""),
139-
Tags: tags.Join(filter.Tags),
140-
TagsAny: tags.Join(filter.TagsAny),
141-
NotTags: tags.Join(filter.NotTags),
142-
NotTagsAny: tags.Join(filter.NotTagsAny),
135+
Name: string(ptr.Deref(filter.Name, "")),
136+
Description: string(ptr.Deref(filter.Description, "")),
137+
NetworkID: ptr.Deref(network.Status.ID, ""),
138+
ProjectID: ptr.Deref(project.Status.ID, ""),
139+
Tags: tags.Join(filter.Tags),
140+
TagsAny: tags.Join(filter.TagsAny),
141+
NotTags: tags.Join(filter.NotTags),
142+
NotTagsAny: tags.Join(filter.NotTagsAny),
143+
AdminStateUp: filter.AdminStateUp,
143144
}
144145

145146
return actuator.osClient.ListPort(ctx, listOpts), nil
@@ -191,10 +192,11 @@ func (actuator portActuator) CreateResource(ctx context.Context, obj *orcv1alpha
191192
}
192193

193194
createOpts := ports.CreateOpts{
194-
NetworkID: *network.Status.ID,
195-
Name: getResourceName(obj),
196-
Description: string(ptr.Deref(resource.Description, "")),
197-
ProjectID: projectID,
195+
NetworkID: *network.Status.ID,
196+
Name: getResourceName(obj),
197+
Description: string(ptr.Deref(resource.Description, "")),
198+
ProjectID: projectID,
199+
AdminStateUp: resource.AdminStateUp,
198200
}
199201

200202
if len(resource.AllowedAddressPairs) > 0 {
@@ -361,6 +363,7 @@ func (actuator portActuator) updateResource(ctx context.Context, obj orcObjectPT
361363
handleDescriptionUpdate(baseUpdateOpts, resource, osResource)
362364
handleAllowedAddressPairsUpdate(baseUpdateOpts, resource, osResource)
363365
handleSecurityGroupRefsUpdate(baseUpdateOpts, resource, osResource, secGroupMap)
366+
handleAdminStateUpUpdate(baseUpdateOpts, resource, osResource)
364367
updateOpts = baseUpdateOpts
365368
}
366369

@@ -527,6 +530,15 @@ func handlePortSecurityUpdate(updateOpts ports.UpdateOptsBuilder, resource *reso
527530
return updateOpts
528531
}
529532

533+
func handleAdminStateUpUpdate(updateOpts *ports.UpdateOpts, resource *resourceSpecT, osResouce *osResourceT) {
534+
adminStateUp := resource.AdminStateUp
535+
if adminStateUp != nil {
536+
if *adminStateUp != osResouce.AdminStateUp {
537+
updateOpts.AdminStateUp = adminStateUp
538+
}
539+
}
540+
}
541+
530542
type portHelperFactory struct{}
531543

532544
var _ helperFactory = portHelperFactory{}

internal/controllers/port/actuator_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,31 @@ func TestHandlePortSecurityUpdate(t *testing.T) {
407407
})
408408
}
409409
}
410+
411+
func TestHandleAdminStateUpUpdate(t *testing.T) {
412+
testCases := []struct {
413+
name string
414+
newValue *bool
415+
existingValue bool
416+
expectChange bool
417+
}{
418+
{name: "Enabled when already enabled", newValue: ptr.To(true), existingValue: true, expectChange: false},
419+
{name: "Enabled when was disabled", newValue: ptr.To(true), existingValue: false, expectChange: true},
420+
{name: "Keep the existing value if newValue is not set", newValue: nil, existingValue: true, expectChange: false},
421+
}
422+
423+
for _, tt := range testCases {
424+
t.Run(tt.name, func(t *testing.T) {
425+
resource := &orcv1alpha1.PortResourceSpec{AdminStateUp: tt.newValue}
426+
osResource := &osclients.PortExt{Port: ports.Port{AdminStateUp: tt.existingValue}}
427+
updateOpts := &ports.UpdateOpts{}
428+
429+
handleAdminStateUpUpdate(updateOpts, resource, osResource)
430+
431+
got, _ := needsUpdate(updateOpts)
432+
if got != tt.expectChange {
433+
t.Errorf("expected needsUpdate=%v, got %v", tt.expectChange, got)
434+
}
435+
})
436+
}
437+
}

internal/controllers/port/status.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func (portStatusWriter) ApplyResourceStatus(log logr.Logger, osResource *osResou
7272
WithPortSecurityEnabled(osResource.PortSecurityEnabled).
7373
WithRevisionNumber(int64(osResource.RevisionNumber)).
7474
WithCreatedAt(metav1.NewTime(osResource.CreatedAt)).
75-
WithUpdatedAt(metav1.NewTime(osResource.UpdatedAt))
75+
WithUpdatedAt(metav1.NewTime(osResource.UpdatedAt)).
76+
WithAdminStateUp(osResource.AdminStateUp)
7677

7778
if osResource.Description != "" {
7879
resourceStatus.WithDescription(osResource.Description)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ status:
77
resource:
88
name: port-create-full-override
99
description: Port from "create full" test
10-
adminStateUp: true
10+
adminStateUp: false
1111
allowedAddressPairs:
1212
- ip: 192.168.122.14
1313
mac: 92:42:9c:13:98:82

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ spec:
6969
resource:
7070
name: port-create-full-override
7171
description: Port from "create full" test
72+
adminStateUp: false
7273
networkRef: port-create-full
7374
allowedAddressPairs:
7475
- ip: "192.168.122.14"

internal/controllers/port/tests/port-import/00-import-resource.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ spec:
1212
filter:
1313
name: port-import-external
1414
description: Port from "port-import" test
15+
adminStateUp: false
1516
tags:
1617
- tag1

0 commit comments

Comments
 (0)