Skip to content

Commit 40ec488

Browse files
authored
Merge pull request #5713 from zhzhuang-zju/ctl-crds
karmadactl init: add CRDs archive verification to enhance file system robustness
2 parents f78e7e2 + b7afcaf commit 40ec488

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

pkg/karmadactl/cmdinit/kubernetes/deploy.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"os"
2525
"path"
26+
"path/filepath"
2627
"strings"
2728
"time"
2829

@@ -43,6 +44,7 @@ import (
4344
globaloptions "github.com/karmada-io/karmada/pkg/karmadactl/options"
4445
"github.com/karmada-io/karmada/pkg/karmadactl/util"
4546
"github.com/karmada-io/karmada/pkg/karmadactl/util/apiclient"
47+
"github.com/karmada-io/karmada/pkg/util/validation"
4648
"github.com/karmada-io/karmada/pkg/version"
4749
)
4850

@@ -381,19 +383,34 @@ func (i *CommandInitOption) genCerts() error {
381383

382384
// prepareCRD download or unzip `crds.tar.gz` to `options.DataPath`
383385
func (i *CommandInitOption) prepareCRD() error {
386+
var filename string
384387
if strings.HasPrefix(i.CRDs, "http") {
385-
filename := i.KarmadaDataPath + "/" + path.Base(i.CRDs)
388+
filename = i.KarmadaDataPath + "/" + path.Base(i.CRDs)
386389
klog.Infof("download crds file:%s", i.CRDs)
387390
if err := utils.DownloadFile(i.CRDs, filename); err != nil {
388391
return err
389392
}
390-
if err := utils.DeCompress(filename, i.KarmadaDataPath); err != nil {
391-
return err
393+
} else {
394+
filename = i.CRDs
395+
klog.Infoln("local crds file name:", i.CRDs)
396+
}
397+
398+
if err := validation.ValidateTarball(filename, validation.ValidateCrdsTarBall); err != nil {
399+
return fmt.Errorf("inValid crd tar, err: %w", err)
400+
}
401+
402+
if err := utils.DeCompress(filename, i.KarmadaDataPath); err != nil {
403+
return err
404+
}
405+
406+
for _, archive := range validation.CrdsArchive {
407+
expectedDir := filepath.Join(i.KarmadaDataPath, archive)
408+
exist, _ := utils.PathExists(expectedDir)
409+
if !exist {
410+
return fmt.Errorf("lacking the necessary file path: %s", expectedDir)
392411
}
393-
return nil
394412
}
395-
klog.Infoln("local crds file name:", i.CRDs)
396-
return utils.DeCompress(i.CRDs, i.KarmadaDataPath)
413+
return nil
397414
}
398415

399416
func (i *CommandInitOption) createCertsSecrets() error {

pkg/karmadactl/cmdinit/utils/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,17 @@ func ListFiles(path string) []string {
157157
}
158158
return files
159159
}
160+
161+
// PathExists check whether the path is exist
162+
func PathExists(path string) (bool, error) {
163+
_, err := os.Stat(path)
164+
if err == nil {
165+
return true, nil
166+
}
167+
168+
if os.IsNotExist(err) {
169+
return false, nil
170+
}
171+
172+
return false, err
173+
}

pkg/util/validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ func TestValidateApplicationFailover(t *testing.T) {
721721
}
722722
}
723723

724-
func TestCheckOperatorCrdsTar(t *testing.T) {
724+
func TestValidateCrdsTarBall(t *testing.T) {
725725
testItems := []struct {
726726
name string
727727
header *tar.Header

0 commit comments

Comments
 (0)