-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathconfig.go
250 lines (198 loc) · 8.18 KB
/
config.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
package ztpconfig
import (
"fmt"
"os"
"strings"
"github.com/golang/glog"
"github.com/kelseyhightower/envconfig"
"github.com/openshift-kni/eco-goinfra/pkg/assisted"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/configmap"
"github.com/openshift-kni/eco-goinfra/pkg/hive"
"github.com/openshift-kni/eco-goinfra/pkg/pod"
"github.com/openshift-kni/eco-goinfra/pkg/secret"
"github.com/openshift-kni/eco-gotests/tests/assisted/internal/assistedconfig"
"github.com/openshift-kni/eco-gotests/tests/assisted/ztp/internal/find"
"github.com/openshift-kni/eco-gotests/tests/assisted/ztp/internal/ztpparams"
"github.com/openshift-kni/eco-gotests/tests/internal/cluster"
. "github.com/openshift-kni/eco-gotests/tests/internal/inittools"
)
// ZTPConfig type contains ztp configuration.
type ZTPConfig struct {
*assistedconfig.AssistedConfig
*HubConfig
*SpokeConfig
}
// HubConfig contains environment information related to the hub cluster.
type HubConfig struct {
HubOCPVersion string
HubOCPXYVersion string
HubAgentServiceConfig *assisted.AgentServiceConfigBuilder
hubAssistedServicePod *pod.Builder
hubAssistedImageServicePod *pod.Builder
HubPullSecret *secret.Builder
HubInstallConfig *configmap.Builder
HubPullSecretOverride map[string][]byte
HubPullSecretOverridePath string `envconfig:"ECO_ASSISTED_ZTP_HUB_PULL_SECRET_OVERRIDE_PATH"`
}
// SpokeConfig contains environment information related to the spoke cluster.
type SpokeConfig struct {
SpokeAPIClient *clients.Settings
SpokeOCPVersion string
SpokeOCPXYVersion string
SpokeClusterName string
SpokeKubeConfig string `envconfig:"ECO_ASSISTED_ZTP_SPOKE_KUBECONFIG"`
SpokeClusterImageSet string `envconfig:"ECO_ASSISTED_ZTP_SPOKE_CLUSTERIMAGESET"`
SpokeClusterDeployment *hive.ClusterDeploymentBuilder
SpokeAgentClusterInstall *assisted.AgentClusterInstallBuilder
SpokeInfraEnv *assisted.InfraEnvBuilder
SpokeInstallConfig *configmap.Builder
}
// NewZTPConfig returns instance of ZTPConfig type.
func NewZTPConfig() *ZTPConfig {
glog.V(ztpparams.ZTPLogLevel).Info("Creating new ZTPConfig struct")
var ztpconfig ZTPConfig
ztpconfig.AssistedConfig = assistedconfig.NewAssistedConfig()
ztpconfig.HubConfig = new(HubConfig)
ztpconfig.SpokeConfig = new(SpokeConfig)
if err := ztpconfig.newHubConfig(); err != nil {
APIClient = nil
return &ztpconfig
}
if err := ztpconfig.newSpokeConfig(); err != nil {
ztpconfig.SpokeConfig.SpokeAPIClient = nil
return &ztpconfig
}
return &ztpconfig
}
// newHubConfig creates a new HubConfig member for a ZTPConfig.
func (ztpconfig *ZTPConfig) newHubConfig() error {
glog.V(ztpparams.ZTPLogLevel).Info("Creating new HubConfig struct")
ztpconfig.HubConfig = new(HubConfig)
err := envconfig.Process("eco_assisted_ztp_hub_", ztpconfig.HubConfig)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to instantiate HubConfig: %v", err)
}
if ztpconfig.HubConfig.HubPullSecretOverridePath != "" {
content, err := os.ReadFile(ztpconfig.HubConfig.HubPullSecretOverridePath)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to read hub pull-secret override path: %v", err)
}
ztpconfig.HubConfig.HubPullSecretOverride = map[string][]byte{
".dockerconfigjson": content,
}
}
ztpconfig.HubConfig.HubOCPVersion, err = find.ClusterVersion(APIClient)
if err != nil {
return err
}
splitVersion := strings.Split(ztpconfig.HubConfig.HubOCPVersion, ".")
if len(splitVersion) >= 2 {
ztpconfig.HubConfig.HubOCPXYVersion = fmt.Sprintf("%s.%s", splitVersion[0], splitVersion[1])
}
ztpconfig.HubConfig.HubAgentServiceConfig, err = assisted.PullAgentServiceConfig(APIClient)
if err != nil {
return err
}
if ztpconfig.HubConfig.HubAgentServiceConfig != nil {
assistedPod := ztpconfig.HubAssistedServicePod()
if assistedPod == nil {
return fmt.Errorf("failed to find hub assisted service pod")
}
assistedImagePod := ztpconfig.HubAssistedImageServicePod()
if assistedImagePod == nil {
return fmt.Errorf("failed to find hub assisted image service pod")
}
}
ztpconfig.HubConfig.HubPullSecret, err = cluster.GetOCPPullSecret(APIClient)
if err != nil {
return err
}
if ztpconfig.DryRun {
return nil
}
ztpconfig.HubConfig.HubInstallConfig, err = configmap.Pull(APIClient, "cluster-config-v1", "kube-system")
if err != nil {
return err
}
return nil
}
// newSpokeConfig creates a new SpokeConfig member for a ZTPConfig.
func (ztpconfig *ZTPConfig) newSpokeConfig() error {
glog.V(ztpparams.ZTPLogLevel).Info("Creating new SpokeConfig struct")
err := envconfig.Process("eco_assisted_ztp_spoke_", ztpconfig.SpokeConfig)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to instantiate SpokeConfig: %v", err)
return err
}
if ztpconfig.SpokeConfig.SpokeKubeConfig != "" {
glog.V(ztpparams.ZTPLogLevel).Infof("Creating spoke api client from %s", ztpconfig.SpokeConfig.SpokeKubeConfig)
if ztpconfig.SpokeConfig.SpokeAPIClient = clients.New(
ztpconfig.SpokeConfig.SpokeKubeConfig); ztpconfig.SpokeConfig.SpokeAPIClient == nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to load provided spoke kubeconfig: %v",
ztpconfig.SpokeConfig.SpokeKubeConfig)
return fmt.Errorf("failed to load provided spoke kubeconfig: %v", ztpconfig.SpokeConfig.SpokeKubeConfig)
}
ztpconfig.SpokeConfig.SpokeClusterName, err =
find.SpokeClusterName(APIClient, ztpconfig.SpokeConfig.SpokeAPIClient)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to find spoke cluster name: %v", err)
return err
}
ztpconfig.SpokeConfig.SpokeOCPVersion, err = find.ClusterVersion(ztpconfig.SpokeConfig.SpokeAPIClient)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to find spoke cluster version: %v", err)
return err
}
splitVersion := strings.Split(ztpconfig.SpokeConfig.SpokeOCPVersion, ".")
if len(splitVersion) >= 2 {
ztpconfig.SpokeConfig.SpokeOCPXYVersion = fmt.Sprintf("%s.%s", splitVersion[0], splitVersion[1])
}
ztpconfig.SpokeConfig.SpokeClusterDeployment, err = hive.PullClusterDeployment(APIClient,
ztpconfig.SpokeConfig.SpokeClusterName, ztpconfig.SpokeConfig.SpokeClusterName)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to find spoke cluster deployment: %v", err)
return err
}
ztpconfig.SpokeConfig.SpokeAgentClusterInstall, err = assisted.PullAgentClusterInstall(APIClient,
ztpconfig.SpokeConfig.SpokeClusterName, ztpconfig.SpokeConfig.SpokeClusterName)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to find spoke agent cluster install: %v", err)
return err
}
ztpconfig.SpokeConfig.SpokeInfraEnv, err = assisted.PullInfraEnvInstall(APIClient,
ztpconfig.SpokeConfig.SpokeClusterName, ztpconfig.SpokeConfig.SpokeClusterName)
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to find spoke infra env: %v", err)
return err
}
ztpconfig.SpokeConfig.SpokeInstallConfig, err = configmap.Pull(ztpconfig.SpokeConfig.SpokeAPIClient,
"cluster-config-v1", "kube-system")
if err != nil {
glog.V(ztpparams.ZTPLogLevel).Infof("failed to find spoke install config: %v", err)
return err
}
} else {
ztpconfig.SpokeConfig.SpokeAPIClient = nil
}
if ztpconfig.SpokeConfig.SpokeClusterImageSet == "" {
ztpconfig.SpokeConfig.SpokeClusterImageSet = ztpconfig.HubOCPXYVersion
}
return nil
}
// HubAssistedServicePod retrieves the assisted service pod from the hub
// and populates hubAssistedServicePod.
func (ztpconfig *ZTPConfig) HubAssistedServicePod() *pod.Builder {
if ztpconfig.hubAssistedServicePod == nil || !ztpconfig.hubAssistedServicePod.Exists() {
ztpconfig.hubAssistedServicePod, _ = find.AssistedServicePod(APIClient)
}
return ztpconfig.hubAssistedServicePod
}
// HubAssistedImageServicePod retrieves the assisted image service pod from the hub
// and populates hubAssistedImageServicePod.
func (ztpconfig *ZTPConfig) HubAssistedImageServicePod() *pod.Builder {
if ztpconfig.hubAssistedImageServicePod == nil || !ztpconfig.hubAssistedImageServicePod.Exists() {
ztpconfig.hubAssistedImageServicePod, _ = find.AssistedImageServicePod(APIClient)
}
return ztpconfig.hubAssistedImageServicePod
}