-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathrunner.go
More file actions
208 lines (179 loc) · 6.14 KB
/
Copy pathrunner.go
File metadata and controls
208 lines (179 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package runner
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
"github.com/padok-team/burrito/internal/burrito/config"
datastore "github.com/padok-team/burrito/internal/datastore/client"
"github.com/padok-team/burrito/internal/runner/tools"
"github.com/padok-team/burrito/internal/utils"
runnerutils "github.com/padok-team/burrito/internal/utils/runner"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Runner struct {
config *config.Config
exec tools.BaseExec
Datastore datastore.Client
Client client.Client
Layer *configv1alpha1.TerraformLayer
Run *configv1alpha1.TerraformRun
Repository *configv1alpha1.TerraformRepository
repoDir string
workingDir string
}
func New(c *config.Config) *Runner {
return &Runner{
config: c,
}
}
// Entrypoint function of the runner. Initializes the runner and executes its action.
func (r *Runner) Exec() error {
err := r.initClients()
if err != nil {
log.Errorf("error initializing runner clients: %s", err)
return err
}
err = r.Init()
if err != nil {
log.Errorf("error initializing runner: %s", err)
return err
}
err = r.ExecInit()
if err != nil {
log.Errorf("error executing init: %s", err)
return err
}
return r.ExecAction()
}
// Initialize the runner clients (kubernetes, datastore).
func (r *Runner) initClients() error {
kubeClient, err := utils.NewK8SClient()
if err != nil {
log.Errorf("error creating kubernetes client: %s", err)
return err
}
r.Client = kubeClient
datastoreClient := datastore.NewDefaultClient(r.config.Datastore)
r.Datastore = datastoreClient
return nil
}
// Initialize the runner: retrieve linked resources (layer, run, repository),
// fetch the repository content, install the binaries and configure Hermitcrab mirror.
func (r *Runner) Init() error {
log.Infof("retrieving linked TerraformLayer and TerraformRepository")
err := r.GetResources()
if err != nil {
log.Errorf("error getting kubernetes resources: %s", err)
return err
}
r.repoDir = filepath.Join(r.config.Runner.RepositoryPath, "content")
r.workingDir = filepath.Join(r.repoDir, r.Layer.Spec.Path)
err = r.cloneGitBundle()
if err != nil {
log.Errorf("error getting git bundle: %s", err)
return err
}
log.Infof("installing binaries...")
r.exec, err = tools.InstallBinaries(r.Layer, r.Repository, r.config.Runner.RunnerBinaryPath, r.workingDir)
if err != nil {
log.Errorf("error installing binaries: %s", err)
return err
}
if r.config.Hermitcrab.Enabled {
log.Infof("Hermitcrab configuration detected, creating network mirror configuration...")
return r.EnableHermitcrab()
}
return nil
}
// Enable Hermitcrab network mirror configuration.
func (r *Runner) EnableHermitcrab() error {
log.Infof("Hermitcrab configuration detected, creating network mirror configuration...")
err := runnerutils.CreateNetworkMirrorConfig(r.config.Runner.RepositoryPath, r.config.Hermitcrab.URL)
if err != nil {
log.Errorf("error creating network mirror configuration: %s", err)
}
return err
}
// Disable Hermitcrab network mirror configuration.
func (r *Runner) DisableHermitcrab() error {
log.Infof("removing Hermitcrab network mirror configuration...")
err := runnerutils.RemoveNetworkMirrorConfig(r.config.Runner.RepositoryPath)
if err != nil {
log.Errorf("error removing network mirror configuration: %s", err)
}
return err
}
// Retrieve linked resources (layer, run, repository) from the Kubernetes API.
func (r *Runner) GetResources() error {
layer := &configv1alpha1.TerraformLayer{}
log.Infof("getting layer %s/%s", r.config.Runner.Layer.Namespace, r.config.Runner.Layer.Name)
err := r.Client.Get(context.TODO(), types.NamespacedName{
Namespace: r.config.Runner.Layer.Namespace,
Name: r.config.Runner.Layer.Name,
}, layer)
if err != nil {
return err
}
log.Infof("successfully retrieved layer")
r.Layer = layer
run := &configv1alpha1.TerraformRun{}
log.Infof("getting run %s/%s", layer.Namespace, layer.Status.LastRun.Name)
err = r.Client.Get(context.TODO(), types.NamespacedName{
Namespace: layer.Namespace,
Name: r.config.Runner.Run,
}, run)
if err != nil {
return err
}
log.Infof("successfully retrieved run")
r.Run = run
repository := &configv1alpha1.TerraformRepository{}
log.Infof("getting repo %s/%s", layer.Spec.Repository.Namespace, layer.Spec.Repository.Name)
err = r.Client.Get(context.TODO(), types.NamespacedName{
Namespace: layer.Spec.Repository.Namespace,
Name: layer.Spec.Repository.Name,
}, repository)
if err != nil {
return err
}
log.Infof("successfully retrieved repo")
r.Repository = repository
log.Infof("kubernetes resources successfully retrieved")
return nil
}
func (r *Runner) cloneGitBundle() error {
bundle, err := r.Datastore.GetGitBundle(r.Repository.Namespace, r.Repository.Name, r.Layer.Spec.Branch, r.Run.Spec.Layer.Revision)
if err != nil {
log.Errorf("error fetching git bundle from datastore: %s", err)
return err
}
err = os.MkdirAll(r.config.Runner.RepositoryPath, 0755)
if err != nil {
log.Errorf("error creating repository directory: %s", err)
}
sanitizedBranch := strings.ReplaceAll(r.Layer.Spec.Branch, "/", "--")
bundlePath := filepath.Join(r.config.Runner.RepositoryPath, fmt.Sprintf("%s-%s.gitbundle", sanitizedBranch, r.Run.Spec.Layer.Revision))
err = os.WriteFile(bundlePath, bundle, 0644)
if err != nil {
log.Errorf("error writing git bundle to disk: %s", err)
return err
}
// Remove prefix not authorized by `git clone` command (because users could provide `refs/tags/v1.0.0` or `refs/heads/main`)
// in their TerraformLayer spec.
branch := strings.TrimPrefix(r.Layer.Spec.Branch, "refs/heads/")
branch = strings.TrimPrefix(branch, "refs/tags/")
cmd := exec.Command("git", "clone", bundlePath, r.repoDir, "--branch", branch)
err = cmd.Run()
if err != nil {
log.Errorf("error cloning repository: %s", err)
return err
}
log.Infof("successfully fetched and opened git bundle from the datastore: repo=%s/%s ref=%s rev=%s", r.Repository.Namespace, r.Repository.Name, r.Layer.Spec.Branch, r.Run.Spec.Layer.Revision)
return nil
}