|
| 1 | +// Package deployment provides the logic for mapping a Kubernetes deployment to a |
| 2 | +// LogicMonitor w. |
| 3 | +package deployment |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/logicmonitor/k8s-argus/pkg/constants" |
| 9 | + "github.com/logicmonitor/k8s-argus/pkg/types" |
| 10 | + "github.com/logicmonitor/k8s-argus/pkg/utilities" |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | + "k8s.io/api/apps/v1beta2" |
| 13 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + resource = "deployments" |
| 20 | +) |
| 21 | + |
| 22 | +// Watcher represents a watcher type that watches deployments. |
| 23 | +type Watcher struct { |
| 24 | + types.DeviceManager |
| 25 | +} |
| 26 | + |
| 27 | +// ApiVersion is a function that implements the Watcher interface. |
| 28 | +func (w *Watcher) ApiVersion() string { |
| 29 | + return constants.K8sApiVersion_apps_v1beta2 |
| 30 | +} |
| 31 | + |
| 32 | +// Resource is a function that implements the Watcher interface. |
| 33 | +func (w *Watcher) Resource() string { |
| 34 | + return resource |
| 35 | +} |
| 36 | + |
| 37 | +// ObjType is a function that implements the Watcher interface. |
| 38 | +func (w *Watcher) ObjType() runtime.Object { |
| 39 | + return &v1beta2.Deployment{} |
| 40 | +} |
| 41 | + |
| 42 | +// AddFunc is a function that implements the Watcher interface. |
| 43 | +func (w *Watcher) AddFunc() func(obj interface{}) { |
| 44 | + return func(obj interface{}) { |
| 45 | + deployment := obj.(*v1beta2.Deployment) |
| 46 | + |
| 47 | + log.Infof("Handling add deployment event: %s", deployment.Name) |
| 48 | + |
| 49 | + w.add(deployment) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// UpdateFunc is a function that implements the Watcher interface. |
| 54 | +func (w *Watcher) UpdateFunc() func(oldObj, newObj interface{}) { |
| 55 | + return func(oldObj, newObj interface{}) { |
| 56 | + old := oldObj.(*v1beta2.Deployment) |
| 57 | + new := newObj.(*v1beta2.Deployment) |
| 58 | + |
| 59 | + w.update(old, new) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// DeleteFunc is a function that implements the Watcher interface. |
| 64 | +func (w *Watcher) DeleteFunc() func(obj interface{}) { |
| 65 | + return func(obj interface{}) { |
| 66 | + deployment := obj.(*v1beta2.Deployment) |
| 67 | + |
| 68 | + // Delete the deployment. |
| 69 | + if w.Config().DeleteDevices { |
| 70 | + if err := w.DeleteByDisplayName(fmtDeploymentDisplayName(deployment)); err != nil { |
| 71 | + log.Errorf("Failed to delete deployment: %v", err) |
| 72 | + return |
| 73 | + } |
| 74 | + log.Infof("Deleted deployment %s", deployment.Name) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + // Move the deployment. |
| 79 | + w.move(deployment) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// nolint: dupl |
| 84 | +func (w *Watcher) add(deployment *v1beta2.Deployment) { |
| 85 | + if _, err := w.Add( |
| 86 | + w.args(deployment, constants.DeploymentCategory)..., |
| 87 | + ); err != nil { |
| 88 | + log.Errorf("Failed to add deployment %q: %v", fmtDeploymentDisplayName(deployment), err) |
| 89 | + return |
| 90 | + } |
| 91 | + log.Infof("Added deployment %q", fmtDeploymentDisplayName(deployment)) |
| 92 | +} |
| 93 | + |
| 94 | +func (w *Watcher) update(old, new *v1beta2.Deployment) { |
| 95 | + if _, err := w.UpdateAndReplaceByDisplayName( |
| 96 | + fmtDeploymentDisplayName(old), |
| 97 | + w.args(new, constants.DeploymentCategory)..., |
| 98 | + ); err != nil { |
| 99 | + log.Errorf("Failed to update deployment %q: %v", fmtDeploymentDisplayName(new), err) |
| 100 | + return |
| 101 | + } |
| 102 | + log.Infof("Updated deployment %q", fmtDeploymentDisplayName(old)) |
| 103 | +} |
| 104 | + |
| 105 | +func (w *Watcher) move(deployment *v1beta2.Deployment) { |
| 106 | + if _, err := w.UpdateAndReplaceFieldByDisplayName(fmtDeploymentDisplayName(deployment), constants.CustomPropertiesFieldName, w.args(deployment, constants.DeploymentDeletedCategory)...); err != nil { |
| 107 | + log.Errorf("Failed to move deployment %q: %v", fmtDeploymentDisplayName(deployment), err) |
| 108 | + return |
| 109 | + } |
| 110 | + log.Infof("Moved deployment %q", fmtDeploymentDisplayName(deployment)) |
| 111 | +} |
| 112 | + |
| 113 | +func (w *Watcher) args(deployment *v1beta2.Deployment, category string) []types.DeviceOption { |
| 114 | + categories := utilities.BuildSystemCategoriesFromLabels(category, deployment.Labels) |
| 115 | + return []types.DeviceOption{ |
| 116 | + w.Name(deployment.Name), |
| 117 | + w.ResourceLabels(deployment.Labels), |
| 118 | + w.DisplayName(fmtDeploymentDisplayName(deployment)), |
| 119 | + w.SystemCategories(categories), |
| 120 | + w.Auto("name", deployment.Name), |
| 121 | + w.Auto("namespace", deployment.Namespace), |
| 122 | + w.Auto("selflink", deployment.SelfLink), |
| 123 | + w.Auto("uid", string(deployment.UID)), |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// fmtDeploymentDisplayName implements the conversion for the deployment display name |
| 128 | +func fmtDeploymentDisplayName(deployment *v1beta2.Deployment) string { |
| 129 | + return fmt.Sprintf("%s.%s.deploy-%s", deployment.Name, deployment.Namespace, string(deployment.UID)) |
| 130 | +} |
| 131 | + |
| 132 | +// GetDeploymentsMap implements the getting deployments map info from k8s |
| 133 | +func GetDeploymentsMap(k8sClient *kubernetes.Clientset, namespace string) (map[string]string, error) { |
| 134 | + deploymentsMap := make(map[string]string) |
| 135 | + deploymentList, err := k8sClient.AppsV1beta2().Deployments(namespace).List(v1.ListOptions{}) |
| 136 | + if err != nil || deploymentList == nil { |
| 137 | + log.Warnf("Failed to get the deployments from k8s") |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + for _, deploymentInfo := range deploymentList.Items { |
| 141 | + deploymentsMap[fmtDeploymentDisplayName(&deploymentInfo)] = deploymentInfo.Name |
| 142 | + } |
| 143 | + |
| 144 | + return deploymentsMap, nil |
| 145 | +} |
0 commit comments