Skip to content

Commit

Permalink
Fix sha generation for pr automations
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Dec 7, 2024
1 parent 772a292 commit 325776f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/pluralsh/console/go/controller/internal/utils"
)

func (in *PrAutomationReconciler) attributes(ctx context.Context, pra *v1alpha1.PrAutomation) (*console.PrAutomationAttributes, error) {
func (in *PrAutomationReconciler) Attributes(ctx context.Context, pra *v1alpha1.PrAutomation) (*console.PrAutomationAttributes, error) {
helper := utils.NewConsoleHelper(ctx, in.ConsoleClient, in.Client)

clusterID, err := helper.IDFromRef(pra.Spec.ClusterRef, &v1alpha1.Cluster{})
Expand Down
47 changes: 27 additions & 20 deletions go/controller/internal/controller/prautomation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,8 @@ func (in *PrAutomationReconciler) Reconcile(ctx context.Context, req reconcile.R
return *result, err
}

// Get PrAutomation SHA that can be saved back in the status to check for changes
changed, sha, err := prAutomation.Diff(utils.HashObject)
if err != nil {
logger.Error(err, "unable to calculate prAutomation SHA")
utils.MarkFalse(prAutomation.SetCondition, v1alpha1.SynchronizedConditionType, v1alpha1.SynchronizedConditionReasonError, err.Error())
return ctrl.Result{}, err
}

// Sync PrAutomation CRD with the Console API
apiPrAutomation, err := in.sync(ctx, prAutomation, changed)
apiPrAutomation, sha, err := in.sync(ctx, prAutomation)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(prAutomation.SetCondition, v1alpha1.SynchronizedConditionType, metav1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
Expand Down Expand Up @@ -150,37 +142,52 @@ func (in *PrAutomationReconciler) addOrRemoveFinalizer(ctx context.Context, prAu
return nil, nil
}

func (in *PrAutomationReconciler) sync(ctx context.Context, prAutomation *v1alpha1.PrAutomation, changed bool) (*console.PrAutomationFragment, error) {
func (in *PrAutomationReconciler) sync(ctx context.Context, prAutomation *v1alpha1.PrAutomation) (pra *console.PrAutomationFragment, sha string, err error) {
logger := log.FromContext(ctx)
exists, err := in.ConsoleClient.IsPrAutomationExistsByName(ctx, prAutomation.ConsoleName())
if err != nil {
return nil, err
return pra, sha, err
}

if exists && !prAutomation.Status.HasID() {
return nil, nil
return pra, sha, err
}

if err = in.ensure(prAutomation); err != nil {
return pra, sha, err
}
if err := in.ensure(prAutomation); err != nil {
return nil, err

attributes, err := in.Attributes(ctx, prAutomation)
if err != nil {
return pra, sha, err
}
attributes, err := in.attributes(ctx, prAutomation)

logger.Info("gathered pr automation attributes", "name", attributes.Name, "catalogID", attributes.CatalogID)

// Get PrAutomation SHA that can be saved back in the status to check for changes
sha, err = utils.HashObject(attributes)
if err != nil {
return nil, err
logger.Error(err, "unable to calculate prAutomation SHA")
utils.MarkFalse(prAutomation.SetCondition, v1alpha1.SynchronizedConditionType, v1alpha1.SynchronizedConditionReasonError, err.Error())
return pra, sha, err
}

// Update only if PrAutomation has changed
if changed && exists {
if prAutomation.Status.SHA != nil && *prAutomation.Status.SHA != sha && exists {
logger.Info("Updating PR automation")
return in.ConsoleClient.UpdatePrAutomation(ctx, prAutomation.Status.GetID(), *attributes)
pra, err = in.ConsoleClient.UpdatePrAutomation(ctx, prAutomation.Status.GetID(), *attributes)
return pra, sha, err
}

// Read the PrAutomation from Console API if it already exists
if exists {
return in.ConsoleClient.GetPrAutomation(ctx, prAutomation.Status.GetID())
pra, err = in.ConsoleClient.GetPrAutomation(ctx, prAutomation.Status.GetID())
return pra, sha, err
}

logger.Info("Creating PR automation")
return in.ConsoleClient.CreatePrAutomation(ctx, *attributes)
pra, err = in.ConsoleClient.CreatePrAutomation(ctx, *attributes)
return pra, sha, err
}

func (in *PrAutomationReconciler) updateReadyCondition(prAutomation *v1alpha1.PrAutomation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var _ = Describe("PR Automation Controller", func() {
Name: scmConnectionName,
},
})
_, prAutomationHash, _ = generatedPrAutomation.Diff(utils.HashObject)

prAutomationObjectKey = types.NamespacedName{Name: prAutomationName}
)
Expand Down Expand Up @@ -104,10 +103,13 @@ var _ = Describe("PR Automation Controller", func() {

prAutomation := &v1alpha1.PrAutomation{}
err = k8sClient.Get(ctx, prAutomationObjectKey, prAutomation)
attrs, _ := controllerReconciler.Attributes(ctx, prAutomation)
sha, _ := utils.HashObject(attrs)

Expect(err).NotTo(HaveOccurred())
Expect(common.SanitizeStatusConditions(prAutomation.Status)).To(Equal(common.SanitizeStatusConditions(v1alpha1.Status{
ID: lo.ToPtr(prAutomationConsoleID),
SHA: lo.ToPtr(prAutomationHash),
SHA: lo.ToPtr(sha),
Conditions: []metav1.Condition{
{
Type: v1alpha1.ReadyConditionType.String(),
Expand Down Expand Up @@ -181,7 +183,6 @@ var _ = Describe("PR Automation Controller", func() {
Namespace: clusterNamespace,
},
})
_, prAutomationHash, _ = generatedPrAutomation.Diff(utils.HashObject)

prAutomationObjectKey = types.NamespacedName{Name: prAutomationName}
)
Expand Down Expand Up @@ -239,10 +240,14 @@ var _ = Describe("PR Automation Controller", func() {

prAutomation := &v1alpha1.PrAutomation{}
err = k8sClient.Get(ctx, prAutomationObjectKey, prAutomation)

attrs, _ := controllerReconciler.Attributes(ctx, prAutomation)
sha, _ := utils.HashObject(attrs)

Expect(err).NotTo(HaveOccurred())
Expect(common.SanitizeStatusConditions(prAutomation.Status)).To(Equal(common.SanitizeStatusConditions(v1alpha1.Status{
ID: lo.ToPtr(prAutomationConsoleID),
SHA: lo.ToPtr(prAutomationHash),
SHA: lo.ToPtr(sha),
Conditions: []metav1.Condition{
{
Type: v1alpha1.ReadyConditionType.String(),
Expand Down
2 changes: 1 addition & 1 deletion lib/console/schema/pr_automation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ defmodule Console.Schema.PrAutomation do
|> cast_assoc(:create_bindings)
|> put_new_change(:write_policy_id, &Ecto.UUID.generate/0)
|> put_new_change(:create_policy_id, &Ecto.UUID.generate/0)
|> validate_required([:name, :title, :message, :connection_id])
|> unique_constraint(:name)
|> foreign_key_constraint(:promotion_criteria,
name: :promotion_criteria,
Expand All @@ -138,6 +137,7 @@ defmodule Console.Schema.PrAutomation do
|> foreign_key_constraint(:connection_id)
|> foreign_key_constraint(:project_id)
|> foreign_key_constraint(:catalog_id)
|> validate_required([:name, :title, :message, :connection_id])
end

defp update_changeset(model, attrs) do
Expand Down

0 comments on commit 325776f

Please sign in to comment.