Skip to content

Commit 9e4279f

Browse files
committed
wip
Signed-off-by: Ilya Lesikov <[email protected]>
1 parent 6376b68 commit 9e4279f

File tree

3 files changed

+198
-45
lines changed

3 files changed

+198
-45
lines changed

pkg/helm/nelm/nelm.go

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strconv"
1010
"time"
1111

12-
"github.com/deckhouse/deckhouse/pkg/log"
1312
"helm.sh/helm/v3/pkg/cli"
1413
"k8s.io/cli-runtime/pkg/genericclioptions"
1514
"k8s.io/client-go/rest"
@@ -20,15 +19,17 @@ import (
2019
"github.com/werf/nelm/pkg/action"
2120
)
2221

22+
// FIXME(nelm): get rid of all globals in Nelm to be able to run them sequentially or in parallel
23+
2324
var (
2425
_ client.HelmClient = &NelmClient{}
2526
commonOptions *CommonOptions
2627
)
2728

28-
func Init(opts *CommonOptions, logger *log.Logger) error {
29+
func Init(opts *CommonOptions, logger *NelmLogger) error {
2930
getter := buildConfigFlagsFromEnv(&commonOptions.Namespace, cli.New())
3031

31-
// XXX: add all other options from getter in the same manner
32+
// FIXME(addon-operator): add all other options from getter in the same manner
3233
if opts.KubeContext == "" && getter.Context != nil {
3334
opts.KubeContext = *getter.Context
3435
}
@@ -37,55 +38,48 @@ func Init(opts *CommonOptions, logger *log.Logger) error {
3738
opts.HelmDriver = os.Getenv("HELM_DRIVER")
3839
}
3940

40-
// FIXME(ilya-lesikov): this function does A LOT. And with what addon-operator expects from us
41+
// FIXME(nelm): this function does A LOT. And with what addon-operator expects from us
4142
// in regards to logging, all of this is kind of a mess now
42-
opts.context = action.SetupLogging(context.TODO(), "info", "", "off")
43+
action.SetupLogging(context.TODO(), "info", "", "off")
4344

4445
commonOptions = opts
4546

46-
versionResult, err := action.Version(commonOptions.context, action.VersionOptions{
47+
versionResult, err := action.Version(context.TODO(), action.VersionOptions{
4748
OutputNoPrint: true,
4849
})
4950
if err != nil {
5051
return fmt.Errorf("get nelm version: %w", err)
5152
}
5253

53-
logger.Info("Nelm version", slog.String("version", versionResult.FullVersion))
54+
logger.Info(context.TODO(), "Nelm version", slog.String("version", versionResult.FullVersion))
5455

5556
return nil
5657
}
5758

5859
type CommonOptions struct {
5960
Namespace string
6061
HistoryMax int32
61-
// XXX: no global timeout in Nelm
62+
// FIXME(nelm): add global timeout to Nelm actions
6263
Timeout time.Duration
6364
HelmDriver string
6465
KubeContext string
65-
// XXX: add all other options from *genericclioptions.ConfigFlags
66-
67-
// FIXME(ilya-lesikov): we need a special context with logboek attached to it to be passed to
68-
// Nelm actions to actually log anything
69-
context context.Context
66+
// FIXME(addon-operator): add all other options from *genericclioptions.ConfigFlags
7067
}
7168

72-
func NewNelmClient(logger *log.Logger, labels map[string]string) *NelmClient {
73-
logEntry := logger.With("operator.component", "nelm")
74-
69+
func NewNelmClient(logger *NelmLogger, labels map[string]string) *NelmClient {
7570
return &NelmClient{
76-
logger: logEntry,
71+
logger: logger.With("operator.component", "nelm"),
7772
labels: labels,
7873
}
7974
}
8075

8176
type NelmClient struct {
82-
// FIXME(ilya-lesikov): not gonna be used in Nelm atm
83-
logger *log.Logger
77+
logger *NelmLogger
8478
labels map[string]string
8579
}
8680

8781
func (c *NelmClient) WithLogLabels(logLabels map[string]string) {
88-
c.logger = utils.EnrichLoggerWithLabels(c.logger, logLabels)
82+
c.logger = c.logger.EnrichWithLabels(logLabels)
8983
}
9084

9185
func (c *NelmClient) WithExtraLabels(labels map[string]string) {
@@ -95,13 +89,13 @@ func (c *NelmClient) WithExtraLabels(labels map[string]string) {
9589
}
9690

9791
func (c *NelmClient) LastReleaseStatus(releaseName string) (string, string, error) {
98-
releaseGetResult, err := action.ReleaseGet(commonOptions.context, releaseName, commonOptions.Namespace, action.ReleaseGetOptions{
92+
releaseGetResult, err := action.ReleaseGet(context.TODO(), releaseName, commonOptions.Namespace, action.ReleaseGetOptions{
9993
KubeContext: commonOptions.KubeContext,
10094
OutputNoPrint: true,
10195
ReleaseStorageDriver: commonOptions.HelmDriver,
10296
})
10397
if err != nil {
104-
// FIXME(ilya-lesikov): return a specific error from ReleaseGet if release not found
98+
// FIXME(nelm): return a specific error from ReleaseGet if release not found
10599
if regexp.MustCompile(`.+release ".+" (namespace ".+") not found`).Match([]byte(err.Error())) {
106100
return "0", "", fmt.Errorf("get nelm release %q: %w", releaseName, err)
107101
}
@@ -113,17 +107,15 @@ func (c *NelmClient) LastReleaseStatus(releaseName string) (string, string, erro
113107
}
114108

115109
func (c *NelmClient) UpgradeRelease(releaseName string, chartName string, valuesPaths []string, setValues []string, labels map[string]string, namespace string) error {
116-
c.logger.Info("Running nelm install for release",
110+
c.logger.Info(context.TODO(), "Running nelm install for release",
117111
slog.String("release", releaseName),
118112
slog.String("chart", chartName),
119113
slog.String("namespace", namespace))
120114

121-
// XXX: for some reason upgrade was called twice previously?
122-
// XXX: why we always deployed with upg.SkipCRDs = true?
123-
// XXX: Nelm does not care about pending status and will upgrade pending release just fine. Does it mean we are going to be fine without this: https://github.com/flant/addon-operator/blob/b7c97010128922e066fed35e2e53e91834a250de/pkg/helm/helm3lib/helm3lib.go#L293-L300
124-
// FIXME(ilya-lesikov): allow passing labels to Release.Labels
125-
if err := action.ReleaseInstall(commonOptions.context, releaseName, namespace, action.ReleaseInstallOptions{
126-
// XXX: Nelm doesn't allow anything besides chart dir
115+
// FIXME(nelm): deploy without installing CRDs
116+
// FIXME(nelm): allow passing labels to Release.Labels
117+
if err := action.ReleaseInstall(context.TODO(), releaseName, namespace, action.ReleaseInstallOptions{
118+
// FIXME(nelm): allow passing remote chart path
127119
ChartDirPath: chartName,
128120
ExtraLabels: c.labels,
129121
KubeContext: commonOptions.KubeContext,
@@ -135,7 +127,7 @@ func (c *NelmClient) UpgradeRelease(releaseName string, chartName string, values
135127
return fmt.Errorf("install nelm release %q: %w", releaseName, err)
136128
}
137129

138-
c.logger.Info("Nelm install successful",
130+
c.logger.Info(context.TODO(), "Nelm install successful",
139131
slog.String("release", releaseName),
140132
slog.String("chart", chartName),
141133
slog.String("namespace", namespace))
@@ -148,7 +140,7 @@ func (c *NelmClient) GetReleaseValues(releaseName string) (utils.Values, error)
148140
}
149141

150142
func (c *NelmClient) GetReleaseChecksum(releaseName string) (string, error) {
151-
releaseGetResult, err := action.ReleaseGet(commonOptions.context, releaseName, commonOptions.Namespace, action.ReleaseGetOptions{
143+
releaseGetResult, err := action.ReleaseGet(context.TODO(), releaseName, commonOptions.Namespace, action.ReleaseGetOptions{
152144
KubeContext: commonOptions.KubeContext,
153145
OutputNoPrint: true,
154146
ReleaseStorageDriver: commonOptions.HelmDriver,
@@ -161,25 +153,28 @@ func (c *NelmClient) GetReleaseChecksum(releaseName string) (string, error) {
161153
return checksum, nil
162154
}
163155

164-
// XXX: here was a fallback to get "_addonOperatorModuleChecksum" value from last release values
165-
// instead. In Nelm via action.ReleaseGet we don't expose values of last release, I don't think
166-
// they are relevant when we already expose manifests. Do we really need this?
156+
// FIXME(nelm): expose Values in ReleaseGetResult and uncomment this
157+
// if recordedChecksum, hasKey := releaseGetResult.Values["_addonOperatorModuleChecksum"]; hasKey {
158+
// if recordedChecksumStr, ok := recordedChecksum.(string); ok {
159+
// return recordedChecksumStr, nil
160+
// }
161+
// }
167162

168163
return "", fmt.Errorf("moduleChecksum label not found in nelm release %q", releaseName)
169164
}
170165

171166
func (c *NelmClient) DeleteRelease(releaseName string) error {
172-
c.logger.Debug("nelm release: execute nelm uninstall", slog.String("release", releaseName))
167+
c.logger.Debug(context.TODO(), "nelm release: execute nelm uninstall", slog.String("release", releaseName))
173168

174-
if err := action.ReleaseUninstall(commonOptions.context, releaseName, commonOptions.Namespace, action.ReleaseUninstallOptions{
169+
if err := action.ReleaseUninstall(context.TODO(), releaseName, commonOptions.Namespace, action.ReleaseUninstallOptions{
175170
KubeContext: commonOptions.KubeContext,
176171
ReleaseHistoryLimit: int(commonOptions.HistoryMax),
177172
ReleaseStorageDriver: commonOptions.HelmDriver,
178173
}); err != nil {
179174
return fmt.Errorf("nelm uninstall release %q: %w", releaseName, err)
180175
}
181176

182-
c.logger.Debug("nelm release deleted", slog.String("release", releaseName))
177+
c.logger.Debug(context.TODO(), "nelm release deleted", slog.String("release", releaseName))
183178

184179
return nil
185180
}
@@ -198,20 +193,20 @@ func (c *NelmClient) IsReleaseExists(releaseName string) (bool, error) {
198193
}
199194

200195
func (c *NelmClient) ListReleasesNames() ([]string, error) {
201-
// FIXME(ilya-lesikov): not implemented in Nelm actions yet
196+
// FIXME(nelm): implement ReleaseList as a Nelm action
202197
panic("not implemented yet")
203198
}
204199

205200
func (c *NelmClient) Render(releaseName, chartName string, valuesPaths, setValues []string, namespace string, debug bool) (string, error) {
206-
// XXX: debug arg is not used now. Nelm doesn't return bad manifests on error, instead it has very verbose trace log level which prints them. Do we really need to dump bad manifests on error?
201+
// FIXME(nelm): debug arg is not used now. Nelm doesn't return bad manifests on error, instead it has very verbose trace log level which prints them. Do we really need to dump bad manifests on error?
207202

208-
c.logger.Debug("Render nelm templates for chart ...",
203+
c.logger.Debug(context.TODO(), "Render nelm templates for chart ...",
209204
slog.String("chart", chartName),
210205
slog.String("namespace", namespace))
211206

212-
// FIXME(ilya-lesikov): need to return ChartRenderResultV1, like we did in action.ChartRender
213-
err := action.ChartRender(commonOptions.context, action.ChartRenderOptions{
214-
// XXX: only local chart dirs are supported
207+
// FIXME(nelm): need to return ChartRenderResultV1, like we did in action.ChartRender
208+
err := action.ChartRender(context.TODO(), action.ChartRenderOptions{
209+
// FIXME(nelm): allow passing remote chart path
215210
ChartDirPath: chartName,
216211
ExtraLabels: c.labels,
217212
KubeContext: commonOptions.KubeContext,
@@ -226,9 +221,9 @@ func (c *NelmClient) Render(releaseName, chartName string, valuesPaths, setValue
226221
return "", fmt.Errorf("render nelm chart %q: %w", chartName, err)
227222
}
228223

229-
c.logger.Info("Render nelm templates for chart was successful", slog.String("chart", chartName))
224+
c.logger.Info(context.TODO(), "Render nelm templates for chart was successful", slog.String("chart", chartName))
230225

231-
// FIXME(ilya-lesikov): we should return manifests from ChartRenderResultV1
226+
// FIXME(nelm): we should return manifests from ChartRenderResultV1
232227
panic("not implemented yet")
233228
}
234229

pkg/helm/nelm/nelm_logger.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package nelm
2+
3+
import "context"
4+
5+
var _ Logger = (*NelmLogger)(nil)
6+
7+
// FIXME(addon-operator): reference implementation here: https://github.com/werf/nelm/blob/72b22bcd3959e4083a41765ae0b7362c22a2d23d/internal/log/logboek_logger.go
8+
// Must be thread-safe.
9+
10+
func NewNelmLogger() *NelmLogger {
11+
return &NelmLogger{}
12+
}
13+
14+
type NelmLogger struct{}
15+
16+
func (n *NelmLogger) Trace(ctx context.Context, format string, a ...interface{}) {
17+
// FIXME(addon-operator): implement this
18+
panic("not implemented yet")
19+
}
20+
21+
func (n *NelmLogger) TraceStruct(ctx context.Context, obj interface{}, format string, a ...interface{}) {
22+
// FIXME(addon-operator): implement this
23+
panic("not implemented yet")
24+
}
25+
26+
func (n *NelmLogger) TracePush(ctx context.Context, group, format string, a ...interface{}) {
27+
// FIXME(addon-operator): implement this
28+
panic("not implemented yet")
29+
}
30+
31+
func (n *NelmLogger) TracePop(ctx context.Context, group string) {
32+
// FIXME(addon-operator): implement this
33+
panic("not implemented yet")
34+
}
35+
36+
func (n *NelmLogger) Debug(ctx context.Context, format string, a ...interface{}) {
37+
// FIXME(addon-operator): implement this
38+
panic("not implemented yet")
39+
}
40+
41+
func (n *NelmLogger) DebugPush(ctx context.Context, group, format string, a ...interface{}) {
42+
// FIXME(addon-operator): implement this
43+
panic("not implemented yet")
44+
}
45+
46+
func (n *NelmLogger) DebugPop(ctx context.Context, group string) {
47+
// FIXME(addon-operator): implement this
48+
panic("not implemented yet")
49+
}
50+
51+
func (n *NelmLogger) Info(ctx context.Context, format string, a ...interface{}) {
52+
// FIXME(addon-operator): implement this
53+
panic("not implemented yet")
54+
}
55+
56+
func (n *NelmLogger) InfoPush(ctx context.Context, group, format string, a ...interface{}) {
57+
// FIXME(addon-operator): implement this
58+
panic("not implemented yet")
59+
}
60+
61+
func (n *NelmLogger) InfoPop(ctx context.Context, group string) {
62+
// FIXME(addon-operator): implement this
63+
panic("not implemented yet")
64+
}
65+
66+
func (n *NelmLogger) Warn(ctx context.Context, format string, a ...interface{}) {
67+
// FIXME(addon-operator): implement this
68+
panic("not implemented yet")
69+
}
70+
71+
func (n *NelmLogger) WarnPush(ctx context.Context, group, format string, a ...interface{}) {
72+
// FIXME(addon-operator): implement this
73+
panic("not implemented yet")
74+
}
75+
76+
func (n *NelmLogger) WarnPop(ctx context.Context, group string) {
77+
// FIXME(addon-operator): implement this
78+
panic("not implemented yet")
79+
}
80+
81+
func (n *NelmLogger) Error(ctx context.Context, format string, a ...interface{}) {
82+
// FIXME(addon-operator): implement this
83+
panic("not implemented yet")
84+
}
85+
86+
func (n *NelmLogger) ErrorPush(ctx context.Context, group, format string, a ...interface{}) {
87+
// FIXME(addon-operator): implement this
88+
panic("not implemented yet")
89+
}
90+
91+
func (n *NelmLogger) ErrorPop(ctx context.Context, group string) {
92+
// FIXME(addon-operator): implement this
93+
panic("not implemented yet")
94+
}
95+
96+
func (n *NelmLogger) InfoBlock(ctx context.Context, opts BlockOptions, format string, a ...interface{}) error {
97+
// FIXME(addon-operator): implement this. No need to do all the stuff we do in logboek. Would be
98+
// fine to just print `----------` in the beginning and the end of the block, no indentation.
99+
panic("not implemented yet")
100+
}
101+
102+
func (n *NelmLogger) SetLevel(ctx context.Context, lvl Level) {
103+
// FIXME(addon-operator): implement this
104+
panic("not implemented yet")
105+
}
106+
107+
func (n *NelmLogger) Level(ctx context.Context) Level {
108+
// FIXME(addon-operator): implement this
109+
panic("not implemented yet")
110+
}
111+
112+
func (n *NelmLogger) AcceptLevel(ctx context.Context, lvl Level) bool {
113+
// FIXME(addon-operator): implement this
114+
panic("not implemented yet")
115+
}
116+
117+
func (n *NelmLogger) With(args ...any) *NelmLogger {
118+
// FIXME(addon-operator): should be like this: https://github.com/deckhouse/deckhouse/blob/f208c10b7aa137410ffb21a9864f8b90aa5cb94b/pkg/log/logger.go#L181
119+
panic("not implemented yet")
120+
}
121+
122+
func (n *NelmLogger) EnrichWithLabels(labelsMaps ...map[string]string) *NelmLogger {
123+
// FIXME(addon-operator): should be like this: https://github.com/flant/addon-operator/blob/85252460d4089d60f68321baca1901a50c0fa74b/pkg/utils/merge_labels.go#L22
124+
panic("not implemented yet")
125+
}

pkg/helm/nelm/vendor.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package nelm
2+
3+
import "context"
4+
5+
// FIXME(nelm): initially got this from https://github.com/werf/nelm/blob/main/internal/log/interface.go
6+
// We should expose the Logger interface in Nelm and update it like below, then remove this file
7+
8+
type Logger interface {
9+
Trace(ctx context.Context, format string, a ...interface{})
10+
TraceStruct(ctx context.Context, obj interface{}, format string, a ...interface{})
11+
TracePush(ctx context.Context, group, format string, a ...interface{})
12+
TracePop(ctx context.Context, group string)
13+
Debug(ctx context.Context, format string, a ...interface{})
14+
DebugPush(ctx context.Context, group, format string, a ...interface{})
15+
DebugPop(ctx context.Context, group string)
16+
Info(ctx context.Context, format string, a ...interface{})
17+
InfoPush(ctx context.Context, group, format string, a ...interface{})
18+
InfoPop(ctx context.Context, group string)
19+
Warn(ctx context.Context, format string, a ...interface{})
20+
WarnPush(ctx context.Context, group, format string, a ...interface{})
21+
WarnPop(ctx context.Context, group string)
22+
Error(ctx context.Context, format string, a ...interface{})
23+
ErrorPush(ctx context.Context, group, format string, a ...interface{})
24+
ErrorPop(ctx context.Context, group string)
25+
InfoBlock(ctx context.Context, opts BlockOptions, format string, a ...interface{}) error
26+
SetLevel(ctx context.Context, lvl Level)
27+
Level(ctx context.Context) Level
28+
AcceptLevel(ctx context.Context, lvl Level) bool
29+
}
30+
31+
type Level string
32+
33+
type BlockOptions struct{}

0 commit comments

Comments
 (0)