Skip to content

Commit

Permalink
Move helpers functions to 'utils' and update code to use them
Browse files Browse the repository at this point in the history
  • Loading branch information
arybolovlev committed Jul 19, 2023
1 parent dfe8096 commit 95db1ac
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: API tests
name: Unit tests

on:
schedule:
Expand All @@ -25,5 +25,7 @@ jobs:
with:
go-version-file: 'go.mod'

- name: Run API tests
run: make test-api
- name: Run Unit tests
run: |
make test-api
make test-utils
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ test: manifests generate fmt vet copywrite envtest ## Run tests.
test-api: fmt vet copywrite ## Run API tests.
go test -timeout 30m -count 1 -v ./api/v1alpha2

.PHONY: test-utils
test-utils: fmt vet copywrite ## Run API tests.
go test -timeout 30m -count 1 -v ./utils

##@ Build

.PHONY: build
Expand Down
6 changes: 4 additions & 2 deletions api/v1alpha2/agentpool_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package v1alpha2

import (
"testing"

"github.com/hashicorp/terraform-cloud-operator/utils"
)

func TestValidateAgentPoolSpecAgentToken(t *testing.T) {
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestValidateAgentPoolSpecAgentToken(t *testing.T) {
AgentTokens: []*AgentToken{
{
Name: "this",
CreatedAt: PointerOf(int64(1984)),
CreatedAt: utils.PointerOf(int64(1984)),
},
},
},
Expand All @@ -66,7 +68,7 @@ func TestValidateAgentPoolSpecAgentToken(t *testing.T) {
AgentTokens: []*AgentToken{
{
Name: "this",
LastUsedAt: PointerOf(int64(1984)),
LastUsedAt: utils.PointerOf(int64(1984)),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions controllers/agentpool_controller_autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

appv1alpha2 "github.com/hashicorp/terraform-cloud-operator/api/v1alpha2"
"github.com/hashicorp/terraform-cloud-operator/utils"
)

func getWorkspaceQueueDepth(ctx context.Context, ap *agentPoolInstance, workspaceID string) (int, error) {
Expand Down Expand Up @@ -135,7 +135,7 @@ func (r *AgentPoolReconciler) reconcileAgentAutoscaling(ctx context.Context, ap
} else if (int(*currentReplicas) + queueDepth) > int(*ap.instance.Spec.AgentDeploymentAutoscaling.MaxReplicas) {
desiredReplicas = ap.instance.Spec.AgentDeploymentAutoscaling.MaxReplicas
} else if queueDepth > int(*currentReplicas) {
desiredReplicas = pointer.Int32(int32(int(*currentReplicas) + queueDepth))
desiredReplicas = utils.PointerOf(int32(int(*currentReplicas) + queueDepth))
}

if *desiredReplicas != *currentReplicas {
Expand Down
7 changes: 4 additions & 3 deletions controllers/agentpool_controller_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/hashicorp/terraform-cloud-operator/utils"
)

const (
Expand Down Expand Up @@ -121,7 +122,7 @@ func (r *AgentPoolReconciler) deleteDeployment(ctx context.Context, ap *agentPoo
}

func agentPoolDeployment(ap *agentPoolInstance) *appsv1.Deployment {
var r *int32 = pointer.Int32(1) // default to one replica if not otherwise configured
var r *int32 = utils.PointerOf(int32(1)) // default to one replica if not otherwise configured
if ap.instance.Spec.AgentDeployment.Replicas != nil {
r = ap.instance.Spec.AgentDeployment.Replicas
}
Expand Down Expand Up @@ -159,7 +160,7 @@ func agentPoolDeployment(ap *agentPoolInstance) *appsv1.Deployment {
RollingUpdate: &appsv1.RollingUpdateDeployment{
// this is important to ensure the number of pods does not temporarily
// shoot over the max agents allowed when rolling the deployment.
MaxSurge: appv1alpha2.PointerOf(intstr.FromInt(0)),
MaxSurge: utils.PointerOf(intstr.FromInt(0)),
},
},
Template: corev1.PodTemplateSpec{
Expand Down
15 changes: 8 additions & 7 deletions controllers/agentpool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

tfc "github.com/hashicorp/go-tfe"
appv1alpha2 "github.com/hashicorp/terraform-cloud-operator/api/v1alpha2"
"github.com/hashicorp/terraform-cloud-operator/utils"
)

var _ = Describe("Agent Pool controller", Ordered, func() {
Expand Down Expand Up @@ -284,7 +285,7 @@ var _ = Describe("Agent Pool controller", Ordered, func() {

// ADD EMPTY AgentDeployment BLOCK
instance.Spec.AgentDeployment = &appv1alpha2.AgentDeployment{
Replicas: appv1alpha2.PointerOf(int32(3)),
Replicas: utils.PointerOf(int32(3)),
}
Expect(k8sClient.Update(ctx, instance)).Should(Succeed())

Expand Down Expand Up @@ -372,9 +373,9 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
TargetWorkspaces: []appv1alpha2.TargetWorkspace{
{Name: "test-workspace"},
},
MinReplicas: appv1alpha2.PointerOf(int32(3)),
MaxReplicas: appv1alpha2.PointerOf(int32(5)),
CooldownPeriodSeconds: appv1alpha2.PointerOf(int32(60)),
MinReplicas: utils.PointerOf(int32(3)),
MaxReplicas: utils.PointerOf(int32(5)),
CooldownPeriodSeconds: utils.PointerOf(int32(60)),
}
Expect(k8sClient.Update(ctx, instance)).Should(Succeed())

Expand All @@ -386,9 +387,9 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
Expect(instance.Spec.AgentDeploymentAutoscaling.TargetWorkspaces).To(Equal([]appv1alpha2.TargetWorkspace{
{Name: "test-workspace"},
}))
Expect(instance.Spec.AgentDeploymentAutoscaling.MinReplicas).To(Equal(appv1alpha2.PointerOf(int32(3))))
Expect(instance.Spec.AgentDeploymentAutoscaling.MaxReplicas).To(Equal(appv1alpha2.PointerOf(int32(5))))
Expect(instance.Spec.AgentDeploymentAutoscaling.CooldownPeriodSeconds).To(Equal(appv1alpha2.PointerOf(int32(60))))
Expect(instance.Spec.AgentDeploymentAutoscaling.MinReplicas).To(Equal(utils.PointerOf(int32(3))))
Expect(instance.Spec.AgentDeploymentAutoscaling.MaxReplicas).To(Equal(utils.PointerOf(int32(5))))
Expect(instance.Spec.AgentDeploymentAutoscaling.CooldownPeriodSeconds).To(Equal(utils.PointerOf(int32(60))))
})
})
})
Expand Down
6 changes: 4 additions & 2 deletions controllers/agentpool_controller_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/hashicorp/terraform-cloud-operator/utils"
)

func getAgentPoolTokens(ctx context.Context, ap *agentPoolInstance) (map[string]bool, error) {
Expand Down Expand Up @@ -105,8 +107,8 @@ func (r *AgentPoolReconciler) createAgentPoolTokens(ctx context.Context, ap *age
ap.instance.Status.AgentTokens = append(ap.instance.Status.AgentTokens, &appv1alpha2.AgentToken{
Name: at.Description,
ID: at.ID,
CreatedAt: appv1alpha2.PointerOf(at.CreatedAt.Unix()),
LastUsedAt: appv1alpha2.PointerOf(at.LastUsedAt.Unix()),
CreatedAt: utils.PointerOf(at.CreatedAt.Unix()),
LastUsedAt: utils.PointerOf(at.LastUsedAt.Unix()),
})
ap.log.Info("Reconcile Agent Tokens", "msg", fmt.Sprintf("successfully created a new agent token %q %q", t, at.ID))
// UPDATE SECRET
Expand Down
4 changes: 2 additions & 2 deletions controllers/workspace_controller_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
tfc "github.com/hashicorp/go-tfe"

appv1alpha2 "github.com/hashicorp/terraform-cloud-operator/api/v1alpha2"
"github.com/hashicorp/terraform-cloud-operator/utils"
)

func hasNotificationItem(s []tfc.NotificationConfiguration, f tfc.NotificationConfiguration) (int, bool) {
Expand All @@ -34,7 +34,7 @@ func notificationsDifference(a, b []tfc.NotificationConfiguration) []tfc.Notific
for _, av := range a {
i, t := hasNotificationItem(bc, av)
if t {
bc = appv1alpha2.RemoveFromSlice(bc, i)
bc = utils.RemoveFromSlice(bc, i)
} else {
d = append(d, av)
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha2/helpers.go → utils/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package v1alpha2
package utils

func PointerOf[A any](a A) *A {
return &a
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha2/helpers_test.go → utils/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package v1alpha2
package utils

import (
"reflect"
Expand Down

0 comments on commit 95db1ac

Please sign in to comment.