github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/config/pre_process.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 config
    16  
    17  import (
    18  	"encoding/base64"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/sirupsen/logrus"
    23  	yaml2 "gopkg.in/yaml.v2"
    24  	"sigs.k8s.io/yaml"
    25  
    26  	v1 "github.com/sealerio/sealer/types/api/v1"
    27  )
    28  
    29  const (
    30  	valueProcessorName    = "value"
    31  	toJSONProcessorName   = "toJson"
    32  	toBase64ProcessorName = "toBase64"
    33  	toSecretProcessorName = "toSecret"
    34  	trueLabelValue        = "true"
    35  	trueLabelKey          = "preprocess.value"
    36  )
    37  
    38  type PreProcessor interface {
    39  	Process(config *v1.Config) error
    40  }
    41  
    42  func NewProcessorsAndRun(config *v1.Config) error {
    43  	pMap := map[string]PreProcessor{
    44  		valueProcessorName:    &valueProcessor{},
    45  		toJSONProcessorName:   &toJSONProcessor{},
    46  		toBase64ProcessorName: &toBase64Processor{},
    47  		toSecretProcessorName: nil,
    48  	}
    49  
    50  	processors := strings.Split(config.Spec.Process, "|")
    51  	for _, pName := range processors {
    52  		if pName == "" {
    53  			continue
    54  		}
    55  		processor, ok := pMap[pName]
    56  		if !ok {
    57  			logrus.Warnf("not found config processor: %s", pName)
    58  			continue
    59  		}
    60  		if processor == nil {
    61  			continue
    62  		}
    63  		if err := processor.Process(config); err != nil {
    64  			return err
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  type valueProcessor struct{}
    72  
    73  func (v valueProcessor) Process(config *v1.Config) error {
    74  	config.Labels = make(map[string]string)
    75  	config.Labels[trueLabelKey] = trueLabelValue
    76  	return nil
    77  }
    78  
    79  type toJSONProcessor struct{}
    80  
    81  func (t toJSONProcessor) Process(config *v1.Config) error {
    82  	if v, ok := config.Labels[trueLabelKey]; !ok || v != trueLabelValue {
    83  		json, err := yaml.YAMLToJSON([]byte(config.Spec.Data))
    84  		if err != nil {
    85  			return fmt.Errorf("failed to resolve config data to json: %v", err)
    86  		}
    87  		config.Spec.Data = string(json)
    88  		return nil
    89  	}
    90  
    91  	dataMap := make(map[string]interface{})
    92  	err := yaml.Unmarshal([]byte(config.Spec.Data), &dataMap)
    93  	if err != nil {
    94  		return fmt.Errorf("failed to convert yaml data to map: %v", err)
    95  	}
    96  
    97  	for k, v := range dataMap {
    98  		data, err := yaml.Marshal(v)
    99  		if err != nil {
   100  			return fmt.Errorf("failed to encode yaml: %v", err)
   101  		}
   102  
   103  		bytes, err := yaml.YAMLToJSON(data)
   104  		if err != nil {
   105  			return fmt.Errorf("toJson: failed to convert yaml to json, key is %s: %v", k, err)
   106  		}
   107  		dataMap[k] = string(bytes)
   108  	}
   109  
   110  	data, err := yaml2.Marshal(dataMap)
   111  	if err != nil {
   112  		return fmt.Errorf("failed to convert data map(%v): %v", dataMap, err)
   113  	}
   114  	config.Spec.Data = string(data)
   115  
   116  	return nil
   117  }
   118  
   119  type toBase64Processor struct{}
   120  
   121  func (t toBase64Processor) Process(config *v1.Config) error {
   122  	if v, ok := config.Labels[trueLabelKey]; !ok || v != trueLabelValue {
   123  		config.Spec.Data = base64.StdEncoding.EncodeToString([]byte(config.Spec.Data))
   124  		return nil
   125  	}
   126  
   127  	dataMap := make(map[string]string)
   128  	err := yaml.Unmarshal([]byte(config.Spec.Data), &dataMap)
   129  	if err != nil {
   130  		return fmt.Errorf("tobase64: failed to convert yaml data to map: %v", err)
   131  	}
   132  
   133  	for k, v := range dataMap {
   134  		dataMap[k] = base64.StdEncoding.EncodeToString([]byte(v))
   135  	}
   136  	bs, err := yaml.Marshal(dataMap)
   137  	if err != nil {
   138  		return fmt.Errorf("failed to convert base64 to yaml: %v", err)
   139  	}
   140  
   141  	config.Spec.Data = string(bs)
   142  
   143  	return nil
   144  }