Skip to content

Commit

Permalink
disable GC and backup deletion if Kubernetes is less than v1.7.5
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kriss <[email protected]>
  • Loading branch information
skriss committed Dec 22, 2017
1 parent 8e5feec commit 1c97478
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pkg/cmd/cli/backup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

api "github.com/heptio/ark/pkg/apis/ark/v1"
"github.com/heptio/ark/pkg/client"
"github.com/heptio/ark/pkg/cmd"
"github.com/heptio/ark/pkg/controller"
kubeutil "github.com/heptio/ark/pkg/util/kube"
)

func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
Expand All @@ -37,6 +40,16 @@ func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
os.Exit(1)
}

kubeClient, err := f.KubeClient()
cmd.CheckError(err)

serverVersion, err := kubeutil.ServerVersion(kubeClient.Discovery())
cmd.CheckError(err)

if !serverVersion.AtLeast(controller.MinVersionForDelete) {
cmd.CheckError(errors.Errorf("this command requires the Kubernetes server version to be at least %s", controller.MinVersionForDelete))
}

arkClient, err := f.Client()
cmd.CheckError(err)

Expand Down
42 changes: 26 additions & 16 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"github.com/heptio/ark/pkg/plugin"
"github.com/heptio/ark/pkg/restore"
"github.com/heptio/ark/pkg/util/kube"
kubeutil "github.com/heptio/ark/pkg/util/kube"
"github.com/heptio/ark/pkg/util/logging"
)

Expand Down Expand Up @@ -470,22 +471,31 @@ func (s *server) runControllers(config *api.Config) error {
wg.Done()
}()

gcController := controller.NewGCController(
s.backupService,
s.snapshotService,
config.BackupStorageProvider.Bucket,
config.GCSyncPeriod.Duration,
s.sharedInformerFactory.Ark().V1().Backups(),
s.arkClient.ArkV1(),
s.sharedInformerFactory.Ark().V1().Restores(),
s.arkClient.ArkV1(),
s.logger,
)
wg.Add(1)
go func() {
gcController.Run(ctx, 1)
wg.Done()
}()
serverVersion, err := kubeutil.ServerVersion(s.kubeClient.Discovery())
if err != nil {
return err
}

if !serverVersion.AtLeast(controller.MinVersionForDelete) {
s.logger.Errorf("Garbage-collection is disabled because it requires the Kubernetes server version to be at least %s", controller.MinVersionForDelete)
} else {
gcController := controller.NewGCController(
s.backupService,
s.snapshotService,
config.BackupStorageProvider.Bucket,
config.GCSyncPeriod.Duration,
s.sharedInformerFactory.Ark().V1().Backups(),
s.arkClient.ArkV1(),
s.sharedInformerFactory.Ark().V1().Restores(),
s.arkClient.ArkV1(),
s.logger,
)
wg.Add(1)
go func() {
gcController.Run(ctx, 1)
wg.Done()
}()
}
}

restorer, err := newRestorer(
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/gc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/util/version"

api "github.com/heptio/ark/pkg/apis/ark/v1"
"github.com/heptio/ark/pkg/cloudprovider"
Expand All @@ -42,6 +43,13 @@ import (

const gcFinalizer = "gc.ark.heptio.com"

// MinVersionForDelete is the minimum Kubernetes server version that Ark
// requires in order to be able to properly delete backups (including
// the associated snapshots and object storage files). This is because
// Ark uses finalizers on the backup CRD to implement garbage-collection
// and deletion.
var MinVersionForDelete = version.MustParseSemantic("1.7.5")

// gcController removes expired backup content from object storage.
type gcController struct {
backupService cloudprovider.BackupService
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/kube/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/util/version"
)

// NamespaceAndName returns a string in the format <namespace>/<name>
Expand All @@ -48,3 +50,17 @@ func EnsureNamespaceExists(namespace *v1.Namespace, client corev1.NamespaceInter
return false, errors.Wrapf(err, "error creating namespace %s", namespace.Name)
}
}

func ServerVersion(client discovery.DiscoveryInterface) (*version.Version, error) {
versionInfo, err := client.ServerVersion()
if err != nil {
return nil, errors.Wrap(err, "error getting server version")
}

semVer, err := version.ParseSemantic(versionInfo.String())
if err != nil {
return nil, errors.Wrap(err, "error parsing server version")
}

return semVer, err
}
48 changes: 48 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/verify-util-pkg.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/version/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1c97478

Please sign in to comment.