Skip to content

Commit

Permalink
refactor: cleanup tekton operands
Browse files Browse the repository at this point in the history
Cleanup Tekton operands to improve code quality
and readability.

Signed-off-by: Ben Oukhanov <[email protected]>
  • Loading branch information
codingben committed Aug 7, 2023
1 parent fc1d67c commit 096ed4f
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 131 deletions.
18 changes: 9 additions & 9 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ rules:
- list
- update
- watch
- apiGroups:
- '*'
resources:
- configmaps
verbs:
- create
- delete
- list
- watch
- apiGroups:
- '*'
resources:
Expand Down Expand Up @@ -142,6 +133,15 @@ rules:
- infrastructures
verbs:
- get
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
18 changes: 9 additions & 9 deletions data/olm-catalog/ssp-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ spec:
- list
- update
- watch
- apiGroups:
- '*'
resources:
- configmaps
verbs:
- create
- delete
- list
- watch
- apiGroups:
- '*'
resources:
Expand Down Expand Up @@ -203,6 +194,15 @@ spec:
- infrastructures
verbs:
- get
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
11 changes: 11 additions & 0 deletions internal/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,14 @@ func defaultStatusFunc(obj client.Object) ResourceStatus {
}
return status
}

func AppendDeepCopies[PT interface {
*T
client.Object
}, T any](destination []client.Object, objects []T) []client.Object {
for i := range objects {
var object = PT(&objects[i])
destination = append(destination, object.DeepCopyObject().(client.Object))
}
return destination
}
29 changes: 8 additions & 21 deletions internal/operands/tekton-pipelines/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// +kubebuilder:rbac:groups=tekton.dev,resources=pipelines,verbs=list;watch;create;update;delete
// +kubebuilder:rbac:groups=*,resources=configmaps,verbs=list;watch;create;delete
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=list;watch;create;delete
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,verbs=list;watch;create;update;delete

const (
Expand Down Expand Up @@ -115,33 +115,20 @@ func (t *tektonPipelines) Reconcile(request *common.Request) ([]common.Reconcile

func (t *tektonPipelines) Cleanup(request *common.Request) ([]common.CleanupResult, error) {
var objects []client.Object

if request.CrdList.CrdExists(tektonCrd) {
for _, p := range t.pipelines {
o := p.DeepCopy()
objects = append(objects, o)
}
}
for _, cm := range t.configMaps {
o := cm.DeepCopy()
objects = append(objects, o)
}
for _, rb := range t.roleBindings {
o := rb.DeepCopy()
objects = append(objects, o)
}
for _, sa := range t.serviceAccounts {
o := sa.DeepCopy()
objects = append(objects, o)
objects = common.AppendDeepCopies(objects, t.pipelines)
}

objects = common.AppendDeepCopies(objects, t.configMaps)
objects = common.AppendDeepCopies(objects, t.roleBindings)
objects = common.AppendDeepCopies(objects, t.serviceAccounts)

for i := range objects {
objects[i].SetNamespace(getTektonPipelinesNamespace(request))
}

for _, cr := range t.clusterRoles {
o := cr.DeepCopy()
objects = append(objects, o)
}
objects = common.AppendDeepCopies(objects, t.clusterRoles)

return common.DeleteAll(request, objects...)
}
Expand Down
87 changes: 31 additions & 56 deletions internal/operands/tekton-tasks/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
v1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"kubevirt.io/ssp-operator/internal/common"
"kubevirt.io/ssp-operator/internal/operands"
Expand Down Expand Up @@ -87,43 +88,11 @@ type tektonTasks struct {
var _ operands.Operand = &tektonTasks{}

func New(bundle *tektonbundle.Bundle) operands.Operand {
newTasks := []pipeline.Task{}
for _, task := range bundle.Tasks {
if _, ok := AllowedTasks[task.Name]; ok {
newTasks = append(newTasks, task)
}
}
bundle.Tasks = newTasks

newServiceAccounts := []v1.ServiceAccount{}
for _, serviceAccount := range bundle.ServiceAccounts {
if _, ok := AllowedTasks[strings.TrimSuffix(serviceAccount.Name, "-task")]; ok {
newServiceAccounts = append(newServiceAccounts, serviceAccount)
}
}
bundle.ServiceAccounts = newServiceAccounts

newRoleBinding := []rbac.RoleBinding{}
for _, roleBinding := range bundle.RoleBindings {
if _, ok := AllowedTasks[strings.TrimSuffix(roleBinding.Name, "-task")]; ok {
newRoleBinding = append(newRoleBinding, roleBinding)
}
}
bundle.RoleBindings = newRoleBinding

newClusterRole := []rbac.ClusterRole{}
for _, clusterRole := range bundle.ClusterRoles {
if _, ok := AllowedTasks[strings.TrimSuffix(clusterRole.Name, "-task")]; ok {
newClusterRole = append(newClusterRole, clusterRole)
}
}
bundle.ClusterRoles = newClusterRole

return &tektonTasks{
tasks: bundle.Tasks,
serviceAccounts: bundle.ServiceAccounts,
roleBindings: bundle.RoleBindings,
clusterRoles: bundle.ClusterRoles,
tasks: filterTektonResources(bundle.Tasks, false),
serviceAccounts: filterTektonResources(bundle.ServiceAccounts, true),
roleBindings: filterTektonResources(bundle.RoleBindings, true),
clusterRoles: filterTektonResources(bundle.ClusterRoles, true),
}
}

Expand Down Expand Up @@ -174,39 +143,27 @@ func (t *tektonTasks) Reconcile(request *common.Request) ([]common.ReconcileResu

func (t *tektonTasks) Cleanup(request *common.Request) ([]common.CleanupResult, error) {
var objects []client.Object

if request.CrdList.CrdExists(tektonCrd) {
for _, t := range t.tasks {
o := t.DeepCopy()
objects = append(objects, o)
}
}
for _, rb := range t.roleBindings {
o := rb.DeepCopy()
objects = append(objects, o)
}
for _, sa := range t.serviceAccounts {
o := sa.DeepCopy()
objects = append(objects, o)
objects = common.AppendDeepCopies(objects, t.tasks)
}

objects = common.AppendDeepCopies(objects, t.roleBindings)
objects = common.AppendDeepCopies(objects, t.serviceAccounts)

for i := range objects {
objects[i].SetNamespace(getTektonTasksNamespace(request))
}

for _, cr := range t.clusterRoles {
o := cr.DeepCopy()
objects = append(objects, o)
}
objects = common.AppendDeepCopies(objects, t.clusterRoles)

if request.CrdList.CrdExists(tektonCrd) {
clusterTasks, err := listDeprecatedClusterTasks(request)
if err != nil {
return nil, err
}
for _, ct := range clusterTasks {
o := ct.DeepCopy()
objects = append(objects, o)
}

objects = common.AppendDeepCopies(objects, clusterTasks)
}

return common.DeleteAll(request, objects...)
Expand Down Expand Up @@ -307,3 +264,21 @@ func getTektonTasksNamespace(request *common.Request) string {
}
return request.Instance.Namespace
}

func filterTektonResources[PT interface {
*T
metav1.Object
}, T any](items []T, trimSuffix bool) []T {
var results []T
for i := range items {
object := PT(&items[i])
name := object.GetName()
if trimSuffix {
name = strings.TrimSuffix(name, "-task")
}
if _, ok := AllowedTasks[name]; ok {
results = append(results, *object)
}
}
return results
}
Loading

0 comments on commit 096ed4f

Please sign in to comment.