-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add a manifest annotation to be used for object deletion
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package resourcedelete | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
appsclientv1 "k8s.io/client-go/kubernetes/typed/apps/v1" | ||
"k8s.io/klog" | ||
) | ||
|
||
const DeleteAnnotation = "release.openshift.io/delete" | ||
|
||
// DeleteDeploymentv1 checks ... and deletes the required deployment from the cluster. | ||
func DeleteDeploymentv1(ctx context.Context, client appsclientv1.DeploymentsGetter, required *appsv1.Deployment) (bool, error) { | ||
if annotations := required.GetAnnotations(); annotations != nil { | ||
if value, ok := annotations[DeleteAnnotation]; ok { | ||
if value != "true" { | ||
return false, fmt.Errorf("Invalid %s annotation value: %s", DeleteAnnotation, value) | ||
} | ||
existing, err := client.Deployments(required.Namespace).Get(ctx, required.Name, metav1.GetOptions{}) | ||
if apierrors.IsNotFound(err) { | ||
return true, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
if deletionTime := existing.GetDeletionTimestamp(); deletionTime != nil { | ||
klog.V(4).Infof("%s.%s resource is expected to be deleted by %s.", required.Namespace, required.Name, deletionTime.String()) | ||
} else { | ||
if err := client.Deployments(required.Namespace).Delete(ctx, required.Name, metav1.DeleteOptions{}); err != nil { | ||
klog.V(4).Infof("%s.%s resource delete requested.", required.Namespace, required.Name) | ||
return false, err | ||
} | ||
} | ||
return true, nil | ||
} | ||
} | ||
return false, 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