Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Conversation

@chatton
Copy link
Contributor

@chatton chatton commented Jun 18, 2020

All Submissions:

  • Have you signed our CLA?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

This PR replaces the StatefulSet builder with a functional approach.

This allows us to determine if a change is actually taking place without needing to perform an arbitrary time.Sleep (waiting for client cache to clear). We can now use a combination of comparing the before and after of the StatefulSet with out modification function to see if the StatefulSet is ready or not.

(there will be a separate PR to remove the builder and refactor tests if we go ahead with this design)

Side Notes:

  • Before, we were creating the builder, then adding volumes, then ultimately calling build. Now we pass the volumes to the containers as required and keep everything in one place.
  • the statefulset. HaveEqualSpec function is no longer required and was removed

// is currently in the ready state
func (r *ReplicaSetReconciler) isStatefulSetReady(mdb mdbv1.MongoDB, expectedUpdateStrategy appsv1.StatefulSetUpdateStrategyType) (bool, error) {
// TODO: This sleep will be addressed as part of https://jira.mongodb.org/browse/CLOUDP-62444
time.Sleep(time.Second * 5)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// buildStatefulSet takes a MongoDB resource and converts it into
// the corresponding stateful set
func buildStatefulSet(mdb mdbv1.MongoDB) (appsv1.StatefulSet, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

func buildStatefulSet(mdb mdbv1.MongoDB) (appsv1.StatefulSet, error) {
sts := appsv1.StatefulSet{}
buildStatefulSetModificationFunction(mdb)(&sts)
return sts, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can never return an error, then we can just get rid of it

if ready, err := r.isStatefulSetReady(mdb, getUpdateStrategyType(mdb)); err != nil {
currentSts := appsv1.StatefulSet{}
if err := r.client.Get(context.TODO(), mdb.NamespacedName(), &currentSts); err != nil {
r.log.Warnf("Error getting StatefulSet: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] do we want to use r.log.WarnF here? (Almost) everywhere else we use r.log.InfoF(except one case in which we use ErrorF) Maybe we should use the same everywhere for consistency?

(btw I would be more in favour of using WarnF instead of InfoF

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep we need to make sure we're consistent with our logging levels. 👍

func (r *ReplicaSetReconciler) createOrUpdateStatefulSet(mdb mdbv1.MongoDB) error {
sts, err := buildStatefulSet(mdb)
set := appsv1.StatefulSet{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: mdb.Name, Namespace: mdb.Namespace}, &set)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can use the new NamespacedName() function you introduced

@@ -0,0 +1,33 @@
package persistantvolumeclaim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package is called persistAntvolumeclaim instead of persistEntvolumeclaim, which is the term used in k8s. Is this a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost definitely, nice catch!

func WithVolumeClaim(name string, f func(*corev1.PersistentVolumeClaim)) Modification {
return func(set *appsv1.StatefulSet) {
idx := findVolumeClaimIndexByName(name, set.Spec.VolumeClaimTemplates)
if idx == -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have a not_found constant as in podspec_template here.

Copy link
Contributor

@bznein bznein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few minor comments

Copy link
Contributor

@antonlisovenko antonlisovenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love this PR, functional approach suits very well the need to sequentially "enrich" K8s objects - I think this looks clearer and easier to support than the builder pattern
🥇

Copy link
Contributor

@bznein bznein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work!🏅

@chatton chatton merged commit ea21298 into master Jun 22, 2020
@chatton chatton deleted the CLOUDP-62444_turn_sts_builder_to_function_style branch June 22, 2020 10:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants