/
cluster_init.go
79 lines (63 loc) · 2.34 KB
/
cluster_init.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
67
68
69
70
71
72
73
74
75
76
77
78
79
package dev
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Run a function with access to the cluster object. Can be used to directly interact with the cluster.
type ClusterInitFn func(ctx context.Context, cluster *Cluster) error
func (fn ClusterInitFn) Init(ctx context.Context, cl *Cluster) error {
return fn(ctx, cl)
}
// Load objects from given folder paths and applies them into the cluster.
type ClusterLoadObjectsFromFolders []string
func (l ClusterLoadObjectsFromFolders) Init(
ctx context.Context, cluster *Cluster) error {
return cluster.CreateAndWaitFromFolders(ctx, l)
}
// Load objects from given file paths and applies them into the cluster.
type ClusterLoadObjectsFromFiles []string
func (l ClusterLoadObjectsFromFiles) Init(
ctx context.Context, cluster *Cluster) error {
return cluster.CreateAndWaitFromFiles(ctx, l)
}
// Load objects from the given http urls and applies them into the cluster.
type ClusterLoadObjectsFromHttp []string
func (l ClusterLoadObjectsFromHttp) Init(
ctx context.Context, cluster *Cluster) error {
return cluster.CreateAndWaitFromHttp(ctx, l)
}
// Creates the referenced Object and waits for it to be ready.
type ClusterLoadObjectFromClientObject struct {
client.Object
}
func (l ClusterLoadObjectFromClientObject) Init(
ctx context.Context, cluster *Cluster) error {
return cluster.CreateAndWaitForReadiness(ctx, l.Object)
}
// Adds the helm repository, updates repository cache and installs a helm package.
type ClusterHelmInstall struct {
RepoName, RepoURL, PackageName, Namespace, ReleaseName string
SetVars []string
}
func (h ClusterHelmInstall) Init(
ctx context.Context, cluster *Cluster) error {
if err := cluster.Helm.HelmRepoAdd(ctx, h.RepoName, h.RepoURL); err != nil {
return err
}
if err := cluster.Helm.HelmRepoUpdate(ctx); err != nil {
return err
}
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: h.Namespace,
},
}
if err := cluster.CtrlClient.Create(ctx, ns); err != nil && !errors.IsAlreadyExists(err) {
return fmt.Errorf("creating namespace for helm: %w", err)
}
return cluster.Helm.HelmInstall(ctx, cluster, h.RepoName, h.PackageName, h.ReleaseName, h.Namespace, h.SetVars)
}