This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
builder.go
66 lines (57 loc) · 1.58 KB
/
builder.go
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
package client
import (
"context"
"time"
"github.com/acorn-io/baaah/pkg/router"
"github.com/acorn-io/baaah/pkg/watcher"
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/buildclient"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (c *DefaultClient) getOrCreateBuilder(ctx context.Context, name string) (*apiv1.Builder, error) {
builder := &apiv1.Builder{}
if name == "" {
builders := &apiv1.BuilderList{}
if err := c.Client.List(ctx, builders, &kclient.ListOptions{Namespace: c.Namespace}); err != nil {
return nil, err
}
if len(builders.Items) == 0 {
builder = &apiv1.Builder{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: c.Namespace,
},
}
if err := c.Client.Create(ctx, builder); err != nil {
return nil, err
}
} else {
builder = &builders.Items[0]
}
} else {
if err := c.Client.Get(ctx, router.Key(c.Namespace, name), builder); err != nil {
return nil, err
}
}
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
select {
case <-subCtx.Done():
case <-time.After(3 * time.Second):
logrus.Infof("Waiting for builder to start")
}
}()
builder, err := watcher.New[*apiv1.Builder](c.Client).ByObject(ctx, builder, func(builder *apiv1.Builder) (bool, error) {
return builder.Status.Ready, nil
})
if err != nil {
return nil, err
}
if builder.Status.Endpoint != "" {
buildclient.PingBuilder(ctx, builder.Status.Endpoint)
}
return builder, nil
}