Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KO-335] Adding support for HPA #307

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/v1/aerospikecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,8 @@ type AerospikeClusterStatus struct { //nolint:govet // for readability
Pods map[string]AerospikePodStatus `json:"pods" patchStrategy:"strategic"`

// Phase denotes the current phase of Aerospike cluster operation.
Phase AerospikeClusterPhase `json:"phase,omitempty"`
Phase AerospikeClusterPhase `json:"phase,omitempty"`
Selector string `json:"selector"`
}

// AerospikeNetworkType specifies the type of network address to use.
Expand Down Expand Up @@ -955,6 +956,7 @@ type AerospikePodStatus struct { //nolint:govet // for readability
// +kubebuilder:printcolumn:name="HostNetwork",type=boolean,JSONPath=`.spec.podSpec.hostNetwork`
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase"
// +kubebuilder:subresource:scale:specpath=.spec.size,statuspath=.status.size,selectorpath=.status.selector

// AerospikeCluster is the schema for the AerospikeCluster API
// +operator-sdk:csv:customresourcedefinitions:displayName="Aerospike Cluster",resources={{Service, v1},{Pod,v1},{StatefulSet,v1}}
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16713,6 +16713,8 @@ spec:
type: integer
type: object
type: object
selector:
type: string
size:
description: Aerospike cluster size
format: int32
Expand Down Expand Up @@ -17306,11 +17308,17 @@ spec:
- skipWorkDirValidate
- skipXdrDlogFileValidate
type: object
required:
- selector
type: object
type: object
served: true
storage: true
subresources:
scale:
labelSelectorPath: .status.selector
specReplicasPath: .spec.size
statusReplicasPath: .status.size
status: {}
- additionalPrinterColumns:
- jsonPath: .spec.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16713,6 +16713,8 @@ spec:
type: integer
type: object
type: object
selector:
type: string
size:
description: Aerospike cluster size
format: int32
Expand Down Expand Up @@ -17306,11 +17308,17 @@ spec:
- skipWorkDirValidate
- skipXdrDlogFileValidate
type: object
required:
- selector
type: object
type: object
served: true
storage: true
subresources:
scale:
labelSelectorPath: .status.selector
specReplicasPath: .spec.size
statusReplicasPath: .status.size
status: {}
- additionalPrinterColumns:
- jsonPath: .spec.size
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/cluster/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -450,6 +451,9 @@ func (r *SingleClusterReconciler) updateStatus() error {
newAeroCluster.Status.IsReadinessProbeEnabled = clusterReadinessEnable
}

selector := labels.SelectorFromSet(utils.LabelsForAerospikeCluster(newAeroCluster.Name))
newAeroCluster.Status.Selector = selector.String()

err = r.patchStatus(newAeroCluster)
if err != nil {
return fmt.Errorf("error updating status: %w", err)
Expand Down
83 changes: 83 additions & 0 deletions test/cluster/autoscaling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cluster

import (
goctx "context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)

var _ = FDescribe("AutoScaler", func() {
ctx := goctx.TODO()

Context("When doing scale operations", func() {
clusterName := "autoscale"
clusterNamespacedName := getNamespacedName(
clusterName, namespace,
)

BeforeEach(
func() {
aeroCluster := createDummyAerospikeCluster(clusterNamespacedName, 2)
err := deployCluster(k8sClient, ctx, aeroCluster)
Expect(err).ToNot(HaveOccurred())
},
)

AfterEach(
func() {
aeroCluster, err := getCluster(
k8sClient, ctx, clusterNamespacedName,
)
Expect(err).ToNot(HaveOccurred())

_ = deleteCluster(k8sClient, ctx, aeroCluster)
},
)

It(
"Should trigger scale up/down via scale subresource", func() {
// Testing over upgrade as it is a long-running operation
By("Scale up the cluster")

aeroCluster, err := getCluster(k8sClient, ctx, clusterNamespacedName)
Expect(err).ToNot(HaveOccurred())

gvr := schema.GroupVersionResource{
Group: "asdb.aerospike.com", // Replace with your CRD group
Version: "v1", // API version
Resource: "aerospikeclusters", // Replace with your resource
}

dynamicClient := dynamic.NewForConfigOrDie(cfg)
Expect(dynamicClient).ToNot(BeNil())

scale, err := dynamicClient.Resource(gvr).Namespace(aeroCluster.Namespace).Get(context.TODO(),
aeroCluster.GetName(), metav1.GetOptions{}, "scale")
Expect(err).ToNot(HaveOccurred())

Expect(scale.Object["spec"].(map[string]interface{})["replicas"]).To(Equal(int64(2)))

scale.Object["spec"].(map[string]interface{})["replicas"] = 3

_, err = dynamicClient.Resource(gvr).Namespace(aeroCluster.Namespace).Update(context.TODO(),
scale, metav1.UpdateOptions{}, "scale")
Expect(err).ToNot(HaveOccurred())

Eventually(
func() int32 {
aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName)
Expect(err).ToNot(HaveOccurred())

return aeroCluster.Spec.Size
}, 1*time.Minute,
).Should(Equal(int32(3)))
},
)
})
})
1 change: 1 addition & 0 deletions test/cluster/large_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func loadDataInCluster(

fmt.Print(strconv.Itoa(i) + ", ")
}

fmt.Println("added records")

return nil
Expand Down
Loading