1
2
11 package main
12
13 import (
14 "context"
15 "fmt"
16 "os"
17 "os/exec"
18 "strings"
19
20 containerCl "google.golang.org/api/container/v1"
21
22 "edge-infra.dev/hack/tools/cleaner/config"
23 logger "edge-infra.dev/pkg/lib/logging"
24 )
25
26 func main() {
27 ctx := context.Background()
28 log := logger.NewLogger().Logger
29
30 cfg, err := config.NewConfig(os.Args[1:])
31 if err != nil {
32 log.Error(err, "failed to parse startup configuration")
33 os.Exit(1)
34 }
35
36 db, err := cfg.ConnectDatabase(ctx)
37 if err != nil {
38 log.Error(err, "failed to parse startup configuration")
39 os.Exit(1)
40 }
41
42 dbProvider := New(db)
43
44 banners, err := dbProvider.Banners(ctx)
45 if err != nil {
46 log.Error(err, "failed to get banners")
47 os.Exit(1)
48 }
49
50 kubecfgEntries := make([]string, 0)
51
52 for _, banner := range banners {
53 log := log.WithValues("project_id", banner.ProjectID, "dry_run", cfg.DryRun)
54 containerClSvc, err := containerCl.NewService(ctx)
55 if err != nil {
56 log.Error(err, "an error occurred initializing a container cluster service, skipping...")
57 continue
58 }
59
60 res, err := containerClSvc.Projects.Locations.Clusters.List(fmt.Sprintf("projects/%s/locations/-", banner.ProjectID)).Context(ctx).Do()
61 if err != nil {
62 log.Error(err, "an error occurred fetching container clusters")
63 continue
64 }
65
66 for _, clusterRes := range res.Clusters {
67 loggr := log.WithValues("cluster_name", clusterRes.Name)
68 fleet, exists := clusterRes.ResourceLabels["fleet"]
69 if !exists {
70 loggr.Info("fleet resource label does not exist, skipping...")
71 continue
72 }
73 loggr = loggr.WithValues("fleet", fleet)
74 if fleet != "cluster-infra" {
75 loggr.Info("cluster is not a cluster-infra cluster, skipping...")
76 continue
77 }
78 loggr = loggr.WithValues("zone", clusterRes.Zone)
79 args := []string{"container", "clusters", "get-credentials", clusterRes.Name, "--zone", clusterRes.Zone, "--project", banner.ProjectID}
80 loggr.Info("fetching cluster kubeconfig credentials...", "command", strings.Join(args, " "))
81 out, err := exec.CommandContext(ctx, "gcloud", args...).CombinedOutput()
82 if err != nil {
83 loggr.Error(err, "an error occurred fetching kubeconfig credentials for cluster")
84 continue
85 }
86 kubecfgEntries = append(kubecfgEntries, clusterRes.Name)
87 fmt.Printf("\n%s\n", string(out))
88 for _, cluster := range banner.Clusters {
89 if !strings.HasSuffix(cluster.Name, "cluster-infra0") && cluster.Name != "datasync-couchdb-master" {
90 syncedObjName := fmt.Sprintf("cluster-shipment-%s", cluster.ClusterEdgeID)
91 loggr := loggr.WithValues("syncedobject_name", syncedObjName)
92 dryRun := "--dry-run=client"
93 if !cfg.DryRun {
94 dryRun = ""
95 }
96 args := []string{"delete", "syncedobjects", syncedObjName, "-n", cluster.ClusterEdgeID, dryRun}
97 loggr.Info("deleting syncedobject...", "command", fmt.Sprintf("kubectl %s", strings.Join(args, " ")))
98 out, err := exec.CommandContext(ctx, "kubectl", args...).CombinedOutput()
99 if err != nil {
100 log.Error(err, "an error occurred deleting the syncedobject")
101 continue
102 }
103 loggr.Info("syncedobject deleted successfully!!")
104 fmt.Printf("\n%s\n", string(out))
105 }
106 }
107
108 if cfg.DeleteKubeconfigEntries {
109 for _, entry := range kubecfgEntries {
110 args := []string{"config", "delete-cluster", entry}
111 log.Info("deleting kubeconfig entry...", "entry", entry, "command", fmt.Sprintf("kubectl %s", strings.Join(args, " ")))
112 out, err := exec.CommandContext(ctx, "kubectl", args...).CombinedOutput()
113 if err != nil {
114 log.Error(err, "an error occurred deleting the kubeconfig entry")
115 continue
116 }
117 log.Info("kubeconfig entry deleted successfully!!")
118 fmt.Printf("\n%s\n", string(out))
119 }
120 }
121 }
122 }
123 }
124
View as plain text