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

refac(Cluster) - Merge & Standardize DirectAccess and ClusterStatus Controller #624

Merged
merged 130 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
3f31bf8
(chore): common clean up finalizer
abhijith-darshan Oct 7, 2024
6ce79cd
(chore): adds deletion condition
abhijith-darshan Oct 7, 2024
1c3324b
(chore): adds reconcile context helpers
abhijith-darshan Oct 7, 2024
2ed036b
(chore): adds reconcile utilities
abhijith-darshan Oct 7, 2024
5f57aca
(chore): adds common reconcile interface
abhijith-darshan Oct 7, 2024
3973edc
Automatic generation of CRD API Docs
Oct 7, 2024
82f1d60
Automatic application of license header
Oct 7, 2024
898b89b
Merge branch 'main' into feat/reconcile_standardization
abhijith-darshan Oct 7, 2024
5d82e61
(fix): go fmt!
abhijith-darshan Oct 7, 2024
0778314
Automatic generation of CRD API Docs
Oct 7, 2024
0101a0c
(fix): address lint issues
abhijith-darshan Oct 7, 2024
7dcedcf
Merge remote-tracking branch 'origin/feat/reconcile_standardization' …
abhijith-darshan Oct 7, 2024
49c242d
Automatic generation of CRD API Docs
Oct 7, 2024
2440a17
(chore): implements direct access reconcile standardization
abhijith-darshan Oct 7, 2024
a09571d
(chore): update unit tests
abhijith-darshan Oct 7, 2024
423d693
(chore): move to lifecycle pkg
abhijith-darshan Oct 7, 2024
3947bcd
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 7, 2024
53eee91
Automatic generation of CRD API Docs
Oct 7, 2024
7ae721b
Automatic generation of CRD API Docs
Oct 7, 2024
530b585
(chore): remove redundant break
abhijith-darshan Oct 7, 2024
a65d192
Merge remote-tracking branch 'origin/feat/reconcile_standardization' …
abhijith-darshan Oct 7, 2024
dd9374f
Automatic generation of CRD API Docs
Oct 7, 2024
9f74696
(chore): go fmt
abhijith-darshan Oct 7, 2024
e0e1f1d
Automatic generation of CRD API Docs
Oct 7, 2024
1870051
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 7, 2024
8a6c0f8
Automatic generation of CRD API Docs
Oct 7, 2024
732507f
Merge branch 'main' into feat/reconcile_standardization
abhijith-darshan Oct 8, 2024
cf80cf0
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 8, 2024
38e32d1
Merge branch 'main' into feat/reconcile_standardization
abhijith-darshan Oct 8, 2024
a9a6f4a
(chore): merge status controller
abhijith-darshan Oct 8, 2024
e7132dd
(chore): implements statusFunc and adds go docs
abhijith-darshan Oct 8, 2024
5580908
(chore): remove redundant break
abhijith-darshan Oct 8, 2024
c1a1fd2
Automatic generation of CRD API Docs
Oct 8, 2024
90ed170
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 8, 2024
4a43f9d
Apply suggestions from code review
abhijith-darshan Oct 8, 2024
eb2b410
Automatic generation of CRD API Docs
Oct 8, 2024
d19afc7
(chore): remove cluster status reconciler
abhijith-darshan Oct 8, 2024
95bb22f
(chore): adds statsFunc to calculate status conditions
abhijith-darshan Oct 8, 2024
4d096ce
(chore): removes status controller from known controllers
abhijith-darshan Oct 8, 2024
a1e6c51
(chore): removes adding type information
abhijith-darshan Oct 8, 2024
f7247b0
(chore): add go docs
abhijith-darshan Oct 8, 2024
798da5c
Automatic generation of CRD API Docs
Oct 8, 2024
72b7a4e
(fix): go fmt!
abhijith-darshan Oct 8, 2024
6b8dfc6
Merge branch 'feat/reconcile_standardization' of https://github.com/c…
abhijith-darshan Oct 8, 2024
f2b58ff
Automatic generation of CRD API Docs
Oct 8, 2024
96b6619
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 8, 2024
7346281
(fix): go fmt and update test suite
abhijith-darshan Oct 9, 2024
dbfe750
Automatic generation of CRD API Docs
Oct 9, 2024
7ba7c49
(fix): go fmt and lint err
abhijith-darshan Oct 9, 2024
7c2006e
(fix): go fmt and lint err
abhijith-darshan Oct 9, 2024
9b739e5
Merge branch 'refac/direct_access_standardization' of https://github.…
abhijith-darshan Oct 9, 2024
fce2e88
Automatic generation of CRD API Docs
Oct 9, 2024
50fd381
fix: fix remote cluster test after controller merge
Oct 9, 2024
d39184a
Automatic generation of CRD API Docs
Oct 9, 2024
27e03ce
Merge branch 'main' into feat/reconcile_standardization
abhijith-darshan Oct 9, 2024
3ccd727
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 9, 2024
9cc20c9
Automatic generation of CRD API Docs
Oct 9, 2024
ed1f990
(chore): uses common delete condition, implements event filtering
abhijith-darshan Oct 10, 2024
435f590
(chore): adds gvk info to dummy types
abhijith-darshan Oct 10, 2024
e4b4616
(chore): adds finalizer check
abhijith-darshan Oct 10, 2024
617771b
(chore): uses clientutil finalizer methods
abhijith-darshan Oct 10, 2024
c57fa8d
Automatic generation of CRD API Docs
Oct 10, 2024
4d49d9b
Automatic application of license header
Oct 10, 2024
464ee01
(chore): remove gvk for dummy due to regression
abhijith-darshan Oct 10, 2024
d398e1f
(chore): fix lint issues
abhijith-darshan Oct 10, 2024
ee6e57f
Merge branch 'feat/reconcile_standardization' of https://github.com/c…
abhijith-darshan Oct 10, 2024
76157e8
Automatic generation of CRD API Docs
Oct 10, 2024
72b6fc4
(chore): fix lint issues
abhijith-darshan Oct 10, 2024
c289a87
Automatic generation of CRD API Docs
Oct 10, 2024
97407c3
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 10, 2024
4058f01
(chore): go fmt
abhijith-darshan Oct 10, 2024
8bbf8b6
Automatic generation of CRD API Docs
Oct 10, 2024
ed17132
Merge branch 'main' into feat/reconcile_standardization
abhijith-darshan Oct 14, 2024
9fe8b1e
(chore): tidy up!
abhijith-darshan Oct 14, 2024
0ac0d2c
Automatic generation of CRD API Docs
Oct 14, 2024
bd72557
(chore): remove finalizer tests
abhijith-darshan Oct 14, 2024
a415830
(chore): remove unused conditions
abhijith-darshan Oct 14, 2024
03df985
(chore): simplify reconcile
abhijith-darshan Oct 14, 2024
d613091
Automatic generation of CRD API Docs
Oct 14, 2024
30c2a9f
(chore): generate mocks
abhijith-darshan Oct 14, 2024
a64beaa
Automatic application of license header
Oct 14, 2024
53016b6
(chore): fix lint
abhijith-darshan Oct 14, 2024
f5ec06b
Automatic generation of CRD API Docs
Oct 14, 2024
2bf848d
Update pkg/lifecycle/reconcile.go
abhijith-darshan Oct 14, 2024
e29399f
Automatic generation of CRD API Docs
Oct 14, 2024
cedac7d
(chore): re-generates mocks and adds reconcile test
abhijith-darshan Oct 14, 2024
45e852e
Automatic application of license header
Oct 14, 2024
b395af1
Automatic generation of CRD API Docs
Oct 14, 2024
2262d9b
(chore): adds unit test for context
abhijith-darshan Oct 15, 2024
51c7057
(chore): refactor reconcile_test.go
abhijith-darshan Oct 15, 2024
3c7c9b2
Automatic generation of CRD API Docs
Oct 15, 2024
9711125
(fix): go fmt
abhijith-darshan Oct 15, 2024
95b5012
Merge branch 'feat/reconcile_standardization' of https://github.com/c…
abhijith-darshan Oct 15, 2024
e6104b2
Automatic generation of CRD API Docs
Oct 15, 2024
f26af17
(fix): use correct assertions
abhijith-darshan Oct 15, 2024
1ed9009
Merge branch 'feat/reconcile_standardization' of https://github.com/c…
abhijith-darshan Oct 15, 2024
0e0f10a
Automatic generation of CRD API Docs
Oct 15, 2024
23acc61
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 15, 2024
7ea89f2
(chore): resolve conflicts from main
abhijith-darshan Oct 15, 2024
7fee34a
Automatic generation of CRD API Docs
Oct 15, 2024
11b1786
(chore): remove unused controller
abhijith-darshan Oct 15, 2024
65eeabe
Merge branch 'refac/direct_access_standardization' of https://github.…
abhijith-darshan Oct 15, 2024
5d794dc
Automatic generation of CRD API Docs
Oct 15, 2024
5768971
(chore): increase timeout for eventually
abhijith-darshan Oct 15, 2024
a8a1b8d
Automatic generation of CRD API Docs
Oct 15, 2024
94230f7
Update pkg/lifecycle/reconcile.go
abhijith-darshan Oct 16, 2024
d6e6872
Merge branch 'main' into feat/reconcile_standardization
abhijith-darshan Oct 16, 2024
c93b6da
Automatic generation of CRD API Docs
Oct 16, 2024
e62f158
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 16, 2024
6f9fd1d
Automatic generation of CRD API Docs
Oct 16, 2024
54f4e21
(chore): use exports_test to expose pvt func
abhijith-darshan Oct 16, 2024
aecb95b
(chore): fmt
abhijith-darshan Oct 16, 2024
dbca39c
Automatic generation of CRD API Docs
Oct 16, 2024
dd1fc19
Automatic application of license header
Oct 16, 2024
41d6947
Merge branch 'feat/reconcile_standardization' into refac/direct_acces…
abhijith-darshan Oct 16, 2024
7f84544
Automatic generation of CRD API Docs
Oct 16, 2024
4d0cda9
Merge branch 'main' into refac/direct_access_standardization
abhijith-darshan Oct 16, 2024
78d0abc
Automatic generation of CRD API Docs
Oct 16, 2024
b023fb7
(chore): update comment
abhijith-darshan Oct 17, 2024
f2fecc7
Merge branch 'refac/direct_access_standardization' of https://github.…
abhijith-darshan Oct 17, 2024
44f1e7e
Automatic generation of CRD API Docs
Oct 17, 2024
7625f57
Merge branch 'main' into refac/direct_access_standardization
abhijith-darshan Oct 21, 2024
ed8473c
Apply suggestions from code review
abhijith-darshan Oct 21, 2024
b3ecd39
Automatic generation of CRD API Docs
Oct 21, 2024
5c8efa8
Merge branch 'refac/direct_access_standardization' of https://github.…
abhijith-darshan Oct 21, 2024
da5062a
(chore): removes dropping namespace
abhijith-darshan Oct 21, 2024
cbd78d3
(chore): go fmt!
abhijith-darshan Oct 21, 2024
3a3ddd1
Automatic generation of CRD API Docs
Oct 21, 2024
bfd6d09
(chore): drop service account instead of namespace
abhijith-darshan Oct 22, 2024
760b846
Automatic generation of CRD API Docs
Oct 22, 2024
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
10 changes: 4 additions & 6 deletions cmd/greenhouse/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ var knownControllers = map[string]func(controllerName string, mgr ctrl.Manager)
"pluginChartTest": (&plugincontrollers.HelmChartTestReconciler{}).SetupWithManager,

// Cluster controllers
"bootStrap": (&clustercontrollers.BootstrapReconciler{}).SetupWithManager,
"clusterDirectAccess": startClusterDirectAccessReconciler,
"bootStrap": (&clustercontrollers.BootstrapReconciler{}).SetupWithManager,
"clusterReconciler": startClusterReconciler,
// "clusterPropagation": (&clustercontrollers.ClusterPropagationReconciler{}).SetupWithManager,
"clusterStatus": (&clustercontrollers.ClusterStatusReconciler{}).SetupWithManager,
"kubeconfig": (&clustercontrollers.KubeconfigReconciler{}).SetupWithManager,
}

// knownControllers lists the name of known controllers.
Expand Down Expand Up @@ -87,13 +85,13 @@ func startOrganizationDexReconciler(name string, mgr ctrl.Manager) error {
}).SetupWithManager(name, mgr)
}

func startClusterDirectAccessReconciler(name string, mgr ctrl.Manager) error {
func startClusterReconciler(name string, mgr ctrl.Manager) error {
if renewRemoteClusterBearerTokenAfter > remoteClusterBearerTokenValidity {
setupLog.Info("WARN: remoteClusterBearerTokenValidity is less than renewRemoteClusterBearerTokenAfter")
setupLog.Info("Setting renewRemoteClusterBearerTokenAfter to half of remoteClusterBearerTokenValidity")
renewRemoteClusterBearerTokenAfter = remoteClusterBearerTokenValidity / 2
}
return (&clustercontrollers.DirectAccessReconciler{
return (&clustercontrollers.RemoteClusterReconciler{
RemoteClusterBearerTokenValidity: remoteClusterBearerTokenValidity,
RenewRemoteClusterBearerTokenAfter: renewRemoteClusterBearerTokenAfter,
}).SetupWithManager(name, mgr)
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: Greenhouse
version: aecb95b
version: cbd78d3
description: PlusOne operations platform
paths:
/TeamMembership:
Expand Down
11 changes: 8 additions & 3 deletions pkg/apis/greenhouse/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const (

// KubeConfigValid reflects the validity of the kubeconfig of a cluster.
KubeConfigValid ConditionType = "KubeConfigValid"

// ClusterDeletionScheduled reflects the condition type if a cluster is scheduled for deletion
ClusterDeletionScheduled ConditionType = "ClusterDeletionScheduled"
)

// ClusterStatus defines the observed state of Cluster
Expand Down Expand Up @@ -68,6 +65,14 @@ type Cluster struct {
Status ClusterStatus `json:"status,omitempty"`
}

func (c *Cluster) GetConditions() StatusConditions {
return c.Status.StatusConditions
}

func (c *Cluster) SetCondition(condition Condition) {
c.Status.StatusConditions.SetConditions(condition)
}

// GetSecretName returns the Kubernetes secret containing sensitive data for this cluster.
// The secret is for internal usage only and its content must not be exposed to the user.
func (c *Cluster) GetSecretName() string {
Expand Down
16 changes: 10 additions & 6 deletions pkg/apis/greenhouse/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
// ReadyCondition reflects the overall readiness status of a resource.
ReadyCondition ConditionType = "Ready"

// DeleteCondition reflects that the resource has finished it's cleanup process.
DeleteCondition ConditionType = "Deleted"
// DeleteCondition reflects that the resource has finished its cleanup process.
DeleteCondition ConditionType = "Delete"

// ClusterListEmpty is set when the resources ClusterSelector results in an empty ClusterList.
ClusterListEmpty ConditionType = "ClusterListEmpty"
Expand Down Expand Up @@ -68,7 +68,7 @@ func TrueCondition(t ConditionType, reason ConditionReason, message string) Cond
return NewCondition(t, metav1.ConditionTrue, reason, message)
}

// NewTrue returns a Condition with ConditionFalse and the given type, reason and message. LastTransitionTime is set to now.
// FalseCondition returns a Condition with ConditionFalse and the given type, reason and message. LastTransitionTime is set to now.
func FalseCondition(t ConditionType, reason ConditionReason, message string) Condition {
return NewCondition(t, metav1.ConditionFalse, reason, message)
}
Expand All @@ -80,20 +80,24 @@ func UnknownCondition(t ConditionType, reason ConditionReason, message string) C

// Equal returns true if the condition is identical to the supplied condition,
// ignoring the LastTransitionTime.
func (c Condition) Equal(other Condition) bool {
func (c *Condition) Equal(other Condition) bool {
return c.Type == other.Type &&
c.Status == other.Status &&
c.Reason == other.Reason &&
c.Message == other.Message
}

// IsTrue returns true if the condition is true.
func (c Condition) IsTrue() bool {
func (c *Condition) IsTrue() bool {
return c.Status == metav1.ConditionTrue
}

func (c *Condition) IsUnknown() bool {
return c.Status == metav1.ConditionUnknown
}

// IsFalse returns true if the condition is false.
func (c Condition) IsFalse() bool {
func (c *Condition) IsFalse() bool {
return c.Status == metav1.ConditionFalse
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/greenhouse/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"time"

"github.com/cloudoperators/greenhouse/pkg/lifecycle"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -15,7 +17,6 @@ import (
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/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"

greenhouseapis "github.com/cloudoperators/greenhouse/pkg/apis"
Expand All @@ -25,8 +26,8 @@ import (

const serviceAccountName = "greenhouse"

// DirectAccessReconciler reconciles a Cluster object with accessMode=direct set.
type DirectAccessReconciler struct {
// RemoteClusterReconciler reconciles a Cluster object with accessMode=direct set.
type RemoteClusterReconciler struct {
client.Client
recorder record.EventRecorder
RemoteClusterBearerTokenValidity time.Duration
Expand All @@ -43,7 +44,7 @@ type DirectAccessReconciler struct {
//+kubebuilder:rbac:groups="rbac",resources=clusterrolebindings,verbs=get;list;watch;update;patch;create

// SetupWithManager sets up the controller with the Manager.
func (r *DirectAccessReconciler) SetupWithManager(name string, mgr ctrl.Manager) error {
func (r *RemoteClusterReconciler) SetupWithManager(name string, mgr ctrl.Manager) error {
r.Client = mgr.GetClient()
r.recorder = mgr.GetEventRecorderFor(name)

Expand All @@ -57,106 +58,109 @@ func (r *DirectAccessReconciler) SetupWithManager(name string, mgr ctrl.Manager)
Complete(r)
}

func (r *DirectAccessReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var cluster = new(greenhousev1alpha1.Cluster)
if err := r.Get(ctx, req.NamespacedName, cluster); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
func (r *RemoteClusterReconciler) GetEventRecorder() record.EventRecorder {
return r.recorder
}

func (r *RemoteClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return lifecycle.Reconcile(ctx, r.Client, req.NamespacedName, &greenhousev1alpha1.Cluster{}, r, r.setConditions())
}

func (r *RemoteClusterReconciler) EnsureCreated(ctx context.Context, resource lifecycle.RuntimeObject) (ctrl.Result, lifecycle.ReconcileResult, error) {
cluster := resource.(*greenhousev1alpha1.Cluster) //nolint:errcheck
if cluster.Spec.AccessMode != greenhousev1alpha1.ClusterAccessModeDirect {
return ctrl.Result{}, nil
return ctrl.Result{}, lifecycle.Failed, nil
}

// Update metrics at the end of the reconcile function
defer updateMetrics(cluster)

// Deletion Schedule mechanism
isScheduled, schedule, err := clientutil.ExtractDeletionSchedule(cluster.GetAnnotations())
if err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}
if isScheduled && cluster.DeletionTimestamp == nil {
if ok, err := clientutil.ShouldProceedDeletion(time.Now(), schedule); ok && err == nil {
return ctrl.Result{}, r.Client.Delete(ctx, cluster)
err = r.Client.Delete(ctx, cluster)
if err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}
return ctrl.Result{}, lifecycle.Success, nil
}
}

// Cleanup logic
if cluster.DeletionTimestamp != nil && controllerutil.ContainsFinalizer(cluster, greenhouseapis.FinalizerCleanupCluster) {
// delete all plugins that are bound to this cluster
deletionCount, err := deletePlugins(ctx, r.Client, cluster)
if err != nil {
return ctrl.Result{}, err
}
if deletionCount > 0 {
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
}

var kubeConfigSecret = new(corev1.Secret)
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: cluster.GetNamespace(), Name: cluster.GetSecretName()}, kubeConfigSecret); err != nil {
return ctrl.Result{}, err
}
restClientGetter, err := clientutil.NewRestClientGetterFromSecret(kubeConfigSecret, cluster.Namespace)
if err != nil {
return ctrl.Result{}, err
}
remoteClient, err := clientutil.NewK8sClientFromRestClientGetter(restClientGetter)
if err != nil {
return ctrl.Result{}, err
}
// Delete namespace in remote cluster before the secret.
// All remote resources are bound by owner-reference to the namespace
if err := deleteNamespaceInRemoteCluster(ctx, remoteClient, cluster); err != nil {
return ctrl.Result{}, err
}
// A simple Delete won't do. The logic should take into consideration the order, that only a portion of the resources have been deleted, etc.
err = clientutil.RemoveFinalizer(ctx, r.Client, cluster, greenhouseapis.FinalizerCleanupCluster)
return ctrl.Result{}, err
}

// Add finalizer before starting any work.
if err := clientutil.EnsureFinalizer(ctx, r.Client, cluster, greenhouseapis.FinalizerCleanupCluster); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

defer updateMetrics(cluster)
var clusterSecret = new(corev1.Secret)
if err := r.Get(ctx, types.NamespacedName{Name: cluster.GetSecretName(), Namespace: cluster.GetNamespace()}, clusterSecret); err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}

restClientGetter, err := clientutil.NewRestClientGetterFromSecret(clusterSecret, cluster.Namespace)
if err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}

k8sClientForRemoteCluster, err := clientutil.NewK8sClientFromRestClientGetter(restClientGetter)
if err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}

if err := reconcileNamespaceInRemoteCluster(ctx, k8sClientForRemoteCluster, cluster); err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}
if err := reconcileServiceAccountInRemoteCluster(ctx, k8sClientForRemoteCluster, cluster); err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}
if err := reconcileClusterRoleBindingInRemoteCluster(ctx, k8sClientForRemoteCluster, cluster); err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}

var tokenRequestor = &tokenHelper{
var tokenRequest = &tokenHelper{
Client: r.Client,
RemoteClusterBearerTokenValidity: r.RemoteClusterBearerTokenValidity,
RenewRemoteClusterBearerTokenAfter: r.RenewRemoteClusterBearerTokenAfter,
}
if err := tokenRequestor.ReconcileServiceAccountToken(ctx, restClientGetter, cluster); err != nil {
return ctrl.Result{}, err
if err := tokenRequest.ReconcileServiceAccountToken(ctx, restClientGetter, cluster); err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}

if err := reconcileRemoteAPIServerVersion(ctx, restClientGetter, r.Client, cluster); err != nil {
return ctrl.Result{}, err
return ctrl.Result{}, lifecycle.Failed, err
}
return ctrl.Result{RequeueAfter: defaultRequeueInterval}, lifecycle.Success, nil
}

// EnsureDeleted - handles the deletion / cleanup of cluster resource
func (r *RemoteClusterReconciler) EnsureDeleted(ctx context.Context, resource lifecycle.RuntimeObject) (ctrl.Result, lifecycle.ReconcileResult, error) {
cluster := resource.(*greenhousev1alpha1.Cluster) //nolint:errcheck
// delete all plugins that are bound to this cluster
deletionCount, err := deletePlugins(ctx, r.Client, cluster)
if err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}
if deletionCount > 0 {
return ctrl.Result{RequeueAfter: 10 * time.Second}, lifecycle.Pending, nil
}

return ctrl.Result{RequeueAfter: defaultRequeueInterval}, nil
defer updateMetrics(cluster)

kubeConfigSecret := &corev1.Secret{}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: cluster.GetNamespace(), Name: cluster.GetSecretName()}, kubeConfigSecret); err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}
restClientGetter, err := clientutil.NewRestClientGetterFromSecret(kubeConfigSecret, cluster.Namespace)
if err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}
remoteClient, err := clientutil.NewK8sClientFromRestClientGetter(restClientGetter)
if err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}

if err := deleteClusterRoleBindingInRemoteCluster(ctx, remoteClient); err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}

if err := deleteServiceAccountInRemoteCluster(ctx, remoteClient, cluster); err != nil {
return ctrl.Result{}, lifecycle.Failed, err
}
return ctrl.Result{}, lifecycle.Success, nil
}

// generateNewClientKubeConfig generates a kubeconfig for the client to access the cluster from REST config coming from the secret
Expand All @@ -165,7 +169,6 @@ func generateNewClientKubeConfig(_ context.Context, restConfigGetter *clientutil
if err != nil {
return nil, errors.Wrapf(err, "failed to load kube clientConfig for cluster %s", cluster.GetName())
}

// TODO: replace overwrite with https://github.com/kubernetes/kubernetes/pull/119398 after 1.30 upgrade
kubeConfigGenerator := &KubeConfigHelper{
Host: restConfig.Host,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cluster_test

import (
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -80,7 +81,7 @@ var _ = Describe("KubeConfig controller", func() {
Eventually(func() error {
var namespace = new(corev1.Namespace)
return remoteClient.Get(test.Ctx, types.NamespacedName{Namespace: "", Name: setup.Namespace()}, namespace)
}).Should(Succeed(), fmt.Sprintf("eventually the namespace %s should exist", setup.Namespace()))
}, 3*time.Minute).Should(Succeed(), fmt.Sprintf("eventually the namespace %s should exist", setup.Namespace()))

By("Checking service account has been created in remote cluster")
Eventually(func() error {
Expand Down
Loading