Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProwJob ThirdPartyResource and clean up old ones. #2218

Merged
merged 1 commit into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion prow/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ all: build test

HOOK_VERSION = 0.95
LINE_VERSION = 0.86
SINKER_VERSION = 0.5
SINKER_VERSION = 0.6
DECK_VERSION = 0.18
SPLICE_VERSION = 0.16
MARQUE_VERSION = 0.1
Expand Down
6 changes: 6 additions & 0 deletions prow/cluster/prow_job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: extensions/v1beta1
kind: ThirdPartyResource
metadata:
name: prow-job.prow.k8s.io
versions:
- name: v1
2 changes: 1 addition & 1 deletion prow/cluster/sinker_deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ spec:
role: prow
containers:
- name: sinker
image: gcr.io/k8s-prow/sinker:0.5
image: gcr.io/k8s-prow/sinker:0.6
21 changes: 20 additions & 1 deletion prow/cmd/sinker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type kubeClient interface {

ListJobs(labels map[string]string) ([]kube.Job, error)
DeleteJob(name string) error

ListProwJobs(labels map[string]string) ([]kube.ProwJob, error)
DeleteProwJob(name string) error
}

func main() {
Expand All @@ -56,7 +59,23 @@ func main() {
}

func clean(kc kubeClient) {
// Clean up old jobs first.
// Clean up old prow jobs first.
prowJobs, err := kc.ListProwJobs(nil)
if err != nil {
logrus.WithError(err).Error("Error listing prow jobs.")
return
}
for _, prowJob := range prowJobs {
if !prowJob.Status.CompletionTime.IsZero() && time.Since(prowJob.Status.StartTime) > maxAge {
if err := kc.DeleteProwJob(prowJob.Metadata.Name); err == nil {
logrus.WithField("prowjob", prowJob.Metadata.Name).Info("Deleted prowjob.")
} else {
logrus.WithField("prowjob", prowJob.Metadata.Name).WithError(err).Error("Error deleting prowjob.")
}
}
}

// Then old jobs.
jobs, err := kc.ListJobs(nil)
if err != nil {
logrus.WithError(err).Error("Error listing jobs.")
Expand Down
80 changes: 74 additions & 6 deletions prow/cmd/sinker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import (
)

type fakeClient struct {
Pods []kube.Pod
Jobs []kube.Job
Pods []kube.Pod
Jobs []kube.Job
ProwJobs []kube.ProwJob

DeletedPods []kube.Pod
DeletedJobs []kube.Job
DeletedPods []kube.Pod
DeletedJobs []kube.Job
DeletedProwJobs []kube.ProwJob
}

func (c *fakeClient) ListPods(labels map[string]string) ([]kube.Pod, error) {
Expand All @@ -52,6 +54,27 @@ func (c *fakeClient) ListJobs(labels map[string]string) ([]kube.Job, error) {
return jl, nil
}

func (c *fakeClient) ListProwJobs(labels map[string]string) ([]kube.ProwJob, error) {
jl := make([]kube.ProwJob, 0, len(c.ProwJobs))
for _, j := range c.ProwJobs {
if labelsMatch(labels, j.Metadata.Labels) {
jl = append(jl, j)
}
}
return jl, nil
}

func (c *fakeClient) DeleteProwJob(name string) error {
for i, j := range c.ProwJobs {
if j.Metadata.Name == name {
c.ProwJobs = append(c.ProwJobs[:i], c.ProwJobs[i+1:]...)
c.DeletedProwJobs = append(c.DeletedProwJobs, j)
return nil
}
}
return fmt.Errorf("prowjob %s not found", name)
}

func (c *fakeClient) DeleteJob(name string) error {
for i, j := range c.Jobs {
if j.Metadata.Name == name {
Expand Down Expand Up @@ -233,9 +256,40 @@ func TestClean(t *testing.T) {
"old, deleted",
"old, aborted with pod",
}
prowJobs := []kube.ProwJob{
{
Metadata: kube.ObjectMeta{
Name: "old, complete",
},
Status: kube.ProwJobStatus{
StartTime: time.Now().Add(-maxAge).Add(-time.Second),
CompletionTime: time.Now().Add(-time.Second),
},
},
{
Metadata: kube.ObjectMeta{
Name: "old, incomplete",
},
Status: kube.ProwJobStatus{
StartTime: time.Now().Add(-maxAge).Add(-time.Second),
},
},
{
Metadata: kube.ObjectMeta{
Name: "new",
},
Status: kube.ProwJobStatus{
StartTime: time.Now().Add(-time.Second),
},
},
}
deletedProwJobs := []string{
"old, complete",
}
kc := &fakeClient{
Pods: pods,
Jobs: jobs,
Pods: pods,
Jobs: jobs,
ProwJobs: prowJobs,
}
clean(kc)
if len(deletedPods) != len(kc.DeletedPods) {
Expand Down Expand Up @@ -266,4 +320,18 @@ func TestClean(t *testing.T) {
t.Errorf("Did not delete job %s", n)
}
}
if len(deletedProwJobs) != len(kc.DeletedProwJobs) {
t.Errorf("Deleted wrong number of prowjobs: got %v expected %v", kc.DeletedProwJobs, deletedProwJobs)
}
for _, n := range deletedProwJobs {
found := false
for _, j := range kc.DeletedProwJobs {
if j.Metadata.Name == n {
found = true
}
}
if !found {
t.Errorf("Did not delete prowjob %s", n)
}
}
}
21 changes: 21 additions & 0 deletions prow/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,27 @@ func (c *Client) DeletePod(name string) error {
}, nil)
}

func (c *Client) ListProwJobs(labels map[string]string) ([]ProwJob, error) {
c.log("ListProwJobs", labels)
var jl struct {
Items []ProwJob `json:"items"`
}
err := c.request(&request{
method: http.MethodGet,
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs", c.namespace),
query: map[string]string{"labelSelector": labelsToSelector(labels)},
}, &jl)
return jl.Items, err
}

func (c *Client) DeleteProwJob(name string) error {
c.log("DeleteProwJob", name)
return c.request(&request{
method: http.MethodDelete,
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs/%s", c.namespace, name),
}, nil)
}

func (c *Client) GetJob(name string) (Job, error) {
c.log("GetJob", name)
var retJob Job
Expand Down
13 changes: 13 additions & 0 deletions prow/kube/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ type ObjectMeta struct {
UID string `json:"uid,omitempty"`
}

type ProwJob struct {
Metadata ObjectMeta `json:"metadata,omitempty"`
Spec ProwJobSpec `json:"spec,omitempty"`
Status ProwJobStatus `json:"status,omitempty"`
}

type ProwJobSpec struct{}

type ProwJobStatus struct {
StartTime time.Time `json:"startTime,omitempty"`
CompletionTime time.Time `json:"completionTime,omitempty"`
}

type Secret struct {
Metadata ObjectMeta `json:"metadata,omitempty"`
Data map[string]string `json:"data,omitempty"`
Expand Down