-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Remove old ns when klusterlet ns is changed (#442)
* Remove old ns when klusterlet ns is changed Signed-off-by: Jian Qiu <jqiu@redhat.com> * Resolve comments Signed-off-by: Jian Qiu <jqiu@redhat.com> --------- Signed-off-by: Jian Qiu <jqiu@redhat.com>
- Loading branch information
Showing
7 changed files
with
232 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...r/operators/klusterlet/controllers/klusterletcontroller/klusterlet_namespace_reconcile.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package klusterletcontroller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
operatorapiv1 "open-cluster-management.io/api/operator/v1" | ||
) | ||
|
||
// namespaceReconcile is the reconciler to handle the case when spec.namespace is changed, and we need to | ||
// clean the old namespace. The reconciler should be run as the last reconciler, and only operate when | ||
// apply status is true, which ensures that old namespace should only be deleted after the agent in new | ||
// namespace has been deployed. | ||
type namespaceReconcile struct { | ||
managedClusterClients *managedClusterClients | ||
} | ||
|
||
func (r *namespaceReconcile) reconcile( | ||
ctx context.Context, | ||
klusterlet *operatorapiv1.Klusterlet, | ||
config klusterletConfig) (*operatorapiv1.Klusterlet, reconcileState, error) { | ||
cond := meta.FindStatusCondition(klusterlet.Status.Conditions, operatorapiv1.ConditionKlusterletApplied) | ||
if cond == nil || cond.Status == metav1.ConditionFalse || klusterlet.Generation != klusterlet.Status.ObservedGeneration { | ||
return klusterlet, reconcileContinue, nil | ||
} | ||
// filters namespace for klusterlet and not in hosted mode | ||
namespaces, err := r.managedClusterClients.kubeClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{ | ||
LabelSelector: fmt.Sprintf( | ||
"%s=%s", | ||
klusterletNamespaceLabelKey, klusterlet.Name), | ||
}) | ||
if err != nil { | ||
return klusterlet, reconcileStop, err | ||
} | ||
for _, ns := range namespaces.Items { | ||
if ns.Name == config.KlusterletNamespace || ns.Name == config.KlusterletNamespace+"-addon" { | ||
continue | ||
} | ||
if err := r.managedClusterClients.kubeClient.CoreV1().Namespaces().Delete( | ||
ctx, ns.Name, metav1.DeleteOptions{}); err != nil && !errors.IsNotFound(err) { | ||
return klusterlet, reconcileStop, err | ||
} | ||
} | ||
return klusterlet, reconcileContinue, nil | ||
} | ||
|
||
func (r *namespaceReconcile) clean(_ context.Context, klusterlet *operatorapiv1.Klusterlet, | ||
_ klusterletConfig) (*operatorapiv1.Klusterlet, reconcileState, error) { | ||
// noop | ||
return klusterlet, reconcileContinue, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters