Skip to content

Commit 94b9434

Browse files
authored
Merge pull request #407 from cryptobioz/add-watch-namespaces-option
feat(kubernetes): allow backup volumes from a custom list of namespaces
2 parents 66275b9 + 68e4490 commit 94b9434

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

cmd/manager/manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func init() {
9292
envs["KUBERNETES_NAMESPACE"] = "kubernetes.namespace"
9393
managerCmd.Flags().BoolVarP(&Orchestrators.Kubernetes.AllNamespaces, "kubernetes.all-namespaces", "", false, "Backup volumes of all namespaces.")
9494
envs["KUBERNETES_ALL_NAMESPACES"] = "kubernetes.all-namespaces"
95+
managerCmd.Flags().StringVarP(&Orchestrators.Kubernetes.WatchNamespaces, "kubernetes.watch-namespaces", "", "", "List of namespaces to watch for volumes to backup.")
96+
envs["KUBERNETES_WATCH_NAMESPACES"] = "kubernetes.watch-namespaces"
9597
managerCmd.Flags().StringVarP(&Orchestrators.Kubernetes.KubeConfig, "kubernetes.kubeconfig", "", "", "Path to your kuberconfig file.")
9698
envs["KUBERNETES_KUBECONFIG"] = "kubernetes.kubeconfig"
9799
managerCmd.Flags().StringVarP(&Orchestrators.Kubernetes.AgentServiceAccount, "kubernetes.agent-service-account", "", "", "Specify service account for agents.")

pkg/orchestrators/kubernetes.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
type KubernetesConfig struct {
2525
Namespace string
2626
AllNamespaces bool
27+
WatchNamespaces string
2728
KubeConfig string
2829
AgentServiceAccount string
2930
AgentLabelsInline string
@@ -224,8 +225,8 @@ func (o *KubernetesOrchestrator) DeployAgent(image string, cmd, envs []string, v
224225
RestartPolicy: "Never",
225226
Volumes: kvs,
226227
ServiceAccountName: o.config.AgentServiceAccount,
227-
SecurityContext: &apiv1.PodSecurityContext{
228-
SupplementalGroups: managerPod.Spec.SecurityContext.SupplementalGroups,
228+
SecurityContext: &apiv1.PodSecurityContext{
229+
SupplementalGroups: managerPod.Spec.SecurityContext.SupplementalGroups,
229230
},
230231

231232
Containers: []apiv1.Container{
@@ -556,6 +557,20 @@ func (o *KubernetesOrchestrator) getNamespaces() (namespaces []string, err error
556557
for _, namespace := range nms.Items {
557558
namespaces = append(namespaces, namespace.Name)
558559
}
560+
} else if o.config.WatchNamespaces != "" {
561+
nms, err := o.client.CoreV1().Namespaces().List(metav1.ListOptions{})
562+
if err != nil {
563+
err = fmt.Errorf("failed to retrieve the list of namespaces: %s", err)
564+
return []string{}, err
565+
}
566+
watchNamespaces := strings.Split(o.config.WatchNamespaces, ",")
567+
for _, namespace := range nms.Items {
568+
for _, ns := range watchNamespaces {
569+
if ns == namespace.Name {
570+
namespaces = append(namespaces, namespace.Name)
571+
}
572+
}
573+
}
559574
} else {
560575
namespaces = append(namespaces, o.config.Namespace)
561576
}

0 commit comments

Comments
 (0)