Skip to content

Commit

Permalink
add release ci flow and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YZ775 committed May 31, 2023
1 parent 1c1c08e commit 708a618
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
pull_request:
push:
branches:
- 'develop'
- 'main'
jobs:
build:
runs-on: ubuntu-22.04
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Release
on:
push:
tags:
- 'v*'
jobs:
image:
name: Push Container image
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v2
- run: make docker-build
- name: Login to ghcr.io
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push to ghcr.io
run: |
IMAGE_TAG=${GITHUB_REF#refs/tags/v} # Remove "v" prefix.
docker tag zombie-detector:dev ghcr.io/cybozu-go/zombie-detector:$IMAGE_TAG
docker push ghcr.io/cybozu-go/zombie-detector:$IMAGE_TAG
release:
name: Release on GitHub
needs: image
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: Create release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION=${GITHUB_REF#refs/tags/} # Don't remove "v" prefix.
if echo ${VERSION} | grep -q -e '-'; then PRERELEASE_FLAG=-p; fi
gh release create $VERSION $PRERELEASE_FLAG \
-t "Release $VERSION" \
-n "See [CHANGELOG.md](./CHANGELOG.md) for details."
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ Zombie detector
Zombie detector is CronJob to detect resources that elapsed for a long time since deletion.

## Features
- detect resources who's elapsed time exceeds the threthord
- informations about detected resources are pushed into Pushgateway
- It detects resources that remain undeleted after a certain period with a ```deletionTimestamp```.
- Information about detected resources are pushed into Pushgateway.
- We can use this both inside and outside cluster.

## CLI usage
## Build
CLI
```
go build
```
Docker Image
```
make docker-build
```

## Usage
```
Usage:
zombie-detector [flags]
Expand All @@ -28,14 +39,18 @@ Flags:
--pushgateway string URL of Pushgateway's endpoint
--threshold string threshold of detection (default "24h")
```
example usage
### example

```
zombie-detector --incluster=false --pushgateway=<YOUR PUSHGATEWAY URL> --threshold=24h30m
zombie-detector --incluster=false --pushgateway=<YOUR PUSHGATEWAY ADDRESS> --threshold=24h30m
```
[releases]: https://github.com/cybozu-go/zombie-detector/releases

## example manifest
## Example manifest
We can run zombie-detector periodically as CronJob in a Kubernetes Cluster.

These are example manifests.

conjob.yaml
```yaml
apiVersion: batch/v1
Expand Down
7 changes: 2 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/spf13/cobra"
apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -92,7 +92,7 @@ func getAllResources(ctx context.Context, config *rest.Config) ([]unstructured.U
}
for _, resource := range resList.APIResources {
groupeResourceDef := schema.GroupVersionResource{Group: gv.Group, Version: gv.Version, Resource: resource.Name}
listResponse, err := dynamicClient.Resource(groupeResourceDef).Namespace(apiv1.NamespaceAll).List(ctx, metav1.ListOptions{})
listResponse, err := dynamicClient.Resource(groupeResourceDef).Namespace(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
if strings.Contains(err.Error(), errorFaildtoList) || strings.Contains(err.Error(), errorMethodNotAllowed) {
continue
Expand Down Expand Up @@ -217,10 +217,7 @@ func rootMain(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
printAllResources(allResources)
fmt.Println("---")
zombieResources := detectZombieResources(allResources, threshold)
printAllResources(zombieResources)
err = postZombieResourcesMetrics(zombieResources, pushgatewayEndpointFlag)
if err != nil {
return err
Expand Down
13 changes: 6 additions & 7 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"

Expand All @@ -33,7 +32,7 @@ func TestDetectZombieResource(t *testing.T) {
}{
{
name: "No problem Pod",
resouce: &apiv1.Pod{
resouce: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -50,7 +49,7 @@ func TestDetectZombieResource(t *testing.T) {
},
{
name: "Zombie below the threshold Pod",
resouce: &apiv1.Pod{
resouce: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -67,7 +66,7 @@ func TestDetectZombieResource(t *testing.T) {
},
{
name: "Zombie over the threshold Pod",
resouce: &apiv1.Pod{
resouce: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -84,7 +83,7 @@ func TestDetectZombieResource(t *testing.T) {
},
{
name: "Zombie over the threshold Pod with finalizer",
resouce: &apiv1.Pod{
resouce: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -101,7 +100,7 @@ func TestDetectZombieResource(t *testing.T) {
},
{
name: "Zombie over the threshold(changed) Pod",
resouce: &apiv1.Pod{
resouce: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand All @@ -118,7 +117,7 @@ func TestDetectZombieResource(t *testing.T) {
},
{
name: "Zombie threshold boundary Pod",
resouce: &apiv1.Pod{
resouce: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
Expand Down
4 changes: 2 additions & 2 deletions config/cronjob/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
image: zombie-detector:dev
command:
- ./zombie-detector
- --threshold=1s
- --pushgateway=http://pushgateway.monitoring.svc.cluster.local:9091
- --threshold=24h
- --pushgateway=http://pushgateway.monitoring.svc:9091
restartPolicy: OnFailure
8 changes: 8 additions & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resources:
- ../namespace
- ../rbac
- ../cronjob
images:
- name: zombie-detector:dev
newName: ghcr.io/cybozu-go/zombie-detector
newTag: 0.0.1
3 changes: 1 addition & 2 deletions config/dev/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
resources:
- ../namespace
- ../rbac
- ../cronjob
- ../pushgateway
- ../cronjob
20 changes: 20 additions & 0 deletions e2e/manifests/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: zombie-detector-cronjob
namespace: zombie-detector
spec:
schedule: "0 0 */1 * *"
jobTemplate:
spec:
template:
spec:
serviceAccountName: zombie-detector-sa
containers:
- name: zombie-detector
image: zombie-detector:dev
command:
- ./zombie-detector
- --threshold=1s
- --pushgateway=http://pushgateway.monitoring.svc:9091
restartPolicy: OnFailure
2 changes: 1 addition & 1 deletion e2e/manifests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
app: test-pod-deployment
spec:
containers:
- name: foo
- name: test
image: quay.io/cybozu/ubuntu:20.04
command: ["pause"]
2 changes: 1 addition & 1 deletion e2e/manifests/observer_pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ spec:
runAsUser: 10000
runAsGroup: 10000
containers:
- name: foo
- name: test
image: quay.io/cybozu/ubuntu:20.04
command: ["pause"]
2 changes: 1 addition & 1 deletion e2e/manifests/pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ spec:
runAsUser: 10000
runAsGroup: 10000
containers:
- name: foo
- name: test
image: quay.io/cybozu/ubuntu:20.04
command: ["pause"]
Empty file removed e2e/manifests/serviceaccount.yaml
Empty file.
31 changes: 19 additions & 12 deletions e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/json"
)

Expand All @@ -33,8 +34,25 @@ var _ = BeforeSuite(func() {
_, err = kubectl(nil, "apply", "-f", "./manifests/observer_pod.yaml")
Expect(err).NotTo(HaveOccurred())

By("waiting for observer pod to be started")
Eventually(func() error {
res, err := kubectl(nil, "get", "pod", "observer-pod", "-n", "monitoring", "-o", "json")
if err != nil {
return err
}
pod := corev1.Pod{}
err = json.Unmarshal(res, &pod)
if err != nil {
return err
}
if pod.Status.Phase == corev1.PodRunning {
return fmt.Errorf("observer-pod is not ready yet")
}
return nil
}).Should(Succeed())

By("applying cronjob manifest")
_, err = kubectl(nil, "apply", "-f", "../config/cronjob/cronjob.yaml")
_, err = kubectl(nil, "apply", "-f", "./manifests/cronjob.yaml")
Expect(err).NotTo(HaveOccurred())

res, err := kubectl(nil, "get", "cronjob", "zombie-detector-cronjob", "-n", "zombie-detector", "-o", "json")
Expand All @@ -60,15 +78,4 @@ var _ = BeforeSuite(func() {
}
return nil
}).Should(Succeed())

})

var _ = AfterSuite(func() {
// By("deleting test resources")
// _, err := kubectl(nil, "patch", "deployment", "test-deployment", "--patch-file", "./manifests/patch.yaml")
// Expect(err).NotTo(HaveOccurred())
// _, err = kubectl(nil, "patch", "pod", "test-pod", "--patch-file", "./manifests/patch.yaml")
// Expect(err).NotTo(HaveOccurred())
// _, err = kubectl(nil, "patch", "configmap", "test-configmap", "--patch-file", "./manifests/patch.yaml")
// Expect(err).NotTo(HaveOccurred())
})
6 changes: 3 additions & 3 deletions e2e/zombie-detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"

apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"

"k8s.io/apimachinery/pkg/util/json"
)
Expand Down Expand Up @@ -59,7 +59,7 @@ var _ = Describe("zombie-detector e2e test", func() {
if err != nil {
return err
}
pod := apiv1.Pod{}
pod := corev1.Pod{}
err = json.Unmarshal(res, &pod)
if err != nil {
return err
Expand All @@ -77,7 +77,7 @@ var _ = Describe("zombie-detector e2e test", func() {
if err != nil {
return err
}
configmap := apiv1.ConfigMap{}
configmap := corev1.ConfigMap{}
err = json.Unmarshal(res, &configmap)
if err != nil {
return err
Expand Down

0 comments on commit 708a618

Please sign in to comment.