// CheckMigsAndNodes checks if all migs have all required nodes. func CheckMigsAndNodes(nodes []*kube_api.Node, gceManager *gce.GceManager) error { migCount := make(map[string]int) migs := make(map[string]*config.MigConfig) for _, node := range nodes { instanceConfig, err := config.InstanceConfigFromProviderId(node.Spec.ProviderID) if err != nil { return err } migConfig, err := gceManager.GetMigForInstance(instanceConfig) if err != nil { return err } url := migConfig.Url() count, _ := migCount[url] migCount[url] = count + 1 migs[url] = migConfig } for url, mig := range migs { size, err := gceManager.GetMigSize(mig) if err != nil { return err } count := migCount[url] if size != int64(count) { return fmt.Errorf("wrong number of nodes for mig: %s expected: %d actual: %d", url, size, count) } } return nil }
// GetNodeInfosForMigs finds NodeInfos for all migs used to manage the given nodes. It also returns a mig to sample node mapping. func GetNodeInfosForMigs(nodes []*kube_api.Node, gceManager *gce.GceManager, kubeClient *kube_client.Client) (map[string]*schedulercache.NodeInfo, map[string]*kube_api.Node, error) { sampleNodes := make(map[string]*kube_api.Node) for _, node := range nodes { instanceConfig, err := config.InstanceConfigFromProviderId(node.Spec.ProviderID) if err != nil { return map[string]*schedulercache.NodeInfo{}, map[string]*kube_api.Node{}, err } migConfig, err := gceManager.GetMigForInstance(instanceConfig) if err != nil { return map[string]*schedulercache.NodeInfo{}, map[string]*kube_api.Node{}, err } url := migConfig.Url() sampleNodes[url] = node } result := make(map[string]*schedulercache.NodeInfo) for url, node := range sampleNodes { nodeInfo, err := simulator.BuildNodeInfoForNode(node.Name, kubeClient) if err != nil { return map[string]*schedulercache.NodeInfo{}, map[string]*kube_api.Node{}, err } result[url] = nodeInfo } return result, sampleNodes, nil }
// ScaleDown tries to scale down the cluster. It returns ScaleDownResult indicating if any node was // removed and error if such occured. func ScaleDown( nodes []*kube_api.Node, unneededNodes map[string]time.Time, unneededTime time.Duration, pods []*kube_api.Pod, gceManager *gce.GceManager, client *kube_client.Client, predicateChecker *simulator.PredicateChecker) (ScaleDownResult, error) { now := time.Now() candidates := make([]*kube_api.Node, 0) for _, node := range nodes { if val, found := unneededNodes[node.Name]; found { glog.V(2).Infof("%s was unneeded for %s", node.Name, now.Sub(val).String()) // Check how long the node was underutilized. if !val.Add(unneededTime).Before(now) { continue } // Check mig size. instance, err := config.InstanceConfigFromProviderId(node.Spec.ProviderID) if err != nil { glog.Errorf("Error while parsing providerid of %s: %v", node.Name, err) continue } migConfig, err := gceManager.GetMigForInstance(instance) if err != nil { glog.Errorf("Error while checking mig config for instance %v: %v", instance, err) continue } size, err := gceManager.GetMigSize(migConfig) if err != nil { glog.Errorf("Error while checking mig size for instance %v: %v", instance, err) continue } if size <= int64(migConfig.MinSize) { glog.V(1).Infof("Skipping %s - mig min size reached", node.Name) continue } candidates = append(candidates, node) } } if len(candidates) == 0 { glog.Infof("No candidates for scale down") return ScaleDownNoUnneeded, nil } nodesToRemove, err := simulator.FindNodesToRemove(candidates, nodes, pods, client, predicateChecker, 1, false) if err != nil { return ScaleDownError, fmt.Errorf("Find node to remove failed: %v", err) } if len(nodesToRemove) == 0 { glog.V(1).Infof("No node to remove") return ScaleDownNoNodeDeleted, nil } nodeToRemove := nodesToRemove[0] glog.Infof("Removing %s", nodeToRemove.Name) instanceConfig, err := config.InstanceConfigFromProviderId(nodeToRemove.Spec.ProviderID) if err != nil { return ScaleDownError, fmt.Errorf("Failed to get instance config for %s: %v", nodeToRemove.Name, err) } err = gceManager.DeleteInstances([]*config.InstanceConfig{instanceConfig}) if err != nil { return ScaleDownError, fmt.Errorf("Failed to delete %v: %v", instanceConfig, err) } return ScaleDownNodeDeleted, nil }