Skip to content

Commit 94647d1

Browse files
authored
[feature] check settings (#713)
Signed-off-by: Stepan Paksashvili <[email protected]>
1 parent 5e84a04 commit 94647d1

File tree

5 files changed

+107
-6
lines changed

5 files changed

+107
-6
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
77
github.com/deckhouse/deckhouse/pkg/log v0.1.0
88
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.3.0
9-
github.com/deckhouse/module-sdk v0.5.0
9+
github.com/deckhouse/module-sdk v0.6.0
1010
github.com/dominikbraun/graph v0.23.0
1111
github.com/ettle/strcase v0.2.0
1212
github.com/flant/kube-client v1.5.0
@@ -140,7 +140,7 @@ require (
140140
github.com/google/go-containerregistry v0.20.6 // indirect
141141
github.com/google/gofuzz v1.2.0 // indirect
142142
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
143-
github.com/google/uuid v1.6.0 // indirect
143+
github.com/google/uuid v1.6.0
144144
github.com/gorilla/mux v1.8.1 // indirect
145145
github.com/gorilla/websocket v1.5.1 // indirect
146146
github.com/gosuri/uitable v0.0.4 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ github.com/deckhouse/deckhouse/pkg/log v0.1.0 h1:2aPfyiHHSIJlX4x7ysyPOaIb7CLmyY+
102102
github.com/deckhouse/deckhouse/pkg/log v0.1.0/go.mod h1:pbAxTSDcPmwyl3wwKDcEB3qdxHnRxqTV+J0K+sha8bw=
103103
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.3.0 h1:xZvbKuexrSQGEw6CB4n3UC7XbOb9QNLbm8UhcGZ2R1I=
104104
github.com/deckhouse/deckhouse/pkg/metrics-storage v0.3.0/go.mod h1:Rz++SzCLkFW03WGgftnn91TimGU2shiKb5S/YuxcBuE=
105-
github.com/deckhouse/module-sdk v0.5.0 h1:b2GJUzMKQLr7oJVJy5lXHvyymNyvNiFXpBie7MwEWwE=
106-
github.com/deckhouse/module-sdk v0.5.0/go.mod h1:+EbBnP8z+poIihgL4l1oxHng5ePqDUK44c39u7sEBss=
105+
github.com/deckhouse/module-sdk v0.6.0 h1:CuyiLEkPr/3dRYzvvvzR0pInTMSEeThrURuFesIehHE=
106+
github.com/deckhouse/module-sdk v0.6.0/go.mod h1:+EbBnP8z+poIihgL4l1oxHng5ePqDUK44c39u7sEBss=
107107
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
108108
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
109109
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=

pkg/module_manager/models/hooks/kind/batch_hook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ func remapSDKConfigToConfig(input *sdkhook.BatchHookConfig) (*BatchHookConfig, e
303303
cfg.Readiness = input.Readiness
304304
cfg.Hooks[BatchHookReadyKey] = input.Readiness
305305
}
306+
307+
if input.HasSettingsCheck {
308+
cfg.HasSettingsCheck = true
309+
}
306310
default:
307311
return nil, fmt.Errorf("unknown version '%s'", input.Version)
308312
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2025 Flant JSC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kind
16+
17+
import (
18+
"bytes"
19+
"context"
20+
"encoding/json"
21+
"fmt"
22+
"log/slog"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
27+
"github.com/deckhouse/deckhouse/pkg/log"
28+
"github.com/deckhouse/module-sdk/pkg/settingscheck"
29+
"github.com/google/uuid"
30+
31+
"github.com/flant/addon-operator/pkg/utils"
32+
"github.com/flant/shell-operator/pkg/executor"
33+
)
34+
35+
type SettingsCheck struct {
36+
path string
37+
tmp string
38+
logger *log.Logger
39+
}
40+
41+
func NewSettingsCheck(path, tmpPath string, logger *log.Logger) *SettingsCheck {
42+
return &SettingsCheck{
43+
path: path,
44+
tmp: tmpPath,
45+
logger: logger,
46+
}
47+
}
48+
49+
// Check runs the setting check via the OS interpreter and returns the result of the execution
50+
func (c *SettingsCheck) Check(ctx context.Context, settings utils.Values) (settingscheck.Result, error) {
51+
// tmp files has uuid in name and create only in tmp folder (because of RO filesystem)
52+
tmp, err := c.prepareTmpFile(settings)
53+
if err != nil {
54+
return settingscheck.Result{}, err
55+
}
56+
57+
// Remove tmp files after execution
58+
defer func() {
59+
if err = os.Remove(tmp); err != nil {
60+
c.logger.Error("remove tmp file", slog.String("file", tmp), log.Err(err))
61+
}
62+
}()
63+
64+
envs := os.Environ()
65+
envs = append(envs, fmt.Sprintf("%s=%s", settingscheck.EnvSettingsPath, tmp))
66+
67+
result := settingscheck.Result{
68+
Valid: true,
69+
}
70+
71+
cmd := executor.NewExecutor("", c.path, []string{"hook", "check"}, envs).WithLogger(c.logger.Named("executor"))
72+
if _, err = cmd.RunAndLogLines(ctx, make(map[string]string)); err != nil {
73+
trimmed := bytes.NewBufferString(strings.TrimPrefix(err.Error(), "stderr:"))
74+
75+
if err = json.NewDecoder(trimmed).Decode(&result); err != nil {
76+
return settingscheck.Result{}, fmt.Errorf("parse output: %s", err)
77+
}
78+
}
79+
80+
return result, nil
81+
}
82+
83+
// prepareTmpFile creates temporary files for hook and returns environment variables with paths
84+
func (c *SettingsCheck) prepareTmpFile(settings utils.Values) (string, error) {
85+
data, err := settings.JsonBytes()
86+
if err != nil {
87+
return "", err
88+
}
89+
90+
path := filepath.Join(c.tmp, fmt.Sprintf("%s.json", uuid.New().String()))
91+
if err = utils.DumpData(path, data); err != nil {
92+
return "", err
93+
}
94+
95+
return path, err
96+
}

pkg/module_manager/models/hooks/kind/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sdkhook "github.com/deckhouse/module-sdk/pkg/hook"
55
const BatchHookReadyKey = "ready"
66

77
type BatchHookConfig struct {
8-
Hooks map[string]*sdkhook.HookConfig
9-
Readiness *sdkhook.HookConfig
8+
Hooks map[string]*sdkhook.HookConfig
9+
Readiness *sdkhook.HookConfig
10+
HasSettingsCheck bool
1011
}

0 commit comments

Comments
 (0)