/
pod.go
151 lines (123 loc) · 4.02 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright 2022 The SODA Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package backup
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
metaservice "github.com/soda-cdm/kahu/providerframework/metaservice/lib/go"
"github.com/soda-cdm/kahu/utils"
)
func (ctrl *controller) podBackup(namespace string,
backup *PrepareBackup, backupClient metaservice.MetaService_BackupClient) error {
ctrl.logger.Infoln("Starting collecting pods")
var podLabelList []map[string]string
var labelSelectors map[string]string
if backup.Spec.Label != nil {
labelSelectors = backup.Spec.Label.MatchLabels
}
selectors := labels.Set(labelSelectors).String()
podList, err := ctrl.kubeClient.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: selectors,
})
if err != nil {
return err
}
var podAllList []string
for _, pod := range podList.Items {
podAllList = append(podAllList, pod.Name)
}
podAllList = utils.FindMatchedStrings(utils.Pod, podAllList, backup.Spec.IncludeResources,
backup.Spec.ExcludeResources)
// only for backup
for _, pod := range podList.Items {
if utils.Contains(podAllList, pod.Name) {
// skiping the pod backup because it is not deployed independently
if pod.GetOwnerReferences() != nil {
ctrl.logger.Infof("skipping the backup for pod:%s, because it has owenreference", pod.Name)
continue
}
// Run pre hooks for the pod
// backup the deployment yaml
err = ctrl.backupSend(&pod, pod.Name, backupClient)
if err != nil {
return err
}
// backup the volumespec releted object like, configmaps, secret, pvc and sc
err = ctrl.GetVolumesSpec(pod.Spec, pod.Namespace, backupClient)
if err != nil {
return err
}
// get service account relared objects
err = ctrl.GetServiceAccountSpec(pod.Spec, pod.Namespace, backupClient)
if err != nil {
return err
}
// collect all the clusterrolebindings
err = ctrl.getclusterRoleBindings(pod.Spec.ServiceAccountName, backup, backupClient)
if err != nil {
return err
}
// collect all the rolebindings
err = ctrl.getRoleBindings(pod.Spec.ServiceAccountName, pod.Namespace,
backup, backupClient)
if err != nil {
return err
}
// collect all envFrom and backup
err = ctrl.getEnvFromsForPodSpec(pod.Spec, pod.Namespace, backupClient)
if err != nil {
return err
}
// collect all env value and backup
err = ctrl.getEnvValueForPodSpec(pod.Spec, pod.Namespace, backupClient)
if err != nil {
return err
}
// append the lables of pods to list
podLabelList = append(podLabelList, pod.Labels)
}
}
// get services used by pod
err = ctrl.GetServiceForPod(namespace, podLabelList, backupClient)
if err != nil {
return err
}
return nil
}
func (ctrl *controller) GetServiceForPod(namespace string, podLabelList []map[string]string,
backupClient metaservice.MetaService_BackupClient) error {
allServices, err := ctrl.kubeClient.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
var allServicesList []string
for _, sc := range allServices.Items {
allServicesList = append(allServicesList, sc.Name)
}
for _, service := range allServices.Items {
for skey, svalue := range service.Spec.Selector {
for _, labels := range podLabelList {
for lkey, lvalue := range labels {
if skey == lkey && svalue == lvalue {
err = ctrl.backupSend(&service, service.Name, backupClient)
if err != nil {
return err
}
}
}
}
}
}
return nil
}