-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
78 lines (68 loc) · 2.56 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/forbearing/k8s/deployment"
"github.com/forbearing/k8s/util/annotations"
)
var (
ctx, cancel = context.WithTimeout(context.Background(), time.Minute*10)
namespace = "test"
filename = "../../testdata/examples/deployment.yaml"
name = "mydep"
)
func main() {
handler := deployment.NewOrDie(ctx, "", namespace)
defer cleanup(handler)
deploy, err := handler.Apply(filename)
if err != nil {
log.Fatal(err)
}
// has annotation?
log.Println(annotations.Has(deploy, "region")) // true
log.Println(annotations.Has(deploy, "region=east")) // true
log.Println(annotations.Has(deploy, "region=west")) // false
log.Println(annotations.Has(deploy, "zone")) // false
// get all annotations
log.Println(annotations.GetAll(deploy)) // map[region:east]
// get a annotation
log.Println(annotations.Get(deploy, "region")) // east
log.Println(annotations.Get(deploy, "region=east")) // east
log.Println(annotations.Get(deploy, "region=west")) // east
// set annotation
fmt.Println()
log.Println("before annotations: ", annotations.GetAll(deploy))
annotations.Set(deploy, "zone=us", "master=true")
log.Println("after annotations : ", annotations.GetAll(deploy))
annotations.Set(deploy, "zone=us", "master=true")
log.Println("after annotations : ", annotations.GetAll(deploy))
// Output:
//2022/08/27 17:17:34 before annotations: map[region:east]
//2022/08/27 17:17:34 after annotations : map[master:true region:east zone:us]
//2022/08/27 17:17:34 after annotations : map[master:true region:east zone:us]
// remove annotation
fmt.Println()
log.Println("before annotations: ", annotations.GetAll(deploy))
annotations.Remove(deploy, "zone=us", "master=true")
log.Println("after annotations : ", annotations.GetAll(deploy))
annotations.Remove(deploy, "zone=us", "master=true")
log.Println("after annotations : ", annotations.GetAll(deploy))
// Output
//2022/08/27 17:18:27 before annotations: map[master:true region:east zone:us]
//2022/08/27 17:18:27 after annotations : map[region:east]
//2022/08/27 17:18:27 after annotations : map[region:east]
// remove all annotations
fmt.Println()
log.Println("before annotations: ", annotations.GetAll(deploy))
annotations.RemoveAll(deploy)
log.Println("after annotations: ", annotations.GetAll(deploy))
// Output:
//2022/08/27 17:19:00 before annotations: map[region:east]
//2022/08/27 17:19:00 after annotations: map[]
}
// cleanup will delete or prune created deployments.
func cleanup(handler *deployment.Handler) {
handler.Delete(name)
}