【Backstage】Github CopilotでBookInfoのcatalog-info.yamlを生成してみる

2024.05.12
2024.05.12
開発環境
BackstageGithub CopilotIstio

はじめに

Github Copilotを使って、Backstageのcatalog-info.yamlを生成してみたいと思います。

Backstageのcatalog-info.yaml

BackstageのSoftware Catalogでは、YAMLファイルを使って、カタログ情報を管理します。

Catalogでは、下記のようなシステムモデルが使われています。

しかし、ComponentやAPIが多くなってくると、これらのYAMLファイルを手で書くのは大変です。そこで、Github Copilotを使って、自動生成してできないか試してみたいと思います。

IstioのBookInfoアプリ

BookInfoアプリとは、Istioのサンプルアプリケーションであり、下記のように4つのマイクロサービスから構成されています。今回は、4つそれぞれに対してcatalog-info.yamlを生成してみます。

公式ドキュメントより

istio/samples/bookinfo at master · istio/istio

istio/samples/bookinfo at master · istio/istio

Connect, secure, control, and observe services. Contribute to istio/istio development by creating an account on GitHub.

使うプロンプト

今回は、下記のComponentとAPIの情報を生成するプロンプトを使います。なるべく、埋められる部分は埋めて、自動で生成して欲しい部分を{}で示しています。

1@workspace
2本プロジェクトのソースコードをもとに、Backstageで管理する下記のcatalog-info.yamlの`{}`を埋めてください。
3
4```yaml
5apiVersion: backstage.io/v1alpha1
6kind: Component
7metadata:
8  name: {Component entity name}
9  description: {Component entity description}
10spec:
11  type: {The type of Component entity}
12  lifecycle: production
13  owner: bookinfo-team
14  system: bookinfo
15  providesApis:
16    - {The API entities that are provided by the Component entity}
17  consumesApis:
18    - {The API entities that are consumed by the Component entity}
19---
20apiVersion: backstage.io/v1alpha1
21kind: API
22metadata:
23  name: {The name of the API entity which is in providesApis field in the Component entity}
24  description: {API entity description}
25spec:
26  type: openapi
27  lifecycle: production
28  owner: bookinfo-team
29  system: bookinfo
30  definition: |
31    {The definition of the API based on OpenAPI format}
32```

catalog-info.yamlを生成してみる

上記のプロンプトを使って、生成されたcatalog-info.yamlは下記のようになりました。Copilotを実行するときは、情報のソースとなるコードが含まれるファイルを開いておくようにしました。

productpage/catalog-info.yaml
1apiVersion: backstage.io/v1alpha1
2kind: Component
3metadata:
4  name: productpage
5  description: The main web page interface for the BookInfo app
6spec:
7  type: service
8  lifecycle: production
9  owner: bookinfo-team
10  system: bookinfo
11  providesApis:
12    - productpage-api
13  consumesApis:
14    - details-api
15    - reviews-api
16---
17apiVersion: backstage.io/v1alpha1
18kind: API
19metadata:
20  name: productpage-api
21  description: API for accessing product page data
22spec:
23  type: openapi
24  lifecycle: production
25  owner: bookinfo-team
26  system: bookinfo
27  definition: |
28    openapi: 3.0.0
29    info:
30      title: ProductPage API
31      version: 1.0.0
32    paths:
33      /details:
34        get:
35          summary: Get details for a product
36          responses:
37            '200':
38              description: A product detail object
39      /reviews:
40        get:
41          summary: Get reviews for a product
42          responses:
43            '200':
44              description: A list of review objects
details/catalog-info.yaml
1apiVersion: backstage.io/v1alpha1
2kind: Component
3metadata:
4  name: details-service
5  description: A microservice that provides details of books
6spec:
7  type: service
8  lifecycle: production
9  owner: bookinfo-team
10  system: bookinfo
11  providesApis:
12    - details-api
13  consumesApis: []
14---
15apiVersion: backstage.io/v1alpha1
16kind: API
17metadata:
18  name: details-api
19  description: API for fetching book details
20spec:
21  type: openapi
22  lifecycle: production
23  owner: bookinfo-team
24  system: bookinfo
25  definition: |
26    openapi: 3.0.0
27    info:
28      version: 1.0.0
29      title: Details API
30      description: API for fetching book details
31    paths:
32      /details/{id}:
33        get:
34          summary: Get details of a book
35          parameters:
36            - name: id
37              in: path
38              required: true
39              schema:
40                type: integer
41          responses:
42            '200':
43              description: Details of the book
44              content:
45                application/json:
46                  schema:
47                    type: object
48                    properties:
49                      id:
50                        type: integer
51                      author:
52                        type: string
53                      year:
54                        type: integer
55                      type:
56                        type: string
57                      pages:
58                        type: integer
59                      publisher:
60                        type: string
61                      language:
62                        type: string
63                      ISBN-10:
64                        type: string
65                      ISBN-13:
66                        type: string
reviews/catalog-info.yaml
1apiVersion: backstage.io/v1alpha1
2kind: Component
3metadata:
4  name: reviews-application
5  description: A Java application that provides review services via REST API.
6spec:
7  type: service
8  lifecycle: production
9  owner: bookinfo-team
10  system: bookinfo
11  providesApis:
12    - reviews-api
13  consumesApis: []
14---
15apiVersion: backstage.io/v1alpha1
16kind: API
17metadata:
18  name: reviews-api
19  description: API for accessing review services.
20spec:
21  type: openapi
22  lifecycle: production
23  owner: bookinfo-team
24  system: bookinfo
25  definition: |
26    openapi: 3.0.0
27    info:
28      title: Reviews API
29      version: 1.0.0
30    paths:
31      /reviews/{productId}:
32        get:
33          summary: Get reviews by product ID
34          parameters:
35            - name: productId
36              in: path
37              required: true
38              schema:
39                type: integer
40          responses:
41            '200':
42              description: A JSON object of reviews
43              content:
44                application/json:
45                  schema:
46                    type: object
47                    properties:
48                      productId:
49                        type: integer
50                      reviews:
51                        type: array
52                        items:
53                          type: object
54                          properties:
55                            reviewer:
56                              type: string
57                            stars:
58                              type: integer
ratings/catalog-info.yaml
1apiVersion: backstage.io/v1alpha1
2kind: Component
3metadata:
4  name: ratings-service
5  description: Service for managing product ratings
6spec:
7  type: service
8  lifecycle: production
9  owner: bookinfo-team
10  system: bookinfo
11  providesApis:
12    - ratings-api
13  consumesApis: []
14---
15apiVersion: backstage.io/v1alpha1
16kind: API
17metadata:
18  name: ratings-api
19  description: API for managing product ratings
20spec:
21  type: openapi
22  lifecycle: production
23  owner: bookinfo-team
24  system: bookinfo
25  definition: |
26    openapi: 3.0.0
27    info:
28      version: 1.0.0
29      title: Ratings API
30      description: API for managing product ratings
31    paths:
32      /ratings/{productId}:
33        get:
34          summary: Get ratings for a product
35          parameters:
36            - name: productId
37              in: path
38              required: true
39              schema:
40                type: integer
41        post:
42          summary: Post ratings for a product
43          parameters:
44            - name: productId
45              in: path
46              required: true
47              schema:
48                type: integer
49          requestBody:
50            required: true
51            content:
52              application/json:
53                schema:
54                  type: object
55                  properties:
56                    ratings:
57                      type: object
58                      additionalProperties:
59                        type: integer

ある程度いい感じに生成されている部分もありますが、まだまだ間違っている部分もありました。例えば、APIのdefinitionの中身が間違っていたり、reviews ComponentのconsumesApisが空になっていたりしました。

Backstageで確認

生成したcatalog-info.yamlをBackstageに登録して、グラフを確認してみます。

もとになるBookInfoのSystemと合わせて、生成したそれぞれのcatalog-info.yamlを登録します。今回は、Backstageアプリのexamples/bookinfo.yamlにまとめて、app-config.yamlから読み込むように設定しました。

examples/bookinfo.yaml
1apiVersion: backstage.io/v1alpha1
2kind: System
3metadata:
4  name: bookinfo
5  description: BookInfo application
6spec:
7  owner: bookinfo-team
8---
9
10# Copilotで生成したそれぞれのcatalog-info.yaml
11
app-config.yaml
1    # Istio book info app
2    - type: file
3      target: ../../examples/bookinfo.yaml

productpageやBookInfoのSystemのグラフは下記のようになっています。

まとめ

Github Copilotを使って、IstioのBookInfoアプリのソースコードからcatalog-info.yamlを生成してみました。

ベースとなる部分を記載しておくことで、ある程度の情報は生成されているのが分かりましたが、まだまだ間違っている部分も多く、同じプロンプトで生成しても、結果が異なることもありました。もう少し、部分的に生成するようにしたり(APIのdefinitionのみ生成させたり、ComponentのconsumesApisのみ生成させたり)、わかる部分はなるべく埋めてから生成させることで、さらに精度を高めることができるかもしれません。

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee