@@ -20,6 +20,7 @@ import (
20
20
"encoding/json"
21
21
"fmt"
22
22
23
+ "github.com/pkg/errors"
23
24
corev1 "k8s.io/api/core/v1"
24
25
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -94,7 +95,11 @@ systemd:
94
95
EnvironmentFile=/run/metadata/*`
95
96
)
96
97
97
- func newClusterClassCluster () clusterv1.Cluster {
98
+ func newClusterTopologyCluster () (clusterv1.Cluster , error ) {
99
+ variables , err := clusterTopologyVariables ()
100
+ if err != nil {
101
+ return clusterv1.Cluster {}, errors .Wrap (err , "failed to create ClusterTopologyCluster template" )
102
+ }
98
103
return clusterv1.Cluster {
99
104
TypeMeta : metav1.TypeMeta {
100
105
APIVersion : clusterv1 .GroupVersion .String (),
@@ -121,17 +126,34 @@ func newClusterClassCluster() clusterv1.Cluster {
121
126
},
122
127
},
123
128
},
124
- Variables : clusterTopologyVariables () ,
129
+ Variables : variables ,
125
130
},
126
131
},
127
- }
132
+ }, nil
128
133
}
129
134
130
- func clusterTopologyVariables () []clusterv1.ClusterVariable {
131
- sshKey , _ := json .Marshal (env .VSphereSSHAuthorizedKeysVar )
132
- controlPlaneIP , _ := json .Marshal (env .ControlPlaneEndpointVar )
133
- secretName , _ := json .Marshal (env .ClusterNameVar )
134
- kubeVipPod , _ := json .Marshal (kubeVIPPodYaml ())
135
+ func clusterTopologyVariables () ([]clusterv1.ClusterVariable , error ) {
136
+ sshKey , err := json .Marshal (env .VSphereSSHAuthorizedKeysVar )
137
+ if err != nil {
138
+ return nil , errors .Wrapf (err , "failed to json-encode variable VSphereSSHAuthorizedKeysVar: %q" , env .VSphereSSHAuthorizedKeysVar )
139
+ }
140
+ controlPlaneIP , err := json .Marshal (env .ControlPlaneEndpointVar )
141
+ if err != nil {
142
+ return nil , errors .Wrapf (err , "failed to json-encode variable ControlPlaneEndpointVar: %q" , env .ControlPlaneEndpointVar )
143
+ }
144
+ secretName , err := json .Marshal (env .ClusterNameVar )
145
+ if err != nil {
146
+ return nil , errors .Wrapf (err , "failed to json-encode variable ClusterNameVar: %q" , env .ClusterNameVar )
147
+ }
148
+ kubeVipPodYaml := kubeVIPPodYaml ()
149
+ kubeVipPod , err := json .Marshal (kubeVipPodYaml )
150
+ if err != nil {
151
+ return nil , errors .Wrapf (err , "failed to json-encode variable kubeVipPod: %q" , kubeVipPodYaml )
152
+ }
153
+ infraServerValue , err := getInfraServerValue ()
154
+ if err != nil {
155
+ return nil , err
156
+ }
135
157
return []clusterv1.ClusterVariable {
136
158
{
137
159
Name : "sshKey" ,
@@ -142,7 +164,7 @@ func clusterTopologyVariables() []clusterv1.ClusterVariable {
142
164
{
143
165
Name : "infraServer" ,
144
166
Value : apiextensionsv1.JSON {
145
- Raw : getInfraServerValue () ,
167
+ Raw : infraServerValue ,
146
168
},
147
169
},
148
170
{
@@ -164,15 +186,19 @@ func clusterTopologyVariables() []clusterv1.ClusterVariable {
164
186
Raw : secretName ,
165
187
},
166
188
},
167
- }
189
+ }, nil
168
190
}
169
191
170
- func getInfraServerValue () []byte {
171
- byteArr , _ := json .Marshal (map [string ]string {
192
+ func getInfraServerValue () ( []byte , error ) {
193
+ byteArr , err := json .Marshal (map [string ]string {
172
194
"url" : env .VSphereServerVar ,
173
195
"thumbprint" : env .VSphereThumbprint ,
174
196
})
175
- return byteArr
197
+ if err != nil {
198
+ return nil , errors .Wrapf (err , "failed to json-encode, VSphereServerVar: %s, VSphereThumbprint: %s" ,
199
+ env .VSphereServerVar , env .VSphereThumbprint )
200
+ }
201
+ return byteArr , nil
176
202
}
177
203
178
204
func newVSphereCluster () infrav1.VSphereCluster {
0 commit comments