UNCLASSIFIED

Commit 87d31e2b authored by Jason van Brackel's avatar Jason van Brackel
Browse files

feat: add functionality to update the state of a group object upon error

parent 19224546
...@@ -49,6 +49,7 @@ const ( ...@@ -49,6 +49,7 @@ const (
errorGettingGroupVariablesFromGitlab = "Error getting group variables from Gitlab" errorGettingGroupVariablesFromGitlab = "Error getting group variables from Gitlab"
errorCreatingGroupVariableInGitlab = "Error creating group variable in Gitlab" errorCreatingGroupVariableInGitlab = "Error creating group variable in Gitlab"
errorUpdatingGroupVariableInGitLab = "Error updating group variable in Gitlab" errorUpdatingGroupVariableInGitLab = "Error updating group variable in Gitlab"
errorWhileUpdatingGroupState = "Error while updating group state in status"
) )
// Group Status // Group Status
...@@ -113,6 +114,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl ...@@ -113,6 +114,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
} }
if r.gitlabClient, err = r.gitlabClientConfiguration.SetupClient(r.Client, group.Spec.GitlabCredentialsName); err != nil { if r.gitlabClient, err = r.gitlabClientConfiguration.SetupClient(r.Client, group.Spec.GitlabCredentialsName); err != nil {
r.Log.Error(err, errorUnableToSetupGitlabClient) r.Log.Error(err, errorUnableToSetupGitlabClient)
_ = r.updateState(ctx, group, errorUnableToSetupGitlabClient)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
} }
...@@ -121,34 +123,40 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl ...@@ -121,34 +123,40 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
var gitlabGroup *gitlab.Group var gitlabGroup *gitlab.Group
found, gitlabGroup, err := r.groupExists(group) found, gitlabGroup, err := r.groupExists(group)
if err != nil { if err != nil {
r.Log.Error(err, errorWhileSearchingGroups, "status") r.Log.Error(err, errorWhileSearchingGroups)
_ = r.updateState(ctx, group, errorWhileSearchingGroups)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
if !found { if !found {
if gitlabGroup, _, err = r.createGroup(group); err != nil { if gitlabGroup, _, err = r.createGroup(group); err != nil {
r.Log.Error(err, errorWhileCreatingGitlabGroup, "group", group) r.Log.Error(err, errorWhileCreatingGitlabGroup, "group", group)
_ = r.updateState(ctx, group, errorWhileCreatingGitlabGroup)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
} else { } else {
if gitlabGroup, _, err = r.updateGroup(group); err != nil { if gitlabGroup, _, err = r.updateGroup(group); err != nil {
r.Log.Error(err, errorWhileUpdatingGitlabGroup, "group", group) r.Log.Error(err, errorWhileUpdatingGitlabGroup, "group", group)
_ = r.updateState(ctx, group, errorWhileUpdatingGitlabGroup)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
} }
if err = r.updateCiVariables(gitlabGroup.ID, group); err != nil { if err = r.updateCiVariables(gitlabGroup.ID, group); err != nil {
r.Log.Error(err, errorWhileUpdatingGitlabGroupCiVariables, "group", group) r.Log.Error(err, errorWhileUpdatingGitlabGroupCiVariables, "group", group)
_ = r.updateState(ctx, group, errorWhileUpdatingGitlabGroupCiVariables)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
if err = r.updateStatus(ctx, gitlabGroup.ID, group); err != nil { if err = r.updateStatus(ctx, gitlabGroup.ID, group); err != nil {
r.Log.Error(err, errorUnableToUpdateStatus, "group", group) r.Log.Error(err, errorUnableToUpdateStatus, "group", group)
_ = r.updateState(ctx, group, errorUnableToUpdateStatus)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
if err = r.processProjects(ctx, group, group.Spec.ProjectSpecs); err != nil { if err = r.processProjects(ctx, group, group.Spec.ProjectSpecs); err != nil {
r.Log.Error(err, errorUnableToProcessProjects, "group", group) r.Log.Error(err, errorUnableToProcessProjects, "group", group)
_ = r.updateState(ctx, group, errorUnableToProcessProjects)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
...@@ -291,12 +299,17 @@ func (r *GroupReconciler) updateProject(ctx context.Context, groupID int, spec g ...@@ -291,12 +299,17 @@ func (r *GroupReconciler) updateProject(ctx context.Context, groupID int, spec g
func (r *GroupReconciler) updateStatus(ctx context.Context, id int, group *gitlabv1alpha1.Group) error { func (r *GroupReconciler) updateStatus(ctx context.Context, id int, group *gitlabv1alpha1.Group) error {
if id != 0 { if id != 0 {
group.Status.State = "OK"
group.Status.LastUpdatedTime = metav1.Time{ group.Status.LastUpdatedTime = metav1.Time{
Time: time.Now(), Time: time.Now(),
} }
} }
return r.Status().Update(ctx, group) if err := r.Status().Update(ctx, group); err != nil {
r.Log.Error(err, errorUnableToUpdateStatus)
return err
}
return nil
} }
// SetupWithManager sets up the controller with the Manager. // SetupWithManager sets up the controller with the Manager.
...@@ -308,7 +321,7 @@ func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error { ...@@ -308,7 +321,7 @@ func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
} }
func (r *GroupReconciler) updateCiVariables(ID int, group *gitlabv1alpha1.Group) error { func (r *GroupReconciler) updateCiVariables(ID int, group *gitlabv1alpha1.Group) error {
var variablesToUpdate []string = []string{KustomizeStagingPathVariableName, KustomizeProductionPathVariableName, ManifestRepoURLVariableName} var variablesToUpdate = []string{KustomizeStagingPathVariableName, KustomizeProductionPathVariableName, ManifestRepoURLVariableName}
var err error var err error
var groupVariable *gitlab.GroupVariable var groupVariable *gitlab.GroupVariable
...@@ -379,3 +392,12 @@ func (r *GroupReconciler) GetGroupVariableValueByName(variableToCreate string, g ...@@ -379,3 +392,12 @@ func (r *GroupReconciler) GetGroupVariableValueByName(variableToCreate string, g
} }
return "" return ""
} }
func (r *GroupReconciler) updateState(ctx context.Context, group *gitlabv1alpha1.Group, state string) error {
group.Status.State = state
if err := r.Client.Status().Update(ctx, group) ; err != nil {
r.Log.Error(err, errorWhileUpdatingGroupState, "group", group)
return err
}
return nil
}
...@@ -192,6 +192,9 @@ var _ = Describe("Reconcile", func() { ...@@ -192,6 +192,9 @@ var _ = Describe("Reconcile", func() {
})) }))
Expect(err).ToNot(BeNil()) Expect(err).ToNot(BeNil())
}) })
It("should update the state of the object", func() {
Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorUnableToSetupGitlabClient))
})
}) })
Context("groupExists fails", func() { Context("groupExists fails", func() {
sut, logger, _, _, _ := getGroupControllerWithMocksInGreenTestState() sut, logger, _, _, _ := getGroupControllerWithMocksInGreenTestState()
...@@ -217,6 +220,9 @@ var _ = Describe("Reconcile", func() { ...@@ -217,6 +220,9 @@ var _ = Describe("Reconcile", func() {
It("should return an error", func() { It("should return an error", func() {
Expect(err).NotTo(BeNil()) Expect(err).NotTo(BeNil())
}) })
It("should update the state of the object", func() {
Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorWhileSearchingGroups))
})
}) })
Context("createGroup fails", func() { Context("createGroup fails", func() {
sut, logger, _, _, _ := getGroupControllerWithMocksInGreenTestState() sut, logger, _, _, _ := getGroupControllerWithMocksInGreenTestState()
...@@ -246,6 +252,9 @@ var _ = Describe("Reconcile", func() { ...@@ -246,6 +252,9 @@ var _ = Describe("Reconcile", func() {
It("should return an error", func() { It("should return an error", func() {
Expect(err).NotTo(BeNil()) Expect(err).NotTo(BeNil())
}) })
It("should update the state of the object", func() {
Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorWhileCreatingGitlabGroup))
})
}) })
Context("updateGroup fails", func() { Context("updateGroup fails", func() {
sut, logger, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState() sut, logger, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState()
...@@ -267,6 +276,9 @@ var _ = Describe("Reconcile", func() { ...@@ -267,6 +276,9 @@ var _ = Describe("Reconcile", func() {
It("should return an error", func() { It("should return an error", func() {
Expect(err).NotTo(BeNil()) Expect(err).NotTo(BeNil())
}) })
It("should update the state of the object", func() {
Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorWhileUpdatingGitlabGroup))
})
}) })
Context("getGroupVariable fails", func() { Context("getGroupVariable fails", func() {
sut, log, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState() sut, log, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState()
...@@ -284,6 +296,9 @@ var _ = Describe("Reconcile", func() { ...@@ -284,6 +296,9 @@ var _ = Describe("Reconcile", func() {
It("should return an error", func() { It("should return an error", func() {
Expect(err).ToNot(BeNil()) Expect(err).ToNot(BeNil())
}) })
It("should update the state of the object", func() {
Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorWhileUpdatingGitlabGroupCiVariables))
})
}) })
Context("createCiVariable fails", func() { Context("createCiVariable fails", func() {
sut, log, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState() sut, log, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState()
...@@ -301,6 +316,9 @@ var _ = Describe("Reconcile", func() { ...@@ -301,6 +316,9 @@ var _ = Describe("Reconcile", func() {
It("should return an error", func() { It("should return an error", func() {
Expect(err).ToNot(BeNil()) Expect(err).ToNot(BeNil())
}) })
It("should update the state of the object", func() {
Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorWhileUpdatingGitlabGroupCiVariables))
})
}) })
Context("updateGroupVariable fails", func() { Context("updateGroupVariable fails", func() {
sut, log, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState() sut, log, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState()
...@@ -318,25 +336,8 @@ var _ = Describe("Reconcile", func() { ...@@ -318,25 +336,8 @@ var _ = Describe("Reconcile", func() {
It("should return an error", func() { It("should return an error", func() {
Expect(err).ToNot(BeNil()) Expect(err).ToNot(BeNil())
}) })
}) It("should update the state of the object", func() {
Context("updateStatus fails", func() { Expect(sut.Status().(*MockStatusWriter).updatedObject.(*gitlabv1alpha1.Group).Status.State).To(Equal(errorWhileUpdatingGitlabGroupCiVariables))
sut, logger, _, kubernetesClient, _ := getGroupControllerWithMocksInGreenTestState()
kubernetesClient.statusWriter = &MockStatusWriter{
updateFunction: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return &MockError{"updateStatus fails"}
},
}
result, err := sut.Reconcile(context.TODO(), getGreenGroupControllerRequest())
It("should requeue the object", func() {
Expect(result).To(Equal(ctrl.Result{Requeue: true}))
})
It("should log the error", func() {
Expect(logger.logLevelCalled).To(Equal("Error"))
Expect(logger.loggedMessage).To(Equal(errorUnableToUpdateStatus))
})
It("should return an error", func() {
Expect(err).NotTo(BeNil())
}) })
}) })
Context("processProjects fails", func() { Context("processProjects fails", func() {
...@@ -362,6 +363,42 @@ var _ = Describe("Reconcile", func() { ...@@ -362,6 +363,42 @@ var _ = Describe("Reconcile", func() {
}) })
}) })
var _ = Describe("updateState fails", func() {
sut, logger, _, _, _ := getGroupControllerWithMocksInGreenTestState()
sut.Status().(*MockStatusWriter).updateFunction = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return &MockError{message: "mock updateState error"}
}
result := sut.updateState(context.TODO(), getGreenGroup(), "")
It("should log the error", func() {
Expect(logger.logLevelCalled).To(Equal("Error"))
Expect(logger.loggedMessage).To(Equal(errorWhileUpdatingGroupState))
})
It("should return an error", func() {
Expect(result).ToNot(BeNil())
})
})
var _ = Describe("updateStatus fails", func() {
sut, logger, _, kubernetesClient, _ := getGroupControllerWithMocksInGreenTestState()
kubernetesClient.statusWriter = &MockStatusWriter{
updateFunction: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return &MockError{"updateStatus fails"}
},
}
ctx := context.TODO()
group := getGreenGroup()
err := sut.updateStatus(ctx, 1, group)
It("should log the error", func() {
Expect(logger.logLevelCalled).To(Equal("Error"))
Expect(logger.loggedMessage).To(Equal(errorUnableToUpdateStatus))
})
It("should return an error", func() {
Expect(err).NotTo(BeNil())
})
})
var _ = Describe("groupExists", func() { var _ = Describe("groupExists", func() {
When("the gitlab client fails", func() { When("the gitlab client fails", func() {
sut, _, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState() sut, _, _, _, gitlabClient := getGroupControllerWithMocksInGreenTestState()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment