@@ -21,6 +21,7 @@ import (
2121 corev1 "k8s.io/api/core/v1"
2222 apierrors "k8s.io/apimachinery/pkg/api/errors"
2323 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425 "k8s.io/apimachinery/pkg/types"
2526 "sigs.k8s.io/controller-runtime/pkg/client"
2627 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -29,6 +30,7 @@ import (
2930 networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
3031 "github.com/liqotech/liqo/pkg/consts"
3132 gwforge "github.com/liqotech/liqo/pkg/gateway/forge"
33+ enutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/external-network/utils"
3234 "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/forge"
3335 networkingutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/utils"
3436 "github.com/liqotech/liqo/pkg/liqoctl/factory"
@@ -37,6 +39,7 @@ import (
3739 tenantnamespace "github.com/liqotech/liqo/pkg/tenantNamespace"
3840 liqoutils "github.com/liqotech/liqo/pkg/utils"
3941 "github.com/liqotech/liqo/pkg/utils/getters"
42+ "github.com/liqotech/liqo/pkg/utils/maps"
4043)
4144
4245// Cluster contains the information about a cluster.
@@ -194,6 +197,130 @@ func (c *Cluster) SetupConfiguration(ctx context.Context, conf *networkingv1beta
194197 return nil
195198}
196199
200+ // CheckTemplateGwClient checks if the GatewayClient template is correctly set up.
201+ func (c * Cluster ) CheckTemplateGwClient (ctx context.Context , opts * Options ) error {
202+ templateName := opts .ClientTemplateName
203+ templateNamespace := opts .ClientTemplateNamespace
204+ templateGVR := opts .ClientGatewayType
205+
206+ s := c .local .Printer .StartSpinner (fmt .Sprintf ("Checking gateway client template \" %s/%s\" " ,
207+ templateName , templateNamespace ))
208+
209+ _ , err := c .checkTemplate (ctx , templateName , templateNamespace , templateGVR )
210+ if err != nil {
211+ s .Fail (fmt .Sprintf ("An error occurred while checking gateway client template \" %s/%s\" : %v" ,
212+ templateName , templateNamespace , output .PrettyErr (err )))
213+ return err
214+ }
215+
216+ s .Success (fmt .Sprintf ("Gateway client template \" %s/%s\" correctly checked" ,
217+ templateName , templateNamespace ))
218+ return nil
219+ }
220+
221+ // CheckTemplateGwServer checks if the GatewayServer template is correctly set up.
222+ func (c * Cluster ) CheckTemplateGwServer (ctx context.Context , opts * Options ) error {
223+ templateName := opts .ServerTemplateName
224+ templateNamespace := opts .ServerTemplateNamespace
225+ templateGVR := opts .ServerGatewayType
226+
227+ s := c .local .Printer .StartSpinner (fmt .Sprintf ("Checking gateway server template \" %s/%s\" " ,
228+ templateName , templateNamespace ))
229+
230+ template , err := c .checkTemplate (ctx , templateName , templateNamespace , templateGVR )
231+ if err != nil {
232+ s .Fail (fmt .Sprintf ("An error occurred while checking gateway server template \" %s/%s\" : %v" ,
233+ templateName , templateNamespace , output .PrettyErr (err )))
234+ return err
235+ }
236+
237+ if err := c .checkTemplateServerService (template , opts ); err != nil {
238+ s .Fail (fmt .Sprintf ("An error occurred while checking gateway server template \" %s/%s\" : %v" ,
239+ templateName , templateNamespace , output .PrettyErr (err )))
240+ return err
241+ }
242+
243+ s .Success (fmt .Sprintf ("Gateway server template \" %s/%s\" correctly checked" ,
244+ templateName , templateNamespace ))
245+ return nil
246+ }
247+
248+ func (c * Cluster ) checkTemplate (ctx context.Context , templateName , templateNamespace , templateGvr string ) (* unstructured.Unstructured , error ) {
249+ // Server Template Reference
250+ gvr , err := enutils .ParseGroupVersionResource (templateGvr )
251+ if err != nil {
252+ return nil , err
253+ }
254+
255+ template , err := c .local .DynClient .Resource (gvr ).Namespace (templateNamespace ).Get (ctx , templateName , metav1.GetOptions {})
256+ if err != nil {
257+ return nil , err
258+ }
259+
260+ return template , nil
261+ }
262+
263+ // checkTemplateServerService checks if the GatewayServer service template is correctly set up.
264+ func (c * Cluster ) checkTemplateServerService (template * unstructured.Unstructured , opts * Options ) error {
265+ switch corev1 .ServiceType (opts .ServerServiceType .Value ) {
266+ case corev1 .ServiceTypeClusterIP :
267+ return c .checkTemplateServerServiceClusterIP (template , opts )
268+ case corev1 .ServiceTypeNodePort :
269+ return c .checkTemplateServerServiceNodePort (template , opts )
270+ case corev1 .ServiceTypeLoadBalancer :
271+ return c .checkTemplateServerServiceLoadBalancer (template , opts )
272+ case corev1 .ServiceTypeExternalName :
273+ return fmt .Errorf ("externalName service type not supported" )
274+ }
275+ return nil
276+ }
277+
278+ func (c * Cluster ) checkTemplateServerServiceClusterIP (_ * unstructured.Unstructured , _ * Options ) error {
279+ return nil
280+ }
281+ func (c * Cluster ) checkTemplateServerServiceNodePort (template * unstructured.Unstructured , opts * Options ) error {
282+ if opts .ServerServiceNodePort == 0 {
283+ return nil
284+ }
285+
286+ path := "spec.template.spec.service.spec.ports"
287+ templateServicePorts , err := maps .GetNestedField (template .Object , path )
288+ if err != nil {
289+ return fmt .Errorf ("unable to get %s of the server template" , path )
290+ }
291+
292+ templateServicePortsSlice , ok := templateServicePorts .([]interface {})
293+ if ! ok {
294+ return fmt .Errorf ("unable to cast %s to []interface{}" , path )
295+ }
296+
297+ port , ok := templateServicePortsSlice [0 ].(map [string ]interface {})
298+ if ! ok {
299+ return fmt .Errorf ("unable to cast %s to map[string]interface{}" , path )
300+ }
301+
302+ _ , err = maps .GetNestedField (port , "nodePort" )
303+ if err != nil {
304+ return fmt .Errorf ("unable to get spec.template.spec.service.spec.ports[0].nodePort int the server template, " +
305+ "since you specified the flag \" --server-service-nodeport\" you need to add the \" nodePort\" field in the template" )
306+ }
307+
308+ return nil
309+ }
310+ func (c * Cluster ) checkTemplateServerServiceLoadBalancer (template * unstructured.Unstructured , opts * Options ) error {
311+ if opts .ServerServiceLoadBalancerIP == "" {
312+ return nil
313+ }
314+
315+ path := "spec.template.spec.service.spec.loadBalancerIP"
316+ _ , err := maps .GetNestedField (template .Object , path )
317+ if err != nil {
318+ return fmt .Errorf ("unable to get %s of the server template, " +
319+ "since you specified the flag \" --server-service-loadbalancerip\" you need to add the \" loadBalancerIP\" field in the template" , path )
320+ }
321+ return nil
322+ }
323+
197324// CheckNetworkInitialized checks if the network is initialized correctly.
198325func (c * Cluster ) CheckNetworkInitialized (ctx context.Context , remoteClusterID liqov1beta1.ClusterID ) error {
199326 s := c .local .Printer .StartSpinner ("Checking network is initialized correctly" )
0 commit comments