|
| 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 | +} |
0 commit comments