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

Fetch cronjobs from 'batch/v1beta1' #3044

Merged
merged 2 commits into from
Jan 30, 2018
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion app/api_topologies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/gorilla/mux"
"github.com/ugorji/go/codec"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/client-go/pkg/api/v1"

"github.com/weaveworks/common/test"
"github.com/weaveworks/scope/app"
Expand Down
50 changes: 34 additions & 16 deletions probe/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
"github.com/weaveworks/common/backoff"

log "github.com/Sirupsen/logrus"
apiappsv1beta1 "k8s.io/api/apps/v1beta1"
apibatchv1 "k8s.io/api/batch/v1"
apibatchv1beta1 "k8s.io/api/batch/v1beta1"
apibatchv2alpha1 "k8s.io/api/batch/v2alpha1"
apiv1 "k8s.io/api/core/v1"
apiextensionsv1beta1 "k8s.io/api/extensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
apiv1 "k8s.io/client-go/pkg/api/v1"
apiappsv1beta1 "k8s.io/client-go/pkg/apis/apps/v1beta1"
apibatchv1 "k8s.io/client-go/pkg/apis/batch/v1"
apibatchv2alpha1 "k8s.io/client-go/pkg/apis/batch/v2alpha1"
apiextensionsv1beta1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -133,16 +134,20 @@ func NewClient(config ClientConfig) (Client, error) {
client: c,
}

result.cronJobStore, err = result.setupCronjobStore()
if err != nil {
return nil, err
}

podStore := NewEventStore(result.triggerPodWatches, cache.MetaNamespaceKeyFunc)
result.podStore = result.setupStore(c.CoreV1Client.RESTClient(), "pods", &apiv1.Pod{}, podStore)
result.serviceStore = result.setupStore(c.CoreV1Client.RESTClient(), "services", &apiv1.Service{}, nil)
result.nodeStore = result.setupStore(c.CoreV1Client.RESTClient(), "nodes", &apiv1.Node{}, nil)
result.namespaceStore = result.setupStore(c.CoreV1Client.RESTClient(), "namespaces", &apiv1.Namespace{}, nil)
result.deploymentStore = result.setupStore(c.ExtensionsV1beta1Client.RESTClient(), "deployments", &apiextensionsv1beta1.Deployment{}, nil)
result.daemonSetStore = result.setupStore(c.ExtensionsV1beta1Client.RESTClient(), "daemonsets", &apiextensionsv1beta1.DaemonSet{}, nil)
result.jobStore = result.setupStore(c.BatchV1Client.RESTClient(), "jobs", &apibatchv1.Job{}, nil)
result.cronJobStore = result.setupStore(c.BatchV2alpha1Client.RESTClient(), "cronjobs", &apibatchv2alpha1.CronJob{}, nil)
result.statefulSetStore = result.setupStore(c.AppsV1beta1Client.RESTClient(), "statefulsets", &apiappsv1beta1.StatefulSet{}, nil)
result.podStore = result.setupStore(c.CoreV1().RESTClient(), "pods", &apiv1.Pod{}, podStore)
result.serviceStore = result.setupStore(c.CoreV1().RESTClient(), "services", &apiv1.Service{}, nil)
result.nodeStore = result.setupStore(c.CoreV1().RESTClient(), "nodes", &apiv1.Node{}, nil)
result.namespaceStore = result.setupStore(c.CoreV1().RESTClient(), "namespaces", &apiv1.Namespace{}, nil)
result.deploymentStore = result.setupStore(c.ExtensionsV1beta1().RESTClient(), "deployments", &apiextensionsv1beta1.Deployment{}, nil)
result.daemonSetStore = result.setupStore(c.ExtensionsV1beta1().RESTClient(), "daemonsets", &apiextensionsv1beta1.DaemonSet{}, nil)
result.jobStore = result.setupStore(c.BatchV1().RESTClient(), "jobs", &apibatchv1.Job{}, nil)
result.statefulSetStore = result.setupStore(c.AppsV1beta1().RESTClient(), "statefulsets", &apiappsv1beta1.StatefulSet{}, nil)

return result, nil
}
Expand Down Expand Up @@ -175,6 +180,20 @@ func (c *client) setupStore(kclient rest.Interface, resource string, itemType in
return store
}

func (c *client) setupCronjobStore() (cache.Store, error) {
const resource = "cronjobs"
ok, err := c.isResourceSupported(c.client.BatchV1beta1().RESTClient().APIVersion(), resource)
if err != nil {
return nil, err
}
if ok {
// kubernetes >= 1.8
return c.setupStore(c.client.BatchV1beta1().RESTClient(), resource, &apibatchv1beta1.CronJob{}, nil), nil
}
// kubernetes < 1.8
return c.setupStore(c.client.BatchV2alpha1().RESTClient(), resource, &apibatchv2alpha1.CronJob{}, nil), nil
}

// runReflectorUntil runs cache.Reflector#ListAndWatch in an endless loop, after checking that the resource is supported by kubernetes.
// Errors are logged and retried with exponential backoff.
func (c *client) runReflectorUntil(r *cache.Reflector, groupVersion schema.GroupVersion, resource string) {
Expand Down Expand Up @@ -287,8 +306,7 @@ func (c *client) WalkCronJobs(f func(CronJob) error) error {
jobs[j.UID] = j
}
for _, m := range c.cronJobStore.List() {
cj := m.(*apibatchv2alpha1.CronJob)
if err := f(NewCronJob(cj, jobs)); err != nil {
if err := f(NewCronJob(m, jobs)); err != nil {
return err
}
}
Expand Down
46 changes: 42 additions & 4 deletions probe/kubernetes/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"time"

batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
batchv2alpha1 "k8s.io/api/batch/v2alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
batchv1 "k8s.io/client-go/pkg/apis/batch/v1"
batchv2alpha1 "k8s.io/client-go/pkg/apis/batch/v2alpha1"

"github.com/weaveworks/scope/report"
)
Expand All @@ -29,14 +30,25 @@ type CronJob interface {
}

type cronJob struct {
*batchv2alpha1.CronJob
*batchv1beta1.CronJob
Meta
jobs []*batchv1.Job
}

// NewCronJob creates a new cron job. jobs should be all jobs, which will be filtered
// for those matching this cron job.
func NewCronJob(cj *batchv2alpha1.CronJob, jobs map[types.UID]*batchv1.Job) CronJob {
func NewCronJob(cji interface{}, jobs map[types.UID]*batchv1.Job) CronJob {
switch cj := cji.(type) {
case *batchv2alpha1.CronJob:
return newCronJob(upgradeCronJob(cj), jobs)
case *batchv1beta1.CronJob:
return newCronJob(cj, jobs)
default:
panic(fmt.Sprintf("interface conversion: interface{} is %T, not *batchv2alpha1.CronJob or *batchv1beta1.CronJob", cj))
}
}

func newCronJob(cj *batchv1beta1.CronJob, jobs map[types.UID]*batchv1.Job) CronJob {
myJobs := []*batchv1.Job{}
for _, o := range cj.Status.Active {
if j, ok := jobs[o.UID]; ok {
Expand Down Expand Up @@ -74,3 +86,29 @@ func (cj *cronJob) GetNode() report.Node {
}
return cj.MetaNode(report.MakeCronJobNodeID(cj.UID())).WithLatests(latest)
}

func upgradeCronJob(legacy *batchv2alpha1.CronJob) *batchv1beta1.CronJob {
jobTemplate := batchv1beta1.JobTemplateSpec{
ObjectMeta: legacy.Spec.JobTemplate.ObjectMeta,
Spec: legacy.Spec.JobTemplate.Spec,
}
spec := batchv1beta1.CronJobSpec{
Schedule: legacy.Spec.Schedule,
StartingDeadlineSeconds: legacy.Spec.StartingDeadlineSeconds,
ConcurrencyPolicy: batchv1beta1.ConcurrencyPolicy(legacy.Spec.ConcurrencyPolicy),
Suspend: legacy.Spec.Suspend,
JobTemplate: jobTemplate,
SuccessfulJobsHistoryLimit: legacy.Spec.SuccessfulJobsHistoryLimit,
FailedJobsHistoryLimit: legacy.Spec.FailedJobsHistoryLimit,
}
status := batchv1beta1.CronJobStatus{
Active: legacy.Status.Active,
LastScheduleTime: legacy.Status.LastScheduleTime,
}
return &batchv1beta1.CronJob{
TypeMeta: legacy.TypeMeta,
ObjectMeta: legacy.ObjectMeta,
Spec: spec,
Status: status,
}
}
2 changes: 1 addition & 1 deletion probe/kubernetes/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package kubernetes
import (
"fmt"

apiv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
apiv1beta1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"

"github.com/weaveworks/scope/report"
)
Expand Down
4 changes: 2 additions & 2 deletions probe/kubernetes/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (

"github.com/weaveworks/scope/report"

apiv1 "k8s.io/api/core/v1"
apiv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
apiv1 "k8s.io/client-go/pkg/api/v1"
apiv1beta1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"
)

// These constants are keys used in node metadata
Expand Down
2 changes: 1 addition & 1 deletion probe/kubernetes/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kubernetes
import (
"github.com/weaveworks/scope/report"

apiv1 "k8s.io/client-go/pkg/api/v1"
apiv1 "k8s.io/api/core/v1"
)

// NamespaceResource represents a Kubernetes namespace
Expand Down
2 changes: 1 addition & 1 deletion probe/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/weaveworks/scope/report"

apiv1 "k8s.io/client-go/pkg/api/v1"
apiv1 "k8s.io/api/core/v1"
)

// These constants are keys used in node metadata
Expand Down
2 changes: 1 addition & 1 deletion probe/kubernetes/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"strings"
"testing"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
apiv1 "k8s.io/client-go/pkg/api/v1"

"github.com/weaveworks/scope/common/xfer"
"github.com/weaveworks/scope/probe/controls"
Expand Down
2 changes: 1 addition & 1 deletion probe/kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package kubernetes
import (
"github.com/weaveworks/scope/report"

apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
apiv1 "k8s.io/client-go/pkg/api/v1"
)

// These constants are keys used in node metadata
Expand Down
2 changes: 1 addition & 1 deletion probe/kubernetes/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package kubernetes
import (
"fmt"

"k8s.io/api/apps/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/pkg/apis/apps/v1beta1"

"github.com/weaveworks/scope/report"
)
Expand Down
Loading