Skip to content

Commit

Permalink
Use a regular expression in server ListOSResourcesForAdoption
Browse files Browse the repository at this point in the history
In adoption use a regular expression to get a specific server instead of using the name
as it is which can lead to getting servers that contain the server name in their names
Example: We look for 'foo' where there is a server 'foobar'

Also added a test for server adoption
 - Create one server named 'create-adoption'.
- Create another server with the substring of the first - 'adoption'
- Test that the resource name of the second server is 'adoption'.

Done the same for import - same fix and added a test
Fixes k-orc#250
  • Loading branch information
itzikb-redhat committed Feb 16, 2025
1 parent 88322db commit 806f927
Show file tree
Hide file tree
Showing 21 changed files with 409 additions and 2 deletions.
3 changes: 3 additions & 0 deletions hack/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ trap cleanup EXIT
export E2E_KUTTL_OSCLOUDS="${creds_dir}/clouds.yaml"
export E2E_KUTTL_CACERT_OPT

# default flavor name to use for tests
export E2E_KUTTL_FLAVOR=${E2E_KUTTL_FLAVOR:-m1.tiny}

# Name of the openstack credentials to use from the E2E_OSCLOUDS file
E2E_OPENSTACK_CLOUD_NAME=${E2E_OPENSTACK_CLOUD_NAME:-devstack}
# Name of the openstack admin credentials to use from the E2E_OSCLOUDS file
Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/server/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func (actuator serverActuator) ListOSResourcesForAdoption(ctx context.Context, o
}

listOpts := servers.ListOpts{
Name: string(getResourceName(obj)),
Name: fmt.Sprintf("^%s$", string(getResourceName(obj))),
}
return actuator.osClient.ListServers(ctx, listOpts), true
}

func (actuator serverActuator) ListOSResourcesForImport(ctx context.Context, filter filterT) serverIterator {
listOpts := servers.ListOpts{
Name: string(ptr.Deref(filter.Name, "")),
Name: fmt.Sprintf("^%s$", string(ptr.Deref(filter.Name, ""))),
}
return actuator.osClient.ListServers(ctx, listOpts)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: create-adoption
status:
resource:
name: create-adoption
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: kubectl create secret generic openstack-clouds --from-file=clouds.yaml=${E2E_KUTTL_OSCLOUDS} ${E2E_KUTTL_CACERT_OPT}
namespaced: true
- script: cat ../templates/create-flavor.tmpl |envsubst | kubectl -n ${NAMESPACE} apply -f -
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Image
metadata:
name: create-adoption
spec:
cloudCredentialsRef:
cloudName: openstack-admin
secretName: openstack-clouds
managementPolicy: managed
resource:
content:
diskFormat: qcow2
download:
url: https://download.cirros-cloud.net/0.6.3/cirros-0.6.3-x86_64-disk.img
visibility: public
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Network
metadata:
name: create-adoption
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: managed
resource:
name: create-adoption
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Subnet
metadata:
name: create-adoption
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: managed
networkRef: create-adoption
resource:
ipVersion: 4
cidr: 192.168.200.0/24
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Port
metadata:
name: create-adoption
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: managed
networkRef: create-adoption
resource:
addresses:
- subnetRef: create-adoption
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: create-adoption
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: managed
resource:
imageRef: create-adoption
flavorRef: server-flavor
ports:
- portRef: create-adoption
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Port
metadata:
name: adoption
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: managed
networkRef: create-adoption
resource:
addresses:
- subnetRef: create-adoption
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: adoption
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: managed
resource:
imageRef: create-adoption
flavorRef: server-flavor
ports:
- portRef: adoption
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: adoption
status:
resource:
name: adoption
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
commands:
- script: "! kubectl get server create-adoption --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get port create-adoption --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get server adoption --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get port adoption --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get subnet create-adoption --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get network create-adoption --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get flavor server-flavor --namespace $NAMESPACE"
skipLogOutput: true
- script: "! kubectl get image create-adoption --namespace $NAMESPACE"
skipLogOutput: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
delete:
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
name: create-adoption
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Port
name: create-adoption
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
name: adoption
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Port
name: adoption
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Subnet
name: create-adoption
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Network
name: create-adoption
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Flavor
name: server-flavor
- apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Image
name: create-adoption
13 changes: 13 additions & 0 deletions internal/controllers/server/tests/create-adoption-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Create two servers to test adoption

## Step 00

Import a flavor, create an image,network,subnet and a port and then create a server with name 'create-adoption'.

## Step 01
Create another server with the name 'adoption'. The second server should have a resource name of 'adoption'

## Step 02

Validate we're able to delete resources.
Cleaning up resources also avoids a race where kuttl could delete the secret before the other resources.
30 changes: 30 additions & 0 deletions internal/controllers/server/tests/import-server/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: import
status:
conditions:
- type: Available
message: Waiting for OpenStack resource to be created externally
status: "False"
reason: Progressing
- type: Progressing
message: Waiting for OpenStack resource to be created externally
status: "True"
reason: Progressing
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: import-error
status:
conditions:
- type: Available
message: Waiting for OpenStack resource to be created externally
status: "False"
reason: Progressing
- type: Progressing
message: Waiting for OpenStack resource to be created externally
status: "True"
reason: Progressing
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: kubectl create secret generic openstack-clouds --from-file=clouds.yaml=${E2E_KUTTL_OSCLOUDS} ${E2E_KUTTL_CACERT_OPT}
namespaced: true
- script: cat ../templates/create-flavor.tmpl |envsubst | kubectl -n ${NAMESPACE} apply -f -
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: import-error
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: unmanaged
import:
filter:
name: external
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: import
spec:
cloudCredentialsRef:
cloudName: openstack
secretName: openstack-clouds
managementPolicy: unmanaged
import:
filter:
name: import-external
32 changes: 32 additions & 0 deletions internal/controllers/server/tests/import-server/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: import
status:
conditions:
- type: Available
message: OpenStack resource is available
status: "True"
reason: Success
- type: Progressing
message: OpenStack resource is up to date
status: "False"
reason: Success
resource:
name: import-external
---
apiVersion: openstack.k-orc.cloud/v1alpha1
kind: Server
metadata:
name: import-error
status:
conditions:
- type: Available
message: Waiting for OpenStack resource to be created externally
status: "False"
reason: Progressing
- type: Progressing
message: Waiting for OpenStack resource to be created externally
status: "True"
reason: Progressing
Loading

0 comments on commit 806f927

Please sign in to comment.