Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude default serviceaccount on backup #195

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions charts/rancher-backup-crd/templates/resourceset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ spec:
type: string
nullable: true
type: array
excludeResourceNameRegexp:
nullable: true
type: string
kinds:
items:
nullable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- apiVersion: "v1"
kindsRegexp: "^serviceaccounts$"
namespaceRegexp: "^cattle-fleet-|^fleet-|^cluster-fleet-"
excludeResourceNameRegexp: "^default$"
- apiVersion: "v1"
kindsRegexp: "^configmaps$"
namespaceRegexp: "^cattle-fleet-|^fleet-|^cluster-fleet-"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
kindsRegexp: "^serviceaccounts$"
namespaces:
- "rancher-operator-system"
excludeResourceNameRegexp: "^default$"
- apiVersion: "rbac.authorization.k8s.io/v1"
kindsRegexp: "^clusterrolebindings$"
resourceNames:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- apiVersion: "v1"
kindsRegexp: "^serviceaccounts$"
namespaceRegexp: "^cattle-|^p-|^c-|^local$|^user-|^u-"
excludeResourceNameRegexp: "^default$"
- apiVersion: "v1"
kindsRegexp: "^configmaps$"
namespaces:
Expand Down
19 changes: 10 additions & 9 deletions pkg/apis/resources.cattle.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ type ResourceSet struct {

// regex+list = OR //separate fields :AND
type ResourceSelector struct {
APIVersion string `json:"apiVersion"`
Kinds []string `json:"kinds,omitempty"`
KindsRegexp string `json:"kindsRegexp,omitempty"`
ResourceNames []string `json:"resourceNames,omitempty"`
ResourceNameRegexp string `json:"resourceNameRegexp,omitempty"`
Namespaces []string `json:"namespaces,omitempty"`
NamespaceRegexp string `json:"namespaceRegexp,omitempty"`
LabelSelectors *metav1.LabelSelector `json:"labelSelectors,omitempty"`
ExcludeKinds []string `json:"excludeKinds,omitempty"`
APIVersion string `json:"apiVersion"`
Kinds []string `json:"kinds,omitempty"`
KindsRegexp string `json:"kindsRegexp,omitempty"`
ResourceNames []string `json:"resourceNames,omitempty"`
ResourceNameRegexp string `json:"resourceNameRegexp,omitempty"`
Namespaces []string `json:"namespaces,omitempty"`
NamespaceRegexp string `json:"namespaceRegexp,omitempty"`
LabelSelectors *metav1.LabelSelector `json:"labelSelectors,omitempty"`
ExcludeKinds []string `json:"excludeKinds,omitempty"`
ExcludeResourceNameRegexp string `json:"excludeResourceNameRegexp,omitempty"`
}

type ControllerReference struct {
Expand Down
20 changes: 19 additions & 1 deletion pkg/resourcesets/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (h *ResourceHandler) filterByNameAndLabel(ctx context.Context, dr dynamic.R
}
filteredByNameMap := make(map[*unstructured.Unstructured]bool)

if len(filter.ResourceNames) == 0 && filter.ResourceNameRegexp == "" {
if len(filter.ResourceNames) == 0 && filter.ResourceNameRegexp == "" && filter.ExcludeResourceNameRegexp == "" {
// no filters for names of the resource, return all objects obtained from the list call
return resourceObjectsList.Items, nil
}
Expand All @@ -255,6 +255,24 @@ func (h *ResourceHandler) filterByNameAndLabel(ctx context.Context, dr dynamic.R
}
}

// filter out using ExcludeResourceNameRegexp
if filter.ExcludeResourceNameRegexp != "" {
for _, resObj := range resourceObjectsList.Items {
metadata := resObj.Object["metadata"].(map[string]interface{})
name := metadata["name"].(string)
nameMatched, err := regexp.MatchString(filter.ExcludeResourceNameRegexp, name)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding this correctly, this line should return true if the string in the variable name starts or ends with default, but I tried this in the go playground using the pattern ^default$ and got false instead. I do get that behavior if I use the pattern ^default|default$, but I see we are using the ^<string>$ pattern to match other things already so am I missing something? https://go.dev/play/p/7EtHr0IZKfR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exclude is for service accounts called default, the playground shows comparing token names with the regex. We are excluding the default service accounts created in each namespace and they are named default. Let me know if I need to add some clarification in the issue or code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, makes sense now. I think the comment you linked to is good, this is just my first time reviewing the backup-restore-operator.

if err != nil {
return filteredByName, err
}
if nameMatched {
logrus.Debugf("Skipping %s because it did not match ExcludeResourceNameRegexp %s", name, filter.ExcludeResourceNameRegexp)
continue
}
filteredByName = append(filteredByName, resObj)
filteredByNameMap[&resObj] = true
}
}

// filter by names as fieldSelector:
if len(filter.ResourceNames) > 0 {
// TODO: POST-preview-2: set resourceVersion later when it becomes clear how to use it
Expand Down