forked from operator-framework/operator-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 82011ff9f7ea80eaab282388642bf3c1a23c2fdc Author: Bryce Palmer <bpalmer@redhat.com> Date: Thu Apr 7 11:19:36 2022 -0400 add more test cases more testing coverage of the auto pruning functionality references operator-framework#101 Signed-off-by: Bryce Palmer <bpalmer@redhat.com> commit 7a9d915 Author: Bryce Palmer <bpalmer@redhat.com> Date: Wed Apr 6 17:11:56 2022 -0400 refactor and add defaults refactored to make more readable and added some default IsPruneableFunc functions references operator-framework#101 commit 24409f4 Author: Bryce Palmer <bpalmer@redhat.com> Date: Wed Apr 6 13:29:31 2022 -0400 update a comment in tests explain why k8s objects created with unstructured.Unstructured related to operator-framework#101 commit 93334f6 Author: Bryce Palmer <bpalmer@redhat.com> Date: Wed Apr 6 13:24:52 2022 -0400 first draft of updated pruning API enable pruning of more than just Pod & Job resources related to operator-framework#101 Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
- Loading branch information
1 parent
d389ad4
commit 1e2f73b
Showing
11 changed files
with
820 additions
and
729 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,104 @@ | ||
// Copyright 2021 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prune | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// Some default pruneable functions | ||
|
||
// DefaultPodIsPruneable is a default IsPruneableFunc to be used specifically with Pod resources. | ||
// This can be overridden by registering your own IsPruneableFunc via the RegisterIsPruneableFunc method | ||
func DefaultPodIsPruneable(obj client.Object) error { | ||
gvk := schema.GroupVersionKind{ | ||
Group: "core", | ||
Version: "v1", | ||
Kind: "Pod", | ||
} | ||
|
||
// if the object is not a Pod then it is not pruneable | ||
if obj.GetObjectKind().GroupVersionKind() != gvk { | ||
return fmt.Errorf("can not prune object as it is not a Pod") | ||
} | ||
|
||
// convert to a Pod object so we can do Pod specific checks | ||
pod := &corev1.Pod{} | ||
err := ConvertClientObjectToKind(obj, pod) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// If the pod has Succeeded then we can remove it | ||
if pod.Status.Phase != corev1.PodSucceeded { | ||
return fmt.Errorf("can not prune Pod as it has not succeeded") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DefaultJobIsPruneable is a default IsPruneableFunc to be used specifically with Job resources. | ||
// This can be overridden by registering your own IsPruneableFunc via the RegisterIsPruneableFunc method | ||
func DefaultJobIsPruneable(obj client.Object) error { | ||
|
||
gvk := schema.GroupVersionKind{ | ||
Group: "batch", | ||
Version: "v1", | ||
Kind: "Job", | ||
} | ||
|
||
if obj.GetObjectKind().GroupVersionKind() != gvk { | ||
return fmt.Errorf("can not prune object as it is not a Job") | ||
} | ||
|
||
job := &batchv1.Job{} | ||
err := ConvertClientObjectToKind(obj, job) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// If the job has completed we can remove it | ||
if job.Status.CompletionTime == nil { | ||
return fmt.Errorf("can not prune Job as it has not completed") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ConvertClientObjectToKind is a helper function to convert from a client.Object to a specified interface{} | ||
func ConvertClientObjectToKind(object client.Object, kind interface{}) error { | ||
unstructuredObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(object) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to convert object to Unstructured -- %s", err) | ||
} | ||
|
||
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObject, kind) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to convert object to kind -- %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.