github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/access.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package analyzer
    21  
    22  import (
    23  	"encoding/json"
    24  	"path/filepath"
    25  
    26  	analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
    27  	"github.com/replicatedhq/troubleshoot/pkg/collect"
    28  
    29  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    30  	"github.com/1aal/kubeblocks/pkg/cli/preflight/util"
    31  )
    32  
    33  func GetClusterVersionPath() string {
    34  	// hardcode ClusterVersionPath will make preflight fail in windows
    35  	// due to the correct path is "cluster-info\cluster_version.json" in windows
    36  	return filepath.Join("cluster-info", "cluster_version.json")
    37  }
    38  
    39  type AnalyzeClusterAccess struct {
    40  	analyzer *preflightv1beta2.ClusterAccessAnalyze
    41  }
    42  
    43  func (a *AnalyzeClusterAccess) Title() string {
    44  	return util.TitleOrDefault(a.analyzer.AnalyzeMeta, "Cluster Access")
    45  }
    46  
    47  func (a *AnalyzeClusterAccess) IsExcluded() (bool, error) {
    48  	return util.IsExcluded(a.analyzer.Exclude)
    49  }
    50  
    51  func (a *AnalyzeClusterAccess) Analyze(getFile GetCollectedFileContents, findFiles GetChildCollectedFileContents) ([]*analyze.AnalyzeResult, error) {
    52  	isAccess := true
    53  	collected, err := getFile(GetClusterVersionPath())
    54  	if err != nil {
    55  		isAccess = false
    56  	} else {
    57  		if err := json.Unmarshal(collected, &collect.ClusterVersion{}); err != nil {
    58  			isAccess = false
    59  		}
    60  	}
    61  
    62  	result := analyze.AnalyzeResult{
    63  		Title: a.Title(),
    64  	}
    65  	for _, outcome := range a.analyzer.Outcomes {
    66  		if outcome.Fail != nil && !isAccess {
    67  			result.IsFail = true
    68  			result.Message = outcome.Fail.Message
    69  			result.URI = outcome.Fail.URI
    70  		} else if outcome.Pass != nil && isAccess {
    71  			result.IsPass = true
    72  			result.Message = outcome.Pass.Message
    73  			result.URI = outcome.Pass.URI
    74  		}
    75  	}
    76  	result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
    77  	return []*analyze.AnalyzeResult{&result}, nil
    78  }