|
| 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 | +} |
0 commit comments