欢迎关注我的公众号:

目前刚开始写一个月,一共写了18篇原创文章,文章目录如下:
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 SetLastAppliedOptions struct {//set-last-applied结构体
CreateAnnotation bool
PrintFlags *genericclioptions.PrintFlags
PrintObj printers.ResourcePrinterFunc
FilenameOptions resource.FilenameOptions
infoList []*resource.Info
namespace string
enforceNamespace bool
dryRun bool
shortOutput bool
output string
patchBufferList []PatchBuffer
builder *resource.Builder
unstructuredClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
genericclioptions.IOStreams
}
// PatchBuffer caches changes that are to be applied.
type PatchBuffer struct {//patch结构体
Patch []byte
PatchType types.PatchType
}
func NewSetLastAppliedOptions(ioStreams genericclioptions.IOStreams) *SetLastAppliedOptions {
return &SetLastAppliedOptions{//初始化结构体
PrintFlags: genericclioptions.NewPrintFlags("configured").WithTypeSetter(scheme.Scheme),
IOStreams: ioStreams,
}
}
//创建set-last-applied命令
func NewCmdApplySetLastApplied(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := NewSetLastAppliedOptions(ioStreams)//初始化结构体
cmd := &cobra.Command{//创建cobra命令
Use: "set-last-applied -f FILENAME",
DisableFlagsInUseLine: true,
Short: i18n.T("Set the last-applied-configuration annotation on a live object to match the contents of a file."),
Long: applySetLastAppliedLong,
Example: applySetLastAppliedExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(f, cmd))//准备
cmdutil.CheckErr(o.Validate())//校验
cmdutil.CheckErr(o.RunSetLastApplied())//运行
},
}
o.PrintFlags.AddFlags(cmd)//打印选项
cmdutil.AddDryRunFlag(cmd)//干跑选项
cmd.Flags().BoolVar(&o.CreateAnnotation, "create-annotation", o.CreateAnnotation, "Will create 'last-applied-configuration' annotations if current objects doesn't have one")//create-annotation选项
cmdutil.AddJsonFilenameFlag(cmd.Flags(), &o.FilenameOptions.Filenames, "Filename, directory, or URL to files that contains the last-applied-configuration annotations")//文件选项
return cmd
}
//准备函数
func (o *SetLastAppliedOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
o.dryRun = cmdutil.GetDryRunFlag(cmd)//获取干跑选项
o.output = cmdutil.GetFlagString(cmd, "output")//获取output选项
o.shortOutput = o.output == "name"//判断是否短输出
var err error
o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()//获取namespace和enforceNamespace
if err != nil {
return err
}
o.builder = f.NewBuilder()//设置builder
o.unstructuredClientForMapping = f.UnstructuredClientForMapping//设置unstructuredClientForMapping
if o.dryRun {//如果是干跑,则准备干跑
// TODO(juanvallejo): This can be cleaned up even further by creating
// a PrintFlags struct that binds the --dry-run flag, and whose
// ToPrinter method returns a printer that understands how to print
// this success message.
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter()//print flag转printer
if err != nil {
return err
}
o.PrintObj = printer.PrintObj//设置printObj函数
return nil
}
//校验
func (o *SetLastAppliedOptions) Validate() error {
r := o.builder.
Unstructured().
NamespaceParam(o.namespace).DefaultNamespace().
FilenameParam(o.enforceNamespace, &o.FilenameOptions).
Flatten().
Do()//构造result对象
err := r.Visit(func(info *resource.Info, err error) error {// visit result
if err != nil {
return err
}
patchBuf, diffBuf, patchType, err := editor.GetApplyPatch(info.Object.(runtime.Unstructured))//获取patch
if err != nil {
return err
}
// Verify the object exists in the cluster before trying to patch it.
if err := info.Get(); err != nil {//get远程对象判断是否存在
if errors.IsNotFound(err) {
return err
}
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
}
originalBuf, err := util.GetOriginalConfiguration(info.Object)//获取原始的注解内容
if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
}
if originalBuf == nil && !o.CreateAnnotation {//如果原始注解内容为空,且create-annotation为false,则报错
return fmt.Errorf("no last-applied-configuration annotation found on resource: %s, to create the annotation, run the command with --create-annotation", info.Name)
}
//only add to PatchBufferList when changed
if !bytes.Equal(cmdutil.StripComments(originalBuf), cmdutil.StripComments(diffBuf)) {//比较原始注解和新patch是否相同,如果不同则执行下面代码
p := PatchBuffer{Patch: patchBuf, PatchType: patchType}//构造patchBuffer
o.patchBufferList = append(o.patchBufferList, p)// append patchbuffer
o.infoList = append(o.infoList, info)//info append到infolist
} else {
fmt.Fprintf(o.Out, "set-last-applied %s: no changes required.\n", info.Name)
}
return nil
})
return err
}
//运行
func (o *SetLastAppliedOptions) RunSetLastApplied() error {
for i, patch := range o.patchBufferList {//遍历patchbufferList
info := o.infoList[i]//获取info
finalObj := info.Object//获取info的object
if !o.dryRun {//非干跑
mapping := info.ResourceMapping()//获取info的mapping
client, err := o.unstructuredClientForMapping(mapping)//通过mapping获取client
if err != nil {
return err
}
helper := resource.NewHelper(client, mapping)//构造helper
finalObj, err = helper.Patch(o.namespace, info.Name, patch.PatchType, patch.Patch, nil)//应用patch到服务端
if err != nil {
return err
}
}
if err := o.PrintObj(finalObj, o.Out); err != nil {//打印对象
return err
}
}
return nil
}
2338

被折叠的 条评论
为什么被折叠?



