|
| 1 | +/* |
| 2 | +Copyright 2023 The Multi-Cluster App Dispatcher Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "flag" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota" |
| 24 | + "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota/core" |
| 25 | + klog "k8s.io/klog/v2" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + klog.InitFlags(nil) |
| 30 | + flag.Set("v", "4") |
| 31 | + flag.Set("skip_headers", "true") |
| 32 | + klog.SetOutput(os.Stdout) |
| 33 | + flag.Parse() |
| 34 | + defer klog.Flush() |
| 35 | + |
| 36 | + prefix := "../../../samples/tree/" |
| 37 | + treeFileName := prefix + "tree.json" |
| 38 | + caFileName := prefix + "ca.json" |
| 39 | + cbFileName := prefix + "cb.json" |
| 40 | + ccFileName := prefix + "cc.json" |
| 41 | + cdFileName := prefix + "cd.json" |
| 42 | + ceFileName := prefix + "ce.json" |
| 43 | + |
| 44 | + // create a quota manager |
| 45 | + fmt.Println("==> Creating Quota Manager") |
| 46 | + fmt.Println("**************************") |
| 47 | + quotaManager := quota.NewManager() |
| 48 | + treeJsonString, err := os.ReadFile(treeFileName) |
| 49 | + if err != nil { |
| 50 | + fmt.Printf("error reading quota tree file: %s", treeFileName) |
| 51 | + return |
| 52 | + } |
| 53 | + quotaManager.SetMode(quota.Normal) |
| 54 | + |
| 55 | + // add a quota tree from file |
| 56 | + treeName, err := quotaManager.AddTreeFromString(string(treeJsonString)) |
| 57 | + if err != nil { |
| 58 | + fmt.Printf("error adding tree %s: %v", treeName, err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // allocate consumers |
| 63 | + allocate(quotaManager, treeName, caFileName, false) |
| 64 | + allocate(quotaManager, treeName, cbFileName, false) |
| 65 | + allocate(quotaManager, treeName, ccFileName, false) |
| 66 | + |
| 67 | + // try and undo allocation |
| 68 | + allocate(quotaManager, treeName, cdFileName, true) |
| 69 | + undoAllocate(quotaManager, treeName, cdFileName) |
| 70 | + |
| 71 | + // allocate consumers |
| 72 | + allocate(quotaManager, treeName, ceFileName, false) |
| 73 | +} |
| 74 | + |
| 75 | +// allocate consumer from file |
| 76 | +func allocate(quotaManager *quota.Manager, treeName string, consumerFileName string, try bool) { |
| 77 | + consumerInfo := getConsumerInfo(consumerFileName) |
| 78 | + if consumerInfo == nil { |
| 79 | + fmt.Printf("error reading consumer file: %s", consumerFileName) |
| 80 | + return |
| 81 | + } |
| 82 | + consumerID := consumerInfo.GetID() |
| 83 | + fmt.Println("==> Allocating consumer " + consumerID) |
| 84 | + fmt.Println("**************************") |
| 85 | + quotaManager.AddConsumer(consumerInfo) |
| 86 | + |
| 87 | + var allocResponse *core.AllocationResponse |
| 88 | + var err error |
| 89 | + if try { |
| 90 | + allocResponse, err = quotaManager.TryAllocate(treeName, consumerID) |
| 91 | + } else { |
| 92 | + allocResponse, err = quotaManager.Allocate(treeName, consumerID) |
| 93 | + } |
| 94 | + if err != nil { |
| 95 | + fmt.Printf("error allocating consumer: %v", err) |
| 96 | + return |
| 97 | + } |
| 98 | + fmt.Println(allocResponse) |
| 99 | + fmt.Println(quotaManager) |
| 100 | +} |
| 101 | + |
| 102 | +// undo most recent consumer allocation |
| 103 | +func undoAllocate(quotaManager *quota.Manager, treeName string, consumerFileName string) { |
| 104 | + consumerInfo := getConsumerInfo(consumerFileName) |
| 105 | + if consumerInfo == nil { |
| 106 | + fmt.Printf("error reading consumer file: %s", consumerFileName) |
| 107 | + return |
| 108 | + } |
| 109 | + consumerID := consumerInfo.GetID() |
| 110 | + fmt.Println("==> Undo allocating consumer " + consumerID) |
| 111 | + fmt.Println("**************************") |
| 112 | + quotaManager.UndoAllocate(treeName, consumerID) |
| 113 | + fmt.Println(quotaManager) |
| 114 | +} |
| 115 | + |
| 116 | +// get consumer info from yaml file |
| 117 | +func getConsumerInfo(consumerFileName string) *quota.ConsumerInfo { |
| 118 | + consumerInfo, err := quota.NewConsumerInfoFromFile(consumerFileName) |
| 119 | + if err != nil { |
| 120 | + fmt.Printf("error reading consumer file: %s", consumerFileName) |
| 121 | + return nil |
| 122 | + } |
| 123 | + return consumerInfo |
| 124 | +} |
0 commit comments