Skip to content

Commit a821b7d

Browse files
committed
Refactor devbuild service initialization and CloudEvents
1 parent 2b5c122 commit a821b7d

File tree

6 files changed

+61
-56
lines changed

6 files changed

+61
-56
lines changed
+4-23
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,27 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76

8-
"github.com/rs/zerolog/log"
97
"gopkg.in/yaml.v3"
108

11-
"github.com/PingCAP-QE/ee-apps/tibuild/internal/database/ent"
129
"github.com/PingCAP-QE/ee-apps/tibuild/pkg/config"
1310

1411
_ "github.com/go-sql-driver/mysql"
1512
)
1613

1714
// Load and parse configuration
18-
func loadConfig(configFile string) (config.Service, error) {
15+
func loadConfig(configFile string) (*config.Service, error) {
1916
var config config.Service
2017
{
2118
configData, err := os.ReadFile(configFile)
2219
if err != nil {
23-
return config, fmt.Errorf("error reading config file: %v", err)
20+
return nil, fmt.Errorf("error reading config file: %v", err)
2421
}
2522
if err := yaml.Unmarshal(configData, &config); err != nil {
26-
return config, fmt.Errorf("error parsing config file: %v", err)
23+
return nil, fmt.Errorf("error parsing config file: %v", err)
2724
}
2825
}
29-
return config, nil
30-
}
31-
32-
func newStoreClient(cfg config.Store) (*ent.Client, error) {
33-
db, err := ent.Open(cfg.Driver, cfg.DSN)
34-
if err != nil {
35-
log.Err(err).Msgf("failed opening connection to %s", cfg.Driver)
36-
return nil, err
37-
}
38-
39-
// Run the auto migration tool.
40-
if err := db.Schema.Create(context.Background()); err != nil {
41-
log.Err(err).Msg("failed creating schema resources")
42-
return nil, err
43-
}
44-
45-
return db, nil
26+
return &config, nil
4627
}

experiments/tibuild-v2/cmd/tibuild/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
"net"
@@ -67,12 +68,11 @@ func main() {
6768
artifactSvc = impl.NewArtifact(&logger)
6869
}
6970
{
70-
dbClient, err := newStoreClient(cfg.Store)
71-
if err != nil {
72-
log.Fatalf(ctx, err, "failed to create store client")
73-
}
7471
logger := zerolog.New(os.Stderr).With().Timestamp().Str("service", devbuild.ServiceName).Logger()
75-
devbuildSvc = impl.NewDevbuild(&logger, dbClient)
72+
devbuildSvc = impl.NewDevbuild(&logger, cfg)
73+
if devbuildSvc == nil {
74+
log.Fatalf(ctx, errors.New("failed to initialize devbuild service"), "please check the configuration")
75+
}
7676
}
7777
}
7878

experiments/tibuild-v2/internal/service/impl/devbuild.go

+38-14
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,39 @@ import (
1414
"github.com/PingCAP-QE/ee-apps/tibuild/internal/database/ent"
1515
entdevbuild "github.com/PingCAP-QE/ee-apps/tibuild/internal/database/ent/devbuild"
1616
"github.com/PingCAP-QE/ee-apps/tibuild/internal/service/gen/devbuild"
17+
"github.com/PingCAP-QE/ee-apps/tibuild/pkg/config"
1718
)
1819

1920
// devbuild service example implementation.
2021
// The example methods log the requests and return zero values.
2122
type devbuildsrvc struct {
22-
logger *zerolog.Logger
23-
dbClient *ent.Client
24-
productRepoMap map[string]string
25-
ghClient *github.Client
26-
tektonClient tektonClient
27-
jenkinsClient *gojenkins.Jenkins
28-
tknListenerURL string
23+
logger *zerolog.Logger
24+
dbClient *ent.Client
25+
productRepoMap map[string]string
26+
ghClient *github.Client
27+
tektonCloudEventClient cloudevents.Client
28+
jenkinsClient *gojenkins.Jenkins
2929
}
3030

31-
type tektonClient struct {
32-
client cloudevents.Client
33-
listenerURL string
34-
}
31+
func NewDevbuild(logger *zerolog.Logger, cfg *config.Service) devbuild.Service {
32+
dbClient, err := newStoreClient(cfg.Store)
33+
if err != nil {
34+
logger.Err(err).Msg("failed to create store client")
35+
return nil
36+
}
37+
38+
client, err := cloudevents.NewClientHTTP(cloudevents.WithTarget(cfg.Tekton.CloudeventEndpoint))
39+
if err != nil {
40+
logger.Err(err).Msg("failed to create cloud event client")
41+
return nil
42+
}
3543

36-
func NewDevbuild(logger *zerolog.Logger, client *ent.Client) devbuild.Service {
3744
return &devbuildsrvc{
38-
logger: logger,
39-
dbClient: client,
45+
logger: logger,
46+
dbClient: dbClient,
47+
productRepoMap: map[string]string{"pd": "tikv/pd"},
48+
ghClient: github.NewClientWithEnvProxy().WithAuthToken(cfg.Github.Token),
49+
tektonCloudEventClient: client,
4050
}
4151
}
4252

@@ -177,3 +187,17 @@ func (s *devbuildsrvc) Rerun(ctx context.Context, p *devbuild.RerunPayload) (res
177187

178188
return transformDevBuild(newBuild), nil
179189
}
190+
191+
func newStoreClient(cfg config.Store) (*ent.Client, error) {
192+
db, err := ent.Open(cfg.Driver, cfg.DSN)
193+
if err != nil {
194+
return nil, err
195+
}
196+
197+
// Run the auto migration tool.
198+
if err := db.Schema.Create(context.Background()); err != nil {
199+
return nil, err
200+
}
201+
202+
return db, nil
203+
}

experiments/tibuild-v2/internal/service/impl/devbuild_create.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package impl
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"log/slog"
6+
"net/http"
87
"strconv"
98
"strings"
109
"time"
@@ -24,7 +23,7 @@ func (s *devbuildsrvc) newBuildEntity(ctx context.Context, p *devbuild.CreatePay
2423
// 1. get the github full repo by product.
2524
githubFullRepo := s.productRepoMap[p.Request.Product]
2625
if githubFullRepo == "" {
27-
return nil, errors.New("github full repo not found")
26+
return nil, &devbuild.HTTPError{Code: http.StatusBadRequest, Message: "github full repo not found"}
2827
}
2928

3029
// 2. get the commit sha
@@ -49,20 +48,17 @@ func (s *devbuildsrvc) newBuildEntity(ctx context.Context, p *devbuild.CreatePay
4948
}
5049

5150
func (s *devbuildsrvc) triggerTknBuild(ctx context.Context, record *ent.DevBuild) (*ent.DevBuild, error) {
52-
// TODO: trigger the actual build process according to the record.
53-
//
54-
//
5551
// 1. Compose a cloud event from the record.
5652
event, err := newDevBuildCloudEvent(record)
5753
if err != nil {
54+
s.logger.Err(err).Msg("failed to create cloud event")
5855
return nil, err
5956
}
60-
// 2. Send the cloud event to tekton listener that serves for tibuild.
6157

62-
c := cloudevents.ContextWithTarget(ctx, s.tektonClient.listenerURL)
63-
if result := s.tektonClient.client.Send(c, *event); !protocol.IsACK(result) {
64-
slog.ErrorContext(ctx, "failed to send", "reason", result)
65-
return nil, fmt.Errorf("failed to send ce:%w", result)
58+
// 2. Send the cloud event to tekton listener that serves for tibuild.
59+
if result := s.tektonCloudEventClient.Send(ctx, *event); !protocol.IsACK(result) {
60+
s.logger.Err(result).Msg("failed to send cloud event")
61+
return nil, fmt.Errorf("failed to send cloud event: %w", result)
6662
}
6763

6864
return nil, nil

experiments/tibuild-v2/internal/service/impl/devbuild_github.go

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
)
1010

1111
func getGhRefSha(ctx context.Context, ghClient *github.Client, fullRepo, ref string) string {
12+
if ghClient == nil {
13+
return ""
14+
}
15+
1216
parts := strings.SplitN(fullRepo, "/", 2)
1317
if len(parts) != 2 {
1418
return ""

experiments/tibuild-v2/pkg/config/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ type Jenkins struct {
2323
}
2424

2525
type Tekton struct {
26-
Cloudevent_Endpoint string `yaml:"cloudevent_endpoint,omitempty" json:"cloudevent_endpoint,omitempty"`
27-
ViewURL string `yaml:"view_url,omitempty" json:"view_url,omitempty"`
28-
OciFileDownloadURL string `yaml:"oci_file_download_url,omitempty" json:"oci_file_download_url,omitempty"`
26+
CloudeventEndpoint string `yaml:"cloudevent_endpoint,omitempty" json:"cloudevent_endpoint,omitempty"`
27+
ViewURL string `yaml:"view_url,omitempty" json:"view_url,omitempty"`
28+
OciFileDownloadURL string `yaml:"oci_file_download_url,omitempty" json:"oci_file_download_url,omitempty"`
2929
}
3030

3131
type RestApiSecret struct {

0 commit comments

Comments
 (0)