Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

.PHONY: lint
lint: ## Run golangci-lint against code.
@bash -o pipefail -c 'curl -fsSL https://raw.githubusercontent.com/open-cluster-management-io/sdk-go/main/ci/lint/run-lint.sh | bash'

.PHONY: deps
deps:
go mod tidy
Expand Down
12 changes: 0 additions & 12 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ limitations under the License.
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)
Expand All @@ -46,13 +44,3 @@ var (
func Resource(resource string) schema.GroupResource {
return schema.GroupResource{Group: GroupVersion.Group, Resource: resource}
}

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(GroupVersion,
&ClusterPermission{},
&ClusterPermissionList{},
)
metav1.AddToGroupVersion(scheme, GroupVersion)
return nil
}
22 changes: 10 additions & 12 deletions controllers/clusterpermission_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"

addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
workv1 "open-cluster-management.io/api/work/v1"
Expand All @@ -45,8 +46,6 @@ import (
msacommon "open-cluster-management.io/managed-serviceaccount/pkg/common"
)

const VALIDATION_MW_RETRY_INTERVAL = 10 * time.Second

// ClusterPermissionReconciler reconciles a ClusterPermission object
type ClusterPermissionReconciler struct {
client.Client
Expand Down Expand Up @@ -285,7 +284,7 @@ func (r *ClusterPermissionReconciler) Reconcile(ctx context.Context, req ctrl.Re
err = r.Get(ctx, types.NamespacedName{Name: mwName, Namespace: clusterPermission.Namespace}, &mw)
if apierrors.IsNotFound(err) {
log.Info("creating ManifestWork")
err = r.Client.Create(ctx, manifestWork)
err = r.Create(ctx, manifestWork)
if err != nil {
log.Error(err, "unable to create ManifestWork")
return ctrl.Result{}, err
Expand All @@ -295,7 +294,7 @@ func (r *ClusterPermissionReconciler) Reconcile(ctx context.Context, req ctrl.Re
if !equality.Semantic.DeepEqual(mw.Spec, manifestWork.Spec) {
log.Info("updating ManifestWork - spec has changed")
mw.Spec = manifestWork.Spec
err = r.Client.Update(ctx, &mw)
err = r.Update(ctx, &mw)
if err != nil {
log.Error(err, "unable to update ManifestWork")
return ctrl.Result{}, err
Expand Down Expand Up @@ -355,13 +354,12 @@ func (r *ClusterPermissionReconciler) validateSubject(ctx context.Context, subje
func getSubjects(subject *rbacv1.Subject, subjects []rbacv1.Subject) []rbacv1.Subject {
if len(subjects) > 0 {
return subjects
} else {
// should be safe since one of them has to exist due to CRD validation
if subject == nil {
return []rbacv1.Subject{}
}
return []rbacv1.Subject{*subject}
}
// should be safe since one of them has to exist due to CRD validation
if subject == nil {
return []rbacv1.Subject{}
}
return []rbacv1.Subject{*subject}
}

// generateSubjects checks if the subjects in the subjects array is a ManagedServiceAccount
Expand Down Expand Up @@ -530,7 +528,7 @@ func (r *ClusterPermissionReconciler) generateManifestWorkPayload(ctx context.Co
}

nsList := &corev1.NamespaceList{}
if err = r.Client.List(ctx, nsList, &client.ListOptions{LabelSelector: labelSelector}); err != nil {
if err = r.List(ctx, nsList, &client.ListOptions{LabelSelector: labelSelector}); err != nil {
return nil, nil, nil, nil, nil, err
}

Expand Down Expand Up @@ -587,7 +585,7 @@ func (r *ClusterPermissionReconciler) generateManifestWorkPayload(ctx context.Co
}

nsList := &corev1.NamespaceList{}
if err = r.Client.List(ctx, nsList, &client.ListOptions{LabelSelector: labelSelector}); err != nil {
if err = r.List(ctx, nsList, &client.ListOptions{LabelSelector: labelSelector}); err != nil {
return nil, nil, nil, nil, nil, err
}

Expand Down
6 changes: 4 additions & 2 deletions controllers/clusterpermission_status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand All @@ -11,15 +12,16 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
workv1 "open-cluster-management.io/api/work/v1"
cpv1alpha1 "open-cluster-management.io/cluster-permission/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

workv1 "open-cluster-management.io/api/work/v1"
cpv1alpha1 "open-cluster-management.io/cluster-permission/api/v1alpha1"
)

// ClusterPermissionStatusReconciler reconciles ManifestWork objects and updates
Expand Down
11 changes: 7 additions & 4 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

workv1 "open-cluster-management.io/api/work/v1"
cpv1alpha1 "open-cluster-management.io/cluster-permission/api/v1alpha1"
)
Expand Down Expand Up @@ -75,7 +76,8 @@ func extractRoleReferencesForValidation(clusterPermission *cpv1alpha1.ClusterPer
// Extract Role references from RoleBindings
if clusterPermission.Spec.RoleBindings != nil {
for _, rb := range *clusterPermission.Spec.RoleBindings {
if rb.RoleRef.Kind == "Role" {
switch rb.RoleRef.Kind {
case "Role":
// For roles, we need to know the namespace
namespace := rb.Namespace
if namespace != "" {
Expand All @@ -89,7 +91,7 @@ func extractRoleReferencesForValidation(clusterPermission *cpv1alpha1.ClusterPer
seenRefs[key] = true
}
}
} else if rb.RoleRef.Kind == "ClusterRole" {
case "ClusterRole":
key := "ClusterRole:" + rb.RoleRef.Name
if !seenRefs[key] {
roleRefs = append(roleRefs, ValidationRoleRef{
Expand Down Expand Up @@ -140,7 +142,8 @@ func buildManifestWork(clusterPermission cpv1alpha1.ClusterPermission, manifestW

if validateCP && len(roleRefs) > 0 {
for _, roleRef := range roleRefs {
if roleRef.Kind == "ClusterRole" {
switch roleRef.Kind {
case "ClusterRole":
manifestConfigs = append(manifestConfigs, workv1.ManifestConfigOption{
ResourceIdentifier: workv1.ResourceIdentifier{
Group: "rbac.authorization.k8s.io",
Expand All @@ -160,7 +163,7 @@ func buildManifestWork(clusterPermission cpv1alpha1.ClusterPermission, manifestW
Name: roleRef.Name,
},
}}})
} else if roleRef.Kind == "Role" {
case "Role":
manifestConfigs = append(manifestConfigs, workv1.ManifestConfigOption{
ResourceIdentifier: workv1.ResourceIdentifier{
Group: "rbac.authorization.k8s.io",
Expand Down
4 changes: 2 additions & 2 deletions controllers/managed_cluster_addon_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ManagedClusterAddOnInformer struct {
// stopCh is used to signal the informer to stop
stopCh chan struct{}
// workqueue is used to queue events for processing
workqueue workqueue.RateLimitingInterface
workqueue workqueue.TypedRateLimitingInterface[any]
// informer is the Kubernetes informer
informer cache.SharedIndexInformer
// eventHandler is the function to call when events occur
Expand Down Expand Up @@ -93,7 +93,7 @@ func NewManagedClusterAddOnInformer(config *rest.Config, eventHandler func(obj *

// Create workqueue with rate limiting
workqueue := workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(),
workqueue.DefaultTypedControllerRateLimiter[any](),
"ManagedClusterAddOnInformer",
)

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
workv1 "open-cluster-management.io/api/work/v1"
cpv1alpha1 "open-cluster-management.io/cluster-permission/api/v1alpha1"
"open-cluster-management.io/cluster-permission/controllers"
msav1beta1 "open-cluster-management.io/managed-serviceaccount/apis/authentication/v1beta1"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

// Options for command line flag parsing
Expand Down
Loading