/
manifests.go
195 lines (177 loc) · 5.12 KB
/
manifests.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Jackal Authors
// Package bigbang contains the logic for installing Big Bang and Flux
package bigbang
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/semver/v3"
fluxHelmCtrl "github.com/fluxcd/helm-controller/api/v2beta1"
fluxSrcCtrl "github.com/fluxcd/source-controller/api/v1"
"github.com/racer159/jackal/src/types/extensions"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const bbV1JackalCredentialsValues = `
registryCredentials:
registry: "###JACKAL_REGISTRY###"
username: "jackal-pull"
password: "###JACKAL_REGISTRY_AUTH_PULL###"
git:
existingSecret: "private-git-server" # -- Chart created secrets with user defined values
credentials:
# -- HTTP git credentials, both username and password must be provided
username: "###JACKAL_GIT_PUSH###"
password: "###JACKAL_GIT_AUTH_PUSH###"
# -- Big Bang v1 Kyverno Support
kyvernopolicies:
values:
exclude:
any:
- resources:
namespaces:
- jackal # don't have Kyverno prevent Jackal from doing jackal things
`
const bbV2JackalCredentialsValues = `
registryCredentials:
registry: "###JACKAL_REGISTRY###"
username: "jackal-pull"
password: "###JACKAL_REGISTRY_AUTH_PULL###"
git:
existingSecret: "private-git-server" # -- Chart created secrets with user defined values
credentials:
# -- HTTP git credentials, both username and password must be provided
username: "###JACKAL_GIT_PUSH###"
password: "###JACKAL_GIT_AUTH_PUSH###"
# -- Big Bang v2 Kyverno Support
kyvernoPolicies:
values:
exclude:
any:
- resources:
namespaces:
- jackal # don't have Kyverno prevent Jackal from doing jackal things
`
func manifestJackalCredentials(version string) corev1.Secret {
values := bbV1JackalCredentialsValues
semverVersion, err := semver.NewVersion(version)
if err == nil && semverVersion.Major() == 2 {
values = bbV2JackalCredentialsValues
}
return corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: bb,
Name: "jackal-credentials",
},
StringData: map[string]string{
"values.yaml": values,
},
}
}
// manifestGitRepo generates a GitRepository object for the Big Bang umbrella repo.
func manifestGitRepo(cfg *extensions.BigBang) fluxSrcCtrl.GitRepository {
apiVersion := "source.toolkit.fluxcd.io/v1beta2"
// Set apiVersion to v1 on BB v2.7.0 or higher falling back to v1beta2 as needed
semverVersion, _ := semver.NewVersion(cfg.Version)
if semverVersion != nil {
c, _ := semver.NewConstraint(">= 2.7.0")
if c != nil {
updateFlux, _ := c.Validate(semverVersion)
if updateFlux && !cfg.SkipFlux {
apiVersion = "source.toolkit.fluxcd.io/v1"
}
}
}
return fluxSrcCtrl.GitRepository{
TypeMeta: metav1.TypeMeta{
Kind: fluxSrcCtrl.GitRepositoryKind,
APIVersion: apiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: bb,
Namespace: bb,
},
Spec: fluxSrcCtrl.GitRepositorySpec{
URL: cfg.Repo,
Interval: tenMins,
Reference: &fluxSrcCtrl.GitRepositoryRef{
Tag: cfg.Version,
},
},
}
}
// manifestValuesFile generates a Secret object for the Big Bang umbrella repo.
func manifestValuesFile(idx int, path string) (secret corev1.Secret, err error) {
// Read the file from the path.
file, err := os.ReadFile(path)
if err != nil {
return secret, err
}
// Get the base file name for this file.
baseName := filepath.Base(path)
// Define the name as the file name without the extension.
baseName = strings.TrimSuffix(baseName, filepath.Ext(baseName))
// Add the name prefix.
name := fmt.Sprintf("bb-usr-vals-%d-%s", idx, baseName)
// Create a secret with the file contents.
secret = corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: bb,
Name: name,
},
StringData: map[string]string{
"values.yaml": string(file),
},
}
return secret, nil
}
// manifestHelmRelease generates a HelmRelease object for the Big Bang umbrella repo.
func manifestHelmRelease(values []fluxHelmCtrl.ValuesReference) fluxHelmCtrl.HelmRelease {
return fluxHelmCtrl.HelmRelease{
TypeMeta: metav1.TypeMeta{
Kind: fluxHelmCtrl.HelmReleaseKind,
APIVersion: "helm.toolkit.fluxcd.io/v2beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: bb,
Namespace: bb,
},
Spec: fluxHelmCtrl.HelmReleaseSpec{
Timeout: &tenMins,
Chart: fluxHelmCtrl.HelmChartTemplate{
Spec: fluxHelmCtrl.HelmChartTemplateSpec{
Chart: "./chart",
SourceRef: fluxHelmCtrl.CrossNamespaceObjectReference{
Kind: fluxSrcCtrl.GitRepositoryKind,
Name: bb,
},
},
},
Install: &fluxHelmCtrl.Install{
Remediation: &fluxHelmCtrl.InstallRemediation{
Retries: -1,
},
},
Upgrade: &fluxHelmCtrl.Upgrade{
Remediation: &fluxHelmCtrl.UpgradeRemediation{
Retries: 5,
},
CleanupOnFail: true,
},
Rollback: &fluxHelmCtrl.Rollback{
CleanupOnFail: true,
},
ValuesFrom: values,
},
}
}