-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcomponent.go
More file actions
104 lines (86 loc) · 3.17 KB
/
component.go
File metadata and controls
104 lines (86 loc) · 3.17 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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package gardenlinux
import (
"embed"
"path"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener-landscape-kit/componentvector"
"github.com/gardener/gardener-landscape-kit/pkg/components"
"github.com/gardener/gardener-landscape-kit/pkg/utils/files"
)
const (
// ComponentDirectory is the garden component directory within the base components directory.
ComponentDirectory = "gardener-extensions/os-gardenlinux"
)
var (
// baseTemplateDir is the directory where the base templates are stored.
baseTemplateDir = "templates/base"
//go:embed templates/base
baseTemplates embed.FS
// landscapeTemplateDir is the directory where the landscape templates are stored.
landscapeTemplateDir = "templates/landscape"
//go:embed templates/landscape
landscapeTemplates embed.FS
)
type component struct{}
// NewComponent creates a new garden component.
func NewComponent() components.Interface {
return &component{}
}
// Name returns the component name.
func (c *component) Name() string {
return "os-gardenlinux"
}
// GenerateBase generates the component base directory.
func (c *component) GenerateBase(options components.Options) error {
for _, op := range []func(components.Options) error{
writeBaseTemplateFiles,
} {
if err := op(options); err != nil {
return err
}
}
return nil
}
// GenerateLandscape generates the component landscape directory.
func (c *component) GenerateLandscape(options components.LandscapeOptions) error {
for _, op := range []func(components.LandscapeOptions) error{
writeLandscapeTemplateFiles,
} {
if err := op(options); err != nil {
return err
}
}
return nil
}
func getTemplateValues(opts components.Options) (map[string]any, error) {
return components.GetComponentVectorTemplateValues(opts, componentvector.NameGardenerGardenerExtensionOsGardenlinux)
}
func writeBaseTemplateFiles(opts components.Options) error {
objects, err := files.RenderTemplateFiles(baseTemplates, baseTemplateDir, nil)
if err != nil {
return err
}
return files.WriteObjectsToFilesystem(objects, opts.GetTargetPath(), path.Join(components.DirName, ComponentDirectory), opts.GetFilesystem(), opts.GetMergeMode())
}
func writeLandscapeTemplateFiles(opts components.LandscapeOptions) error {
var (
relativeComponentPath = path.Join(components.DirName, ComponentDirectory)
relativeRepoRoot = files.CalculatePathToComponentBase(opts.GetRelativeLandscapePath(), relativeComponentPath)
)
renderValue, err := getTemplateValues(opts)
if err != nil {
return err
}
values := utils.MergeMaps(renderValue, map[string]any{
"relativePathToBaseComponent": path.Join(relativeRepoRoot, opts.GetRelativeBasePath(), relativeComponentPath),
"landscapeComponentPath": path.Join(opts.GetRelativeLandscapePath(), relativeComponentPath),
})
objects, err := files.RenderTemplateFiles(landscapeTemplates, landscapeTemplateDir, values)
if err != nil {
return err
}
return files.WriteObjectsToFilesystem(objects, opts.GetTargetPath(), path.Join(components.DirName, ComponentDirectory), opts.GetFilesystem(), opts.GetMergeMode())
}