Skip to content

Commit

Permalink
Order assets according to their creation preference
Browse files Browse the repository at this point in the history
Make sure Roles are created before RoleBindings, followed
by other resources like DS and Deployments.
  • Loading branch information
bertinatto authored and openshift-cherrypick-robot committed Jan 4, 2024
1 parent ac65fc2 commit d3a2692
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
12 changes: 6 additions & 6 deletions pkg/generated-assets/generated_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (a *CSIDriverAssets) getRawAsset(generatedAssetName string) ([]byte, error)
// namespace or standalone cluster. These assets should be managed by a StaticResourcesController.
// Any Deployment is filtered out from the list, they're supposed to be handled by DeploymentController.
func (a *CSIDriverAssets) GetControllerStaticAssetNames() []string {
var names []string
assets := new(AssetOrderer)
for name, yaml := range a.ControllerAssets {
kind, err := getYAMLKind(yaml)
if err != nil {
Expand All @@ -95,16 +95,16 @@ func (a *CSIDriverAssets) GetControllerStaticAssetNames() []string {
continue
}
klog.V(4).Infof("Added %s %s to controller static assets", kind, name)
names = append(names, name)
assets.Add(name, kind)
}
return names
return assets.GetAll()
}

// GetGuestStaticAssetNames returns the generated names of all static assets deployed in the guest cluster (or
// standalone cluster). These assets should be managed by a StaticResourcesController. Any DaemonSet, StorageClass or
// VolumeSnapshotClass is filtered out from the list, they must be handled by their own specific controllers.
func (a *CSIDriverAssets) GetGuestStaticAssetNames() []string {
var names []string
assets := new(AssetOrderer)
for name, yaml := range a.GuestAssets {
kind, err := getYAMLKind(yaml)
if err != nil {
Expand All @@ -115,9 +115,9 @@ func (a *CSIDriverAssets) GetGuestStaticAssetNames() []string {
continue
}
klog.V(4).Infof("Added %s %s to guest static assets", kind, name)
names = append(names, name)
assets.Add(name, kind)
}
return names
return assets.GetAll()
}

// GetStorageClassAssetNames returns the names of all generated StorageClass assets.
Expand Down
38 changes: 38 additions & 0 deletions pkg/generated-assets/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,41 @@ func initialComments(src []byte) ([]byte, error) {
comments.Write(line)
}
}

// AssetOrderer organizes assets into three stages based on their kind.
// Its main purpose is to order asset names according to their creation preference.
// For instance, typically RBAC Roles should be created before RoleBindings.
type AssetOrderer struct {
// stage1 contains standalone resources that are typically referenced by other resources.
// Examples include ClusterRoles and Roles.
stage1 []string

// stage2 contains resources that reference other resources from stage1.
// Examples include ClusterRoleBindings and RoleBindings.
stage2 []string

// stage3 contains resources that should be created last and may depend on resources from stage1 and stage2.
// Examples include Deployments and DaemonSets.
stage3 []string
}

// Add adds an asset to the appropriate stage based on its kind.
func (a *AssetOrderer) Add(name, kind string) {
switch kind {
case "ClusterRole.rbac.authorization.k8s.io", "Role.rbac.authorization.k8s.io":
a.stage1 = append(a.stage1, name)
case "ClusterRoleBinding.rbac.authorization.k8s.io", "RoleBinding.rbac.authorization.k8s.io":
a.stage2 = append(a.stage2, name)
default:
a.stage3 = append(a.stage3, name)
}
}

// Get returns a concatenated list of assets from all stages.
func (a *AssetOrderer) GetAll() []string {
result := make([]string, 0, len(a.stage1)+len(a.stage2)+len(a.stage3))
result = append(result, a.stage1...)
result = append(result, a.stage2...)
result = append(result, a.stage3...)
return result
}
2 changes: 1 addition & 1 deletion pkg/generator/asset_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"strconv"

"github.com/openshift/csi-operator/pkg/generated-assets"
generated_assets "github.com/openshift/csi-operator/pkg/generated-assets"
)

// AssetGenerator generates assets for CSI driver operators.
Expand Down

0 comments on commit d3a2692

Please sign in to comment.