Skip to content

Commit

Permalink
Add util.GetControlPlaneMachines
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Feb 21, 2019
1 parent 0252bc6 commit f776460
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ var (
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)

// RandomToken returns a random token
// RandomToken returns a random token.
func RandomToken() string {
return fmt.Sprintf("%s.%s", RandomString(6), RandomString(16))
}

// RandomString returns a random alphanumeric string
// RandomString returns a random alphanumeric string.
func RandomString(n int) string {
result := make([]byte, n)
for i := range result {
Expand All @@ -64,7 +64,7 @@ func RandomString(n int) string {
return string(result)
}

// GetControlPlaneMachine returns the control plane machine from a slice
// GetControlPlaneMachine returns the control plane machine from a slice.
func GetControlPlaneMachine(machines []*clusterv1.Machine) *clusterv1.Machine {
for _, machine := range machines {
if IsControlPlaneMachine(machine) {
Expand All @@ -74,7 +74,17 @@ func GetControlPlaneMachine(machines []*clusterv1.Machine) *clusterv1.Machine {
return nil
}

// MachineP converts a slice of machines into a slice of machine pointers
// GetControlPlaneMachines returns a slice containing control plane machines.
func GetControlPlaneMachines(machines []*clusterv1.Machine) (res []*clusterv1.Machine) {
for _, machine := range machines {
if IsControlPlaneMachine(machine) {
res = append(res, machine)
}
}
return
}

// MachineP converts a slice of machines into a slice of machine pointers.
func MachineP(machines []clusterv1.Machine) []*clusterv1.Machine {
// Convert to list of pointers
ret := make([]*clusterv1.Machine, 0, len(machines))
Expand All @@ -84,7 +94,7 @@ func MachineP(machines []clusterv1.Machine) []*clusterv1.Machine {
return ret
}

// Home returns the user home directory
// Home returns the user home directory.
func Home() string {
home := os.Getenv("HOME")
if strings.Contains(home, "root") {
Expand Down Expand Up @@ -130,13 +140,12 @@ func GetMachineIfExists(c client.Client, namespace, name string) (*clusterv1.Mac
return machine, nil
}

// IsControlPlaneMachine checks machine is a control plane node
// TODO(robertbailey): Remove this function
// IsControlPlaneMachine checks machine is a control plane node.
func IsControlPlaneMachine(machine *clusterv1.Machine) bool {
return machine.Spec.Versions.ControlPlane != ""
}

// IsNodeReady returns true if a node is ready
// IsNodeReady returns true if a node is ready.
func IsNodeReady(node *v1.Node) bool {
for _, condition := range node.Status.Conditions {
if condition.Type == v1.NodeReady {
Expand All @@ -147,7 +156,7 @@ func IsNodeReady(node *v1.Node) bool {
return false
}

// Copy deep copies a Machine object
// Copy deep copies a Machine object.
func Copy(m *clusterv1.Machine) *clusterv1.Machine {
ret := &clusterv1.Machine{}
ret.APIVersion = m.APIVersion
Expand All @@ -160,7 +169,7 @@ func Copy(m *clusterv1.Machine) *clusterv1.Machine {
return ret
}

// ExecCommand Executes a local command in the current shell
// ExecCommand Executes a local command in the current shell.
func ExecCommand(name string, args ...string) string {
cmdOut, err := exec.Command(name, args...).Output()
if err != nil {
Expand All @@ -170,7 +179,7 @@ func ExecCommand(name string, args ...string) string {
return string(cmdOut)
}

// Filter filters a list for a string
// Filter filters a list for a string.
func Filter(list []string, strToFilter string) (newList []string) {
for _, item := range list {
if item != strToFilter {
Expand All @@ -180,7 +189,7 @@ func Filter(list []string, strToFilter string) (newList []string) {
return
}

// Contains returns true if a list contains a string
// Contains returns true if a list contains a string.
func Contains(list []string, strToSearch string) bool {
for _, item := range list {
if item == strToSearch {
Expand All @@ -191,15 +200,15 @@ func Contains(list []string, strToSearch string) bool {
}

// GetNamespaceOrDefault returns the default namespace if given empty
// output
// output.
func GetNamespaceOrDefault(namespace string) string {
if namespace == "" {
return v1.NamespaceDefault
}
return namespace
}

// ParseClusterYaml parses a YAML file for cluster objects
// ParseClusterYaml parses a YAML file for cluster objects.
func ParseClusterYaml(file string) (*clusterv1.Cluster, error) {
reader, err := os.Open(file)

Expand All @@ -225,7 +234,7 @@ func ParseClusterYaml(file string) (*clusterv1.Cluster, error) {
return &cluster, nil
}

// ParseMachinesYaml extracts machine objects from a file
// ParseMachinesYaml extracts machine objects from a file.
func ParseMachinesYaml(file string) ([]*clusterv1.Machine, error) {
reader, err := os.Open(file)

Expand All @@ -251,6 +260,7 @@ func ParseMachinesYaml(file string) ([]*clusterv1.Machine, error) {
}
return nil, err
}

// TODO: this is O(n^2) and must be optimized
for _, ml := range bytes {
if err := json.Unmarshal(ml, &machineList); err != nil {
Expand Down Expand Up @@ -283,12 +293,12 @@ func ParseMachinesYaml(file string) ([]*clusterv1.Machine, error) {
}

// isMissingKind reimplements runtime.IsMissingKind as the YAMLOrJSONDecoder
// hides the error type
// hides the error type.
func isMissingKind(err error) bool {
return strings.Contains(err.Error(), "Object 'Kind' is missing in")
}

// decodeClusterV1Kinds returns a slice of objects matching the clusterv1 kind
// decodeClusterV1Kinds returns a slice of objects matching the clusterv1 kind.
func decodeClusterV1Kinds(decoder *yaml.YAMLOrJSONDecoder, kind string) ([][]byte, error) {

outs := [][]byte{}
Expand Down

0 comments on commit f776460

Please sign in to comment.