欢迎关注我的公众号:
目前刚开始写一个月,一共写了18篇原创文章,文章目录如下:
istio多集群探秘,部署了50次多集群后我得出的结论
istio多集群链路追踪,附实操视频
istio防故障利器,你知道几个,istio新手不要读,太难!
istio业务权限控制,原来可以这么玩
istio实现非侵入压缩,微服务之间如何实现压缩
不懂envoyfilter也敢说精通istio系列-http-rbac-不要只会用AuthorizationPolicy配置权限
不懂envoyfilter也敢说精通istio系列-02-http-corsFilter-不要只会vs
不懂envoyfilter也敢说精通istio系列-03-http-csrf filter-再也不用再代码里写csrf逻辑了
不懂envoyfilter也敢说精通istio系列http-jwt_authn-不要只会RequestAuthorization
不懂envoyfilter也敢说精通istio系列-05-fault-filter-故障注入不止是vs
不懂envoyfilter也敢说精通istio系列-06-http-match-配置路由不只是vs
不懂envoyfilter也敢说精通istio系列-07-负载均衡配置不止是dr
不懂envoyfilter也敢说精通istio系列-08-连接池和断路器
不懂envoyfilter也敢说精通istio系列-09-http-route filter
不懂envoyfilter也敢说精通istio系列-network filter-redis proxy
不懂envoyfilter也敢说精通istio系列-network filter-HttpConnectionManager
不懂envoyfilter也敢说精通istio系列-ratelimit-istio ratelimit完全手册
————————————————
type QuotaOpts struct {
CreateSubcommandOptions *CreateSubcommandOptions //create命令公用的option
}
//创建create quota命令
func NewCmdCreateQuota(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
options := &QuotaOpts{//初始化option
CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
}
cmd := &cobra.Command{//创建cobra命令
Use: "quota NAME [--hard=key1=value1,key2=value2] [--scopes=Scope1,Scope2] [--dry-run=bool]",
DisableFlagsInUseLine: true,
Aliases: []string{"resourcequota"},
Short: i18n.T("Create a quota with the specified name."),
Long: quotaLong,
Example: quotaExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.Complete(f, cmd, args))//做准备工作
cmdutil.CheckErr(options.Run())//执行命令逻辑
},
}
options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)//创建print选项
cmdutil.AddApplyAnnotationFlags(cmd)//创建save-config选项
cmdutil.AddValidateFlags(cmd)//创建validate选项
cmdutil.AddGeneratorFlags(cmd, generateversioned.ResourceQuotaV1GeneratorName)//创建validate选项
cmd.Flags().String("hard", "", i18n.T("A comma-delimited set of resource=quantity pairs that define a hard limit."))//创建hard选项
cmd.Flags().String("scopes", "", i18n.T("A comma-delimited set of quota scopes that must all match each object tracked by the quota."))//创建scope选项
return cmd
}
//准备工作
func (o *QuotaOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
name, err := NameFromCommandArgs(cmd, args)//获取quota name
if err != nil {
return err
}
var generator generate.StructuredGenerator
switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {//后去generator,并判断generator是否有效
case generateversioned.ResourceQuotaV1GeneratorName:
generator = &generateversioned.ResourceQuotaGeneratorV1{//生成generator
Name: name,
Hard: cmdutil.GetFlagString(cmd, "hard"),
Scopes: cmdutil.GetFlagString(cmd, "scopes"),
}
default:
return errUnsupportedGenerator(cmd, generatorName)
}
return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)//运行createsubcommandoption的complete
}
//create命令公用的option的complete
func (o *CreateSubcommandOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, generator generate.StructuredGenerator) error {
name, err := NameFromCommandArgs(cmd, args)//获取资源名称
if err != nil {
return err
}
o.Name = name//设置名称
o.StructuredGenerator = generator//设置generator
o.DryRun = cmdutil.GetDryRunFlag(cmd)//后去dry-run值
o.CreateAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)//后去save-config选项值
if o.DryRun {//干跑运行干跑的complete
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter()//printflag转化为printer
if err != nil {
return err
}
o.PrintObj = func(obj kruntime.Object, out io.Writer) error {
return printer.PrintObj(obj, out)//设置printObj函数
}
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()//获取namespace和强制namspace
if err != nil {
return err
}
o.DynamicClient, err = f.DynamicClient()//设置client
if err != nil {
return err
}
o.Mapper, err = f.ToRESTMapper()//设置mapper
if err != nil {
return err
}
return nil
}
func (o *QuotaOpts) Run() error {//运行run
return o.CreateSubcommandOptions.Run()//运行createSubCommandOption的run
}
func (o *CreateSubcommandOptions) Run() error {
obj, err := o.StructuredGenerator.StructuredGenerate()//根据generator获取结构化的对象
if err != nil {
return err
}
if !o.DryRun {//非干跑
// create subcommands have compiled knowledge of things they create, so type them directly
gvks, _, err := scheme.Scheme.ObjectKinds(obj)//从obj中获取groupversionkind
if err != nil {
return err
}
gvk := gvks[0]
mapping, err := o.Mapper.RESTMapping(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}, gvk.Version)//根据greoup kind version获取mapping
if err != nil {
return err
}
if err := util.CreateOrUpdateAnnotation(o.CreateAnnotation, obj, scheme.DefaultJSONEncoder()); err != nil {//创建更新last-applied-configuration注解
return err
}
asUnstructured := &unstructured.Unstructured{}//创建非结构化对象
if err := scheme.Scheme.Convert(obj, asUnstructured, nil); err != nil {//把obj转化为非结构化对象
return err
}
if mapping.Scope.Name() == meta.RESTScopeNameRoot {//判断mapping的scope
o.Namespace = ""
}
actualObject, err := o.DynamicClient.Resource(mapping.Resource).Namespace(o.Namespace).Create(asUnstructured, metav1.CreateOptions{})//用client创建quota对象
if err != nil {
return err
}
// ensure we pass a versioned object to the printer
obj = actualObject//把服务器返回结构设置到obj
} else {
if meta, err := meta.Accessor(obj); err == nil && o.EnforceNamespace {
meta.SetNamespace(o.Namespace)
}
}
return o.PrintObj(obj, o.Out)//打印obj,到输出
}