@@ -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,123 @@ 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 .ServerTemplateName
203+ templateNamespace := opts .ServerTemplateNamespace
204+ templateGVR := opts .ServerGatewayType
205+
206+ s := c .local .Printer .StartSpinner (fmt .Sprintf ("Checking gateway client template \" %s/%s\" " , templateName , templateNamespace ))
207+
208+ _ , err := c .checkTemplate (ctx , templateName , templateNamespace , templateGVR )
209+ if err != nil {
210+ s .Fail (fmt .Sprintf ("An error occurred while checking gateway template \" %s/%s\" : %v" , templateName , templateNamespace , output .PrettyErr (err )))
211+ return err
212+ }
213+
214+ s .Success (fmt .Sprintf ("Gateway client template \" %s/%s\" correctly checked" , templateName , templateNamespace ))
215+ return nil
216+ }
217+
218+ // CheckTemplateGwServer checks if the GatewayServer template is correctly set up.
219+ func (c * Cluster ) CheckTemplateGwServer (ctx context.Context , opts * Options ) error {
220+ templateName := opts .ServerTemplateName
221+ templateNamespace := opts .ServerTemplateNamespace
222+ templateGVR := opts .ServerGatewayType
223+
224+ s := c .local .Printer .StartSpinner (fmt .Sprintf ("Checking gateway client template \" %s/%s\" " , templateName , templateNamespace ))
225+
226+ template , err := c .checkTemplate (ctx , templateName , templateNamespace , templateGVR )
227+ if err != nil {
228+ s .Fail (fmt .Sprintf ("An error occurred while checking gateway template \" %s/%s\" : %v" , templateName , templateNamespace , output .PrettyErr (err )))
229+ return err
230+ }
231+
232+ if err := c .checkTemplateServerService (template , opts ); err != nil {
233+ s .Fail (fmt .Sprintf ("An error occurred while checking gateway template \" %s/%s\" : %v" , templateName , templateNamespace , output .PrettyErr (err )))
234+ return err
235+ }
236+
237+ s .Success (fmt .Sprintf ("Gateway client template \" %s/%s\" correctly checked" , templateName , templateNamespace ))
238+ return nil
239+ }
240+
241+ func (c * Cluster ) checkTemplate (ctx context.Context , templateName , templateNamespace , templateGvr string ) (* unstructured.Unstructured , error ) {
242+ // Server Template Reference
243+ gvr , err := enutils .ParseGroupVersionResource (templateGvr )
244+ if err != nil {
245+ return nil , err
246+ }
247+
248+ template , err := c .local .DynClient .Resource (gvr ).Namespace (templateNamespace ).Get (ctx , templateName , metav1.GetOptions {})
249+ if err != nil {
250+ return nil , err
251+ }
252+
253+ return template , nil
254+ }
255+
256+ // checkTemplateServerService checks if the GatewayServer service template is correctly set up.
257+ func (c * Cluster ) checkTemplateServerService (template * unstructured.Unstructured , opts * Options ) error {
258+ switch corev1 .ServiceType (opts .ServerServiceType .Value ) {
259+ case corev1 .ServiceTypeClusterIP :
260+ return c .checkTemplateServerServiceClusterIP (template , opts )
261+ case corev1 .ServiceTypeNodePort :
262+ return c .checkTemplateServerServiceNodePort (template , opts )
263+ case corev1 .ServiceTypeLoadBalancer :
264+ return c .checkTemplateServerServiceLoadBalancer (template , opts )
265+ case corev1 .ServiceTypeExternalName :
266+ return fmt .Errorf ("externalName service type not supported" )
267+ }
268+ return nil
269+ }
270+
271+ func (c * Cluster ) checkTemplateServerServiceClusterIP (_ * unstructured.Unstructured , _ * Options ) error {
272+ return nil
273+ }
274+ func (c * Cluster ) checkTemplateServerServiceNodePort (template * unstructured.Unstructured , opts * Options ) error {
275+ if opts .ServerServiceNodePort == 0 {
276+ return nil
277+ }
278+
279+ path := "spec.template.spec.service.spec.ports"
280+ templateServicePorts , err := maps .GetNestedField (template .Object , path )
281+ if err != nil {
282+ return fmt .Errorf ("unable to get %s of the server template" , path )
283+ }
284+
285+ templateServicePortsSlice , ok := templateServicePorts .([]interface {})
286+ if ! ok {
287+ return fmt .Errorf ("unable to cast %s to []interface{}" , path )
288+ }
289+
290+ port , ok := templateServicePortsSlice [0 ].(map [string ]interface {})
291+ if ! ok {
292+ return fmt .Errorf ("unable to cast %s to map[string]interface{}" , path )
293+ }
294+
295+ _ , err = maps .GetNestedField (port , "nodePort" )
296+ if err != nil {
297+ return fmt .Errorf ("unable to get spec.template.spec.service.spec.ports[0].nodePort int the server template, " +
298+ "since you specified the flag \" --server-service-nodeport\" you need to add the \" nodePort\" field in the template" )
299+ }
300+
301+ return nil
302+ }
303+ func (c * Cluster ) checkTemplateServerServiceLoadBalancer (template * unstructured.Unstructured , opts * Options ) error {
304+ if opts .ServerServiceLoadBalancerIP == "" {
305+ return nil
306+ }
307+
308+ path := "spec.template.spec.service.spec.loadBalancerIP"
309+ _ , err := maps .GetNestedField (template .Object , path )
310+ if err != nil {
311+ return fmt .Errorf ("unable to get %s of the server template, " +
312+ "since you specified the flag \" --server-service-loadbalancerip\" you need to add the \" loadBalancerIP\" field in the template" , path )
313+ }
314+ return nil
315+ }
316+
197317// CheckNetworkInitialized checks if the network is initialized correctly.
198318func (c * Cluster ) CheckNetworkInitialized (ctx context.Context , remoteClusterID liqov1beta1.ClusterID ) error {
199319 s := c .local .Printer .StartSpinner ("Checking network is initialized correctly" )
0 commit comments