-
Notifications
You must be signed in to change notification settings - Fork 457
/
bundle.go
166 lines (134 loc) · 4.23 KB
/
bundle.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
// 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 bundle
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v3"
clientconfig "github.com/talos-systems/talos/pkg/machinery/client/config"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/generate"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
)
// NewConfigBundle returns a new bundle.
//
//nolint:gocyclo,cyclop
func NewConfigBundle(opts ...Option) (*ConfigBundle, error) {
options := DefaultOptions()
for _, opt := range opts {
if err := opt(&options); err != nil {
return nil, err
}
}
bundle := &ConfigBundle{}
// Configs already exist, we'll pull them in.
if options.ExistingConfigs != "" {
if options.InputOptions != nil {
return bundle, fmt.Errorf("both existing config path and input options specified")
}
// Pull existing machine configs of each type
for _, configType := range []machine.Type{machine.TypeInit, machine.TypeControlPlane, machine.TypeWorker} {
data, err := ioutil.ReadFile(filepath.Join(options.ExistingConfigs, strings.ToLower(configType.String())+".yaml"))
if err != nil {
if configType == machine.TypeInit && os.IsNotExist(err) {
continue
}
return bundle, err
}
unmarshalledConfig := &v1alpha1.Config{}
if err := yaml.Unmarshal(data, unmarshalledConfig); err != nil {
return bundle, err
}
switch configType {
case machine.TypeInit:
bundle.InitCfg = unmarshalledConfig
case machine.TypeControlPlane:
bundle.ControlPlaneCfg = unmarshalledConfig
case machine.TypeWorker:
bundle.WorkerCfg = unmarshalledConfig
case machine.TypeUnknown:
fallthrough
default:
panic("unreachable")
}
}
if err := applyJSONPatches(bundle, options); err != nil {
return nil, err
}
// Pull existing talosconfig
talosConfig, err := os.Open(filepath.Join(options.ExistingConfigs, "talosconfig"))
if err != nil {
return bundle, err
}
defer talosConfig.Close() //nolint:errcheck
if bundle.TalosCfg, err = clientconfig.ReadFrom(talosConfig); err != nil {
return bundle, err
}
return bundle, nil
}
// Handle generating net-new configs
if options.Verbose {
fmt.Println("generating PKI and tokens")
}
if options.InputOptions == nil {
return nil, fmt.Errorf("no WithInputOptions is defined")
}
secrets, err := generate.NewSecretsBundle(generate.NewClock(), options.InputOptions.GenOptions...)
if err != nil {
return bundle, err
}
var input *generate.Input
input, err = generate.NewInput(
options.InputOptions.ClusterName,
options.InputOptions.Endpoint,
options.InputOptions.KubeVersion,
secrets,
options.InputOptions.GenOptions...,
)
if err != nil {
return bundle, err
}
for _, configType := range []machine.Type{machine.TypeInit, machine.TypeControlPlane, machine.TypeWorker} {
var generatedConfig *v1alpha1.Config
generatedConfig, err = generate.Config(configType, input)
if err != nil {
return bundle, err
}
switch configType {
case machine.TypeInit:
bundle.InitCfg = generatedConfig
case machine.TypeControlPlane:
bundle.ControlPlaneCfg = generatedConfig
case machine.TypeWorker:
bundle.WorkerCfg = generatedConfig
case machine.TypeUnknown:
fallthrough
default:
panic("unreachable")
}
}
if err = applyJSONPatches(bundle, options); err != nil {
return nil, err
}
bundle.TalosCfg, err = generate.Talosconfig(input, options.InputOptions.GenOptions...)
if err != nil {
return bundle, err
}
return bundle, nil
}
func applyJSONPatches(bundle *ConfigBundle, options Options) error {
if err := bundle.ApplyPatches(options.Patches, true, true); err != nil {
return fmt.Errorf("error patching configs: %w", err)
}
if err := bundle.ApplyPatches(options.PatchesControlPlane, true, false); err != nil {
return fmt.Errorf("error patching control plane configs: %w", err)
}
if err := bundle.ApplyPatches(options.PatchesWorker, false, true); err != nil {
return fmt.Errorf("error patching worker config: %w", err)
}
return nil
}