github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/desired/desired.go (about)

     1  package desired
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	yamlfile "github.com/caos/orbos/internal/utils/yaml"
    10  	"gopkg.in/yaml.v3"
    11  
    12  	"github.com/caos/orbos/internal/operator/boom/labels"
    13  	"github.com/caos/orbos/internal/operator/boom/name"
    14  	"github.com/caos/orbos/internal/utils/helper"
    15  	"github.com/caos/orbos/internal/utils/kustomize"
    16  	"github.com/caos/orbos/mntr"
    17  )
    18  
    19  func Apply(monitor mntr.Monitor, resultFilePath, namespace string, appName name.Application, force bool) error {
    20  	resultFileDirPath := filepath.Dir(resultFilePath)
    21  
    22  	if err := prepareAdditionalFiles(resultFilePath, namespace, appName); err != nil {
    23  		return err
    24  	}
    25  
    26  	// apply resources
    27  	cmd, err := kustomize.New(resultFileDirPath)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	cmd = cmd.Apply(force)
    32  	err = helper.Run(monitor, cmd.Build())
    33  	if err != nil {
    34  		return fmt.Errorf("failed to apply with file %s: %w", resultFilePath, err)
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func Get(monitor mntr.Monitor, resultFilePath, namespace string, appName name.Application) ([]*helper.Resource, error) {
    41  	resultFileDirPath := filepath.Dir(resultFilePath)
    42  
    43  	if err := prepareAdditionalFiles(resultFilePath, namespace, appName); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	// apply resources
    48  	cmd, err := kustomize.New(resultFileDirPath)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	out, err := helper.RunWithOutput(monitor, cmd.Build())
    54  	if err != nil {
    55  		return nil, fmt.Errorf("failed to apply with file %s: %w", resultFilePath, err)
    56  	}
    57  
    58  	resources := make([]*helper.Resource, 0)
    59  
    60  	parts := strings.Split(string(out), "\n---\n")
    61  	for _, part := range parts {
    62  		if part == "" {
    63  			continue
    64  		}
    65  		var resource helper.Resource
    66  
    67  		if err := yaml.Unmarshal([]byte(part), &resource); err != nil {
    68  			return nil, err
    69  		}
    70  
    71  		resources = append(resources, &resource)
    72  	}
    73  
    74  	return resources, nil
    75  }
    76  
    77  func prepareAdditionalFiles(resultFilePath, namespace string, appName name.Application) error {
    78  	resultFileDirPath := filepath.Dir(resultFilePath)
    79  
    80  	resultFileKustomizePath := filepath.Join(resultFileDirPath, "kustomization.yaml")
    81  	resultFileTransformerPath := filepath.Join(resultFileDirPath, "transformer.yaml")
    82  
    83  	if helper.FileExists(resultFileKustomizePath) {
    84  		if err := os.Remove(resultFileKustomizePath); err != nil {
    85  			return err
    86  		}
    87  	}
    88  
    89  	if helper.FileExists(resultFileTransformerPath) {
    90  		if err := os.Remove(resultFileTransformerPath); err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	transformer := &kustomize.LabelTransformer{
    96  		ApiVersion: "builtin",
    97  		Kind:       "LabelTransformer",
    98  		Metadata: &kustomize.Metadata{
    99  			Name: "LabelTransformer",
   100  		},
   101  		Labels:     labels.GetAllApplicationLabels(appName),
   102  		FieldSpecs: []*kustomize.FieldSpec{&kustomize.FieldSpec{Path: "metadata/labels", Create: true}},
   103  	}
   104  	if err := yamlfile.New(resultFileTransformerPath).AddStruct(transformer); err != nil {
   105  		return err
   106  	}
   107  
   108  	kustomizeFile := kustomize.File{
   109  		Namespace:    namespace,
   110  		Resources:    []string{filepath.Base(resultFilePath)},
   111  		Transformers: []string{filepath.Base(resultFileTransformerPath)},
   112  	}
   113  
   114  	if err := yamlfile.New(resultFileKustomizePath).AddStruct(kustomizeFile); err != nil {
   115  		return err
   116  	}
   117  	return nil
   118  }