Skip to content

Commit

Permalink
Merge pull request #947 from wzshiming/feat/compress-yaml
Browse files Browse the repository at this point in the history
Support compress snapshot yaml
  • Loading branch information
wzshiming authored Feb 1, 2024
2 parents 438865f + 9bde4bb commit 622224c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
23 changes: 14 additions & 9 deletions pkg/kwokctl/cmd/snapshot/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ func runE(ctx context.Context, flags *flagpole) error {
return err
}

f, err := file.Open(flags.Path)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()

logger := log.FromContext(ctx)

pagerConfig := &snapshot.PagerConfig{
Expand Down Expand Up @@ -130,7 +122,20 @@ func runE(ctx context.Context, flags *flagpole) error {
return err
}

var writer io.Writer = f
f, err := file.Open(flags.Path)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()

press := file.Compress(flags.Path, f)
defer func() {
_ = press.Close()
}()

var writer io.Writer = press

startTime := time.Now()
writer = recording.NewWriteHook(writer, func(b []byte) []byte {
Expand Down
7 changes: 6 additions & 1 deletion pkg/kwokctl/cmd/snapshot/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func runE(ctx context.Context, flags *flagpole) error {
_ = f.Close()
}()

var writer io.Writer = f
press := file.Compress(flags.Path, f)
defer func() {
_ = press.Close()
}()

var writer io.Writer = press

startTime := time.Now()
writer = recording.NewWriteHook(writer, func(bytes []byte) []byte {
Expand Down
10 changes: 9 additions & 1 deletion pkg/kwokctl/cmd/snapshot/replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ func runE(ctx context.Context, flags *flagpole) error {
_ = f.Close()
}()

var reader io.Reader = f
press, err := file.Decompress(flags.Path, f)
if err != nil {
return err
}
defer func() {
_ = press.Close()
}()

var reader io.Reader = press

startTime := time.Now()
reader = recording.NewReadHook(reader, func(bytes []byte) []byte {
Expand Down
74 changes: 74 additions & 0 deletions pkg/utils/file/compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package file

import (
"bufio"
"compress/gzip"
"io"
"path/filepath"
)

// Compress compresses a writer
func Compress(name string, w io.Writer) io.WriteCloser {
ext := filepath.Ext(name)
switch ext {
case ".gz", ".tgz":
z, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
return z
}

return struct {
io.Writer
io.Closer
}{
Writer: w,
Closer: nopCloser{},
}
}

type nopCloser struct{}

func (nopCloser) Close() error { return nil }

const (
gzipID1 = 0x1f
gzipID2 = 0x8b
)

// Decompress decompresses a reader
// It will return a raw reader if the reader is not compressed
func Decompress(name string, r io.Reader) (io.ReadCloser, error) {
ext := filepath.Ext(name)
switch ext {
case ".gz", ".tgz":
bufReader := bufio.NewReader(r)

prefix, err := bufReader.Peek(2)
if err != nil {
return nil, err
}

if prefix[0] == gzipID1 && prefix[1] == gzipID2 {
return gzip.NewReader(bufReader)
}

return io.NopCloser(bufReader), nil
}

return io.NopCloser(r), nil
}

0 comments on commit 622224c

Please sign in to comment.