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

Update SCC and RBAC handling for DevWorkspaces #954

Merged
merged 10 commits into from
Nov 7, 2022
8 changes: 8 additions & 0 deletions pkg/common/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,18 @@ func WorkspaceSCCRolebindingName(sccName string) string {
return fmt.Sprintf("devworkspace-use-%s", sccName)
}

// OldWorkspaceRoleName returns the name used for the workspace serviceaccount role
//
// Deprecated: use WorkspaceRoleName() instead.
// TODO: remove for DevWorkspace Operator v0.19
func OldWorkspaceRoleName() string {
return "workspace"
}

// OldWorkspaceRolebindingName returns the name used for the workspace serviceaccount rolebinding
//
// Deprecated: use WorkspaceRoleBindingName() instead.
// TODO: remove for DevWorkspace Operator v0.19
func OldWorkspaceRolebindingName() string {
return constants.ServiceAccount + "dw"
}
3 changes: 3 additions & 0 deletions pkg/provision/workspace/rbac/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func wrapSyncError(err error) error {
}

func SyncRBAC(workspace *common.DevWorkspaceWithConfig, api sync.ClusterAPI) error {
if err := cleanupDeprecatedRBAC(workspace.Namespace, api); err != nil {
return err
}
if err := syncRoles(workspace, api); err != nil {
return err
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/provision/workspace/rbac/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2019-2022 Red Hat, Inc.
// 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 rbac

import (
"fmt"

"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
rbacv1 "k8s.io/api/rbac/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
)

// cleanupDeprecatedRBAC removes old Roles and RoleBindings created by an earlier version
// of the DevWorkspace Operator. These earlier roles and rolebindings are no longer used
// and need to be removed directly as there is no usual mechanism for their removal.
// TODO: Remove this functionality for DevWorkspace Operator v0.19
func cleanupDeprecatedRBAC(namespace string, api sync.ClusterAPI) error {
role := &rbacv1.Role{}
roleNN := types.NamespacedName{
Name: common.OldWorkspaceRoleName(),
Namespace: namespace,
}
err := api.Client.Get(api.Ctx, roleNN, role)
switch {
case err == nil:
if err := api.Client.Delete(api.Ctx, role); err != nil {
return err
}
return &RetryError{fmt.Errorf("deleted deprecated DevWorkspace Role")}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Want to ask why does it return RetryError when deletion succeeds? Is there a reason for restarting the reconciliation instead of continuing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's the general flow we follow for most things. Here it's likely safe to continue the reconcile, but in general the operator works by doing one thing at a time (i.e. given a cluster state, what's the next step towards moving it towards the intended state?). In other words, we're treating "no deprecated DevWorkspace Role exists" as a precondition for continuing with creating the new DevWorkspace Role.

The benefit of doing this is that it should be possible to track what each reconcile did to the cluster (e.g. you would see "reconciling DevWorkspace (...) deleted deprecated DevWorkspace Role".

Though, you make a good point. We could probably get away with just logging that we deleted the role here. I'll update the PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

On further thought -- the tests use this error to verify migration is proceeding as expected, so I think it's better to leave for now. The current plan is to remove code related to cleaning up old resources around the DWO 0.19.0 or 0.20.0 timeframe.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see thank you @amisevsk , I was just curious, sounds good 👍

case k8sErrors.IsNotFound(err):
break
default:
return err
}
rolebinding := &rbacv1.RoleBinding{}
rolebindingNN := types.NamespacedName{
Name: common.OldWorkspaceRolebindingName(),
Namespace: namespace,
}
err = api.Client.Get(api.Ctx, rolebindingNN, rolebinding)
switch {
case err == nil:
if err := api.Client.Delete(api.Ctx, rolebinding); err != nil {
return err
}
return &RetryError{fmt.Errorf("deleted deprecated DevWorkspace RoleBinding")}
case k8sErrors.IsNotFound(err):
break
default:
return err
}
return nil
}