github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/load_spec.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 preflight 21 22 import ( 23 "os" 24 25 "github.com/pkg/errors" 26 "k8s.io/client-go/kubernetes/scheme" 27 28 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 29 "github.com/1aal/kubeblocks/pkg/cli/cmd/cluster" 30 "github.com/1aal/kubeblocks/pkg/cli/preflight/util" 31 ) 32 33 // LoadPreflightSpec loads content of preflightSpec and hostPreflightSpec against yamlFiles from args 34 func LoadPreflightSpec(checkFileList []string, checkYamlData [][]byte) (*preflightv1beta2.Preflight, *preflightv1beta2.HostPreflight, string, error) { 35 var ( 36 preflightSpec *preflightv1beta2.Preflight 37 hostPreflightSpec *preflightv1beta2.HostPreflight 38 preflightContent []byte 39 preflightName string 40 err error 41 ) 42 for _, fileName := range checkFileList { 43 // support to load yaml from stdin, local file and URI 44 if preflightContent, err = cluster.MultipleSourceComponents(fileName, os.Stdin); err != nil { 45 return preflightSpec, hostPreflightSpec, preflightName, err 46 } 47 checkYamlData = append(checkYamlData, preflightContent) 48 } 49 for _, yamlData := range checkYamlData { 50 obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(yamlData, nil, nil) 51 if err != nil { 52 return preflightSpec, hostPreflightSpec, preflightName, errors.Wrapf(err, "failed to parse %s", string(yamlData)) 53 } 54 if spec, ok := obj.(*preflightv1beta2.Preflight); ok { 55 preflightSpec = ConcatPreflightSpec(preflightSpec, spec) 56 preflightName = preflightSpec.Name 57 } else if spec, ok := obj.(*preflightv1beta2.HostPreflight); ok { 58 hostPreflightSpec = ConcatHostPreflightSpec(hostPreflightSpec, spec) 59 preflightName = hostPreflightSpec.Name 60 } 61 } 62 return preflightSpec, hostPreflightSpec, preflightName, nil 63 } 64 65 func init() { 66 // register the scheme of troubleshoot API and decode function 67 if err := util.AddToScheme(scheme.Scheme); err != nil { 68 return 69 } 70 }