-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cd describe to show backup configuration
- Loading branch information
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: kbcli clusterdefinition describe | ||
--- | ||
|
||
Describe ClusterDefinition. | ||
|
||
``` | ||
kbcli clusterdefinition describe [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
# describe a specified cluster definition | ||
kbcli clusterdefinition describe myclusterdef | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for describe | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace. | ||
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups. | ||
--as-uid string UID to impersonate for the operation. | ||
--cache-dir string Default cache directory (default "$HOME/.kube/cache") | ||
--certificate-authority string Path to a cert file for the certificate authority | ||
--client-certificate string Path to a client certificate file for TLS | ||
--client-key string Path to a client key file for TLS | ||
--cluster string The name of the kubeconfig cluster to use | ||
--context string The name of the kubeconfig context to use | ||
--disable-compression If true, opt-out of response compression for all requests to the server | ||
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure | ||
--kubeconfig string Path to the kubeconfig file to use for CLI requests. | ||
--match-server-version Require server version to match client version | ||
-n, --namespace string If present, the namespace scope for this CLI request | ||
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0") | ||
-s, --server string The address and port of the Kubernetes API server | ||
--tls-server-name string Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used | ||
--token string Bearer token for authentication to the API server | ||
--user string The name of the kubeconfig user to use | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [kbcli clusterdefinition](kbcli_clusterdefinition.md) - ClusterDefinition command. | ||
|
||
#### Go Back to [CLI Overview](cli.md) Homepage. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
Copyright (C) 2022-2023 ApeCloud Co., Ltd | ||
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 clusterdefinition | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/client-go/dynamic" | ||
clientset "k8s.io/client-go/kubernetes" | ||
cmdutil "k8s.io/kubectl/pkg/cmd/util" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/apecloud/kubeblocks/apis/apps/v1alpha1" | ||
"github.com/apecloud/kubeblocks/internal/cli/printer" | ||
"github.com/apecloud/kubeblocks/internal/cli/types" | ||
"github.com/apecloud/kubeblocks/internal/cli/util" | ||
"github.com/apecloud/kubeblocks/internal/constant" | ||
dptypes "github.com/apecloud/kubeblocks/internal/dataprotection/types" | ||
) | ||
|
||
var ( | ||
describeExample = templates.Examples(` | ||
# describe a specified cluster definition | ||
kbcli clusterdefinition describe myclusterdef`) | ||
) | ||
|
||
type describeOptions struct { | ||
factory cmdutil.Factory | ||
client clientset.Interface | ||
dynamic dynamic.Interface | ||
namespace string | ||
|
||
names []string | ||
genericclioptions.IOStreams | ||
} | ||
|
||
func NewDescribeCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { | ||
o := &describeOptions{ | ||
factory: f, | ||
IOStreams: streams, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "describe", | ||
Short: "Describe ClusterDefinition.", | ||
Example: describeExample, | ||
Aliases: []string{"desc"}, | ||
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterDefGVR()), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdutil.CheckErr(o.complete(args)) | ||
cmdutil.CheckErr(o.run()) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func (o *describeOptions) complete(args []string) error { | ||
var err error | ||
|
||
if len(args) == 0 { | ||
return fmt.Errorf("cluster definition name should be specified") | ||
} | ||
o.names = args | ||
|
||
if o.client, err = o.factory.KubernetesClientSet(); err != nil { | ||
return err | ||
} | ||
|
||
if o.dynamic, err = o.factory.DynamicClient(); err != nil { | ||
return err | ||
} | ||
|
||
if o.namespace, _, err = o.factory.ToRawKubeConfigLoader().Namespace(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *describeOptions) run() error { | ||
for _, name := range o.names { | ||
if err := o.describeClusterDef(name); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (o *describeOptions) describeClusterDef(name string) error { | ||
// get cluster definition | ||
clusterDefObject, err := o.dynamic.Resource(types.ClusterDefGVR()).Get(context.TODO(), name, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
clusterDef := v1alpha1.ClusterDefinition{} | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(clusterDefObject.Object, &clusterDef); err != nil { | ||
return err | ||
} | ||
|
||
// get backup policy templates of the cluster definition | ||
opts := metav1.ListOptions{ | ||
LabelSelector: fmt.Sprintf("%s=%s", constant.ClusterDefLabelKey, name), | ||
} | ||
backupTemplatesListObj, err := o.dynamic.Resource(types.BackupPolicyTemplateGVR()).List(context.TODO(), opts) | ||
if err != nil { | ||
return err | ||
} | ||
var backupPolicyTemplates []*v1alpha1.BackupPolicyTemplate | ||
for _, item := range backupTemplatesListObj.Items { | ||
backupTemplate := v1alpha1.BackupPolicyTemplate{} | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, &backupTemplate); err != nil { | ||
return err | ||
} | ||
backupPolicyTemplates = append(backupPolicyTemplates, &backupTemplate) | ||
} | ||
|
||
showClusterDef(&clusterDef, o.Out) | ||
|
||
showBackupConfig(backupPolicyTemplates, o.Out) | ||
|
||
return nil | ||
} | ||
|
||
func showClusterDef(cd *v1alpha1.ClusterDefinition, out io.Writer) { | ||
if cd == nil { | ||
return | ||
} | ||
fmt.Fprintf(out, "Name: %s\t Type: %s\n\n", cd.Name, cd.Spec.Type) | ||
} | ||
|
||
func showBackupConfig(backupPolicyTemplates []*v1alpha1.BackupPolicyTemplate, out io.Writer) { | ||
if len(backupPolicyTemplates) == 0 { | ||
return | ||
} | ||
fmt.Fprintf(out, "Backup Config:\n") | ||
tbl := printer.NewTablePrinter(out) | ||
tbl.SetHeader("AUTO-BACKUP", "BACKUP-SCHEDULE", "BACKUP-METHOD", "BACKUP-RETENTION") | ||
defaultBackupPolicyTemplate := &v1alpha1.BackupPolicyTemplate{} | ||
for _, item := range backupPolicyTemplates { | ||
if item.Annotations[dptypes.DefaultBackupPolicyTemplateAnnotationKey] == "true" { | ||
defaultBackupPolicyTemplate = item | ||
break | ||
} | ||
} | ||
for _, policy := range defaultBackupPolicyTemplate.Spec.BackupPolicies { | ||
for _, schedule := range policy.Schedules { | ||
scheduleEnable := "Disabled" | ||
if schedule.Enabled != nil && *schedule.Enabled { | ||
scheduleEnable = "Enabled" | ||
} | ||
tbl.AddRow(scheduleEnable, schedule.CronExpression, schedule.BackupMethod, policy.RetentionPeriod) | ||
} | ||
} | ||
tbl.Print() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters