Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
  • Loading branch information
sergenyalcin committed Nov 1, 2022
1 parent 6929e31 commit af088a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
46 changes: 22 additions & 24 deletions pkg/migration/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package migration
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -40,45 +39,44 @@ func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSyste
f(fs)
}

files, err := fs.afero.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, fmt.Sprintf("cannot read %s", path))
}

for _, file := range files {
if file.IsDir() {
continue
if info.IsDir() {
return nil
}
data, err := fs.afero.ReadFile(filepath.Join(dir, file.Name()))

data, err := fs.afero.ReadFile(path)
if err != nil {
return nil, err
return errors.Wrap(err, "cannot read source file")
}

decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(string(data)), 1024)
u := &unstructured.Unstructured{}
if err := decoder.Decode(&u); err != nil {
if err != nil {
return nil, err
}
return errors.Wrap(err, "cannot decode read data")
}

fs.items = append(fs.items, UnstructuredWithMetadata{
Object: *u,
Metadata: Metadata{
Path: filepath.Join(dir, file.Name()),
Path: path,
},
})

return nil
}); err != nil {
return nil, errors.Wrap(err, "cannot read source directory")
}

return fs, nil
}

// HasNext checks the next item
func (fs *FileSystemSource) HasNext() (bool, error) {
if fs.index < len(fs.items) {
return true, nil
}
return false, nil
return fs.index < len(fs.items), nil
}

// Next returns the next item of slice
Expand All @@ -88,7 +86,7 @@ func (fs *FileSystemSource) Next() (UnstructuredWithMetadata, error) {
fs.index++
return item, nil
}
return UnstructuredWithMetadata{}, errors.New("failed to get next element")
return UnstructuredWithMetadata{}, errors.New("no more elements")
}

// FileSystemTarget is a target implementation to write/patch/delete resources to file system
Expand Down Expand Up @@ -121,26 +119,26 @@ func NewFileSystemTarget(opts ...FileSystemTargetOption) *FileSystemTarget {
func (ft *FileSystemTarget) Put(o UnstructuredWithMetadata) error {
b, err := sigsyaml.Marshal(o.Object.Object)
if err != nil {
return err
return errors.Wrap(err, "cannot marshal object")
}
if o.Metadata.Parents != "" {
f, err := ft.afero.OpenFile(o.Metadata.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
return errors.Wrap(err, "cannot open file")
}

defer f.Close() //nolint:errcheck

if _, err = f.WriteString(fmt.Sprintf("\n---\n\n%s", string(b))); err != nil {
return err
return errors.Wrap(err, "cannot write file")
}
} else {
f, err := ft.afero.Create(o.Metadata.Path)
if err != nil {
return err
return errors.Wrap(err, "cannot create file")
}
if _, err := f.Write(b); err != nil {
return err
return errors.Wrap(err, "cannot write file")
}
}

Expand Down
20 changes: 12 additions & 8 deletions pkg/migration/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ type KubernetesSource struct {
}

// NewKubernetesSource returns a KubernetesSource
// DynamicClient is used here to query resources.
// Elements of gvks (slice of GroupVersionKind) are passed to the Dynamic Client
// in a loop to get list of resources.
// An example element of gvks slice:
// Group: "ec2.aws.upbound.io",
// Version: "v1beta1",
// Kind: "VPC",
func NewKubernetesSource(dynamicClient dynamic.Interface, gvks []schema.GroupVersionKind) (*KubernetesSource, error) {
ks := &KubernetesSource{
dynamicClient: dynamicClient,
Expand All @@ -34,7 +41,7 @@ func NewKubernetesSource(dynamicClient dynamic.Interface, gvks []schema.GroupVer
})
unstructuredList, err := ri.List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cannot list resources")
}
for _, u := range unstructuredList.Items {
ks.items = append(ks.items, UnstructuredWithMetadata{
Expand All @@ -50,10 +57,7 @@ func NewKubernetesSource(dynamicClient dynamic.Interface, gvks []schema.GroupVer

// HasNext checks the next item
func (ks *KubernetesSource) HasNext() (bool, error) {
if ks.index < len(ks.items) {
return true, nil
}
return false, nil
return ks.index < len(ks.items), nil
}

// Next returns the next item of slice
Expand All @@ -63,18 +67,18 @@ func (ks *KubernetesSource) Next() (UnstructuredWithMetadata, error) {
ks.index++
return item, nil
}
return UnstructuredWithMetadata{}, errors.New("failed to get next element")
return UnstructuredWithMetadata{}, errors.New("no more elements")
}

// InitializeDynamicClient returns a dynamic client
func InitializeDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cannot create rest config object")
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cannot initialize dynamic client")
}
return dynamicClient, nil
}

0 comments on commit af088a6

Please sign in to comment.