...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/helm/chart.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/helm

     1  // Copyright 2022 Chaos Mesh Authors.
     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  
    16  package helm
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"io"
    22  	"net/http"
    23  	"os"
    24  
    25  	"github.com/pkg/errors"
    26  	"helm.sh/helm/v3/pkg/chart"
    27  	"helm.sh/helm/v3/pkg/chart/loader"
    28  )
    29  
    30  const ChaosMeshHelmRepo = "https://charts.chaos-mesh.org"
    31  
    32  func FetchChaosMeshChart(ctx context.Context, version, local string) (*chart.Chart, error) {
    33  	var (
    34  		tgzPath string
    35  		err     error
    36  	)
    37  	if local != "" {
    38  		if tgzPath, err = GetChaosMeshChartTgzPath(ctx, version, local); err != nil {
    39  			return nil, err
    40  		}
    41  	} else {
    42  		if tgzPath, err = DownloadChaosMeshChartTgz(ctx, version); err != nil {
    43  			return nil, err
    44  		}
    45  	}
    46  
    47  	requestedChart, err := loader.Load(tgzPath)
    48  	if err != nil {
    49  		return nil, errors.Wrapf(err, "load helm chart from %s", tgzPath)
    50  	}
    51  	return requestedChart, nil
    52  }
    53  
    54  func GetChaosMeshChartTgzPath(ctx context.Context, version, local string) (string, error) {
    55  	fileName := fmt.Sprintf("chaos-mesh-%s.tgz", version)
    56  	tgzPath := fmt.Sprintf("%s/%s", os.TempDir(), fileName)
    57  	if local != "" {
    58  		tgzPath = fmt.Sprintf("%s/%s", local, fileName)
    59  	}
    60  
    61  	if _, err := os.Stat(tgzPath); err != nil {
    62  		return "", err
    63  	}
    64  	return tgzPath, nil
    65  }
    66  
    67  func DownloadChaosMeshChartTgz(ctx context.Context, version string) (string, error) {
    68  	url := fmt.Sprintf("%s/chaos-mesh-%s.tgz", ChaosMeshHelmRepo, version)
    69  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
    70  	if err != nil {
    71  		return "", errors.Wrapf(err, "failed to generate http request for url %s", url)
    72  	}
    73  	response, err := http.DefaultClient.Do(req)
    74  	if err != nil {
    75  		return "", errors.Wrapf(err, "download helm chart from %s", url)
    76  	}
    77  	defer response.Body.Close()
    78  
    79  	if response.StatusCode != http.StatusOK {
    80  		return "", errors.Wrapf(err, "download helm chart from %s", url)
    81  	}
    82  	target, err := os.CreateTemp("", fmt.Sprintf("chaos-mesh-%s-*.tgz", version))
    83  	if err != nil {
    84  		return "", errors.Wrapf(err, "download helm chart as temp file")
    85  	}
    86  	defer target.Close()
    87  	_, err = io.Copy(target, response.Body)
    88  	if err != nil {
    89  		return "", errors.Wrapf(err, "download helm chart content")
    90  	}
    91  	return target.Name(), nil
    92  }
    93