-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
211 lines (183 loc) · 5.59 KB
/
helpers.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
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
209
210
211
/*
Copyright 2024 Chia Network Inc.
*/
package chiacrawler
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strconv"
k8schianetv1 "github.com/chia-network/chia-operator/api/v1"
"github.com/chia-network/chia-operator/internal/controller/common/consts"
)
// getChiaVolumes retrieves the requisite volumes from the Chia config struct
func (r *ChiaCrawlerReconciler) getChiaVolumes(ctx context.Context, crawler k8schianetv1.ChiaCrawler) []corev1.Volume {
var v []corev1.Volume
// secret ca volume
if crawler.Spec.ChiaConfig.CASecretName != nil {
v = append(v, corev1.Volume{
Name: "secret-ca",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: *crawler.Spec.ChiaConfig.CASecretName,
},
},
})
}
// CHIA_ROOT volume -- PVC is respected first if both it and hostpath are specified, falls back to hostPath if specified
// If both are empty, fall back to emptyDir so chia-exporter can mount CHIA_ROOT
var chiaRootAdded bool = false
if crawler.Spec.Storage != nil && crawler.Spec.Storage.ChiaRoot != nil {
if crawler.Spec.Storage.ChiaRoot.PersistentVolumeClaim != nil {
var pvcName string
if crawler.Spec.Storage.ChiaRoot.PersistentVolumeClaim.GenerateVolumeClaims {
pvcName = fmt.Sprintf(chiacrawlerNamePattern, crawler.Name)
} else {
pvcName = crawler.Spec.Storage.ChiaRoot.PersistentVolumeClaim.ClaimName
}
v = append(v, corev1.Volume{
Name: "chiaroot",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
})
chiaRootAdded = true
} else if crawler.Spec.Storage.ChiaRoot.HostPathVolume != nil {
v = append(v, corev1.Volume{
Name: "chiaroot",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: crawler.Spec.Storage.ChiaRoot.HostPathVolume.Path,
},
},
})
chiaRootAdded = true
}
}
if !chiaRootAdded {
v = append(v, corev1.Volume{
Name: "chiaroot",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
})
}
// Add sidecar volumes if any exist
if len(crawler.Spec.Sidecars.Volumes) > 0 {
v = append(v, crawler.Spec.Sidecars.Volumes...)
}
return v
}
// getChiaVolumeMounts retrieves the requisite volume mounts from the Chia config struct
func (r *ChiaCrawlerReconciler) getChiaVolumeMounts(ctx context.Context, crawler k8schianetv1.ChiaCrawler) []corev1.VolumeMount {
var v []corev1.VolumeMount
// secret ca volume
if crawler.Spec.ChiaConfig.CASecretName != nil {
v = append(v, corev1.VolumeMount{
Name: "secret-ca",
MountPath: "/chia-ca",
})
}
// CHIA_ROOT volume
v = append(v, corev1.VolumeMount{
Name: "chiaroot",
MountPath: "/chia-data",
})
return v
}
// getChiaEnv retrieves the environment variables from the Chia config struct
func (r *ChiaCrawlerReconciler) getChiaEnv(ctx context.Context, crawler k8schianetv1.ChiaCrawler) []corev1.EnvVar {
var env []corev1.EnvVar
// service env var
env = append(env, corev1.EnvVar{
Name: "service",
Value: "crawler",
})
// CHIA_ROOT env var
env = append(env, corev1.EnvVar{
Name: "CHIA_ROOT",
Value: "/chia-data",
})
// keys env var -- no keys required for a crawler
env = append(env, corev1.EnvVar{
Name: "keys",
Value: "none",
})
// ca env var
env = append(env, corev1.EnvVar{
Name: "ca",
Value: "/chia-ca",
})
// testnet env var
if crawler.Spec.ChiaConfig.Testnet != nil && *crawler.Spec.ChiaConfig.Testnet {
env = append(env, corev1.EnvVar{
Name: "testnet",
Value: "true",
})
}
// network env var
if crawler.Spec.ChiaConfig.Network != nil && *crawler.Spec.ChiaConfig.Network != "" {
env = append(env, corev1.EnvVar{
Name: "network",
Value: *crawler.Spec.ChiaConfig.Network,
})
}
// network_port env var
if crawler.Spec.ChiaConfig.NetworkPort != nil && *crawler.Spec.ChiaConfig.NetworkPort != 0 {
env = append(env, corev1.EnvVar{
Name: "network_port",
Value: strconv.Itoa(int(*crawler.Spec.ChiaConfig.NetworkPort)),
})
}
// introducer_address env var
if crawler.Spec.ChiaConfig.IntroducerAddress != nil {
env = append(env, corev1.EnvVar{
Name: "introducer_address",
Value: *crawler.Spec.ChiaConfig.IntroducerAddress,
})
}
// dns_introducer_address env var
if crawler.Spec.ChiaConfig.DNSIntroducerAddress != nil {
env = append(env, corev1.EnvVar{
Name: "dns_introducer_address",
Value: *crawler.Spec.ChiaConfig.DNSIntroducerAddress,
})
}
// TZ env var
if crawler.Spec.ChiaConfig.Timezone != nil {
env = append(env, corev1.EnvVar{
Name: "TZ",
Value: *crawler.Spec.ChiaConfig.Timezone,
})
}
// log_level env var
if crawler.Spec.ChiaConfig.LogLevel != nil {
env = append(env, corev1.EnvVar{
Name: "log_level",
Value: *crawler.Spec.ChiaConfig.LogLevel,
})
}
return env
}
// getOwnerReference gives the common owner reference spec for ChiaCrawler related objects
func (r *ChiaCrawlerReconciler) getOwnerReference(ctx context.Context, crawler k8schianetv1.ChiaCrawler) []metav1.OwnerReference {
return []metav1.OwnerReference{
{
APIVersion: crawler.APIVersion,
Kind: crawler.Kind,
Name: crawler.Name,
UID: crawler.UID,
Controller: &consts.ControllerOwner,
},
}
}
// getFullNodePort determines the correct full_node port to use
func (r *ChiaCrawlerReconciler) getFullNodePort(ctx context.Context, crawler k8schianetv1.ChiaCrawler) int32 {
if crawler.Spec.ChiaConfig.Testnet != nil && *crawler.Spec.ChiaConfig.Testnet {
return consts.TestnetNodePort
}
return consts.MainnetNodePort
}