Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Review 36 imo #41

Merged
merged 20 commits into from
Oct 14, 2021
Merged

Review 36 imo #41

merged 20 commits into from
Oct 14, 2021

Conversation

takaishi
Copy link
Contributor

@takaishi takaishi commented Oct 6, 2021

IMO, nits, refactoring系などコアロジックとは関係ない場所で気になったものをまずはリストアップしています。

#36

@takaishi takaishi mentioned this pull request Oct 6, 2021
@@ -161,6 +163,7 @@ func (r *ReviewAppReconciler) reconcileCheckAppRepository(ctx context.Context, r
return ctrl.Result{}, nil
}

// comment: 何を行う関数なのか、関数名から読み取れませんでした
Copy link
Member

Choose a reason for hiding this comment

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

reconcile 対象の Reviewapp オブジェクトに紐づく At (ApplicationTemplate) , Mt (ManifestsTemplate) カスタムリソースのオブジェクトの更新を確認する関数です。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

省略せずにApplicationTemplateとManifestTemplateを使ってもいいかもしれないすね

Comment on lines +82 to +84
// comment: この関数内で呼んでいるreconcile~の関数名ですが、reconcileとCheck、reconcileとUpdateのように動詞が続いているのが気になりました。
// reconcileCheckAppRepository なら reconcileAppRepository , reconcileUpdateInfraReposiotry なら reconcileInfraReposiotry という名前がよいように思います。
// 追記: でもreconcileDeleteは割とよく使われてるしあんまり気にしなくてもいいかも
Copy link
Member

Choose a reason for hiding this comment

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

指摘されて初めて気づきました、たしかに reconcile (動詞) + check (動詞) は微妙だと感じます。
(cluster-api を参考実装としてるのですが、そこでも reconcileDelete 以外は reconcile 対象のリソース名がサフィックスに付いてそうです)

ちなみにここの reconcileXX 関数の呼び出しは MiroReviewApp コントローラの状態遷移 に対応しています。

Miro に書かれた状態遷移図に習って reconcileWatchingAppRepo だったり reconcileCheckedAppRepo という関数名にするのも案としてあるかと考えましたが、 「reconcile (調整する) + WatchingAppRepo (AppRepo を見ている)」は違うよなあとなっており、悩みどころです。
(Miro の状態遷移図の状態名が悪そうな気がしてきていますが自分の中で改善案が出てないです...)

Copy link
Member

@ShotaKitazawa ShotaKitazawa Oct 9, 2021

Choose a reason for hiding this comment

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

reconcileCheckAppRepository なら reconcileAppRepository , reconcileUpdateInfraReposiotry なら reconcileInfraReposiotry という名前がよいように思います。

上記に対する回答の前に、 MiroReviewApp コントローラの状態遷移 の図を文章で説明させてください。
今回自分が作った状態遷移図は 1 state = 1 resource のようになっておらず、以下の4つを state としそれらの間を遷移しています。

  • WatchingAppRepo : app-repo の PR の open/close の watch をする state
  • WatchingTemplate : k8s ApplicationTemplate, ManifestsTemplate オブジェクトの watch をする state
  • CheckedAppRepo : WatchingAppRepo または WatchingTemplate にて watch 対象に更新が会ったときにの遷移先 state。更新内容に沿った infra-repo へのマニフェストの push を実行する
  • UpdatedInfraRepo : CheckedAppRepo の push が成功した後の遷移先 state。k8s cluster の ArgoCD Application オブジェクトを watch し更新があれば app-repo の該当 PR に comment を POST したあと WatchingAppRepo に遷移する。

ここで、現状の reviewapp コントローラの「1 state = 1 resource のようになっていない」という設計より、たとえば「AppRepo を reconcile する」ことは WatchingAppRepo state でも実施するし UpdatedInfraRepo state でも実施しているので、上記で示してもらっているような命名 (「reconcileCheckAppRepository なら reconcileAppRepository」) はコンフリクトしてるなーという違和感を持っています。

// そもそも状態遷移図の作り方が悪いのかなと思い始めているので、 Miro の図に対する指摘があると嬉しいです🙇‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ここは今日の夜あたり考えてみます

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ちょっと遷移図含めて考えてみたんですが、こういう感じだと違和感ないかなと思いました。

  • 自身のStatus以外に存在する変更を加える対象はInfraRepoとPR上のコメントの2つなので、reconcileをつける関数は reconcileInfraReporeconcileCommentOfReviewAppURL だけ
  • reconcileCheckAppRepositoryreconcileCheckAtAndMt はどちらかというと InfraRepoとコメントを作成・更新すべきかどうかの判断を行っていると思うので、reconcileという関数名は使わない(とりあえずconfirmとしてみたけどなんとなくニュアンスが違う気もする)
  • 遷移的には、CheckedAppRepoNeedToUpdateInfraRepo のようにInfraRepoを更新する必要がある状態としたほうがわかりやすいかもしれない
	if reflect.DeepEqual(ra.Status, dreamkastv1beta1.ReviewAppStatus{}) ||
		ra.Status.Sync.Status == dreamkastv1beta1.SyncStatusCodeWatchingAppRepo {
		// AppRepoが更新されているかどうかを確認する
		result, err = r.confirmAppRepoIsUpdated(ctx, ra)
		if err != nil {
			errs = append(errs, err)
		}
	}

	// ApplicationTemplateとManifestTemplateが更新されているかどうかを確認する
	if ra.Status.Sync.Status == dreamkastv1beta1.SyncStatusCodeWatchingTemplates {
		result, err = r.confirmTemplatesAreUpdated(ctx, ra)
		if err != nil {
			errs = append(errs, err)
		}
	}

	// Infra Repoをspec.applicationとspec.manifestの内容に更新する
	if ra.Status.Sync.Status == dreamkastv1beta1.SyncStatusCodeNeedToUpdateInfraRepo {
		result, err = r.reconcileInfraReposiotry(ctx, ra)
		if err != nil {
			errs = append(errs, err)
		}
	}

    // ReviewAppのURLをPRのコメントとして投稿・更新する
	if ra.Status.Sync.Status == dreamkastv1beta1.SyncStatusCodeUpdatedInfraRepo {
		result, err = r.reconcileCommentOfReviewAppURL(ctx, ra)
		if err != nil {
			errs = append(errs, err)
		}
	}

Copy link
Member

@ShotaKitazawa ShotaKitazawa Oct 13, 2021

Choose a reason for hiding this comment

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

元々よりとても良くなってると感じますが、「confirm」も「infra-repo への commit」も「app-repo の PR へのコメントの POST」も全部引っくるめて reconcile だと思っていて、 confirm と reconcile が同じレベルにあるのは個人的にまだ違和感です...

上記処理自体が reconcile メソッド内の処理なので状態遷移する関数名には reconcile を使わない方針で、以下でどうでしょうか!

  • reconcileInfraReposiotrycommitToInfraRepo
  • reconcileCommentOfReviewAppURLcommentToAppRepoPullRequest
    • comment 内容は yaml で外から与える値なので CommentOfReviewAppURL ではないかなと思いました

追記

いや、reconcileDelete も「infra-repo に対する commit」を実施するので commitToInfraRepo はふさわしくなさそうでした。 deployReviewAppManifestsToInfraRepo でどうでしょう?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🙆‍♂️

@@ -143,7 +143,7 @@ func (r *ReviewAppReconciler) reconcileCheckAppRepository(ctx context.Context, r
}

// get ArgoCD Application name
argocdAppNamespacedName, err := kubernetes.PickNamespacedNameFromObjectStr(ctx, ra.Spec.Application)
argocdAppNamespacedName, err := kubernetes.GetNamespacedNameFromObjectStr(ctx, ra.Spec.Application)
Copy link
Member

Choose a reason for hiding this comment

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

  • GetXXX は Kubernetes Cluster からリソースの GET を実行
  • PickXXX はメソッドの引数に与えた string 型なオブジェクトから特定フィールドの値を抜き出す

という書き分けです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

なるほど!

@ShotaKitazawa
Copy link
Member

  • cd9763e : gateway 以下は外部サービスに対する問い合わせのインタフェースを提供する層であり、外部サービスへの接続方法によってパッケージを切ったほうが明確になるかなという思いで、 gateway 以下に gitcommandgitapi でパッケージを分けていました
    • 後ほどの c4c3341 にもある通り gitapigithubapi に rename したほうが良いとは思っています。

@ShotaKitazawa
Copy link
Member

ShotaKitazawa commented Oct 11, 2021

基本的にこの変更後のほうが良いと思います! 1 点だけ気になるところを挙げるならば、 wrappergateways というパッケージ名、構造体名に rename したい気持ちがあります。 (構造体名が元々 driver だったのは自分のミスです:pray:)

というのも、gateways は "Clean Architecture の例の図" に出てくる名前として 「interface を通して Usecase (reviewapp-operator の場合 service パッケージ) からのみ呼び出される」という役割が名前からある程度予測できると思っていますが、対して wrapper というパッケージ名は「どのパッケージからも呼び出されることを許容している」ような名前であると個人的に思っています。

現状 gateways パッケージ以下のメソッドは services 以下からしか呼び出されておらず、今後機能拡張をした際もその制限を破りたくないと思っているため、上述したとおり gateways という名前を採用したいです。

ちなみに「どのパッケージからも呼び出されることを許容している」パッケージとして reviewapp-operator では utils というディレクトリを切っているので、もし wrapper という命名にするならば utils 以下に入れたいです。

@ShotaKitazawa
Copy link
Member

ShotaKitazawa commented Oct 11, 2021

#41 (comment) (9c8f8e7) の追記です。

現状 gateways パッケージ以下のメソッドは services 以下からしか呼び出されておらず、今後機能拡張をした際もその制限を破りたくないと思っている

結局自分が達成したいことは ↑ なので、それさえ認識が合えば wrapper という名前でもいいかと思って来ました! (ただ現状の自分の知識に基づくとベストは gateways かなーと思ってます、こっちのほうが良いみたいな意見があればいただけると嬉しいです:pray:)

それと、modelsgateways/iface パッケージを廃止したのに伴い services → gateways に単純に依存する (interface は挟んでいるが DIP ではない) ようになったため、Clean Architecture というよりは Layered Architecture ですね、、(なので gateways という名前は人によってはピンとこないかも知れない?)

@ShotaKitazawa ShotaKitazawa mentioned this pull request Oct 12, 2021
@takaishi
Copy link
Contributor Author

パッケージ名は自分ならこうするかな、くらいなので強い意見はないです。なのでgatewayでも特に問題はないです。どちらかというとinterface1個しかないのにパッケージを分けるような細かいパッケージ分割がGoっぽくないかなと感じています。

パッケージ分割まわりはIMOなので、反映するかはおまかせします〜。

@ShotaKitazawa ShotaKitazawa mentioned this pull request Oct 13, 2021
@takaishi takaishi merged commit 22d9703 into main Oct 14, 2021
@takaishi takaishi deleted the review-36-imo branch October 14, 2021 10:11
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.

2 participants