-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backups: include clusterroles/bindings that reference serviceaccounts
Signed-off-by: Steve Kriss <[email protected]>
- Loading branch information
Showing
4 changed files
with
156 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2018 the Heptio Ark contributors. | ||
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 ( | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
||
rbac "k8s.io/api/rbac/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/heptio/ark/pkg/apis/ark/v1" | ||
) | ||
|
||
// serviceAccountAction implements ItemAction. | ||
type serviceAccountAction struct { | ||
log logrus.FieldLogger | ||
clientset kubernetes.Interface | ||
} | ||
|
||
// NewServiceAccountAction creates a new ItemAction for service accounts. | ||
func NewServiceAccountAction(log logrus.FieldLogger, clientset kubernetes.Interface) ItemAction { | ||
return &serviceAccountAction{ | ||
log: log, | ||
clientset: clientset, | ||
} | ||
} | ||
|
||
// AppliesTo returns a ResourceSelector that applies only to service accounts. | ||
func (a *serviceAccountAction) AppliesTo() (ResourceSelector, error) { | ||
return ResourceSelector{ | ||
IncludedResources: []string{"serviceaccounts"}, | ||
}, nil | ||
} | ||
|
||
var ( | ||
crbGroupResource = schema.GroupResource{Group: "rbac.authorization.k8s.io", Resource: "clusterrolebindings"} | ||
crGroupResource = schema.GroupResource{Group: "rbac.authorization.k8s.io", Resource: "clusterroles"} | ||
) | ||
|
||
// Execute checks for any ClusterRoleBindings that have this service account as a subject, and | ||
// adds the ClusterRoleBinding and associated ClusterRole to the list of additional items to | ||
// be backed up. | ||
func (a *serviceAccountAction) Execute(item runtime.Unstructured, backup *v1.Backup) (runtime.Unstructured, []ResourceIdentifier, error) { | ||
a.log.Info("Running serviceAccountAction") | ||
defer a.log.Info("Done running serviceAccountAction") | ||
|
||
crbList, err := a.clientset.RbacV1().ClusterRoleBindings().List(metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, nil, errors.WithStack(err) | ||
} | ||
|
||
objectMeta, err := meta.Accessor(item) | ||
if err != nil { | ||
return nil, nil, errors.WithStack(err) | ||
} | ||
|
||
var ( | ||
namespace = objectMeta.GetNamespace() | ||
name = objectMeta.GetName() | ||
bindings = sets.NewString() | ||
roles = sets.NewString() | ||
) | ||
|
||
for _, crb := range crbList.Items { | ||
for _, subj := range crb.Subjects { | ||
if subj.Kind == rbac.ServiceAccountKind && subj.Namespace == namespace && subj.Name == name { | ||
bindings.Insert(crb.Name) | ||
roles.Insert(crb.RoleRef.Name) | ||
break | ||
} | ||
} | ||
} | ||
|
||
var additionalItems []ResourceIdentifier | ||
for binding := range bindings { | ||
additionalItems = append(additionalItems, ResourceIdentifier{ | ||
GroupResource: crbGroupResource, | ||
Name: binding, | ||
}) | ||
} | ||
|
||
for role := range roles { | ||
additionalItems = append(additionalItems, ResourceIdentifier{ | ||
GroupResource: crGroupResource, | ||
Name: role, | ||
}) | ||
} | ||
|
||
return item, additionalItems, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters