github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/vmo/utils.go (about) 1 // Copyright (C) 2020, 2021, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package vmo 5 6 import ( 7 "crypto/rand" 8 "math/big" 9 10 "github.com/verrazzano/verrazzano-monitoring-operator/pkg/constants" 11 corev1 "k8s.io/api/core/v1" 12 "k8s.io/apimachinery/pkg/labels" 13 ) 14 15 // StorageClassInfo contains storage class info for PVC 16 type StorageClassInfo struct { 17 Name string 18 PvcAcceptsZone bool 19 PvcZoneMatchLabel string 20 } 21 22 // Returns a list of ready and schedulable nodes 23 func getSchedulableNodes(controller *Controller) ([]corev1.Node, error) { 24 var schedulableNodes []corev1.Node 25 26 nodes, err := controller.nodeLister.List(labels.Everything()) 27 for _, node := range nodes { 28 ready := false 29 for _, condition := range node.Status.Conditions { 30 if condition.Type == constants.K8sReadyCondition && condition.Status == "True" { 31 ready = true 32 } 33 } 34 schedulable := true 35 for _, taint := range node.Spec.Taints { 36 if taint.Effect == constants.K8sTaintNoScheduleEffect { 37 schedulable = false 38 } 39 } 40 if ready && schedulable { 41 schedulableNodes = append(schedulableNodes, *node) 42 } 43 } 44 return schedulableNodes, err 45 } 46 47 // Returns a list of ADs which contain scheduable nodes 48 func getSchedulableADs(controller *Controller) ([]string, error) { 49 var schedulableADs []string 50 schedulableNodes, err := getSchedulableNodes(controller) 51 if err != nil { 52 return schedulableADs, err 53 } 54 for _, node := range schedulableNodes { 55 ad, ok := node.Labels[constants.K8sZoneLabel] 56 if ok { 57 if !contains(schedulableADs, ad) { 58 schedulableADs = append(schedulableADs, ad) 59 } 60 } 61 } 62 return schedulableADs, err 63 } 64 func contains(s []string, e string) bool { 65 for _, a := range s { 66 if a == e { 67 return true 68 } 69 } 70 return false 71 } 72 73 // Returns a random element from the given slice 74 func chooseRandomElementFromSlice(slice []string) string { 75 if len(slice) > 0 { 76 nBig, err := rand.Int(rand.Reader, big.NewInt(int64(len(slice)))) 77 if err != nil { 78 panic("failed to generate random number") 79 } 80 return slice[nBig.Int64()] 81 } 82 return "" 83 }