Skip to content

Commit

Permalink
Extend YAML processing to handle Machines
Browse files Browse the repository at this point in the history
machines.yaml can now contain standard Machines objects
as well as MachineLists

clusterctl will merge these all into a final slice of Machines

Signed-off-by: Naadir Jeewa <[email protected]>
  • Loading branch information
randomvariable committed Jan 28, 2019
1 parent ba2610b commit 436dbf3
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"k8s.io/apimachinery/pkg/util/json"
"math/rand"
Expand Down Expand Up @@ -188,7 +189,7 @@ func ParseClusterYaml(file string) (*clusterv1.Cluster, error) {

var cluster clusterv1.Cluster

err = json.Unmarshal(bytes, &cluster)
err = json.Unmarshal(bytes[0], &cluster)

if err != nil {
return nil, err
Expand All @@ -198,42 +199,80 @@ func ParseClusterYaml(file string) (*clusterv1.Cluster, error) {
}

func ParseMachinesYaml(file string) ([]*clusterv1.Machine, error) {
bytes, err := FindGVKInFile(file, "MachineList")
bytes, err := FindGVKInFile(file, "Machine")
if err != nil {
return nil, err
}

var list clusterv1.MachineList
machines := []clusterv1.Machine{}

for _, m := range bytes {
var machine clusterv1.Machine
err = json.Unmarshal(m, &machine)
if err != nil {
return nil, err
}
machines = append(machines, machine)
}

machinesP := MachineP(machines)
machineList, err := ParseMachineListYaml(file)

if err != nil {
return nil, err
}

err = json.Unmarshal(bytes, &list)
return append(machinesP, machineList...), nil
}

func ParseMachineListYaml(file string) ([]*clusterv1.Machine, error) {
bytes, err := FindGVKInFile(file, "MachineList")
if err != nil {
return nil, err
}

var list clusterv1.MachineList

for _, l := range bytes {
var nestedList clusterv1.MachineList
err = json.Unmarshal(l, &nestedList)
if err != nil {
return nil, err
}
list.Items = append(list.Items, nestedList.Items...)
}

return MachineP(list.Items), nil
}

func FindGVKInFile(file string, kind string) ([]byte, error) {
func FindGVKInFile(file string, kind string) ([][]byte, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
return []byte{}, err
return [][]byte{}, err
}

outs := [][]byte{}

reader := bytes.NewReader(b)
decoder := yaml.NewYAMLOrJSONDecoder(reader, 32)

for {
var out unstructured.Unstructured
err = decoder.Decode(&out)
if err != nil {
return []byte{}, err

if err != io.EOF && err != nil {
return outs, err
} else if err == io.EOF {
return outs, nil
}

if out.GetKind() == kind && out.GetAPIVersion() == clusterv1.SchemeGroupVersion.String() {
var marshaled []byte
marshaled, err = out.MarshalJSON()
return marshaled, nil
if err != nil {
return outs, err
}
outs = append(outs, marshaled)
}
}
}

0 comments on commit 436dbf3

Please sign in to comment.