This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from deliveroo/CPR-569/add-cleanup-pods-command
CPR-569: New command to clean up pods
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.21.3 | ||
0.22.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cleanup | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// CleanUpCmd represents the clean up command | ||
var CleanUpCmd = &cobra.Command{ | ||
Use: "cleanup", | ||
Short: "Clean up resources", | ||
Long: "Cleans up all resources that are in finished state or not needed", | ||
} | ||
|
||
func init() { | ||
CleanUpCmd.AddCommand(podsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cleanup | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/deliveroo/paddle/cli/pipeline" | ||
"github.com/spf13/cobra" | ||
v12 "k8s.io/api/core/v1" | ||
k8errors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
v1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
) | ||
|
||
var clientset kubernetes.Interface | ||
var logFatalf = log.Fatalf | ||
|
||
var podsCmd = &cobra.Command{ | ||
Use: "pods", | ||
Short: "Clean up all finished pods", | ||
Long: `Fetch all pods and delete the ones in complete state | ||
Example: | ||
$ paddle cleanup pods | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runPodsCleanup() | ||
}, | ||
} | ||
|
||
func init() { | ||
config, err := pipeline.GetKubernetesConfig() | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
clientset, err = kubernetes.NewForConfig(config) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
} | ||
|
||
func runPodsCleanup() { | ||
pods := clientset.CoreV1().Pods("modeltraining") | ||
podList, err := pods.List(metav1.ListOptions{}) | ||
if err != nil { | ||
logFatalf("[paddle] error fetching list of pods: %s", err.Error()) | ||
} | ||
|
||
for _, pod := range podList.Items { | ||
switch string(pod.Status.Phase) { | ||
case "Succeeded": | ||
deletePod(pods, pod) | ||
case "Failed": | ||
diff := time.Now().UTC().Sub(pod.CreationTimestamp.UTC()) | ||
if (diff.Hours() / 24) >= 1 { | ||
deletePod(pods, pod) | ||
} | ||
default: | ||
continue | ||
} | ||
} | ||
} | ||
|
||
func deletePod(podInterface v1.PodInterface, pod v12.Pod) { | ||
err := podInterface.Delete(pod.Name, &metav1.DeleteOptions{}) | ||
if err != nil { | ||
if k8errors.IsNotFound(err) { | ||
log.Printf("[paddle] deleted pod %s", pod.Name) | ||
} else { | ||
log.Printf("[paddle] error deleting pod %s", pod.Name) | ||
} | ||
} | ||
log.Printf("[paddle] deleted pod with pod name: %s, pod status: %s", pod.Name, string(pod.Status.Phase)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters