Skip to content

Commit b537e8f

Browse files
committed
k8s: added cronjob probe
1 parent b47f182 commit b537e8f

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed

etc/skydive.yml.default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ k8s:
368368
probes:
369369
- cluster
370370
- container
371+
- cronjob
371372
- deployment
372373
- ingress
373374
- job

statics/img/cronjob.png

22.3 KB
Loading

statics/js/components/layout.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var nodeImgMap = setupFixedImages({
2727
"bond": "port",
2828
"cluster": "cluster",
2929
"container": "container",
30+
"cronjob": "cronjob",
3031
"daemonset": "daemonset",
3132
"deployment": "deployment",
3233
"ingress": "ingress",

tests/k8s/cronjob.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: batch/v1beta1
2+
kind: CronJob
3+
metadata:
4+
name: skydive-test-cronjob
5+
spec:
6+
schedule: "*/1 * * * *"
7+
jobTemplate:
8+
spec:
9+
template:
10+
spec:
11+
containers:
12+
- name: hello
13+
image: busybox
14+
args:
15+
- /bin/sh
16+
- -c
17+
- date; echo Hello from the Kubernetes cluster
18+
restartPolicy: OnFailure
19+

tests/k8s_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ func TestK8sContainerNode(t *testing.T) {
165165
testNodeCreationFromConfig(t, "container", objName+"-container", "Image", "Labels", "Pod")
166166
}
167167

168+
func TestK8sCronJobNode(t *testing.T) {
169+
testNodeCreationFromConfig(t, "cronjob", objName+"-cronjob")
170+
}
171+
168172
func TestK8sDeploymentNode(t *testing.T) {
169173
testNodeCreationFromConfig(t, "deployment", objName+"-deployment")
170174
}

topology/probes/k8s/cronjob.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (C) 2018 IBM, Inc.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
*/
22+
23+
package k8s
24+
25+
import (
26+
"fmt"
27+
28+
"github.com/skydive-project/skydive/logging"
29+
"github.com/skydive-project/skydive/probe"
30+
"github.com/skydive-project/skydive/topology/graph"
31+
32+
"k8s.io/api/batch/v1beta1"
33+
"k8s.io/client-go/tools/cache"
34+
)
35+
36+
type cronJobProbe struct {
37+
defaultKubeCacheEventHandler
38+
*kubeCache
39+
graph *graph.Graph
40+
}
41+
42+
func dumpCronJob(cj *v1beta1.CronJob) string {
43+
return fmt.Sprintf("cronjob{Namespace: %s, Name: %s}", cj.Namespace, cj.Name)
44+
}
45+
46+
func (p *cronJobProbe) newMetadata(cj *v1beta1.CronJob) graph.Metadata {
47+
m := newMetadata("cronjob", cj.Namespace, cj.Name, cj)
48+
m.SetField("Schedule", cj.Spec.Schedule)
49+
m.SetField("Suspended", cj.Spec.Suspend != nil && *cj.Spec.Suspend)
50+
return m
51+
}
52+
53+
func cronJobUID(cj *v1beta1.CronJob) graph.Identifier {
54+
return graph.Identifier(cj.GetUID())
55+
}
56+
57+
func (p *cronJobProbe) OnAdd(obj interface{}) {
58+
if cj, ok := obj.(*v1beta1.CronJob); ok {
59+
p.graph.Lock()
60+
defer p.graph.Unlock()
61+
62+
newNode(p.graph, cronJobUID(cj), p.newMetadata(cj))
63+
logging.GetLogger().Debugf("Added %s", dumpCronJob(cj))
64+
}
65+
}
66+
67+
func (p *cronJobProbe) OnUpdate(oldObj, newObj interface{}) {
68+
if cj, ok := newObj.(*v1beta1.CronJob); ok {
69+
p.graph.Lock()
70+
defer p.graph.Unlock()
71+
72+
if node := p.graph.GetNode(cronJobUID(cj)); node != nil {
73+
addMetadata(p.graph, node, cj)
74+
logging.GetLogger().Debugf("Updated %s", dumpCronJob(cj))
75+
}
76+
}
77+
}
78+
79+
func (p *cronJobProbe) OnDelete(obj interface{}) {
80+
if cj, ok := obj.(*v1beta1.CronJob); ok {
81+
p.graph.Lock()
82+
defer p.graph.Unlock()
83+
84+
if node := p.graph.GetNode(cronJobUID(cj)); node != nil {
85+
p.graph.DelNode(node)
86+
logging.GetLogger().Debugf("Deleted %s", dumpCronJob(cj))
87+
}
88+
}
89+
}
90+
91+
func (p *cronJobProbe) Start() {
92+
p.kubeCache.Start()
93+
}
94+
95+
func (p *cronJobProbe) Stop() {
96+
p.kubeCache.Stop()
97+
}
98+
99+
func newCronJobKubeCache(handler cache.ResourceEventHandler) *kubeCache {
100+
return newKubeCache(getClientset().BatchV1beta1().RESTClient(), &v1beta1.CronJob{}, "cronjobs", handler)
101+
}
102+
103+
func newCronJobProbe(g *graph.Graph) probe.Probe {
104+
p := &cronJobProbe{
105+
graph: g,
106+
}
107+
p.kubeCache = newCronJobKubeCache(p)
108+
return p
109+
}

topology/probes/k8s/namespace.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type namespaceProbe struct {
4545

4646
func newObjectIndexer(g *graph.Graph) *graph.MetadataIndexer {
4747
ownedByNamespaceFilter := filters.NewOrFilter(
48+
filters.NewTermStringFilter("Type", "cronjob"),
4849
filters.NewTermStringFilter("Type", "deployment"),
4950
filters.NewTermStringFilter("Type", "daemonset"),
5051
filters.NewTermStringFilter("Type", "ingress"),

topology/probes/k8s/probe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func makeProbeBundle(g *graph.Graph) *probe.ProbeBundle {
5151
name2ctor := map[string](func(*graph.Graph) probe.Probe){
5252
"cluster": newClusterProbe,
5353
"container": newContainerProbe,
54+
"cronjob": newCronJobProbe,
5455
"daemonset": newDaemonSetProbe,
5556
"deployment": newDeploymentProbe,
5657
"ingress": newIngressProbe,

0 commit comments

Comments
 (0)