Skip to content

Commit

Permalink
Sort assets according to their creation order preference
Browse files Browse the repository at this point in the history
Make sure SAs and Roles are created before bindings, followed
by other resources like DS and Deployments.
  • Loading branch information
bertinatto committed Dec 14, 2023
1 parent c537bf7 commit 515e5eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
48 changes: 42 additions & 6 deletions pkg/generated-assets/generated_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,47 @@ func (a *CSIDriverAssets) getRawAsset(generatedAssetName string) ([]byte, error)
return nil, fmt.Errorf("asset %s not found", generatedAssetName)
}

// assetManager organizes assets into three stages based on their kind.
type assetManager 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 *assetManager) 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 *assetManager) 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
}

// GetControllerStaticAssetNames returns the generated names of all static assets deployed in the control plane
// 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(assetManager)
for name, yaml := range a.ControllerAssets {
kind, err := getYAMLKind(yaml)
if err != nil {
Expand All @@ -95,16 +131,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(assetManager)
for name, yaml := range a.GuestAssets {
kind, err := getYAMLKind(yaml)
if err != nil {
Expand All @@ -115,9 +151,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
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 515e5eb

Please sign in to comment.