Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: prefer using StorageProvider from the dataprotection API group #324

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions docs/user_docs/cli/kbcli_dataprotection_restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ kbcli dataprotection restore [flags]
### Options

```
--cluster string The cluster to restore
--effective-common-component-def this backup will be restored for all components which refer to common ComponentDefinition.
-h, --help help for restore
--restore-to-time string point in time recovery(PITR)
--volume-restore-policy string the volume claim restore policy, supported values: [Serial, Parallel] (default "Parallel")
--cluster string The cluster to restore
-h, --help help for restore
--restore-to-time string point in time recovery(PITR)
--volume-restore-policy string the volume claim restore policy, supported values: [Serial, Parallel] (default "Parallel")
```

### Options inherited from parent commands
Expand Down
38 changes: 36 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/99designs/keyring v1.2.2
github.com/Masterminds/semver/v3 v3.2.1
github.com/apecloud/kubebench v0.0.0-20240327101848-6a031d3f4ebe
github.com/apecloud/kubeblocks v0.9.0-beta.12
github.com/apecloud/kubeblocks v0.9.0-beta.15
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/benbjohnson/clock v1.3.5
github.com/briandowns/spinner v1.23.0
Expand Down Expand Up @@ -69,7 +69,7 @@ require (
k8s.io/klog/v2 v2.110.1
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00
k8s.io/kubectl v0.28.2
k8s.io/metrics v0.28.2
k8s.io/metrics v0.28.3
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.16.5
sigs.k8s.io/kustomize/kyaml v0.14.3
Expand Down Expand Up @@ -337,6 +337,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.28.3 // indirect
k8s.io/component-helpers v0.28.3 // indirect
k8s.io/kubernetes v1.28.3 // indirect
oras.land/oras-go v1.2.4 // indirect
periph.io/x/host/v3 v3.8.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand All @@ -351,3 +352,36 @@ replace (
github.com/docker/docker => github.com/moby/moby v24.0.7+incompatible
github.com/spf13/afero => github.com/spf13/afero v1.2.2
)

replace (
k8s.io/api => k8s.io/api v0.28.3
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.28.3
k8s.io/apimachinery => k8s.io/apimachinery v0.28.3
k8s.io/apiserver => k8s.io/apiserver v0.28.3
k8s.io/cli-runtime => k8s.io/cli-runtime v0.28.3
k8s.io/client-go => k8s.io/client-go v0.28.3
k8s.io/cloud-provider => k8s.io/cloud-provider v0.28.3
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.28.3
k8s.io/code-generator => k8s.io/code-generator v0.28.3
k8s.io/component-base => k8s.io/component-base v0.28.3
k8s.io/component-helpers => k8s.io/component-helpers v0.28.3
k8s.io/controller-manager => k8s.io/controller-manager v0.28.3
k8s.io/cri-api => k8s.io/cri-api v0.28.3
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.28.3
k8s.io/dynamic-resource-allocation => k8s.io/dynamic-resource-allocation v0.28.3
k8s.io/endpointslice => k8s.io/endpointslice v0.28.3
k8s.io/kms => k8s.io/kms v0.28.3
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.28.3
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.28.3
k8s.io/kube-proxy => k8s.io/kube-proxy v0.28.3
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.28.3
k8s.io/kubectl => k8s.io/kubectl v0.28.3
k8s.io/kubelet => k8s.io/kubelet v0.28.3
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.28.3
k8s.io/metrics => k8s.io/metrics v0.28.3
k8s.io/mount-utils => k8s.io/mount-utils v0.28.3
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.28.3
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.28.3
k8s.io/sample-cli-plugin => k8s.io/sample-cli-plugin v0.28.3
k8s.io/sample-controller => k8s.io/sample-controller v0.28.3
)
191 changes: 108 additions & 83 deletions go.sum

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions pkg/cmd/backuprepo/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ package backuprepo

import (
"encoding/json"
"fmt"

jsonpatch "github.com/evanphx/json-patch"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"

"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
storagev1alpha1 "github.com/apecloud/kubeblocks/apis/storage/v1alpha1"
)

const (
Expand All @@ -43,3 +52,51 @@ func createPatchData(oldObj, newObj runtime.Object) ([]byte, error) {
}
return jsonpatch.CreateMergePatch(oldData, newData)
}

func tryConvertLegacyStorageProvider(dynamic dynamic.Interface, name string) (*dpv1alpha1.StorageProvider, error) {
provider := &storagev1alpha1.StorageProvider{}
err := util.GetK8SClientObject(dynamic, provider, types.LegacyStorageProviderGVR(), "", name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("storage provider \"%s\" is not found", name)
}
return nil, err
}

var parametersSchema *dpv1alpha1.ParametersSchema
if provider.Spec.ParametersSchema != nil {
parametersSchema = &dpv1alpha1.ParametersSchema{
OpenAPIV3Schema: provider.Spec.ParametersSchema.OpenAPIV3Schema,
CredentialFields: provider.Spec.ParametersSchema.CredentialFields,
}
}

newProvider := &dpv1alpha1.StorageProvider{
ObjectMeta: metav1.ObjectMeta{
Name: provider.Name,
Labels: provider.Labels,
Annotations: provider.Annotations,
},
Spec: dpv1alpha1.StorageProviderSpec{
CSIDriverName: provider.Spec.CSIDriverName,
CSIDriverSecretTemplate: provider.Spec.CSIDriverSecretTemplate,
StorageClassTemplate: provider.Spec.StorageClassTemplate,
PersistentVolumeClaimTemplate: provider.Spec.PersistentVolumeClaimTemplate,
DatasafedConfigTemplate: provider.Spec.DatasafedConfigTemplate,
ParametersSchema: parametersSchema,
},
}
return newProvider, nil
}

func getStorageProvider(dynamic dynamic.Interface, name string) (*dpv1alpha1.StorageProvider, error) {
provider := &dpv1alpha1.StorageProvider{}
err := util.GetK8SClientObject(dynamic, provider, types.StorageProviderGVR(), "", name)
if err != nil {
if apierrors.IsNotFound(err) {
return tryConvertLegacyStorageProvider(dynamic, name)
}
return nil, err
}
return provider, nil
}
15 changes: 7 additions & 8 deletions pkg/cmd/backuprepo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"golang.org/x/exp/slices"

dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
storagev1alpha1 "github.com/apecloud/kubeblocks/apis/storage/v1alpha1"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"

"github.com/apecloud/kbcli/pkg/printer"
Expand Down Expand Up @@ -78,7 +77,7 @@ type createOptions struct {

accessMethod string
storageProvider string
providerObject *storagev1alpha1.StorageProvider
providerObject *dpv1alpha1.StorageProvider
isDefault bool
pvReclaimPolicy string
volumeCapacity string
Expand Down Expand Up @@ -206,12 +205,8 @@ func (o *createOptions) parseProviderFlags(cmd *cobra.Command, args []string, f
cmd.Example = ""
return flags.BuildFlagsWithOpenAPISchema(cmd, args, func() (*apiextensionsv1.JSONSchemaProps, error) {
// Get provider info from API server
provider := &storagev1alpha1.StorageProvider{}
err := util.GetK8SClientObject(o.dynamic, provider, types.StorageProviderGVR(), "", o.storageProvider)
provider, err := getStorageProvider(o.dynamic, o.storageProvider)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("storage provider \"%s\" is not found", o.storageProvider)
}
return nil, err
}
o.providerObject = provider
Expand Down Expand Up @@ -475,7 +470,11 @@ func registerFlagCompletionFunc(cmd *cobra.Command, f cmdutil.Factory) {
util.CheckErr(cmd.RegisterFlagCompletionFunc(
providerFlagName,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return utilcomp.CompGetResource(f, util.GVRToString(types.StorageProviderGVR()), toComplete), cobra.ShellCompDirectiveNoFileComp
candidates := utilcomp.CompGetResource(f, util.GVRToString(types.StorageProviderGVR()), toComplete)
if len(candidates) == 0 {
candidates = utilcomp.CompGetResource(f, util.GVRToString(types.LegacyStorageProviderGVR()), toComplete)
}
return candidates, cobra.ShellCompDirectiveNoFileComp
}))
util.CheckErr(cmd.RegisterFlagCompletionFunc(
"access-method",
Expand Down
10 changes: 3 additions & 7 deletions pkg/cmd/backuprepo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
storagev1alpha1 "github.com/apecloud/kubeblocks/apis/storage/v1alpha1"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"

"github.com/apecloud/kbcli/pkg/printer"
Expand Down Expand Up @@ -70,7 +69,7 @@ type updateOptions struct {

repo *dpv1alpha1.BackupRepo
storageProvider string
providerObject *storagev1alpha1.StorageProvider
providerObject *dpv1alpha1.StorageProvider
isDefault bool
repoName string
config map[string]string
Expand Down Expand Up @@ -158,11 +157,8 @@ func (o *updateOptions) parseFlags(cmd *cobra.Command, args []string, f cmdutil.
o.repo = repo
// Get provider info from API server
o.storageProvider = repo.Spec.StorageProviderRef
provider := &storagev1alpha1.StorageProvider{}
if err := util.GetK8SClientObject(o.dynamic, provider, types.StorageProviderGVR(), "", o.storageProvider); err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("storage provider \"%s\" is not found", o.storageProvider)
}
provider, err := getStorageProvider(o.dynamic, o.storageProvider)
if err != nil {
return nil, err
}
o.providerObject = provider
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func setBackup(o *CreateOptions, cluster *appsv1alpha1.Cluster) error {
// _ = runtime.DefaultUnstructuredConverter.FromUnstructured(v, &compSpec)
// componentSpecs = append(componentSpecs, compSpec)
// }
restoreAnnotation, err := restore.GetRestoreFromBackupAnnotation(backup, cluster, o.VolumeRestorePolicy, restoreTimeStr, false)
restoreAnnotation, err := restore.GetRestoreFromBackupAnnotation(backup, o.VolumeRestorePolicy, restoreTimeStr, false)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/dataprotection/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func newRestoreCommand(f cmdutil.Factory, streams genericiooptions.IOStreams) *c

cmd.Flags().StringVar(&clusterName, "cluster", "", "The cluster to restore")
cmd.Flags().StringVar(&o.RestoreSpec.RestoreTimeStr, "restore-to-time", "", "point in time recovery(PITR)")
cmd.Flags().BoolVar(&o.RestoreSpec.EffectiveCommonComponentDef, "effective-common-component-def", false, "this backup will be restored for all components which refer to common ComponentDefinition.")
cmd.Flags().StringVar(&o.RestoreSpec.VolumeRestorePolicy, "volume-restore-policy", "Parallel", "the volume claim restore policy, supported values: [Serial, Parallel]")
return cmd
}
11 changes: 5 additions & 6 deletions pkg/testing/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
appsv1beta1 "github.com/apecloud/kubeblocks/apis/apps/v1beta1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
extensionsv1alpha1 "github.com/apecloud/kubeblocks/apis/extensions/v1alpha1"
storagev1alpha1 "github.com/apecloud/kubeblocks/apis/storage/v1alpha1"
workloadsv1alpha1 "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"
Expand Down Expand Up @@ -1120,16 +1119,16 @@ func FakeEventForObject(name string, namespace string, object string) *corev1.Ev
}
}

func FakeStorageProvider(name string, mutateFunc func(obj *storagev1alpha1.StorageProvider)) *storagev1alpha1.StorageProvider {
storageProvider := &storagev1alpha1.StorageProvider{
func FakeStorageProvider(name string, mutateFunc func(obj *dpv1alpha1.StorageProvider)) *dpv1alpha1.StorageProvider {
storageProvider := &dpv1alpha1.StorageProvider{
TypeMeta: metav1.TypeMeta{
APIVersion: fmt.Sprintf("%s/%s", types.StorageAPIGroup, types.StorageAPIVersion),
APIVersion: fmt.Sprintf("%s/%s", types.DPAPIGroup, types.DPAPIVersion),
Kind: "StorageProvider",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: storagev1alpha1.StorageProviderSpec{
Spec: dpv1alpha1.StorageProviderSpec{
CSIDriverName: "fake-csi-s3",
CSIDriverSecretTemplate: `
accessKeyId: {{ index .Parameters "accessKeyId" }}
Expand All @@ -1141,7 +1140,7 @@ region: {{ index .Parameters "region" }}
endpoint: {{ index .Parameters "endpoint" }}
mountOptions: {{ index .Parameters "mountOptions" | default "" }}
`,
ParametersSchema: &storagev1alpha1.ParametersSchema{
ParametersSchema: &dpv1alpha1.ParametersSchema{
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensionsv1.JSONSchemaProps{
Expand Down
4 changes: 4 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ func AddonGVR() schema.GroupVersionResource {
}

func StorageProviderGVR() schema.GroupVersionResource {
return schema.GroupVersionResource{Group: DPAPIGroup, Version: DPAPIVersion, Resource: ResourceStorageProviders}
}

func LegacyStorageProviderGVR() schema.GroupVersionResource {
return schema.GroupVersionResource{Group: StorageAPIGroup, Version: StorageAPIVersion, Resource: ResourceStorageProviders}
}

Expand Down
Loading