github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/checker/svc_checker.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package checker
    16  
    17  import (
    18  	"fmt"
    19  	"text/template"
    20  
    21  	corev1 "k8s.io/api/core/v1"
    22  
    23  	"github.com/sirupsen/logrus"
    24  
    25  	"github.com/sealerio/sealer/common"
    26  	"github.com/sealerio/sealer/pkg/client/k8s"
    27  	v2 "github.com/sealerio/sealer/types/api/v2"
    28  )
    29  
    30  type SvcChecker struct {
    31  	client *k8s.Client
    32  }
    33  
    34  type SvcNamespaceStatus struct {
    35  	NamespaceName       string
    36  	ServiceCount        int
    37  	EndpointCount       int
    38  	UnhealthServiceList []string
    39  }
    40  
    41  type SvcClusterStatus struct {
    42  	SvcNamespaceStatusList []*SvcNamespaceStatus
    43  }
    44  
    45  func (n *SvcChecker) Check(cluster *v2.Cluster, phase string) error {
    46  	if phase != PhasePost {
    47  		return nil
    48  	}
    49  	// checker if all the node is ready
    50  	c, err := k8s.NewK8sClient()
    51  	if err != nil {
    52  		return err
    53  	}
    54  	n.client = c
    55  
    56  	namespaceSvcList, err := n.client.ListAllNamespacesSvcs()
    57  	var svcNamespaceStatusList []*SvcNamespaceStatus
    58  	if err != nil {
    59  		return err
    60  	}
    61  	for _, svcNamespace := range namespaceSvcList {
    62  		serviceCount := len(svcNamespace.ServiceList.Items)
    63  		var unhaelthService []string
    64  		var endpointCount = 0
    65  		endpointsList, err := n.client.GetEndpointsList(svcNamespace.Namespace.Name)
    66  		if err != nil {
    67  			break
    68  		}
    69  		for _, service := range svcNamespace.ServiceList.Items {
    70  			if IsExistEndpoint(endpointsList, service.Name) {
    71  				endpointCount++
    72  			} else {
    73  				unhaelthService = append(unhaelthService, service.Name)
    74  			}
    75  		}
    76  		svcNamespaceStatus := SvcNamespaceStatus{
    77  			NamespaceName:       svcNamespace.Namespace.Name,
    78  			ServiceCount:        serviceCount,
    79  			EndpointCount:       endpointCount,
    80  			UnhealthServiceList: unhaelthService,
    81  		}
    82  		svcNamespaceStatusList = append(svcNamespaceStatusList, &svcNamespaceStatus)
    83  	}
    84  	err = n.Output(svcNamespaceStatusList)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	return nil
    89  }
    90  
    91  func (n *SvcChecker) Output(svcNamespaceStatusList []*SvcNamespaceStatus) error {
    92  	t := template.New("svc_checker")
    93  	t, err := t.Parse(
    94  		`Cluster Service Status
    95    {{- range . }}
    96    Namespace: {{ .NamespaceName }}
    97    HealthService: {{ .EndpointCount }}/{{ .ServiceCount }}
    98    UnhealthServiceList:
    99      {{- range .UnhealthServiceList }}
   100      ServiceName: {{ . }}
   101      {{- end }}
   102    {{- end }}
   103  `)
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  	t = template.Must(t, err)
   108  	err = t.Execute(common.StdOut, svcNamespaceStatusList)
   109  	if err != nil {
   110  		logrus.Errorf("service checkers template can not be executed: %s", err)
   111  		return fmt.Errorf("service checkers template can not be executed: %s", err)
   112  	}
   113  	return nil
   114  }
   115  
   116  func IsExistEndpoint(endpointList *corev1.EndpointsList, serviceName string) bool {
   117  	for _, ep := range endpointList.Items {
   118  		if ep.Name == serviceName {
   119  			if len(ep.Subsets) > 0 {
   120  				return true
   121  			}
   122  		}
   123  	}
   124  	return false
   125  }
   126  
   127  func NewSvcChecker() Interface {
   128  	return &SvcChecker{}
   129  }