Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

karmadactl init: add CRDs archive verification to enhance file system robustness #5713

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions pkg/karmadactl/cmdinit/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand All @@ -43,6 +44,7 @@ import (
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/apiclient"
"github.com/karmada-io/karmada/pkg/util/validation"
"github.com/karmada-io/karmada/pkg/version"
)

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

// prepareCRD download or unzip `crds.tar.gz` to `options.DataPath`
func (i *CommandInitOption) prepareCRD() error {
var filename string
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)
if err := utils.DownloadFile(i.CRDs, filename); err != nil {
return err
}
if err := utils.DeCompress(filename, i.KarmadaDataPath); err != nil {
return err
} else {
filename = i.CRDs
klog.Infoln("local crds file name:", i.CRDs)
}

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
}
klog.Infoln("local crds file name:", i.CRDs)
return utils.DeCompress(i.CRDs, i.KarmadaDataPath)
return nil
}

func (i *CommandInitOption) createCertsSecrets() error {
Expand Down
14 changes: 14 additions & 0 deletions pkg/karmadactl/cmdinit/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,17 @@ func ListFiles(path string) []string {
}
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
}
2 changes: 1 addition & 1 deletion pkg/util/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ func TestValidateApplicationFailover(t *testing.T) {
}
}

func TestCheckOperatorCrdsTar(t *testing.T) {
func TestValidateCrdsTarBall(t *testing.T) {
testItems := []struct {
name string
header *tar.Header
Expand Down