forked from project-codeflare/multi-cluster-app-dispatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.go
122 lines (106 loc) · 3.56 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2023 The Multi-Cluster App Dispatcher Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"os"
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota"
"k8s.io/klog/v2"
)
func main() {
klog.InitFlags(nil)
flag.Set("v", "4")
flag.Set("skip_headers", "true")
klog.SetOutput(os.Stdout)
flag.Parse()
defer klog.Flush()
fmt.Println("Demo of allocation and de-allocation of consumers on a forest using the quota manager.")
fmt.Println()
prefix := "../../../samples/forest/"
indent := "===> "
forestName := "Context-Service"
treeNames := []string{"ContextTree", "ServiceTree"}
// create a quota manager
fmt.Println(indent + "Creating quota manager ... " + "\n")
quotaManager := quota.NewManager()
quotaManager.SetMode(quota.Normal)
fmt.Println(quotaManager.GetModeString())
fmt.Println()
// create multiple trees
fmt.Println(indent + "Creating multiple trees ..." + "\n")
for _, treeName := range treeNames {
fName := prefix + treeName + ".json"
fmt.Printf("Tree file name: %s\n", fName)
jsonTree, err := os.ReadFile(fName)
if err != nil {
fmt.Printf("error reading quota tree file: %s", fName)
return
}
_, err = quotaManager.AddTreeFromString(string(jsonTree))
if err != nil {
fmt.Printf("error adding tree %s: %v", treeName, err)
return
}
}
// create forest
fmt.Println(indent + "Creating forest " + forestName + " ..." + "\n")
quotaManager.AddForest(forestName)
for _, treeName := range treeNames {
quotaManager.AddTreeToForest(forestName, treeName)
}
fmt.Println(quotaManager)
// create consumer jobs
fmt.Println(indent + "Allocating consumers on forest ..." + "\n")
jobs := []string{"job1", "job2", "job3", "job4", "job5"}
for _, job := range jobs {
// create consumer info
fName := prefix + job + ".json"
fmt.Printf("Consumer file name: %s\n", fName)
consumerInfo, err := quota.NewConsumerInfoFromFile(fName)
if err != nil {
fmt.Printf("error reading consumer file: %s \n", fName)
continue
}
consumerID := consumerInfo.GetID()
// add consumer info to quota manager
quotaManager.AddConsumer(consumerInfo)
// allocate forest consumer instance of the consumer info
if job == "job4" {
_, err = quotaManager.TryAllocateForest(forestName, consumerID)
if err != nil {
fmt.Printf("error allocating consumer: %v \n", err)
}
err = quotaManager.UndoAllocateForest(forestName, "job-4")
if err != nil {
fmt.Printf("error undoing allocation consumer: %v \n", err)
}
_, err = quotaManager.AllocateForest(forestName, consumerID)
} else {
_, err = quotaManager.AllocateForest(forestName, consumerID)
}
if err != nil {
fmt.Printf("error allocating consumer: %v \n", err)
quotaManager.RemoveConsumer((consumerID))
continue
}
}
// de-allocate consumers from forest
fmt.Println(indent + "De-allocating consumers from forest ..." + "\n")
for _, id := range quotaManager.GetAllConsumerIDs() {
quotaManager.DeAllocateForest(forestName, id)
quotaManager.RemoveConsumer(id)
}
fmt.Println()
fmt.Println(quotaManager)
}