github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_unsafe_delete_service_gc_safepoint.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cli
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/pingcap/errors"
    20  	v2 "github.com/pingcap/tiflow/cdc/api/v2"
    21  	apiv2client "github.com/pingcap/tiflow/pkg/api/v2"
    22  	"github.com/pingcap/tiflow/pkg/cmd/context"
    23  	"github.com/pingcap/tiflow/pkg/cmd/factory"
    24  	"github.com/pingcap/tiflow/pkg/cmd/util"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  // unsafeDeleteServiceGcSafepointOptions defines flags
    29  // for the `cli unsafe delete-service-gc-safepoint` command.
    30  type unsafeDeleteServiceGcSafepointOptions struct {
    31  	apiClient        apiv2client.APIV2Interface
    32  	upstreamPDAddrs  string
    33  	upstreamCaPath   string
    34  	upstreamCertPath string
    35  	upstreamKeyPath  string
    36  }
    37  
    38  // newUnsafeDeleteServiceGcSafepointOptions creates new unsafeDeleteServiceGcSafepointOptions
    39  // for the `cli unsafe delete-service-gc-safepoint` command.
    40  func newUnsafeDeleteServiceGcSafepointOptions() *unsafeDeleteServiceGcSafepointOptions {
    41  	return &unsafeDeleteServiceGcSafepointOptions{}
    42  }
    43  
    44  // addFlags receives a *cobra.Command reference and binds
    45  // flags related to template printing to it.
    46  func (o *unsafeDeleteServiceGcSafepointOptions) addFlags(cmd *cobra.Command) {
    47  	if o == nil {
    48  		return
    49  	}
    50  	cmd.PersistentFlags().StringVar(&o.upstreamPDAddrs, "upstream-pd", "",
    51  		"upstream PD address, use ',' to separate multiple PDs")
    52  	cmd.PersistentFlags().StringVar(&o.upstreamCaPath, "upstream-ca", "",
    53  		"CA certificate path for TLS connection to upstream")
    54  	cmd.PersistentFlags().StringVar(&o.upstreamCertPath, "upstream-cert", "",
    55  		"Certificate path for TLS connection to upstream")
    56  	cmd.PersistentFlags().StringVar(&o.upstreamKeyPath, "upstream-key", "",
    57  		"Private key path for TLS connection to upstream")
    58  	// we don't support specify there flags below when cdc version <= 6.3.0
    59  	_ = cmd.PersistentFlags().MarkHidden("upstream-pd")
    60  	_ = cmd.PersistentFlags().MarkHidden("upstream-ca")
    61  	_ = cmd.PersistentFlags().MarkHidden("upstream-cert")
    62  	_ = cmd.PersistentFlags().MarkHidden("upstream-key")
    63  }
    64  
    65  // complete adapts from the command line args to the data and client required.
    66  func (o *unsafeDeleteServiceGcSafepointOptions) complete(f factory.Factory) error {
    67  	apiClient, err := f.APIV2Client()
    68  	if err != nil {
    69  		return err
    70  	}
    71  	o.apiClient = apiClient
    72  	return err
    73  }
    74  
    75  // run runs the `cli unsafe delete-service-gc-safepoint` command.
    76  func (o *unsafeDeleteServiceGcSafepointOptions) run(cmd *cobra.Command) error {
    77  	ctx := context.GetDefaultContext()
    78  
    79  	err := o.apiClient.Unsafe().DeleteServiceGcSafePoint(ctx, o.getUpstreamConfig())
    80  	if err == nil {
    81  		cmd.Println("CDC service GC safepoint truncated in PD!")
    82  	}
    83  
    84  	return errors.Trace(err)
    85  }
    86  
    87  func (o *unsafeDeleteServiceGcSafepointOptions) getUpstreamConfig() *v2.UpstreamConfig {
    88  	var pdAddrs []string
    89  	if o.upstreamPDAddrs != "" {
    90  		pdAddrs = strings.Split(o.upstreamPDAddrs, ",")
    91  	}
    92  	return &v2.UpstreamConfig{
    93  		PDConfig: v2.PDConfig{
    94  			PDAddrs:       pdAddrs,
    95  			CAPath:        o.upstreamCaPath,
    96  			CertPath:      o.upstreamCertPath,
    97  			KeyPath:       o.upstreamKeyPath,
    98  			CertAllowedCN: nil,
    99  		},
   100  	}
   101  }
   102  
   103  // newCmdDeleteServiceGcSafepoint creates the `cli unsafe delete-service-gc-safepoint` command.
   104  func newCmdDeleteServiceGcSafepoint(f factory.Factory, commonOptions *unsafeCommonOptions) *cobra.Command {
   105  	o := newUnsafeDeleteServiceGcSafepointOptions()
   106  
   107  	command := &cobra.Command{
   108  		Use:   "delete-service-gc-safepoint",
   109  		Short: "Delete CDC service GC safepoint in PD, confirm that you know what this command will do and use it at your own risk",
   110  		Args:  cobra.NoArgs,
   111  		Run: func(cmd *cobra.Command, args []string) {
   112  			util.CheckErr(commonOptions.confirmMetaDelete(cmd))
   113  			util.CheckErr(o.complete(f))
   114  			util.CheckErr(o.run(cmd))
   115  		},
   116  	}
   117  	o.addFlags(command)
   118  	return command
   119  }