@@ -48,9 +48,8 @@ func createNestedComponentSts(ctx context.Context,
48
48
ncKind clusterv1.ComponentKind ,
49
49
controlPlaneName , clusterName string , log logr.Logger ) error {
50
50
var (
51
- ncSts appsv1.StatefulSet
52
- ncSvc corev1.Service
53
- err error
51
+ ncSts * appsv1.StatefulSet
52
+ ncSvc * corev1.Service
54
53
)
55
54
// Setup the ownerReferences for all objects
56
55
or := metav1 .NewControllerRef (& ncMeta ,
@@ -59,43 +58,42 @@ func createNestedComponentSts(ctx context.Context,
59
58
// 1. Using the template defined by version/channel to create the
60
59
// StatefulSet and the Service
61
60
// TODO check the template version/channel, if not set, use the default.
62
- if ncSpec .Version == "" && ncSpec .Channel == "" {
63
- log .V (4 ).Info ("The Version and Channel are not set, " +
64
- "will use the default template." )
65
- ncSts , err = genStatefulSetObject (ncMeta , ncSpec , ncKind , controlPlaneName , clusterName , cli , log )
66
- if err != nil {
67
- return fmt .Errorf ("fail to generate the Statefulset object: %v" , err )
68
- }
61
+ if ncSpec .Version != "" && ncSpec .Channel != "" {
62
+ panic ("NOT IMPLEMENT YET" )
63
+ }
64
+
65
+ log .V (4 ).Info ("The Version and Channel are not set, " +
66
+ "will use the default template." )
67
+ if err := genStatefulSetObject (ncMeta , ncSpec , ncKind , controlPlaneName , clusterName , log , ncSts ); err != nil {
68
+ return fmt .Errorf ("fail to generate the Statefulset object: %v" , err )
69
+ }
69
70
70
- if ncKind != clusterv1 .ControllerManager {
71
- // no need to create the service for the NestedControllerManager
72
- ncSvc , err = genServiceObject (ncMeta , ncSpec , ncKind , controlPlaneName , clusterName , log )
73
- ncSvc .SetOwnerReferences ([]metav1.OwnerReference {* or })
74
- if err != nil {
75
- return fmt .Errorf ("fail to generate the Service object: %v" , err )
76
- }
77
- if err := cli .Create (ctx , & ncSvc ); err != nil {
78
- return err
79
- }
80
- log .Info ("successfully create the service for the StatefulSet" ,
81
- "component" , ncKind )
71
+ if ncKind != clusterv1 .ControllerManager {
72
+ // no need to create the service for the NestedControllerManager
73
+ if err := genServiceObject (ncMeta , ncSpec , ncKind , controlPlaneName , clusterName , log , ncSvc ); err != nil {
74
+ return fmt .Errorf ("fail to generate the Service object: %v" , err )
82
75
}
83
76
84
- } else {
85
- panic ("NOT IMPLEMENT YET" )
77
+ ncSvc .SetOwnerReferences ([]metav1.OwnerReference {* or })
78
+ if err := cli .Create (ctx , ncSvc ); err != nil {
79
+ return err
80
+ }
81
+ log .Info ("successfully create the service for the StatefulSet" ,
82
+ "component" , ncKind )
86
83
}
84
+
87
85
// 2. set the NestedComponent object as the owner of the StatefulSet
88
86
ncSts .SetOwnerReferences ([]metav1.OwnerReference {* or })
89
87
90
88
// 4. create the NestedComponent StatefulSet
91
- return cli .Create (ctx , & ncSts )
89
+ return cli .Create (ctx , ncSts )
92
90
}
93
91
94
92
// genServiceObject generates the Service object corresponding to the
95
93
// NestedComponent
96
94
func genServiceObject (ncMeta metav1.ObjectMeta ,
97
95
ncSpec clusterv1.NestedComponentSpec , ncKind clusterv1.ComponentKind ,
98
- controlPlaneName , clusterName string , log logr.Logger ) ( ncSvc corev1.Service , retErr error ) {
96
+ controlPlaneName , clusterName string , log logr.Logger , svc * corev1.Service ) error {
99
97
var templateURL string
100
98
if ncSpec .Version == "" && ncSpec .Channel == "" {
101
99
switch ncKind {
@@ -111,33 +109,23 @@ func genServiceObject(ncMeta metav1.ObjectMeta,
111
109
}
112
110
svcTmpl , err := fetchTemplate (templateURL )
113
111
if err != nil {
114
- retErr = fmt .Errorf ("fail to fetch the default template " +
112
+ return fmt .Errorf ("fail to fetch the default template " +
115
113
"for the %s service: %v" , ncKind , err )
116
- return
117
114
}
118
115
119
116
templateCtx := getTemplateArgs (ncMeta , controlPlaneName , clusterName )
120
117
121
118
svcStr , err := substituteTemplate (templateCtx , svcTmpl )
122
119
if err != nil {
123
- retErr = fmt .Errorf ("fail to substitute the default template " +
120
+ return fmt .Errorf ("fail to substitute the default template " +
124
121
"for the nestedetcd Service: %v" , err )
125
- return
126
122
}
127
- rawSvcObj , err := yamlToObject ([]byte (svcStr ))
128
- if err != nil {
129
- retErr = fmt .Errorf ("fail to convert yaml file to Serivce: %v" , err )
130
- return
123
+ if err := yamlToObject ([]byte (svcStr ), svc ); err != nil {
124
+ return fmt .Errorf ("fail to convert yaml file to Serivce: %v" , err )
131
125
}
132
126
log .Info ("deserialize yaml to runtime object(Service)" )
133
- svcObj , ok := rawSvcObj .(* corev1.Service )
134
- if ! ok {
135
- retErr = fmt .Errorf ("fail to convert runtime object to Serivce" )
136
- return
137
- }
138
- ncSvc = * svcObj
139
- log .Info ("convert runtime object to Service." )
140
- return
127
+
128
+ return nil
141
129
}
142
130
143
131
// genStatefulSetObject generates the StatefulSet object corresponding to the
@@ -146,7 +134,7 @@ func genStatefulSetObject(
146
134
ncMeta metav1.ObjectMeta ,
147
135
ncSpec clusterv1.NestedComponentSpec ,
148
136
ncKind clusterv1.ComponentKind , controlPlaneName , clusterName string ,
149
- cli ctrlcli. Client , log logr.Logger ) ( ncSts appsv1.StatefulSet , retErr error ) {
137
+ log logr.Logger , ncSts * appsv1.StatefulSet ) error {
150
138
var templateURL string
151
139
if ncSpec .Version == "" && ncSpec .Channel == "" {
152
140
log .V (4 ).Info ("The Version and Channel are not set, " +
@@ -168,57 +156,47 @@ func genStatefulSetObject(
168
156
// 1 fetch the statefulset template
169
157
stsTmpl , err := fetchTemplate (templateURL )
170
158
if err != nil {
171
- retErr = fmt .Errorf ("fail to fetch the default template " +
159
+ return fmt .Errorf ("fail to fetch the default template " +
172
160
"for the %s StatefulSet: %v" , ncKind , err )
173
- return
174
161
}
175
162
// 2 substitute the statefulset template
176
163
templateCtx := getTemplateArgs (ncMeta , controlPlaneName , clusterName )
177
164
stsStr , err := substituteTemplate (templateCtx , stsTmpl )
178
165
if err != nil {
179
- retErr = fmt .Errorf ("fail to substitute the default template " +
166
+ return fmt .Errorf ("fail to substitute the default template " +
180
167
"for the %s StatefulSet: %v" , ncKind , err )
181
- return
182
168
}
183
169
// 3 deserialize the yaml string to the StatefulSet object
184
- rawObj , err := yamlToObject ([]byte (stsStr ))
185
- if err != nil {
186
- retErr = fmt .Errorf ("fail to convert yaml file to StatefulSet: %v" , err )
187
- return
170
+
171
+ if err := yamlToObject ([]byte (stsStr ), ncSts ); err != nil {
172
+ return fmt .Errorf ("fail to convert yaml file to StatefulSet: %v" , err )
188
173
}
189
174
log .V (5 ).Info ("deserialize yaml to runtime object(StatefulSet)" )
190
- // 4 convert runtime Object to StatefulSet
191
- stsObj , ok := rawObj .(* appsv1.StatefulSet )
192
- if ! ok {
193
- retErr = fmt .Errorf ("fail to convert runtime object to StatefulSet" )
194
- return
195
- }
196
- log .V (5 ).Info ("convert runtime object to StatefulSet." )
175
+
197
176
// 5 apply NestedComponent.Spec.Resources and NestedComponent.Spec.Replicas
198
177
// to the NestedComponent StatefulSet
199
- for i := range stsObj .Spec .Template .Spec .Containers {
200
- stsObj .Spec .Template .Spec .Containers [i ].Resources =
178
+ for i := range ncSts .Spec .Template .Spec .Containers {
179
+ ncSts .Spec .Template .Spec .Containers [i ].Resources =
201
180
ncSpec .Resources
202
181
}
203
182
if ncSpec .Replicas != 0 {
204
- stsObj .Spec .Replicas = & ncSpec .Replicas
183
+ ncSts .Spec .Replicas = & ncSpec .Replicas
205
184
}
206
185
log .V (5 ).Info ("The NestedEtcd StatefulSet's Resources and " +
207
186
"Replicas fields are set" ,
208
- "StatefulSet" , stsObj .GetName ())
187
+ "StatefulSet" , ncSts .GetName ())
209
188
210
189
// 6 set the "--initial-cluster" command line flag for the Etcd container
211
190
if ncKind == clusterv1 .Etcd {
212
191
icaVal := genInitialClusterArgs (1 , clusterName , clusterName , ncMeta .GetNamespace ())
213
- stsArgs := append (stsObj .Spec .Template .Spec .Containers [0 ].Args ,
192
+ stsArgs := append (ncSts .Spec .Template .Spec .Containers [0 ].Args ,
214
193
"--initial-cluster" , icaVal )
215
- stsObj .Spec .Template .Spec .Containers [0 ].Args = stsArgs
194
+ ncSts .Spec .Template .Spec .Containers [0 ].Args = stsArgs
216
195
log .V (5 ).Info ("The '--initial-cluster' command line option is set" )
217
196
}
218
197
219
198
// 7 TODO validate the patch and apply it to the template.
220
- ncSts = * stsObj
221
- return
199
+ return nil
222
200
}
223
201
224
202
func getTemplateArgs (ncMeta metav1.ObjectMeta , controlPlaneName , clusterName string ) map [string ]string {
@@ -231,14 +209,14 @@ func getTemplateArgs(ncMeta metav1.ObjectMeta, controlPlaneName, clusterName str
231
209
}
232
210
233
211
// yamlToObject deserialize the yaml to the runtime object
234
- func yamlToObject (yamlContent []byte ) ( runtime.Object , error ) {
212
+ func yamlToObject (yamlContent []byte , obj runtime.Object ) error {
235
213
decode := serializer .NewCodecFactory (scheme .Scheme ).
236
214
UniversalDeserializer ().Decode
237
- obj , _ , err := decode (yamlContent , nil , nil )
215
+ _ , _ , err := decode (yamlContent , nil , obj )
238
216
if err != nil {
239
- return nil , err
217
+ return err
240
218
}
241
- return obj , nil
219
+ return nil
242
220
}
243
221
244
222
// substituteTemplate substitutes the template contents with the context
0 commit comments