-
Notifications
You must be signed in to change notification settings - Fork 18
/
09-localize.go
176 lines (152 loc) · 5.68 KB
/
09-localize.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/json"
"fmt"
"github.com/mandelsoft/vfs/pkg/memoryfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/examples/lib/helper"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
v1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/contexts/ocm/download"
ocmutils "github.com/open-component-model/ocm/pkg/contexts/ocm/utils"
"github.com/open-component-model/ocm/pkg/contexts/ocm/utils/localize"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/helm/loader"
"github.com/open-component-model/ocm/pkg/runtime"
"github.com/open-component-model/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/utils/tarutils"
"github.com/open-component-model/ocm/pkg/utils/template"
"helm.sh/helm/v3/pkg/chart"
"sigs.k8s.io/yaml"
)
type DeployDescriptor struct {
ChartResource v1.ResourceReference `json:"chartResource"`
Values json.RawMessage `json:"values"`
Templater string `json:"templater"`
ImageMappings localize.ImageMappings `json:"imageMappings"`
}
func EvaluateDeployDescriptor(cv ocm.ComponentVersionAccess, res ocm.ResourceAccess, resolver ocm.ComponentVersionResolver,
config template.Values, path string, fss ...vfs.FileSystem) ([]byte, *chart.Chart, string, error) {
fs := utils.FileSystem(fss...)
id := res.Meta().GetIdentity(cv.GetDescriptor().Resources)
// first, get deploy descriptor
data, err := ocmutils.GetResourceData(res)
if err != nil {
return evalErr(err, "cannot get deploy descriptor from resource %s", id)
}
var desc DeployDescriptor
err = yaml.Unmarshal(data, &desc)
if err != nil {
return evalErr(err, "cannot unmarshal deploy descriptor from resource %s", id)
}
// second, determine substitutions
mappings, err := localize.LocalizeMappings(desc.ImageMappings, cv, resolver)
if err != nil {
return evalErr(err, "image localization failed")
}
values, err := localize.SubstituteMappingsForData(mappings, desc.Values)
if err != nil {
return evalErr(err, "applying substitutions")
}
if desc.Templater == "" {
desc.Templater = "merge"
}
if config == nil {
config = map[string]interface{}{}
}
templater, err := template.DefaultRegistry().Create(desc.Templater, fs)
if err != nil {
if desc.Templater == "merge" {
templater = template.NewMerge()
} else {
return evalErr(err, "")
}
}
// third, process values with config
effvalues, err := templater.Process(string(values), config)
if err != nil {
return evalErr(err, "error templating with %q", desc.Templater)
}
// fourth, get the helm chart
cres, ccv, err := ocmutils.ResolveResourceReference(cv, desc.ChartResource, resolver)
if err != nil {
return evalErr(err, "cannot resolve chart resource %s", desc.ChartResource)
}
effpath, err := download.DownloadResource(cv.GetContext(), cres, path, download.WithFileSystem(fs))
ccv.Close()
if err != nil {
return evalErr(err, "cannot download chart resource %s", desc.ChartResource)
}
chart, err := loader.Load(effpath, fs)
if err != nil {
return evalErr(err, "cannot load helm chart")
}
return []byte(effvalues), chart, effpath, nil
}
func evalErr(err error, msg string, args ...interface{}) ([]byte, *chart.Chart, string, error) {
return nil, nil, "", errors.Wrapf(err, msg, args...)
}
func Localize(cfg *helper.Config, config template.Values, release, namespace string) error {
ctx := ocm.DefaultContext()
err := ReadConfiguration(ctx, cfg)
if err != nil {
return errors.Wrapf(err, "cannot read ocm configuration")
}
// use the generic form here to enable the specification of any
// supported repository type as target.
fmt.Printf("local repository is %s\n", string(cfg.Target))
repo, err := ctx.RepositoryForConfig(cfg.Target, nil)
if err != nil {
return errors.Wrapf(err, "cannot open local repository")
}
defer repo.Close()
// lookup component in local repo
cv, err := repo.LookupComponentVersion(COMPONENT_NAME, COMPONENT_VERSION)
if err != nil {
return errors.Wrapf(err, "cannot get component version from %s", cfg.Target)
}
defer cv.Close()
res, err := cv.GetResourcesByResourceSelectors(compdesc.ByResourceType(DEPLOY_SCRIPT_TYPE))
if err != nil {
return errors.Wrapf(err, "no deploy descriptor found")
}
fs := memoryfs.New()
values, chart, path, err := EvaluateDeployDescriptor(cv, res[0], nil, config, "chart", fs)
if err != nil {
return errors.Wrapf(err, "invalid deployment")
}
////////////////////////////////////////////////////////////////////////////
// print chart info
fmt.Printf("downloaded chart archive: %s\n", path)
files, err := tarutils.ListArchiveContent(path, fs)
if err != nil {
return errors.Wrapf(err, "cannot list chart files")
}
fmt.Printf("chart files:\n")
for _, f := range files {
fmt.Printf(" - %s\n", f)
}
desc, _ := runtime.ToYAML(chart.Metadata)
fmt.Printf("chart meta data:\n%s\n", utils.IndentLines(string(desc), " ", false))
yamlvalues, err := runtime.ToYAML(values)
if err != nil {
return errors.Wrapf(err, "invalid values")
}
fmt.Printf("localized and configured values:\n%s\n", utils.IndentLines(string(yamlvalues), " ", false))
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// installation
if release != "" {
if namespace == "" {
namespace = "default"
}
err := InstallChart(chart, release, namespace)
if err != nil {
return errors.Wrapf(err, "installation failed")
}
}
return nil
}