-
Notifications
You must be signed in to change notification settings - Fork 457
/
extra_manifest.go
253 lines (201 loc) · 6.74 KB
/
extra_manifest.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package k8s
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/state"
"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-multierror"
"github.com/siderolabs/go-pointer"
"go.uber.org/zap"
k8sadapter "github.com/talos-systems/talos/internal/app/machined/pkg/adapters/k8s"
"github.com/talos-systems/talos/pkg/machinery/resources/k8s"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)
// ExtraManifestController renders manifests based on templates and config/secrets.
type ExtraManifestController struct{}
// Name implements controller.Controller interface.
func (ctrl *ExtraManifestController) Name() string {
return "k8s.ExtraManifestController"
}
// Inputs implements controller.Controller interface.
func (ctrl *ExtraManifestController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: k8s.ControlPlaneNamespaceName,
Type: k8s.ExtraManifestsConfigType,
Kind: controller.InputWeak,
},
{
Namespace: network.NamespaceName,
Type: network.StatusType,
ID: pointer.To(network.StatusID),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *ExtraManifestController) Outputs() []controller.Output {
return []controller.Output{
{
Type: k8s.ManifestType,
Kind: controller.OutputShared,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo
func (ctrl *ExtraManifestController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
// wait for network to be ready as networking is required to download extra manifests
networkResource, err := r.Get(ctx, resource.NewMetadata(network.NamespaceName, network.StatusType, network.StatusID, resource.VersionUndefined))
if err != nil {
if state.IsNotFoundError(err) {
continue
}
return err
}
networkStatus := networkResource.(*network.Status).TypedSpec()
if !(networkStatus.AddressReady && networkStatus.ConnectivityReady) {
continue
}
configResource, err := r.Get(ctx, k8s.NewExtraManifestsConfig().Metadata())
if err != nil {
if state.IsNotFoundError(err) {
if err = ctrl.teardownAll(ctx, r); err != nil {
return fmt.Errorf("error tearing down: %w", err)
}
continue
}
return err
}
config := *configResource.(*k8s.ExtraManifestsConfig).TypedSpec()
var multiErr *multierror.Error
presentManifests := map[resource.ID]struct{}{}
for _, manifest := range config.ExtraManifests {
var id resource.ID
id, err = ctrl.process(ctx, r, logger, manifest)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
presentManifests[id] = struct{}{}
}
if multiErr.ErrorOrNil() != nil {
return multiErr.ErrorOrNil()
}
allManifests, err := r.List(ctx, resource.NewMetadata(k8s.ControlPlaneNamespaceName, k8s.ManifestType, "", resource.VersionUndefined))
if err != nil {
return fmt.Errorf("error listing extra manifests: %w", err)
}
for _, manifest := range allManifests.Items {
if manifest.Metadata().Owner() != ctrl.Name() {
continue
}
if _, exists := presentManifests[manifest.Metadata().ID()]; !exists {
if err = r.Destroy(ctx, manifest.Metadata()); err != nil {
return fmt.Errorf("error cleaning up extra manifest: %w", err)
}
}
}
}
}
func (ctrl *ExtraManifestController) process(ctx context.Context, r controller.Runtime, logger *zap.Logger, manifest k8s.ExtraManifest) (id resource.ID, err error) {
id = fmt.Sprintf("%s-%s", manifest.Priority, manifest.Name)
// inline manifests don't require download
if manifest.InlineManifest != "" {
return id, ctrl.processInline(ctx, r, manifest, id)
}
return id, ctrl.processURL(ctx, r, logger, manifest, id)
}
func (ctrl *ExtraManifestController) processURL(ctx context.Context, r controller.Runtime, logger *zap.Logger, manifest k8s.ExtraManifest, id resource.ID) (err error) {
var tmpDir string
tmpDir, err = ioutil.TempDir("", "talos")
if err != nil {
return
}
defer os.RemoveAll(tmpDir) //nolint:errcheck
// I wish we never used go-getter package, as it doesn't allow downloading into memory.
// But there's not much we can do about it right now, as it supports lots of magic
// users might rely upon.
// Disable netrc since we don't have getent installed, and most likely
// never will.
httpGetter := &getter.HttpGetter{
Netrc: false,
Client: http.DefaultClient,
}
httpGetter.Header = make(http.Header)
for k, v := range manifest.ExtraHeaders {
httpGetter.Header.Add(k, v)
}
getter.Getters["http"] = httpGetter
getter.Getters["https"] = httpGetter
client := &getter.Client{
Ctx: ctx,
Src: manifest.URL,
Dst: filepath.Join(tmpDir, "manifest.yaml"),
Pwd: tmpDir,
Mode: getter.ClientModeFile,
Options: []getter.ClientOption{},
}
if err = client.Get(); err != nil {
err = fmt.Errorf("error downloading %q: %w", manifest.URL, err)
return
}
logger.Sugar().Infof("downloaded manifest %q", manifest.URL)
var contents []byte
contents, err = ioutil.ReadFile(client.Dst)
if err != nil {
return
}
if err = r.Modify(ctx, k8s.NewManifest(k8s.ControlPlaneNamespaceName, id),
func(r resource.Resource) error {
return k8sadapter.Manifest(r.(*k8s.Manifest)).SetYAML(contents)
}); err != nil {
err = fmt.Errorf("error updating manifests: %w", err)
return
}
return nil
}
func (ctrl *ExtraManifestController) processInline(ctx context.Context, r controller.Runtime, manifest k8s.ExtraManifest, id resource.ID) error {
err := r.Modify(
ctx,
k8s.NewManifest(k8s.ControlPlaneNamespaceName, id),
func(r resource.Resource) error {
return k8sadapter.Manifest(r.(*k8s.Manifest)).SetYAML([]byte(manifest.InlineManifest))
},
)
if err != nil {
return fmt.Errorf("error updating manifests: %w", err)
}
return nil
}
//nolint:dupl
func (ctrl *ExtraManifestController) teardownAll(ctx context.Context, r controller.Runtime) error {
manifests, err := r.List(ctx, resource.NewMetadata(k8s.ControlPlaneNamespaceName, k8s.ManifestType, "", resource.VersionUndefined))
if err != nil {
return fmt.Errorf("error listing extra manifests: %w", err)
}
for _, manifest := range manifests.Items {
if manifest.Metadata().Owner() != ctrl.Name() {
continue
}
if err = r.Destroy(ctx, manifest.Metadata()); err != nil {
return fmt.Errorf("error destroying extra manifest: %w", err)
}
}
return nil
}