Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-mk3/cmd/run.go
2497 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"time"
10
11
"github.com/gitpod-io/gitpod/common-go/baseserver"
12
13
"github.com/gitpod-io/gitpod/common-go/log"
14
"github.com/gitpod-io/gitpod/image-builder/api"
15
"github.com/gitpod-io/gitpod/image-builder/pkg/orchestrator"
16
"github.com/gitpod-io/gitpod/image-builder/pkg/resolve"
17
18
"github.com/opentracing/opentracing-go"
19
"github.com/spf13/cobra"
20
)
21
22
// runCmd represents the run command
23
var runCmd = &cobra.Command{
24
Use: "run",
25
Short: "Starts the image-builder service",
26
Run: func(cmd *cobra.Command, args []string) {
27
cfg := getConfig()
28
log.WithField("config", cfg).Info("Starting image-builder-mk3")
29
30
srv, err := baseserver.New("image-builder-mk3",
31
baseserver.WithConfig(cfg.Server),
32
baseserver.WithVersion(Version),
33
)
34
if err != nil {
35
log.WithError(err).Fatal("Failed to setup server.")
36
}
37
38
ctx, cancel := context.WithCancel(context.Background())
39
defer cancel()
40
span, ctx := opentracing.StartSpanFromContext(ctx, "/cmd/Run")
41
defer span.Finish()
42
43
service, err := orchestrator.NewOrchestratingBuilder(cfg.Orchestrator)
44
if err != nil {
45
log.Fatal(err)
46
}
47
if cfg.RefCache.Interval != "" && len(cfg.RefCache.Refs) > 0 {
48
interval, err := time.ParseDuration(cfg.RefCache.Interval)
49
if err != nil {
50
log.WithError(err).WithField("interval", cfg.RefCache.Interval).Fatal("interval is not a valid duration")
51
}
52
53
resolver := &resolve.PrecachingRefResolver{
54
Resolver: &resolve.StandaloneRefResolver{},
55
Candidates: cfg.RefCache.Refs,
56
}
57
go resolver.StartCaching(ctx, interval)
58
service.RefResolver = resolver
59
}
60
61
err = service.RegisterMetrics(srv.MetricsRegistry())
62
if err != nil {
63
log.WithError(err).Fatal("Failed to register metrics.")
64
}
65
66
err = service.Start(ctx)
67
if err != nil {
68
log.WithError(err).Fatal("Failed to start orchestrator service.")
69
}
70
71
api.RegisterImageBuilderServer(srv.GRPC(), service)
72
73
err = srv.ListenAndServe()
74
if err != nil {
75
log.WithError(err).Fatal("Failed to start server")
76
}
77
},
78
}
79
80
func init() {
81
rootCmd.AddCommand(runCmd)
82
}
83
84