-
Notifications
You must be signed in to change notification settings - Fork 7k
feat: Support for DeleteNamespace SyncOption(#4435, #7875) #11821
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
Closed
+289
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1000,6 +1000,19 @@ func (s *Server) Delete(ctx context.Context, q *application.ApplicationDeleteReq | |
| return nil, fmt.Errorf("error deleting application: %w", err) | ||
| } | ||
| s.logAppEvent(a, ctx, argo.EventReasonResourceDeleted, "deleted application") | ||
|
|
||
| // Delete a namespace when both CreateNamespace and DeleteNamespace are set to true. | ||
| if a.Spec.SyncPolicy != nil { | ||
| syncOptions := a.Spec.SyncPolicy.SyncOptions | ||
| if syncOptions.HasOption("CreateNamespace=true") && syncOptions.HasOption("DeleteNamespace=true") { | ||
| err := s.deleteNamespace(ctx, a.Name, a.Spec.Destination.Namespace, metav1.DeleteOptions{}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error deleting namespace: %w", err) | ||
| } | ||
| s.logAppEvent(a, ctx, argo.EventReasonResourceDeleted, fmt.Sprintf("deleted namespace: %s", a.Spec.Destination.Namespace)) | ||
| } | ||
| } | ||
|
|
||
| return &application.ApplicationResponse{}, nil | ||
| } | ||
|
|
||
|
|
@@ -2353,3 +2366,41 @@ func (s *Server) appNamespaceOrDefault(appNs string) string { | |
| func (s *Server) isNamespaceEnabled(namespace string) bool { | ||
| return security.IsNamespaceEnabled(namespace, s.ns, s.enabledNamespaces) | ||
| } | ||
|
|
||
| // The namespace can only be deleted when the namespace has an appropriate label or annotation which shows the namespace is tracked by the application. | ||
| func (s *Server) deleteNamespace(ctx context.Context, appName, namespace string, opts metav1.DeleteOptions) error { | ||
| canDelete, err := s.isNamespaceTrackedByApplication(ctx, appName, namespace, metav1.GetOptions{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if canDelete { | ||
| err := s.kubeclientset.CoreV1().Namespaces().Delete(ctx, namespace, metav1.DeleteOptions{}) | ||
| return err | ||
| } else { | ||
| return fmt.Errorf("error cannot delete namespace %s which is not managed by %s", namespace, appName) | ||
| } | ||
| } | ||
|
|
||
| func (s *Server) isNamespaceTrackedByApplication(ctx context.Context, appName, namespace string, opts metav1.GetOptions) (bool, error) { | ||
| appLabelKey, err := s.settingsMgr.GetAppInstanceLabelKey() | ||
| if err != nil { | ||
| log.Errorf("Could not get appInstanceLabelKey: %v", err) | ||
| return false, err | ||
| } | ||
|
|
||
| ns, err := s.kubeclientset.CoreV1().Namespaces().Get(ctx, namespace, opts) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| unstructedNs, err := kube.ToUnstructured(ns) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| trackingMethod := argo.GetTrackingMethod(s.settingsMgr) | ||
| nsOwner := argo.NewResourceTracking().GetAppName(unstructedNs, appLabelKey, trackingMethod) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How will the app ownership of the namespaces be set? Unless/until something like #11350 is merged, this would need to be a manual process? |
||
|
|
||
| return nsOwner == appName, nil | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general any k8s actions happens in gitops-engine, and would probably be the place we'd want to add this.
I suspect that we could do this in a simpler manner though, by force-pruning managed namespaces when an application is deleted (this would likely need to be added in controller/state.go)