github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/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  	"text/template"
    19  
    20  	corev1 "k8s.io/api/core/v1"
    21  
    22  	"github.com/alibaba/sealer/common"
    23  	"github.com/alibaba/sealer/logger"
    24  	"github.com/alibaba/sealer/pkg/client/k8s"
    25  	v2 "github.com/alibaba/sealer/types/api/v2"
    26  )
    27  
    28  type SvcChecker struct {
    29  	client *k8s.Client
    30  }
    31  
    32  type SvcNamespaceStatus struct {
    33  	NamespaceName       string
    34  	ServiceCount        int
    35  	EndpointCount       int
    36  	UnhealthServiceList []string
    37  }
    38  
    39  type SvcClusterStatus struct {
    40  	SvcNamespaceStatusList []*SvcNamespaceStatus
    41  }
    42  
    43  func (n *SvcChecker) Check(cluster *v2.Cluster, phase string) error {
    44  	if phase != PhasePost {
    45  		return nil
    46  	}
    47  	// checker if all the node is ready
    48  	c, err := k8s.Newk8sClient()
    49  	if err != nil {
    50  		return err
    51  	}
    52  	n.client = c
    53  
    54  	namespaceSvcList, err := n.client.ListAllNamespacesSvcs()
    55  	var svcNamespaceStatusList []*SvcNamespaceStatus
    56  	if err != nil {
    57  		return err
    58  	}
    59  	for _, svcNamespace := range namespaceSvcList {
    60  		serviceCount := len(svcNamespace.ServiceList.Items)
    61  		var unhaelthService []string
    62  		var endpointCount = 0
    63  		endpointsList, err := n.client.GetEndpointsList(svcNamespace.Namespace.Name)
    64  		if err != nil {
    65  			break
    66  		}
    67  		for _, service := range svcNamespace.ServiceList.Items {
    68  			if IsExistEndpoint(endpointsList, service.Name) {
    69  				endpointCount++
    70  			} else {
    71  				unhaelthService = append(unhaelthService, service.Name)
    72  			}
    73  		}
    74  		svcNamespaceStatus := SvcNamespaceStatus{
    75  			NamespaceName:       svcNamespace.Namespace.Name,
    76  			ServiceCount:        serviceCount,
    77  			EndpointCount:       endpointCount,
    78  			UnhealthServiceList: unhaelthService,
    79  		}
    80  		svcNamespaceStatusList = append(svcNamespaceStatusList, &svcNamespaceStatus)
    81  	}
    82  	err = n.Output(svcNamespaceStatusList)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	return nil
    87  }
    88  
    89  func (n *SvcChecker) Output(svcNamespaceStatusList []*SvcNamespaceStatus) error {
    90  	t := template.New("svc_checker")
    91  	t, err := t.Parse(
    92  		`Cluster Service Status
    93    {{- range . }}
    94    Namespace: {{ .NamespaceName }}
    95    HealthService: {{ .EndpointCount }}/{{ .ServiceCount }}
    96    UnhealthServiceList:
    97      {{- range .UnhealthServiceList }}
    98      ServiceName: {{ . }}
    99      {{- end }}
   100    {{- end }}
   101  `)
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  	t = template.Must(t, err)
   106  	err = t.Execute(common.StdOut, svcNamespaceStatusList)
   107  	if err != nil {
   108  		logger.Error("service checkers template can not excute %s", err)
   109  		return err
   110  	}
   111  	return nil
   112  }
   113  
   114  func IsExistEndpoint(endpointList *corev1.EndpointsList, serviceName string) bool {
   115  	for _, ep := range endpointList.Items {
   116  		if ep.Name == serviceName {
   117  			if len(ep.Subsets) > 0 {
   118  				return true
   119  			}
   120  		}
   121  	}
   122  	return false
   123  }
   124  
   125  func NewSvcChecker() Interface {
   126  	return &SvcChecker{}
   127  }