From 83cffe2bedaa58635639629ca8c79afedbb95e92 Mon Sep 17 00:00:00 2001 From: Shanshan Date: Mon, 17 Feb 2025 11:46:02 +0800 Subject: [PATCH 1/2] chore: remove enable-all-logs from kbcli update --- pkg/cmd/cluster/list_logs.go | 3 +- pkg/cmd/cluster/update.go | 214 --------------------------------- pkg/cmd/cluster/update_test.go | 8 -- 3 files changed, 1 insertion(+), 224 deletions(-) diff --git a/pkg/cmd/cluster/list_logs.go b/pkg/cmd/cluster/list_logs.go index 546eebea6..6eef0822c 100644 --- a/pkg/cmd/cluster/list_logs.go +++ b/pkg/cmd/cluster/list_logs.go @@ -145,8 +145,7 @@ func (o *ListLogsOptions) printListLogs(dataObj *cluster.ClusterObjects) error { tbl := printer.NewTablePrinter(o.Out) logFilesData := o.gatherLogFilesData(dataObj.Cluster, dataObj.ClusterDef, dataObj.Pods) if len(logFilesData) == 0 { - fmt.Fprintf(o.ErrOut, "No log files found. You can enable the log feature with the kbcli command below.\n"+ - "kbcli cluster update %s --enable-all-logs=true --namespace %s\n", dataObj.Cluster.Name, dataObj.Cluster.Namespace) + fmt.Fprintf(o.ErrOut, "No log files found. \n") } else { tbl.SetHeader("INSTANCE", "LOG-TYPE", "FILE-PATH", "SIZE", "LAST-WRITTEN", "COMPONENT") for _, f := range logFilesData { diff --git a/pkg/cmd/cluster/update.go b/pkg/cmd/cluster/update.go index f1c212824..e2b7261f8 100644 --- a/pkg/cmd/cluster/update.go +++ b/pkg/cmd/cluster/update.go @@ -20,23 +20,16 @@ along with this program. If not, see . package cluster import ( - "bytes" "context" "encoding/csv" - "encoding/json" "fmt" "strconv" "strings" - "text/template" kbappsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" - opsv1alpha1 "github.com/apecloud/kubeblocks/apis/operations/v1alpha1" - "github.com/google/uuid" - "github.com/pkg/errors" "github.com/robfig/cron/v3" "github.com/spf13/cobra" "github.com/spf13/pflag" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -45,15 +38,10 @@ import ( cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" - appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1" - appsv1beta1 "github.com/apecloud/kubeblocks/apis/apps/v1beta1" dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1" - cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core" cfgutil "github.com/apecloud/kubeblocks/pkg/configuration/util" "github.com/apecloud/kubeblocks/pkg/constant" - "github.com/apecloud/kubeblocks/pkg/controller/configuration" "github.com/apecloud/kubeblocks/pkg/dataprotection/utils" - "github.com/apecloud/kubeblocks/pkg/gotemplate" "github.com/apecloud/kbcli/pkg/action" "github.com/apecloud/kbcli/pkg/cluster" @@ -68,9 +56,6 @@ var clusterUpdateExample = templates.Examples(` # enable cluster monitor kbcli cluster update mycluster --monitor=true - # enable all logs - kbcli cluster update mycluster --enable-all-logs=true - # update cluster tolerations kbcli cluster update mycluster --tolerations='"key=engineType,value=mongo,operator=Equal,effect=NoSchedule","key=diskType,value=ssd,operator=Equal,effect=NoSchedule"' @@ -109,7 +94,6 @@ type UpdatableFlags struct { // Add-on switches for cluster observability DisableExporter bool `json:"monitor"` - EnableAllLogs bool `json:"enableAllLogs"` // Configuration and options for cluster affinity and tolerations PodAntiAffinity string `json:"podAntiAffinity"` @@ -174,7 +158,6 @@ func NewUpdateCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra. func (f *UpdatableFlags) addFlags(cmd *cobra.Command) { cmd.Flags().BoolVar(&f.DisableExporter, "disable-exporter", true, "Enable or disable monitoring") - cmd.Flags().BoolVar(&f.EnableAllLogs, "enable-all-logs", false, "Enable advanced application all log extraction, set to true will ignore enabledLogs of component level, default is false") cmd.Flags().StringVar(&f.TerminationPolicy, "termination-policy", "Delete", "Termination policy, one of: (DoNotTerminate, Delete, WipeOut)") cmd.Flags().StringSliceVar(&f.TolerationsRaw, "tolerations", nil, `Tolerations for cluster, such as "key=value:effect, key:effect", for example '"engineType=mongo:NoSchedule", "diskType:NoSchedule"'`) cmd.Flags().BoolVar(&f.BackupEnabled, "backup-enabled", false, "Specify whether enabled automated backup") @@ -332,7 +315,6 @@ func (o *UpdateOptions) buildPatch() error { // monitor and logs "disable-exporter": {field: "disableExporter", obj: nil, fn: buildComps}, - "enable-all-logs": {field: "enable-all-logs", obj: nil, fn: buildComps}, // backup config "backup-enabled": {field: "enabled", obj: nil, fn: buildBackup}, @@ -424,8 +406,6 @@ func (o *UpdateOptions) buildComponents(field string, val string) error { switch field { case "disableExporter": return o.updateMonitor(val) - case "enable-all-logs": - return o.updateEnabledLog(val) default: return nil } @@ -463,200 +443,6 @@ func (o *UpdateOptions) buildBackup(field string, val string) error { } } -func (o *UpdateOptions) updateEnabledLog(val string) error { - boolVal, err := strconv.ParseBool(val) - if err != nil { - return err - } - - // update --enabled-all-logs=false for all components - if !boolVal { - // TODO: replace with new api - /* for index := range o.cluster.Spec.ComponentSpecs { - o.cluster.Spec.ComponentSpecs[index].EnabledLogs = nil - }*/ - return nil - } - - // update --enabled-all-logs=true for all components - cd, err := cluster.GetClusterDefByName(o.dynamic, o.cluster.Spec.ClusterDef) - if err != nil { - return err - } - // TODO: replace with new api - // set --enabled-all-logs at cluster components - // setEnableAllLogs(o.cluster, cd) - if err = o.reconfigureLogVariables(o.cluster, cd); err != nil { - return errors.Wrap(err, "failed to reconfigure log variables of target cluster") - } - return nil -} - -const logsBlockName = "logsBlock" -const logsTemplateName = "template-logs-block" -const topTPLLogsObject = "component" -const defaultSectionName = "default" - -// reconfigureLogVariables reconfigures the log variables of cluster -func (o *UpdateOptions) reconfigureLogVariables(c *kbappsv1.Cluster, cd *kbappsv1.ClusterDefinition) error { - var ( - err error - configSpec *appsv1alpha1.ComponentConfigSpec - logValue *gotemplate.TplValues - ) - - createReconfigureOps := func(compSpec kbappsv1.ClusterComponentSpec, configSpec *appsv1alpha1.ComponentConfigSpec, logValue *gotemplate.TplValues) error { - var ( - buf bytes.Buffer - keyName string - configTemplate *corev1.ConfigMap - formatter *appsv1beta1.FileFormatConfig - logTPL *template.Template - logVariables map[string]string - unstructuredObj *unstructured.Unstructured - ) - - if configTemplate, formatter, err = findConfigTemplateInfo(o.dynamic, configSpec); err != nil { - return err - } - if keyName, logTPL, err = findLogsBlockTPL(configTemplate.Data); err != nil { - return err - } - if logTPL == nil { - return nil - } - if err = logTPL.Execute(&buf, logValue); err != nil { - return err - } - // TODO: very hack logic for ini config file - formatter.FormatterAction = appsv1beta1.FormatterAction{IniConfig: &appsv1beta1.IniConfig{SectionName: defaultSectionName}} - if logVariables, err = cfgcore.TransformConfigFileToKeyValueMap(keyName, formatter, buf.Bytes()); err != nil { - return err - } - // build OpsRequest and apply this OpsRequest - opsRequest := buildLogsReconfiguringOps(c.Name, c.Namespace, compSpec.Name, configSpec.Name, keyName, logVariables) - if unstructuredObj, err = util.ConvertObjToUnstructured(opsRequest); err != nil { - return err - } - return util.CreateResourceIfAbsent(o.dynamic, types.OpsGVR(), c.Namespace, unstructuredObj) - } - - for _, compSpec := range c.Spec.ComponentSpecs { - if configSpec, err = findFirstConfigSpec(o.dynamic, compSpec.Name, compSpec.ComponentDef); err != nil { - return err - } - if logValue, err = buildLogsTPLValues(&compSpec); err != nil { - return err - } - if err = createReconfigureOps(compSpec, configSpec, logValue); err != nil { - return err - } - } - return nil -} - -func findFirstConfigSpec( - cli dynamic.Interface, - compName string, - compDefName string) (*appsv1alpha1.ComponentConfigSpec, error) { - compDef, err := util.GetComponentDefByName(cli, compDefName) - if err != nil { - return nil, err - } - configSpecs, err := util.GetValidConfigSpecs(true, util.ToV1ComponentConfigSpecs(compDef.Spec.Configs)) - if err != nil { - return nil, err - } - if len(configSpecs) == 0 { - return nil, errors.Errorf("no config templates for component %s", compName) - } - return &configSpecs[0], nil -} - -func findConfigTemplateInfo(dynamic dynamic.Interface, configSpec *appsv1alpha1.ComponentConfigSpec) (*corev1.ConfigMap, *appsv1beta1.FileFormatConfig, error) { - if configSpec == nil { - return nil, nil, errors.New("configTemplateSpec is nil") - } - configTemplate, err := cluster.GetConfigMapByName(dynamic, configSpec.Namespace, configSpec.TemplateRef) - if err != nil { - return nil, nil, err - } - configConstraint, err := cluster.GetConfigConstraintByName(dynamic, configSpec.ConfigConstraintRef) - if err != nil { - return nil, nil, err - } - return configTemplate, configConstraint.Spec.FileFormatConfig, nil -} - -func newConfigTemplateEngine() *template.Template { - customizedFuncMap := configuration.BuiltInCustomFunctions(nil, nil, nil) - engine := gotemplate.NewTplEngine(nil, customizedFuncMap, logsTemplateName, nil, context.TODO()) - return engine.GetTplEngine() -} - -func findLogsBlockTPL(confData map[string]string) (string, *template.Template, error) { - engine := newConfigTemplateEngine() - for key, value := range confData { - if !strings.Contains(value, logsBlockName) { - continue - } - tpl, err := engine.Parse(value) - if err != nil { - return key, nil, err - } - logTPL := tpl.Lookup(logsBlockName) - // find target logs template - if logTPL != nil { - return key, logTPL, nil - } - return "", nil, errors.New("no logs config template found") - } - return "", nil, nil -} - -func buildLogsTPLValues(compSpec *kbappsv1.ClusterComponentSpec) (*gotemplate.TplValues, error) { - compMap := map[string]interface{}{} - bytesData, err := json.Marshal(compSpec) - if err != nil { - return nil, err - } - err = json.Unmarshal(bytesData, &compMap) - if err != nil { - return nil, err - } - value := gotemplate.TplValues{ - topTPLLogsObject: compMap, - } - return &value, nil -} - -func buildLogsReconfiguringOps(clusterName, namespace, compName, configName, keyName string, variables map[string]string) *opsv1alpha1.OpsRequest { - opsName := fmt.Sprintf("%s-%s", "logs-reconfigure", uuid.NewString()) - opsRequest := util.NewOpsRequestForReconfiguring(opsName, namespace, clusterName) - parameterPairs := make([]opsv1alpha1.ParameterPair, 0, len(variables)) - for key, value := range variables { - v := value - parameterPairs = append(parameterPairs, opsv1alpha1.ParameterPair{ - Key: key, - Value: &v, - }) - } - var keys []opsv1alpha1.ParameterConfig - keys = append(keys, opsv1alpha1.ParameterConfig{ - Key: keyName, - Parameters: parameterPairs, - }) - var configurations []opsv1alpha1.ConfigurationItem - configurations = append(configurations, opsv1alpha1.ConfigurationItem{ - Keys: keys, - Name: configName, - }) - reconfigure := opsRequest.Spec.Reconfigures[0] - reconfigure.ComponentName = compName - reconfigure.Configurations = append(reconfigure.Configurations, configurations...) - return opsRequest -} - func (o *UpdateOptions) updateMonitor(val string) error { disableExporter, err := strconv.ParseBool(val) if err != nil { diff --git a/pkg/cmd/cluster/update_test.go b/pkg/cmd/cluster/update_test.go index 301063038..9ee72605d 100644 --- a/pkg/cmd/cluster/update_test.go +++ b/pkg/cmd/cluster/update_test.go @@ -92,14 +92,6 @@ var _ = Describe("cluster update", func() { Expect(o.Complete()).Should(Succeed()) Expect(o.Patch).Should(ContainSubstring("\"disableExporter\":false")) }) - - It("set enable-all-logs", func() { - fakeCluster := testing.FakeCluster("c1", "default") - tf.FakeDynamicClient = testing.FakeDynamicClient(fakeCluster) - Expect(cmd.Flags().Set("enable-all-logs", "false")).Should(Succeed()) - Expect(o.CmdComplete(cmd, args)).Should(Succeed()) - Expect(o.Complete()).Should(Succeed()) - }) }) /* Context("logs variables reconfiguring tests", func() { From 09402df78a665495da8a823bd467670d882c90c1 Mon Sep 17 00:00:00 2001 From: shanshanying Date: Mon, 17 Feb 2025 03:49:29 +0000 Subject: [PATCH 2/2] chore: auto update cli doc changes --- docs/user_docs/cli/kbcli_cluster_create.md | 3 --- docs/user_docs/cli/kbcli_cluster_create_qdrant.md | 2 +- docs/user_docs/cli/kbcli_cluster_create_rabbitmq.md | 2 +- docs/user_docs/cli/kbcli_cluster_custom-ops.md | 4 ---- docs/user_docs/cli/kbcli_cluster_update.md | 4 ---- 5 files changed, 2 insertions(+), 13 deletions(-) diff --git a/docs/user_docs/cli/kbcli_cluster_create.md b/docs/user_docs/cli/kbcli_cluster_create.md index 474aafcae..808727057 100644 --- a/docs/user_docs/cli/kbcli_cluster_create.md +++ b/docs/user_docs/cli/kbcli_cluster_create.md @@ -55,9 +55,7 @@ kbcli cluster create [NAME] [flags] * [kbcli cluster](kbcli_cluster.md) - Cluster command. * [kbcli cluster create apecloud-mysql](kbcli_cluster_create_apecloud-mysql.md) - Create a apecloud-mysql cluster. -* [kbcli cluster create clickhouse](kbcli_cluster_create_clickhouse.md) - Create a clickhouse cluster. * [kbcli cluster create etcd](kbcli_cluster_create_etcd.md) - Create a etcd cluster. -* [kbcli cluster create kafak2](kbcli_cluster_create_kafak2.md) - Create a kafak2 cluster. * [kbcli cluster create kafka](kbcli_cluster_create_kafka.md) - Create a kafka cluster. * [kbcli cluster create mongodb](kbcli_cluster_create_mongodb.md) - Create a mongodb cluster. * [kbcli cluster create mysql](kbcli_cluster_create_mysql.md) - Create a mysql cluster. @@ -65,7 +63,6 @@ kbcli cluster create [NAME] [flags] * [kbcli cluster create qdrant](kbcli_cluster_create_qdrant.md) - Create a qdrant cluster. * [kbcli cluster create rabbitmq](kbcli_cluster_create_rabbitmq.md) - Create a rabbitmq cluster. * [kbcli cluster create redis](kbcli_cluster_create_redis.md) - Create a redis cluster. -* [kbcli cluster create testredis](kbcli_cluster_create_testredis.md) - Create a testredis cluster. #### Go Back to [CLI Overview](cli.md) Homepage. diff --git a/docs/user_docs/cli/kbcli_cluster_create_qdrant.md b/docs/user_docs/cli/kbcli_cluster_create_qdrant.md index 67ef8d082..be0d2f7ba 100644 --- a/docs/user_docs/cli/kbcli_cluster_create_qdrant.md +++ b/docs/user_docs/cli/kbcli_cluster_create_qdrant.md @@ -34,7 +34,7 @@ kbcli cluster create qdrant NAME [flags] --storage float Storage size, the unit is Gi. Value range [1, 10000]. (default 20) --storage-class-name string Storage class name of the data volume --tenancy string Tenancy options, one of: (SharedNode, DedicatedNode) (default "SharedNode") - --termination-policy string The termination policy of cluster. Legal values [DoNotTerminate, Halt, Delete, WipeOut]. (default "Delete") + --termination-policy string The termination policy of cluster. Legal values [DoNotTerminate, Delete, WipeOut]. (default "Delete") --tolerations strings Tolerations for cluster, such as "key=value:effect,key:effect", for example '"engineType=mongo:NoSchedule", "diskType:NoSchedule"' --topology-keys stringArray Topology keys for affinity --version string The version of Qdrant. (default "1.10.0") diff --git a/docs/user_docs/cli/kbcli_cluster_create_rabbitmq.md b/docs/user_docs/cli/kbcli_cluster_create_rabbitmq.md index 168a18cee..70967244d 100644 --- a/docs/user_docs/cli/kbcli_cluster_create_rabbitmq.md +++ b/docs/user_docs/cli/kbcli_cluster_create_rabbitmq.md @@ -35,7 +35,7 @@ kbcli cluster create rabbitmq NAME [flags] --storage float Storage size, the unit is Gi. Value range [1, 10000]. (default 20) --storage-class-name string Storage class name of the data volume --tenancy string Tenancy options, one of: (SharedNode, DedicatedNode) (default "SharedNode") - --termination-policy string The termination policy of cluster. Legal values [DoNotTerminate, Halt, Delete, WipeOut]. (default "Delete") + --termination-policy string The termination policy of cluster. Legal values [DoNotTerminate, Delete, WipeOut]. (default "Delete") --tolerations strings Tolerations for cluster, such as "key=value:effect,key:effect", for example '"engineType=mongo:NoSchedule", "diskType:NoSchedule"' --topology-keys stringArray Topology keys for affinity --version string Cluster version. Legal values [3.13.7, 3.13.2, 3.12.14, 3.11.28, 3.10.25, 3.9.29, 3.8.14]. (default "3.13.7") diff --git a/docs/user_docs/cli/kbcli_cluster_custom-ops.md b/docs/user_docs/cli/kbcli_cluster_custom-ops.md index fc271cf0c..8bd6e19e2 100644 --- a/docs/user_docs/cli/kbcli_cluster_custom-ops.md +++ b/docs/user_docs/cli/kbcli_cluster_custom-ops.md @@ -57,10 +57,6 @@ kbcli cluster custom-ops OpsDef --cluster [fl ### SEE ALSO * [kbcli cluster](kbcli_cluster.md) - Cluster command. -* [kbcli cluster custom-ops kafka-quota](kbcli_cluster_custom-ops_kafka-quota.md) - Create a custom ops with opsDef kafka-quota -* [kbcli cluster custom-ops kafka-topic](kbcli_cluster_custom-ops_kafka-topic.md) - Create a custom ops with opsDef kafka-topic -* [kbcli cluster custom-ops kafka-user-acl](kbcli_cluster_custom-ops_kafka-user-acl.md) - Create a custom ops with opsDef kafka-user-acl -* [kbcli cluster custom-ops mysql-orc-switchover](kbcli_cluster_custom-ops_mysql-orc-switchover.md) - Create a custom ops with opsDef mysql-orc-switchover #### Go Back to [CLI Overview](cli.md) Homepage. diff --git a/docs/user_docs/cli/kbcli_cluster_update.md b/docs/user_docs/cli/kbcli_cluster_update.md index 19c0bdf63..62077f100 100644 --- a/docs/user_docs/cli/kbcli_cluster_update.md +++ b/docs/user_docs/cli/kbcli_cluster_update.md @@ -17,9 +17,6 @@ kbcli cluster update NAME [flags] # enable cluster monitor kbcli cluster update mycluster --monitor=true - # enable all logs - kbcli cluster update mycluster --enable-all-logs=true - # update cluster tolerations kbcli cluster update mycluster --tolerations='"key=engineType,value=mongo,operator=Equal,effect=NoSchedule","key=diskType,value=ssd,operator=Equal,effect=NoSchedule"' @@ -64,7 +61,6 @@ kbcli cluster update NAME [flags] --disable-exporter Enable or disable monitoring (default true) --dry-run string[="unchanged"] Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource. (default "none") --edit Edit the API resource - --enable-all-logs Enable advanced application all log extraction, set to true will ignore enabledLogs of component level, default is false -h, --help help for update -o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file). --pitr-enabled Specify whether enabled point in time recovery