karmadactl init: add CRDs archive verification to enhance file system robustness
Signed-off-by: zhzhuang-zju <m17799853869@163.com>
This commit is contained in:
parent
f78e7e2a3d
commit
b7afcaf5a2
|
@ -23,6 +23,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ import (
|
||||||
globaloptions "github.com/karmada-io/karmada/pkg/karmadactl/options"
|
globaloptions "github.com/karmada-io/karmada/pkg/karmadactl/options"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/util/apiclient"
|
"github.com/karmada-io/karmada/pkg/karmadactl/util/apiclient"
|
||||||
|
"github.com/karmada-io/karmada/pkg/util/validation"
|
||||||
"github.com/karmada-io/karmada/pkg/version"
|
"github.com/karmada-io/karmada/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -381,19 +383,34 @@ func (i *CommandInitOption) genCerts() error {
|
||||||
|
|
||||||
// prepareCRD download or unzip `crds.tar.gz` to `options.DataPath`
|
// prepareCRD download or unzip `crds.tar.gz` to `options.DataPath`
|
||||||
func (i *CommandInitOption) prepareCRD() error {
|
func (i *CommandInitOption) prepareCRD() error {
|
||||||
|
var filename string
|
||||||
if strings.HasPrefix(i.CRDs, "http") {
|
if strings.HasPrefix(i.CRDs, "http") {
|
||||||
filename := i.KarmadaDataPath + "/" + path.Base(i.CRDs)
|
filename = i.KarmadaDataPath + "/" + path.Base(i.CRDs)
|
||||||
klog.Infof("download crds file:%s", i.CRDs)
|
klog.Infof("download crds file:%s", i.CRDs)
|
||||||
if err := utils.DownloadFile(i.CRDs, filename); err != nil {
|
if err := utils.DownloadFile(i.CRDs, filename); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := utils.DeCompress(filename, i.KarmadaDataPath); err != nil {
|
} else {
|
||||||
return err
|
filename = i.CRDs
|
||||||
}
|
klog.Infoln("local crds file name:", i.CRDs)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
klog.Infoln("local crds file name:", i.CRDs)
|
|
||||||
return utils.DeCompress(i.CRDs, i.KarmadaDataPath)
|
if err := validation.ValidateTarball(filename, validation.ValidateCrdsTarBall); err != nil {
|
||||||
|
return fmt.Errorf("inValid crd tar, err: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := utils.DeCompress(filename, i.KarmadaDataPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, archive := range validation.CrdsArchive {
|
||||||
|
expectedDir := filepath.Join(i.KarmadaDataPath, archive)
|
||||||
|
exist, _ := utils.PathExists(expectedDir)
|
||||||
|
if !exist {
|
||||||
|
return fmt.Errorf("lacking the necessary file path: %s", expectedDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *CommandInitOption) createCertsSecrets() error {
|
func (i *CommandInitOption) createCertsSecrets() error {
|
||||||
|
|
|
@ -157,3 +157,17 @@ func ListFiles(path string) []string {
|
||||||
}
|
}
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PathExists check whether the path is exist
|
||||||
|
func PathExists(path string) (bool, error) {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
|
@ -721,7 +721,7 @@ func TestValidateApplicationFailover(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckOperatorCrdsTar(t *testing.T) {
|
func TestValidateCrdsTarBall(t *testing.T) {
|
||||||
testItems := []struct {
|
testItems := []struct {
|
||||||
name string
|
name string
|
||||||
header *tar.Header
|
header *tar.Header
|
||||||
|
|
Loading…
Reference in New Issue