github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/fault/fault_dns.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package fault 21 22 import ( 23 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1" 24 "github.com/spf13/cobra" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/cli-runtime/pkg/genericiooptions" 28 cmdutil "k8s.io/kubectl/pkg/cmd/util" 29 "k8s.io/kubectl/pkg/util/templates" 30 31 "github.com/1aal/kubeblocks/pkg/cli/create" 32 "github.com/1aal/kubeblocks/pkg/cli/util" 33 ) 34 35 var faultDNSExample = templates.Examples(` 36 // Inject DNS faults into all pods under the default namespace, so that any IP is returned when accessing the bing.com domain name. 37 kbcli fault dns random --patterns=bing.com --duration=1m 38 39 // Inject DNS faults into all pods under the default namespace, so that error is returned when accessing the bing.com domain name. 40 kbcli fault dns error --patterns=bing.com --duration=1m 41 `) 42 43 type DNSChaosOptions struct { 44 Patterns []string `json:"patterns"` 45 46 FaultBaseOptions 47 } 48 49 func NewDNSChaosOptions(f cmdutil.Factory, streams genericiooptions.IOStreams, action string) *DNSChaosOptions { 50 o := &DNSChaosOptions{ 51 FaultBaseOptions: FaultBaseOptions{ 52 CreateOptions: create.CreateOptions{ 53 Factory: f, 54 IOStreams: streams, 55 CueTemplateName: CueTemplateDNSChaos, 56 GVR: GetGVR(Group, Version, ResourceDNSChaos), 57 }, 58 Action: action, 59 }, 60 } 61 o.CreateOptions.PreCreate = o.PreCreate 62 o.CreateOptions.Options = o 63 return o 64 } 65 66 func NewDNSChaosCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 67 cmd := &cobra.Command{ 68 Use: "dns", 69 Short: "Inject faults into DNS server.", 70 } 71 cmd.AddCommand( 72 NewRandomCmd(f, streams), 73 NewErrorCmd(f, streams), 74 ) 75 return cmd 76 } 77 78 func NewRandomCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 79 o := NewDNSChaosOptions(f, streams, string(v1alpha1.RandomAction)) 80 cmd := o.NewCobraCommand(Random, RandomShort) 81 82 o.AddCommonFlag(cmd) 83 util.CheckErr(cmd.MarkFlagRequired("patterns")) 84 85 return cmd 86 } 87 88 func NewErrorCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 89 o := NewDNSChaosOptions(f, streams, string(v1alpha1.ErrorAction)) 90 cmd := o.NewCobraCommand(Error, ErrorShort) 91 92 o.AddCommonFlag(cmd) 93 util.CheckErr(cmd.MarkFlagRequired("patterns")) 94 95 return cmd 96 } 97 98 func (o *DNSChaosOptions) NewCobraCommand(use, short string) *cobra.Command { 99 return &cobra.Command{ 100 Use: use, 101 Short: short, 102 Example: faultDNSExample, 103 Run: func(cmd *cobra.Command, args []string) { 104 o.Args = args 105 cmdutil.CheckErr(o.CreateOptions.Complete()) 106 cmdutil.CheckErr(o.Validate()) 107 cmdutil.CheckErr(o.Complete()) 108 cmdutil.CheckErr(o.Run()) 109 }, 110 } 111 } 112 113 func (o *DNSChaosOptions) AddCommonFlag(cmd *cobra.Command) { 114 o.FaultBaseOptions.AddCommonFlag(cmd) 115 116 cmd.Flags().StringArrayVar(&o.Patterns, "patterns", nil, `Select the domain name template that matching the failure behavior & supporting placeholders ? and wildcards *.`) 117 118 // register flag completion func 119 registerFlagCompletionFunc(cmd, o.Factory) 120 } 121 122 func (o *DNSChaosOptions) Validate() error { 123 return o.BaseValidate() 124 } 125 126 func (o *DNSChaosOptions) Complete() error { 127 return o.BaseComplete() 128 } 129 130 func (o *DNSChaosOptions) PreCreate(obj *unstructured.Unstructured) error { 131 c := &v1alpha1.DNSChaos{} 132 if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, c); err != nil { 133 return err 134 } 135 136 data, e := runtime.DefaultUnstructuredConverter.ToUnstructured(c) 137 if e != nil { 138 return e 139 } 140 obj.SetUnstructuredContent(data) 141 return nil 142 }