-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhelpers.go
More file actions
118 lines (101 loc) · 3.49 KB
/
helpers.go
File metadata and controls
118 lines (101 loc) · 3.49 KB
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
// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package component
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"log/slog"
"gopkg.in/yaml.v3"
"github.com/NVIDIA/aicr/pkg/errors"
)
// computeChecksum computes the SHA256 checksum of the given content.
func computeChecksum(content []byte) string {
hash := sha256.Sum256(content)
return hex.EncodeToString(hash[:])
}
// MarshalYAML serializes a value to YAML format.
func MarshalYAML(v any) ([]byte, error) {
// Import yaml package inline to avoid adding it as a top-level dependency
// for packages that don't need it
var buf bytes.Buffer
encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)
if err := encoder.Encode(v); err != nil {
return nil, errors.Wrap(errors.ErrCodeInternal, "failed to marshal YAML", err)
}
return buf.Bytes(), nil
}
// ValuesHeader contains metadata for values.yaml file headers.
type ValuesHeader struct {
ComponentName string
BundlerVersion string
RecipeVersion string
}
// MarshalYAMLWithHeader serializes a value to YAML format with a metadata header.
func MarshalYAMLWithHeader(v any, header ValuesHeader) ([]byte, error) {
var buf bytes.Buffer
// Write header comments
fmt.Fprintf(&buf, "# %s Helm Values\n", header.ComponentName)
buf.WriteString("# Generated from Cloud Native Stack Recipe\n")
fmt.Fprintf(&buf, "# Bundler Version: %s\n", header.BundlerVersion)
fmt.Fprintf(&buf, "# Recipe Version: %s\n", header.RecipeVersion)
buf.WriteString("\n")
// Serialize the values
encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)
if err := encoder.Encode(v); err != nil {
return nil, errors.Wrap(errors.ErrCodeInternal, "failed to marshal YAML", err)
}
return buf.Bytes(), nil
}
// GetConfigValue gets a value from config map with a default fallback.
func GetConfigValue(config map[string]string, key, defaultValue string) string {
if val, ok := config[key]; ok && val != "" {
return val
}
slog.Debug("config value not found, using default", "key", key, "default", defaultValue)
return defaultValue
}
// extractCustomLabels extracts custom labels from config map with "label_" prefix.
func extractCustomLabels(config map[string]string) map[string]string {
labels := make(map[string]string)
for k, v := range config {
if len(k) > 6 && k[:6] == "label_" {
labels[k[6:]] = v
}
}
return labels
}
// extractCustomAnnotations extracts custom annotations from config map with "annotation_" prefix.
func extractCustomAnnotations(config map[string]string) map[string]string {
annotations := make(map[string]string)
for k, v := range config {
if len(k) > 11 && k[:11] == "annotation_" {
annotations[k[11:]] = v
}
}
return annotations
}
// Common string constants for boolean values in Helm templates.
const (
StrTrue = "true"
StrFalse = "false"
)
// parseBoolString parses a string boolean value.
// Returns true if the value is "true" or "1", false otherwise.
func parseBoolString(s string) bool {
return s == StrTrue || s == "1"
}