-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (73 loc) · 1.47 KB
/
main.go
File metadata and controls
86 lines (73 loc) · 1.47 KB
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
package main
import (
"context"
"sync"
"github.com/mqasimca/k8s/client"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
var (
counterIncremental = map[string]map[string]int{
"pods": {
"total": 0,
"deleted": 0,
"error": 0,
},
}
)
type Pod struct {
PodName string `json:"podName"`
Namespace string `json:"namespace"`
Reason string `json:"reason"`
State string `json:"state"`
}
func main() {
var wg sync.WaitGroup
ctx := context.Background()
ch := make(chan Pod)
kClient, err := client.NewK8sClient()
if err != nil {
log.Panic().Err(err)
}
// List pods
wg.Add(1)
go func() {
err = listPods(ctx, kClient, ch)
if err != nil {
log.Panic().Err(err).Msg("Failed to list pods")
}
}()
// PODS
go func() {
for {
p, ok := <-ch
if !ok {
log.Info().Msgf("Total pods found: %d", counterIncremental["pods"]["total"])
log.Info().Msg("Finished deleting pods")
wg.Done()
return
}
log.Info().Fields(
map[string]interface{}{
"action": "delete_pod",
"podName": p.PodName,
"namespace": p.Namespace,
"state": p.State,
"reason": p.Reason,
},
).Msg("pod")
if viper.GetBool("ENABLE_DELETE_PODS") {
counterIncremental["pods"]["delete"]++
log.Info().Msg("Deleing pod")
// Delete pods
}
}
}()
wg.Wait()
}
func init() {
viper.AutomaticEnv()
// Deleting pods is disable
viper.SetDefault("CREATED_AT", 5)
viper.SetDefault("ENABLE_DELETE_PODS", false)
}