Skip to content

Commit df73d3f

Browse files
Merge pull request #308 from rabi/namespacedName
Use namespacedName from apimachinery
2 parents 4b636ff + 9557f3f commit df73d3f

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

api/v1beta1/openstackprovisionserver.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"k8s.io/apimachinery/pkg/types"
78
goClient "sigs.k8s.io/controller-runtime/pkg/client"
89
)
910

@@ -14,18 +15,20 @@ func GetExistingProvServerPorts(
1415
instance *OpenStackProvisionServer,
1516
) (map[string]int32, error) {
1617
found := map[string]int32{}
17-
1818
provServerList := &OpenStackProvisionServerList{}
1919

2020
listOpts := []goClient.ListOption{}
2121

2222
err := c.List(ctx, provServerList, listOpts...)
2323
if err != nil {
24-
return nil, fmt.Errorf("Failed to get list of all OpenStackProvisionServer(s): %s", err.Error())
24+
return nil, fmt.Errorf("failed to get list of all OpenStackProvisionServer(s): %s", err.Error())
2525
}
2626

2727
for _, provServer := range provServerList.Items {
28-
found[fmt.Sprintf("%s-%s", provServer.Name, provServer.Namespace)] = provServer.Spec.Port
28+
namespacedName := types.NamespacedName{
29+
Namespace: provServer.Namespace,
30+
Name: provServer.Name}
31+
found[namespacedName.String()] = provServer.Spec.Port
2932
}
3033

3134
return found, nil
@@ -44,10 +47,12 @@ func AssignProvisionServerPort(
4447
return err
4548
}
4649

50+
namespacedName := types.NamespacedName{
51+
Namespace: instance.Namespace,
52+
Name: instance.Name}
4753
// It's possible that this prov server already exists and we are just dealing with
4854
// a minimized version of it (only its ObjectMeta is set, etc)
49-
instanceKey := fmt.Sprintf("%s-%s", instance.GetName(), instance.GetNamespace())
50-
cur := existingPorts[instanceKey]
55+
cur := existingPorts[namespacedName.String()]
5156
if cur == 0 {
5257
cur = portStart
5358
}
@@ -65,7 +70,7 @@ func AssignProvisionServerPort(
6570
}
6671

6772
if found {
68-
if existingPorts[instanceKey] != cur {
73+
if existingPorts[namespacedName.String()] != cur {
6974
// continue to use the next port in the port range.
7075
continue
7176
} else {

api/v1beta1/openstackprovisionserver_webhook.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/go-playground/validator/v10"
2929
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/types"
3031
"k8s.io/apimachinery/pkg/util/validation/field"
3132
ctrl "sigs.k8s.io/controller-runtime"
3233
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -92,7 +93,8 @@ func (r *OpenStackProvisionServer) validateCr() error {
9293
}
9394

9495
for name, port := range existingPorts {
95-
if name != fmt.Sprintf("%s-%s", r.Name, r.Namespace) && port == r.Spec.Port {
96+
namespacedName := types.NamespacedName{Namespace: r.Namespace, Name: r.Name}
97+
if port == r.Spec.Port && name != namespacedName.String() {
9698
return fmt.Errorf("port %d is already in use by another OpenStackProvisionServer: %s", port, name)
9799
}
98100
}

tests/functional/openstackbaremetalset_controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ var _ = Describe("BaremetalSet Test", func() {
250250

251251
provServer := GetProvisionServer(baremetalSetName)
252252
provServer2 := GetProvisionServer(baremetalSet2Name)
253-
254-
Expect(provServer.Spec.Port).Should(Not(Equal(provServer2.Spec.Port)))
253+
Expect(provServer.Spec.Port).ShouldNot(Equal(provServer2.Spec.Port))
255254
})
256255

257256
It("Should set Provision Server Ready", func() {

0 commit comments

Comments
 (0)