Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util.GetControlPlaneMachines / cleanup #760

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Expand All @@ -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") {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -191,18 +201,17 @@ 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)

if err != nil {
return nil, err
}
Expand All @@ -217,18 +226,16 @@ func ParseClusterYaml(file string) (*clusterv1.Cluster, error) {
}

var cluster clusterv1.Cluster

if err := json.Unmarshal(bytes[0], &cluster); err != nil {
return nil, err
}

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
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand Down