From f888eedffc1e0b909f9bdef48782d08c22b27340 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Thu, 21 Feb 2019 11:50:24 -0800 Subject: [PATCH] Add util.GetControlPlaneMachines (#760) Signed-off-by: Vince Prignano --- pkg/util/util.go | 60 ++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index 9b40837c13cb..b6bd7d926e94 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -47,24 +47,25 @@ const ( ) var ( - r = rand.New(rand.NewSource(time.Now().UnixNano())) + rnd = 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 { - result[i] = CharSet[r.Intn(len(CharSet))] + result[i] = CharSet[rnd.Intn(len(CharSet))] } return string(result) } -// GetControlPlaneMachine returns the control plane machine from a slice +// GetControlPlaneMachine returns a control plane machine from input. +// Deprecated: use GetControlPlaneMachines. func GetControlPlaneMachine(machines []*clusterv1.Machine) *clusterv1.Machine { for _, machine := range machines { if IsControlPlaneMachine(machine) { @@ -74,7 +75,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)) @@ -84,7 +95,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") { @@ -130,13 +141,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 { @@ -147,7 +157,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 @@ -160,7 +170,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 { @@ -170,7 +180,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 { @@ -180,7 +190,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 { @@ -191,7 +201,7 @@ 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 @@ -199,10 +209,9 @@ func GetNamespaceOrDefault(namespace string) string { 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) - if err != nil { return nil, err } @@ -217,7 +226,6 @@ func ParseClusterYaml(file string) (*clusterv1.Cluster, error) { } var cluster clusterv1.Cluster - if err := json.Unmarshal(bytes[0], &cluster); err != nil { return nil, err } @@ -225,10 +233,9 @@ 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) - if err != nil { return nil, err } @@ -251,6 +258,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 { @@ -272,6 +280,7 @@ func ParseMachinesYaml(file string) ([]*clusterv1.Machine, error) { if bytes, err = decodeClusterV1Kinds(decoder, "Machine"); err != nil { return nil, err } + for _, m := range bytes { if err := json.Unmarshal(m, &machine); err != nil { return nil, err @@ -283,29 +292,26 @@ 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{} for { var out unstructured.Unstructured - err := decoder.Decode(&out) - if err == io.EOF { + if err := decoder.Decode(&out); err == io.EOF { break } else if err != nil { return nil, err } if out.GetKind() == kind && out.GetAPIVersion() == clusterv1.SchemeGroupVersion.String() { - var marshaled []byte - marshaled, err = out.MarshalJSON() + marshaled, err := out.MarshalJSON() if err != nil { return outs, err }