1.Error:gzip: invalid header?
error背景:开发语言golang,使用helm.sh/helm/v3/pkg/action-Install sdk部署应用
错误原因:加载helmchart使用了loader.LoadArchive,此方法支持解压加载tgz格式的helmchart,当helmchart为zip、gz等格式时会报错
error:gzip: invalid header
2.解决方案
先解压 压缩包,解压方式根据压缩格式而定,newPackage.Run(filePath1, nil)传入解压后的chart路径,再使用使用 loader.Load加载获取helmchart
压缩包解压方式:
func (base *Compressed) UnpackageCmds(source, destination, fileType string) (err error) {
switch fileType {
case "zip":
cmd := exec.Command("unzip", "-o", source, "-d", destination)
fmt.Println("解包命令为:", cmd)
var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
if err := cmd.Run(); err != nil {
err = errors.New(fmt.Sprintf("打包错误:%s", cmd.Stderr))
return err
}
case "gz":
cmd := exec.Command("tar", "-xzvf", source, "-C", destination)
// cmd := exec.Command("tar", "-xzvf", source, "-C", destination)
fmt.Println("解包命令为:", cmd)
var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
if err := cmd.Run(); err != nil {
err = errors.New(fmt.Sprintf("打包错误:%s\nwindows环境打包推荐格式:zip", cmd.Stderr))
return err
}
case "tgz":
// cmd := exec.Command("tar", "-xzvf", source, "-C", destination)
cmd := exec.Command("tar", "-xzvf", source, "-C", destination)
fmt.Println("解包命令为:", cmd)
var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
if err := cmd.Run(); err != nil {
err = errors.New(fmt.Sprintf("打包错误:%s\nwindows环境打包推荐格式:zip", cmd.Stderr))
return err
}
}
return
}
chart加载demo:
newPackage := action.NewPackage()
chartPath, err := newPackage.Run(filePath1, nil)
if err != nil {
conf.Logger.Error("获取chart文件失败", zap.Error(err))
err = res.InternalServerError.ErrorDetail(err)
NewProPubMsg(PullChartType, "error", fmt.Sprintf("获取chart文件%s失败 \n Error:%s", applicationDeployInfo.ChartName, err.Error()), base.User.SystemId)
return
}
compressed.ChartPath = chartPath
chart, err := loader.FileLoader(chartPath).Load()
if err != nil {
conf.Logger.Error("加载chart文件失败", zap.Error(err))
err = res.InternalServerError.ErrorDetail(err)
NewProPubMsg(PullChartType, "error", fmt.Sprintf("加载chart文件%s失败 \n Error:%s", applicationDeployInfo.ChartName, err.Error()), base.User.SystemId)
return
}
3. 总结
如果chart包固定格式为tgz,则可使用loader.LoadArchive直接加载压缩包,自动解压获取chart文件,如果存在其他格式压缩包,则如上2步,先执行解压,再使用.Load()加载获取helmchart。