Skip to content

Commit aef5257

Browse files
vikasit12siva-portworx
authored andcommitted
PB-3046 :: Restrict portworx ns backup
- Restrict backup of namespace where portworx is installed in case of all namespaces i.e. * - Restrict backup of namespace where portworx is installed in case of label-selector - Allow backup of namespace where portworx is installed in case API is specifically passing it i.e. namsespace=kube-system in API call
1 parent 9c01d43 commit aef5257

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pkg/applicationmanager/controllers/applicationbackup.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,10 @@ func (a *ApplicationBackupController) updateWithAllNamespaces(backup *stork_api.
221221
if err != nil {
222222
return fmt.Errorf("error updating with all namespaces for wildcard: %v", err)
223223
}
224+
pxNs, _ := utils.GetPortworxNamespace()
224225
namespacesToBackup := make([]string, 0)
225226
for _, ns := range namespaces.Items {
226-
if ns.Name != "kube-system" {
227+
if ns.Name != "kube-system" && ns.Name != pxNs {
227228
namespacesToBackup = append(namespacesToBackup, ns.Name)
228229
}
229230
}
@@ -284,6 +285,7 @@ func (a *ApplicationBackupController) handle(ctx context.Context, backup *stork_
284285
return nil
285286
}
286287
if labelSelector := backup.Spec.NamespaceSelector; len(labelSelector) != 0 {
288+
var pxNs string
287289
namespaces, err := core.Instance().ListNamespacesV2(labelSelector)
288290
if err != nil {
289291
errMsg := fmt.Sprintf("error listing namespaces with label selectors: %v, error: %v", labelSelector, err)
@@ -295,8 +297,11 @@ func (a *ApplicationBackupController) handle(ctx context.Context, backup *stork_
295297
return nil
296298
}
297299
var selectedNamespaces []string
300+
if len(backup.Spec.Namespaces) == 0 {
301+
pxNs, _ = utils.GetPortworxNamespace()
302+
}
298303
for _, namespace := range namespaces.Items {
299-
if namespace.Name != "kube-system" {
304+
if namespace.Name != "kube-system" && namespace.Name != pxNs {
300305
selectedNamespaces = append(selectedNamespaces, namespace.Name)
301306
}
302307
}

pkg/utils/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sirupsen/logrus"
1717
v1 "k8s.io/api/core/v1"
1818
"k8s.io/apimachinery/pkg/api/meta"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1920
"k8s.io/apimachinery/pkg/types"
2021
"k8s.io/apimachinery/pkg/util/validation"
2122
)
@@ -93,6 +94,8 @@ const (
9394
StorkAPIVersion = "stork.libopenstorage.org/v1alpha1"
9495
// BackupLocationKind CR kind
9596
BackupLocationKind = "BackupLocation"
97+
// PXServiceName is the name of the portworx service in kubernetes
98+
PXServiceName = "portworx-service"
9699
)
97100

98101
// ParseKeyValueList parses a list of key=values string into a map
@@ -262,3 +265,18 @@ func GetStashedConfigMapName(objKind string, group string, objName string) strin
262265
}
263266
return cmName
264267
}
268+
269+
func GetPortworxNamespace() (string, error) {
270+
allServices, err := core.Instance().ListServices("", metav1.ListOptions{})
271+
if err != nil {
272+
logrus.Errorf("error in getting list of all services")
273+
return "", fmt.Errorf("failed to get list of services. Err: %v", err)
274+
}
275+
for _, svc := range allServices.Items {
276+
if svc.Name == PXServiceName {
277+
return svc.Namespace, nil
278+
}
279+
}
280+
logrus.Warnf("unable to find [%s] service in cluster", PXServiceName)
281+
return "", fmt.Errorf("can't find [%s] Portworx service from list of services", PXServiceName)
282+
}

0 commit comments

Comments
 (0)