// How does this work? /* This application cleans up the v1alpha2 shipment syncedobject by quering the list of banners in a tenant and also the clusters that exist for that banner. We then get the kubeconfig credentials for all the cluster-infra clusters in that banner and connect to the cluster using gcloud. We then delete the v1alpha2 shipment syncedobject using kubectl. */ package main import ( "context" "fmt" "os" "os/exec" "strings" containerCl "google.golang.org/api/container/v1" "edge-infra.dev/hack/tools/cleaner/config" logger "edge-infra.dev/pkg/lib/logging" ) func main() { ctx := context.Background() log := logger.NewLogger().Logger cfg, err := config.NewConfig(os.Args[1:]) if err != nil { log.Error(err, "failed to parse startup configuration") os.Exit(1) } db, err := cfg.ConnectDatabase(ctx) if err != nil { log.Error(err, "failed to parse startup configuration") os.Exit(1) } dbProvider := New(db) banners, err := dbProvider.Banners(ctx) if err != nil { log.Error(err, "failed to get banners") os.Exit(1) } kubecfgEntries := make([]string, 0) for _, banner := range banners { log := log.WithValues("project_id", banner.ProjectID, "dry_run", cfg.DryRun) containerClSvc, err := containerCl.NewService(ctx) if err != nil { log.Error(err, "an error occurred initializing a container cluster service, skipping...") continue } res, err := containerClSvc.Projects.Locations.Clusters.List(fmt.Sprintf("projects/%s/locations/-", banner.ProjectID)).Context(ctx).Do() if err != nil { log.Error(err, "an error occurred fetching container clusters") continue } for _, clusterRes := range res.Clusters { loggr := log.WithValues("cluster_name", clusterRes.Name) fleet, exists := clusterRes.ResourceLabels["fleet"] if !exists { loggr.Info("fleet resource label does not exist, skipping...") continue } loggr = loggr.WithValues("fleet", fleet) if fleet != "cluster-infra" { loggr.Info("cluster is not a cluster-infra cluster, skipping...") continue } loggr = loggr.WithValues("zone", clusterRes.Zone) args := []string{"container", "clusters", "get-credentials", clusterRes.Name, "--zone", clusterRes.Zone, "--project", banner.ProjectID} loggr.Info("fetching cluster kubeconfig credentials...", "command", strings.Join(args, " ")) out, err := exec.CommandContext(ctx, "gcloud", args...).CombinedOutput() //nolint if err != nil { loggr.Error(err, "an error occurred fetching kubeconfig credentials for cluster") continue } kubecfgEntries = append(kubecfgEntries, clusterRes.Name) fmt.Printf("\n%s\n", string(out)) for _, cluster := range banner.Clusters { if !strings.HasSuffix(cluster.Name, "cluster-infra0") && cluster.Name != "datasync-couchdb-master" { syncedObjName := fmt.Sprintf("cluster-shipment-%s", cluster.ClusterEdgeID) loggr := loggr.WithValues("syncedobject_name", syncedObjName) dryRun := "--dry-run=client" if !cfg.DryRun { dryRun = "" } args := []string{"delete", "syncedobjects", syncedObjName, "-n", cluster.ClusterEdgeID, dryRun} loggr.Info("deleting syncedobject...", "command", fmt.Sprintf("kubectl %s", strings.Join(args, " "))) out, err := exec.CommandContext(ctx, "kubectl", args...).CombinedOutput() if err != nil { log.Error(err, "an error occurred deleting the syncedobject") continue } loggr.Info("syncedobject deleted successfully!!") fmt.Printf("\n%s\n", string(out)) } } // Delete Kubeconfig entries added for each banner if cfg.DeleteKubeconfigEntries { for _, entry := range kubecfgEntries { args := []string{"config", "delete-cluster", entry} log.Info("deleting kubeconfig entry...", "entry", entry, "command", fmt.Sprintf("kubectl %s", strings.Join(args, " "))) out, err := exec.CommandContext(ctx, "kubectl", args...).CombinedOutput() if err != nil { log.Error(err, "an error occurred deleting the kubeconfig entry") continue } log.Info("kubeconfig entry deleted successfully!!") fmt.Printf("\n%s\n", string(out)) } } } } }