Skip to content

Commit

Permalink
Enabling Soft Affinity and Soft Anti Affinity
Browse files Browse the repository at this point in the history
  • Loading branch information
Farnaz Babaeian authored and Farnaz Babaeian committed Oct 30, 2023
1 parent c91e119 commit c24fef2
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/cloudstackaffinitygroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// CloudStackAffinityGroupSpec defines the desired state of CloudStackAffinityGroup
type CloudStackAffinityGroupSpec struct {
// Mutually exclusive parameter with AffinityGroupIDs.
// Can be "host affinity" or "host anti-affinity". Will create an affinity group per machine set.
// Can be "host affinity", "host anti-affinity", "non-strict host affinity"or "non-strict host anti-affinity". Will create an affinity group per machine set.
Type string `json:"type,omitempty"`

// Name.
Expand Down
2 changes: 2 additions & 0 deletions api/v1beta1/cloudstackmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
MachineFinalizer = "cloudstackmachine.infrastructure.cluster.x-k8s.io"
ProAffinity = "pro"
AntiAffinity = "anti"
SoftProAffinity = "soft-pro"
SoftAntiAffinity = "soft-anti"
NoAffinity = "no"
)

Expand Down
8 changes: 5 additions & 3 deletions api/v1beta2/cloudstackmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
const MachineFinalizer = "cloudstackmachine.infrastructure.cluster.x-k8s.io"

const (
ProAffinity = "pro"
AntiAffinity = "anti"
NoAffinity = "no"
ProAffinity = "pro"
AntiAffinity = "anti"
SoftProAffinity = "soft-pro"
SoftAntiAffinity = "soft-anti"
NoAffinity = "no"
)

// CloudStackMachineSpec defines the desired state of CloudStackMachine
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta3/cloudstackaffinitygroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AffinityGroupFinalizer = "affinitygroup.infrastructure.cluster.x-k8s.io"
// CloudStackAffinityGroupSpec defines the desired state of CloudStackAffinityGroup
type CloudStackAffinityGroupSpec struct {
// Mutually exclusive parameter with AffinityGroupIDs.
// Can be "host affinity" or "host anti-affinity". Will create an affinity group per machine set.
// Can be "host affinity", "host anti-affinity", "non-strict host affinity" or "non-strict host anti-affinity". Will create an affinity group per machine set.
Type string `json:"type,omitempty"`

// Name.
Expand Down
8 changes: 5 additions & 3 deletions api/v1beta3/cloudstackmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
const MachineFinalizer = "cloudstackmachine.infrastructure.cluster.x-k8s.io"

const (
ProAffinity = "pro"
AntiAffinity = "anti"
NoAffinity = "no"
ProAffinity = "pro"
AntiAffinity = "anti"
SoftProAffinity = "soft-pro"
SoftAntiAffinity = "soft-anti"
NoAffinity = "no"
)

// CloudStackMachineSpec defines the desired state of CloudStackMachine
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta3/cloudstackmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func (r *CloudStackMachineTemplate) ValidateCreate() error {
spec := r.Spec.Template.Spec

affinity := strings.ToLower(spec.Affinity)
if !(affinity == "" || affinity == "no" || affinity == "pro" || affinity == "anti") {
if !(affinity == "" || affinity == "no" || affinity == "pro" || affinity == "anti" || affinity == "soft-pro" || affinity == "soft-anti") {
errorList = append(errorList, field.Invalid(field.NewPath("spec", "Affinity"), spec.Affinity,
`Affinity must be "no", "pro", "anti", or unspecified.`))
`Affinity must be "no", "pro", "anti", "soft-pro", "soft-anti", or unspecified.`))
}
if affinity != "no" && affinity != "" && len(spec.AffinityGroupIDs) > 0 {
errorList = append(errorList, field.Forbidden(field.NewPath("spec", "AffinityGroupIDs"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ spec:
type: string
type:
description: Mutually exclusive parameter with AffinityGroupIDs. Can
be "host affinity" or "host anti-affinity". Will create an affinity
group per machine set.
be "host affinity", "host anti-affinity", "non-strict host affinity"or
"non-strict host anti-affinity". Will create an affinity group per
machine set.
type: string
type: object
status:
Expand Down Expand Up @@ -151,8 +152,9 @@ spec:
type: string
type:
description: Mutually exclusive parameter with AffinityGroupIDs. Can
be "host affinity" or "host anti-affinity". Will create an affinity
group per machine set.
be "host affinity", "host anti-affinity", "non-strict host affinity"
or "non-strict host anti-affinity". Will create an affinity group
per machine set.
type: string
type: object
status:
Expand Down
4 changes: 4 additions & 0 deletions controllers/utils/affinity_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (r *ReconciliationRunner) GetOrCreateAffinityGroup(
ag.Spec.Type = "host affinity"
} else if affinityType == infrav1.AntiAffinity {
ag.Spec.Type = "host anti-affinity"
} else if affinityType == infrav1.SoftProAffinity {
ag.Spec.Type = "non-strict host affinity"
} else if affinityType == infrav1.SoftAntiAffinity {
ag.Spec.Type = "non-strict host anti-affinity"
} else {
return ctrl.Result{}, errors.Errorf("unrecognized affinity type %s", affinityType)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/cloud/affinity_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
)

const (
AntiAffinityGroupType = "host anti-affinity"
AffinityGroupType = "host affinity"
AntiAffinityGroupType = "host anti-affinity"
AffinityGroupType = "host affinity"
SoftAntiAffinityGroupType = "non-stric anti-affinity"
SoftAffinityGroupType = "non-stric host affinity"
)

type AffinityGroup struct {
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ E2E Test Run Logs: https://github.com/blueorangutan/capc-prs/releases/download/c
[PASS] When testing with disk offering Should successfully create a cluster with disk offering
[PASS] When testing affinity group Should have host affinity group when affinity is pro
[PASS] When testing affinity group Should have host affinity group when affinity is anti
[PASS] When testing affinity group Should have host affinity group when affinity is soft-pro
[PASS] When testing affinity group Should have host affinity group when affinity is soft-anti
[PASS] When the specified resource does not exist Should fail due to the specified account is not found [TC4a]
[PASS] When the specified resource does not exist Should fail due to the specified domain is not found [TC4b]
[PASS] When the specified resource does not exist Should fail due to the specified control plane offering is not found [TC7]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/affinity_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func AffinityGroupSpec(ctx context.Context, inputGetter func() CommonSpecInput)
affinityIds = executeTest(ctx, input, namespace, specName, clusterResources, "anti")
})

It("Should have host affinity group when affinity is soft-pro", func() {
affinityIds = executeTest(ctx, input, namespace, specName, clusterResources, "soft-pro")
})

It("Should have host affinity group when affinity is soft-anti", func() {
affinityIds = executeTest(ctx, input, namespace, specName, clusterResources, "soft-anti")
})

AfterEach(func() {
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ func CheckAffinityGroup(client *cloudstack.CloudStackClient, clusterName string,
if affinityType == "anti" && affinity.Type != "host anti-affinity" {
Fail(affinity.Type + " does not match " + affinityType)
}
if affinityType == "soft-pro" && affinity.Type != "non-stric host affinity" {
Fail(affinity.Type + " does not match " + affinityType)
}
if affinityType == "soft-anti" && affinity.Type != "non-strict host anti-affinity" {
Fail(affinity.Type + " does not match " + affinityType)
}
}
}
}
Expand Down

0 comments on commit c24fef2

Please sign in to comment.