Skip to content

Commit

Permalink
Revert "Revert "Add support for CSI snapshot beta APIs (kanisterio#633)…
Browse files Browse the repository at this point in the history
…" (kanisterio#635)" (kanisterio#636)

This reverts commit b24edc9.
  • Loading branch information
tdmanv authored Mar 27, 2020
1 parent b24edc9 commit 98fcab8
Show file tree
Hide file tree
Showing 8 changed files with 1,058 additions and 276 deletions.
18 changes: 18 additions & 0 deletions pkg/kube/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kube

import (
"context"
"fmt"

"k8s.io/client-go/discovery"
)
Expand All @@ -21,3 +22,20 @@ func IsOSAppsGroupAvailable(ctx context.Context, cli discovery.DiscoveryInterfac
}
return false, nil
}

// IsGroupVersionAvailable returns true if given group/version is registered.
func IsGroupVersionAvailable(ctx context.Context, cli discovery.DiscoveryInterface, groupName, version string) (bool, error) {
sgs, err := cli.ServerGroups()
if err != nil {
return false, err
}

for _, g := range sgs.Groups {
for _, v := range g.Versions {
if fmt.Sprintf("%s/%s", groupName, version) == v.GroupVersion {
return true, nil
}
}
}
return false, nil
}
10 changes: 10 additions & 0 deletions pkg/kube/snapshot/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
storage "k8s.io/api/storage/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
Expand All @@ -37,6 +38,15 @@ const (
Version = "v1alpha1"
)

var (
// VolSnapGVR specifies GVR schema for VolumeSnapshots
VolSnapGVR = schema.GroupVersionResource{Group: GroupName, Version: Version, Resource: VolumeSnapshotResourcePlural}
// VolSnapClassGVR specifies GVR schema for VolumeSnapshotClasses
VolSnapClassGVR = schema.GroupVersionResource{Group: GroupName, Version: Version, Resource: VolumeSnapshotClassResourcePlural}
// VolSnapContentGVR specifies GVR schema for VolumeSnapshotContents
VolSnapContentGVR = schema.GroupVersionResource{Group: GroupName, Version: Version, Resource: VolumeSnapshotContentResourcePlural}
)

// VolumeSnapshot is a user's request for taking a snapshot. Upon successful creation of the actual
// snapshot by the volume provider it is bound to the corresponding VolumeSnapshotContent.
// Only the VolumeSnapshot object is accessible to the user in the namespace.
Expand Down
330 changes: 330 additions & 0 deletions pkg/kube/snapshot/apis/v1beta1/types.go

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions pkg/kube/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package snapshot
import (
"context"

"github.com/pkg/errors"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/kube/snapshot/apis/v1alpha1"
"github.com/kanisterio/kanister/pkg/kube/snapshot/apis/v1beta1"
)

type Snapshotter interface {
Expand Down Expand Up @@ -83,9 +86,21 @@ type Source struct {
}

// NewSnapshotter creates and return new Snapshotter object
func NewSnapshotter(kubeCli kubernetes.Interface, dynCli dynamic.Interface) Snapshotter {
return &SnapshotAlpha{
kubeCli: kubeCli,
dynCli: dynCli,
func NewSnapshotter(kubeCli kubernetes.Interface, dynCli dynamic.Interface) (Snapshotter, error) {
ctx := context.Background()
exists, err := kube.IsGroupVersionAvailable(ctx, kubeCli.Discovery(), v1alpha1.GroupName, v1alpha1.Version)
if err != nil {
return nil, errors.Errorf("Failed to call discovery APIs: %v", err)
}
if exists {
return NewSnapshotAlpha(kubeCli, dynCli), nil
}
exists, err = kube.IsGroupVersionAvailable(ctx, kubeCli.Discovery(), v1beta1.GroupName, v1beta1.Version)
if err != nil {
return nil, errors.Errorf("Failed to call discovery APIs: %v", err)
}
if exists {
return NewSnapshotBeta(kubeCli, dynCli), nil
}
return nil, errors.New("Snapshot resources not supported")
}
Loading

0 comments on commit 98fcab8

Please sign in to comment.