mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-05 13:05:17 +00:00
Add new flag --crds-version of default CRD version to generated
If the Velero CLI can't discover the Kubernetes preferred CRD API version, use the flag --crds-version to determine the CRDs version. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>
This commit is contained in:
committed by
Bridget McErlean
parent
616676edc2
commit
fca2b5c417
@@ -25,9 +25,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
|
||||||
"github.com/vmware-tanzu/velero/internal/velero"
|
"github.com/vmware-tanzu/velero/internal/velero"
|
||||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||||
@@ -35,6 +37,7 @@ import (
|
|||||||
"github.com/vmware-tanzu/velero/pkg/cmd"
|
"github.com/vmware-tanzu/velero/pkg/cmd"
|
||||||
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
|
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
|
||||||
"github.com/vmware-tanzu/velero/pkg/cmd/util/output"
|
"github.com/vmware-tanzu/velero/pkg/cmd/util/output"
|
||||||
|
velerodiscovery "github.com/vmware-tanzu/velero/pkg/discovery"
|
||||||
"github.com/vmware-tanzu/velero/pkg/install"
|
"github.com/vmware-tanzu/velero/pkg/install"
|
||||||
kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube"
|
kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube"
|
||||||
)
|
)
|
||||||
@@ -69,6 +72,7 @@ type InstallOptions struct {
|
|||||||
Plugins flag.StringArray
|
Plugins flag.StringArray
|
||||||
NoDefaultBackupLocation bool
|
NoDefaultBackupLocation bool
|
||||||
CRDsOnly bool
|
CRDsOnly bool
|
||||||
|
CRDsVersion string
|
||||||
CACertFile string
|
CACertFile string
|
||||||
Features string
|
Features string
|
||||||
DefaultVolumesToRestic bool
|
DefaultVolumesToRestic bool
|
||||||
@@ -103,6 +107,7 @@ func (o *InstallOptions) BindFlags(flags *pflag.FlagSet) {
|
|||||||
flags.DurationVar(&o.DefaultResticMaintenanceFrequency, "default-restic-prune-frequency", o.DefaultResticMaintenanceFrequency, "How often 'restic prune' is run for restic repositories by default. Optional.")
|
flags.DurationVar(&o.DefaultResticMaintenanceFrequency, "default-restic-prune-frequency", o.DefaultResticMaintenanceFrequency, "How often 'restic prune' is run for restic repositories by default. Optional.")
|
||||||
flags.Var(&o.Plugins, "plugins", "Plugin container images to install into the Velero Deployment")
|
flags.Var(&o.Plugins, "plugins", "Plugin container images to install into the Velero Deployment")
|
||||||
flags.BoolVar(&o.CRDsOnly, "crds-only", o.CRDsOnly, "Only generate CustomResourceDefinition resources. Useful for updating CRDs for an existing Velero install.")
|
flags.BoolVar(&o.CRDsOnly, "crds-only", o.CRDsOnly, "Only generate CustomResourceDefinition resources. Useful for updating CRDs for an existing Velero install.")
|
||||||
|
flags.StringVar(&o.CRDsVersion, "crds-version", o.CRDsVersion, "The version to generate CustomResourceDefinition resources if Velero can't discover the Kubernetes preferred CRD API version. Optional.")
|
||||||
flags.StringVar(&o.CACertFile, "cacert", o.CACertFile, "File containing a certificate bundle to use when verifying TLS connections to the object store. Optional.")
|
flags.StringVar(&o.CACertFile, "cacert", o.CACertFile, "File containing a certificate bundle to use when verifying TLS connections to the object store. Optional.")
|
||||||
flags.StringVar(&o.Features, "features", o.Features, "Comma separated list of Velero feature flags to be set on the Velero deployment and the restic daemonset, if restic is enabled")
|
flags.StringVar(&o.Features, "features", o.Features, "Comma separated list of Velero feature flags to be set on the Velero deployment and the restic daemonset, if restic is enabled")
|
||||||
flags.BoolVar(&o.DefaultVolumesToRestic, "default-volumes-to-restic", o.DefaultVolumesToRestic, "Bool flag to configure Velero server to use restic by default to backup all pod volumes on all backups. Optional.")
|
flags.BoolVar(&o.DefaultVolumesToRestic, "default-volumes-to-restic", o.DefaultVolumesToRestic, "Bool flag to configure Velero server to use restic by default to backup all pod volumes on all backups. Optional.")
|
||||||
@@ -129,6 +134,7 @@ func NewInstallOptions() *InstallOptions {
|
|||||||
UseVolumeSnapshots: true,
|
UseVolumeSnapshots: true,
|
||||||
NoDefaultBackupLocation: false,
|
NoDefaultBackupLocation: false,
|
||||||
CRDsOnly: false,
|
CRDsOnly: false,
|
||||||
|
CRDsVersion: "v1beta1",
|
||||||
DefaultVolumesToRestic: false,
|
DefaultVolumesToRestic: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,6 +193,7 @@ func (o *InstallOptions) AsVeleroOptions() (*install.VeleroOptions, error) {
|
|||||||
NoDefaultBackupLocation: o.NoDefaultBackupLocation,
|
NoDefaultBackupLocation: o.NoDefaultBackupLocation,
|
||||||
CACertData: caCertData,
|
CACertData: caCertData,
|
||||||
Features: strings.Split(o.Features, ","),
|
Features: strings.Split(o.Features, ","),
|
||||||
|
CRDsVersion: o.CRDsVersion,
|
||||||
DefaultVolumesToRestic: o.DefaultVolumesToRestic,
|
DefaultVolumesToRestic: o.DefaultVolumesToRestic,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@@ -247,9 +254,30 @@ This is useful as a starting point for more customized installations.
|
|||||||
|
|
||||||
// Run executes a command in the context of the provided arguments.
|
// Run executes a command in the context of the provided arguments.
|
||||||
func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
|
func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
|
||||||
|
// Find the kube-apiserver group apiextensions.k8s.io preferred API version
|
||||||
|
clientset, err := f.KubeClient()
|
||||||
|
if err == nil {
|
||||||
|
// kubeconfig available
|
||||||
|
discoveryHelper, err := velerodiscovery.NewHelper(clientset.Discovery(), &logrus.Logger{})
|
||||||
|
if err == nil {
|
||||||
|
// kubernetes apiserver available
|
||||||
|
gvr, _, err := discoveryHelper.ResourceFor(
|
||||||
|
schema.GroupVersionResource{
|
||||||
|
Group: "apiextensions.k8s.io",
|
||||||
|
Resource: "customresourcedefinitions",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the group apiextensions.k8s.io preferred API version
|
||||||
|
o.CRDsVersion = gvr.Version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var resources *unstructured.UnstructuredList
|
var resources *unstructured.UnstructuredList
|
||||||
if o.CRDsOnly {
|
if o.CRDsOnly {
|
||||||
resources = install.AllCRDs()
|
resources = install.AllCRDs(o.CRDsVersion)
|
||||||
} else {
|
} else {
|
||||||
vo, err := o.AsVeleroOptions()
|
vo, err := o.AsVeleroOptions()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -316,6 +344,11 @@ func (o *InstallOptions) Validate(c *cobra.Command, args []string, f client.Fact
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check the CRD version is valid.
|
||||||
|
if o.CRDsVersion != "v1beta1" && o.CRDsVersion != "v1" {
|
||||||
|
return errors.Errorf("CRD version must be v1beta1 or v1")
|
||||||
|
}
|
||||||
|
|
||||||
// If we're only installing CRDs, we can skip the rest of the validation.
|
// If we're only installing CRDs, we can skip the rest of the validation.
|
||||||
if o.CRDsOnly {
|
if o.CRDsOnly {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ package install
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
rbacv1 "k8s.io/api/rbac/v1"
|
rbacv1 "k8s.io/api/rbac/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -28,7 +26,9 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
|
||||||
"github.com/vmware-tanzu/velero/config/crd/crds"
|
v1crds "github.com/vmware-tanzu/velero/config/crd/v1/crds"
|
||||||
|
v1beta1crds "github.com/vmware-tanzu/velero/config/crd/v1beta1/crds"
|
||||||
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -216,17 +216,26 @@ type VeleroOptions struct {
|
|||||||
NoDefaultBackupLocation bool
|
NoDefaultBackupLocation bool
|
||||||
CACertData []byte
|
CACertData []byte
|
||||||
Features []string
|
Features []string
|
||||||
|
CRDsVersion string
|
||||||
DefaultVolumesToRestic bool
|
DefaultVolumesToRestic bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func AllCRDs() *unstructured.UnstructuredList {
|
func AllCRDs(perferredAPIVersion string) *unstructured.UnstructuredList {
|
||||||
resources := new(unstructured.UnstructuredList)
|
resources := new(unstructured.UnstructuredList)
|
||||||
// Set the GVK so that the serialization framework outputs the list properly
|
// Set the GVK so that the serialization framework outputs the list properly
|
||||||
resources.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"})
|
resources.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"})
|
||||||
|
|
||||||
for _, crd := range crds.CRDs {
|
switch perferredAPIVersion {
|
||||||
crd.SetLabels(Labels())
|
case "v1beta1":
|
||||||
appendUnstructured(resources, crd)
|
for _, crd := range v1beta1crds.CRDs {
|
||||||
|
crd.SetLabels(Labels())
|
||||||
|
appendUnstructured(resources, crd)
|
||||||
|
}
|
||||||
|
case "v1":
|
||||||
|
for _, crd := range v1crds.CRDs {
|
||||||
|
crd.SetLabels(Labels())
|
||||||
|
appendUnstructured(resources, crd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resources
|
return resources
|
||||||
@@ -235,7 +244,7 @@ func AllCRDs() *unstructured.UnstructuredList {
|
|||||||
// AllResources returns a list of all resources necessary to install Velero, in the appropriate order, into a Kubernetes cluster.
|
// AllResources returns a list of all resources necessary to install Velero, in the appropriate order, into a Kubernetes cluster.
|
||||||
// Items are unstructured, since there are different data types returned.
|
// Items are unstructured, since there are different data types returned.
|
||||||
func AllResources(o *VeleroOptions) *unstructured.UnstructuredList {
|
func AllResources(o *VeleroOptions) *unstructured.UnstructuredList {
|
||||||
resources := AllCRDs()
|
resources := AllCRDs(o.CRDsVersion)
|
||||||
|
|
||||||
ns := Namespace(o.Namespace)
|
ns := Namespace(o.Namespace)
|
||||||
appendUnstructured(resources, ns)
|
appendUnstructured(resources, ns)
|
||||||
|
|||||||
Reference in New Issue
Block a user