Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
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
everettraven committed Apr 7, 2022
1 parent d389ad4 commit 1e2f73b
Show file tree
Hide file tree
Showing 11 changed files with 820 additions and 729 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/go-logr/logr v1.2.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/onsi/gomega v1.19.0
github.com/operator-framework/api v0.10.0
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_model v0.2.0
Expand Down Expand Up @@ -38,10 +38,10 @@ require (
github.com/prometheus/common v0.28.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY=
github.com/onsi/gomega v1.17.0 h1:9Luw4uT5HTjHTN8+aNcSThgH1vdXnmdJ8xIfZ4wyTRE=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/operator-framework/api v0.10.0 h1:TaxbgbrV8D3wnKNyrImZ2zjQVVHMQRc7piWLDmlGoEE=
github.com/operator-framework/api v0.10.0/go.mod h1:tV0BUNvly7szq28ZPBXhjp1Sqg5yHCOeX19ui9K4vjI=
Expand Down Expand Up @@ -667,6 +669,8 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210825183410-e898025ed96a h1:bRuuGXV8wwSdGTB+CtJf+FjgO1APK1CoO39T4BN/XBw=
golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -766,11 +770,15 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 h1:M69LAlWZCshgp0QSzyDcSsSIejIEeuaCVpmwcKwyLMk=
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
46 changes: 0 additions & 46 deletions prune/maxage.go

This file was deleted.

48 changes: 0 additions & 48 deletions prune/maxcount.go

This file was deleted.

104 changes: 104 additions & 0 deletions prune/prunables.go
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
}
Loading

0 comments on commit 1e2f73b

Please sign in to comment.