github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/builder/template/helm_helper.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 template
    21  
    22  import (
    23  	"bufio"
    24  	"bytes"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	corev1 "k8s.io/api/core/v1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/apimachinery/pkg/util/yaml"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  
    33  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    34  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    35  	"github.com/1aal/kubeblocks/pkg/generics"
    36  )
    37  
    38  func scanDirectoryPath(rootPath string) ([]string, error) {
    39  	dirs, err := os.ReadDir(rootPath)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	resourceList := make([]string, 0)
    44  	for _, d := range dirs {
    45  		if d.IsDir() {
    46  			subDirectory, err := scanDirectoryPath(filepath.Join(rootPath, d.Name()))
    47  			if err != nil {
    48  				return nil, err
    49  			}
    50  			resourceList = append(resourceList, subDirectory...)
    51  			continue
    52  		}
    53  		if filepath.Ext(d.Name()) != ".yaml" {
    54  			continue
    55  		}
    56  		resourceList = append(resourceList, filepath.Join(rootPath, d.Name()))
    57  	}
    58  	return resourceList, nil
    59  }
    60  
    61  func getResourceMeta(yamlBytes []byte) (metav1.TypeMeta, error) {
    62  	type k8sObj struct {
    63  		metav1.TypeMeta `json:",inline"`
    64  	}
    65  	var o k8sObj
    66  	err := yaml.Unmarshal(yamlBytes, &o)
    67  	if err != nil {
    68  		return metav1.TypeMeta{}, err
    69  	}
    70  	return o.TypeMeta, nil
    71  }
    72  
    73  func CreateObjectsFromDirectory(rootPath string) ([]client.Object, error) {
    74  	allObjs := make([]client.Object, 0)
    75  
    76  	// create cr from yaml
    77  	resourceList, err := scanDirectoryPath(rootPath)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	for _, resourceFile := range resourceList {
    82  		yamlBytes, err := os.ReadFile(resourceFile)
    83  		if err != nil {
    84  			return nil, err
    85  		}
    86  		objects, err := createObjectsFromYaml(yamlBytes)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		allObjs = append(allObjs, objects...)
    91  	}
    92  	return allObjs, nil
    93  }
    94  
    95  func createObjectsFromYaml(yamlBytes []byte) ([]client.Object, error) {
    96  	objects := make([]client.Object, 0)
    97  	reader := bufio.NewReader(bytes.NewReader(yamlBytes))
    98  	for {
    99  		doc, err := yaml.NewYAMLReader(reader).Read()
   100  		if len(doc) == 0 {
   101  			break
   102  		}
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  		meta, err := getResourceMeta(doc)
   107  		if err != nil {
   108  			return nil, err
   109  		}
   110  		switch meta.Kind {
   111  		case kindFromResource(corev1.ConfigMap{}):
   112  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.ConfigMapSignature))
   113  		case kindFromResource(corev1.Secret{}):
   114  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.SecretSignature))
   115  		case kindFromResource(appsv1alpha1.ConfigConstraint{}):
   116  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.ConfigConstraintSignature))
   117  		case kindFromResource(appsv1alpha1.ClusterDefinition{}):
   118  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.ClusterDefinitionSignature))
   119  		case kindFromResource(appsv1alpha1.ClusterVersion{}):
   120  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.ClusterVersionSignature))
   121  		case kindFromResource(appsv1alpha1.BackupPolicyTemplate{}):
   122  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.BackupPolicyTemplateSignature))
   123  		case kindFromResource(dpv1alpha1.ActionSet{}):
   124  			objects = append(objects, CreateTypedObjectFromYamlByte(doc, generics.ActionSetSignature))
   125  		}
   126  	}
   127  	return objects, nil
   128  }