diff --git a/api/v1alpha1/chronoscaler_types.go b/api/v1alpha1/chronoscaler_types.go index e59a356..52d3a7a 100644 --- a/api/v1alpha1/chronoscaler_types.go +++ b/api/v1alpha1/chronoscaler_types.go @@ -4,8 +4,10 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. +const ( + SUCCESS = "Success" + FAILED = "Failed" +) // ChronoScalerSpec defines the desired state of ChronoScaler type ChronoScalerSpec struct { @@ -32,6 +34,7 @@ type NamespacedName struct { type ChronoScalerStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file + Status string `json:"status,omitempty"` } //+kubebuilder:object:root=true diff --git a/config/crd/bases/api.zszazi.github.io_chronoscalers.yaml b/config/crd/bases/api.zszazi.github.io_chronoscalers.yaml index 80d6c82..8c8892d 100644 --- a/config/crd/bases/api.zszazi.github.io_chronoscalers.yaml +++ b/config/crd/bases/api.zszazi.github.io_chronoscalers.yaml @@ -80,6 +80,12 @@ spec: type: object status: description: ChronoScalerStatus defines the observed state of ChronoScaler + properties: + status: + description: |- + INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + Important: Run "make" to regenerate code after modifying this file + type: string type: object type: object served: true diff --git a/config/samples/api_v1alpha1_chronoscaler.yaml b/config/samples/api_v1alpha1_chronoscaler.yaml index 2411fa2..b3ec49d 100644 --- a/config/samples/api_v1alpha1_chronoscaler.yaml +++ b/config/samples/api_v1alpha1_chronoscaler.yaml @@ -7,9 +7,9 @@ metadata: name: chronoscaler-sample spec: start: "9h23m" - end: "14h41m" + end: "21h41m" replicas: 3 - defaultReplicas: 2 + defaultReplicas: 1 deployments: - name: nginx namespace: default \ No newline at end of file diff --git a/internal/controller/chronoscaler_controller.go b/internal/controller/chronoscaler_controller.go index db2c437..d4bd70f 100644 --- a/internal/controller/chronoscaler_controller.go +++ b/internal/controller/chronoscaler_controller.go @@ -91,6 +91,12 @@ func (r *ChronoScalerReconciler) Reconcile(ctx context.Context, req ctrl.Request deployment.Spec.Replicas = &targetReplicas err := r.Update(ctx, deployment) + if err != nil { + scaler.Status.Status = apiv1alpha1.FAILED + return ctrl.Result{}, err + } + scaler.Status.Status = apiv1alpha1.SUCCESS + err = r.Status().Update(ctx, scaler) if err != nil { return ctrl.Result{}, err } diff --git a/internal/controller/chronoscaler_controller_test.go b/internal/controller/chronoscaler_controller_test.go deleted file mode 100644 index 8ccb91f..0000000 --- a/internal/controller/chronoscaler_controller_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package controller - -import ( - "context" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - apiv1alpha1 "github.com/zszazi/Chrono-Scaler/api/v1alpha1" -) - -var _ = Describe("ChronoScaler Controller", func() { - Context("When reconciling a resource", func() { - const resourceName = "test-resource" - - ctx := context.Background() - - typeNamespacedName := types.NamespacedName{ - Name: resourceName, - Namespace: "default", // TODO(user):Modify as needed - } - chronoscaler := &apiv1alpha1.ChronoScaler{} - - BeforeEach(func() { - By("creating the custom resource for the Kind ChronoScaler") - err := k8sClient.Get(ctx, typeNamespacedName, chronoscaler) - if err != nil && errors.IsNotFound(err) { - resource := &apiv1alpha1.ChronoScaler{ - ObjectMeta: metav1.ObjectMeta{ - Name: resourceName, - Namespace: "default", - }, - // TODO(user): Specify other spec details if needed. - } - Expect(k8sClient.Create(ctx, resource)).To(Succeed()) - } - }) - - AfterEach(func() { - // TODO(user): Cleanup logic after each test, like removing the resource instance. - resource := &apiv1alpha1.ChronoScaler{} - err := k8sClient.Get(ctx, typeNamespacedName, resource) - Expect(err).NotTo(HaveOccurred()) - - By("Cleanup the specific resource instance ChronoScaler") - Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) - }) - It("should successfully reconcile the resource", func() { - By("Reconciling the created resource") - controllerReconciler := &ChronoScalerReconciler{ - Client: k8sClient, - Scheme: k8sClient.Scheme(), - } - - _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: typeNamespacedName, - }) - Expect(err).NotTo(HaveOccurred()) - // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. - // Example: If you expect a certain status condition after reconciliation, verify it here. - }) - }) -})