热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

kubectl源码分析之rolloutrestart

欢迎关注我的公众号:目前刚开始写一个月,一共写了18篇原创文章,文章目录如下:istio多集群探秘,部署了5
 欢迎关注我的公众号:

 目前刚开始写一个月,一共写了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完全手册

 

加qq群,请联系:


————————————————

type RestartOptions struct {//restart结构体PrintFlags *genericclioptions.PrintFlagsToPrinter func(string) (printers.ResourcePrinter, error)Resources []stringBuilder func() *resource.BuilderRestarter polymorphichelpers.ObjectRestarterFuncNamespace stringEnforceNamespace boolresource.FilenameOptionsgenericclioptions.IOStreams
}

func NewRolloutRestartOptions(streams genericclioptions.IOStreams) *RestartOptions {return &RestartOptions{//初始化结构体PrintFlags: genericclioptions.NewPrintFlags("restarted").WithTypeSetter(scheme.Scheme),IOStreams: streams,}
}

//创建restart命令
func NewCmdRolloutRestart(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {o := NewRolloutRestartOptions(streams)//初始化结构体validArgs := []string{"deployment", "daemonset", "statefulset"}cmd := &cobra.Command{//创建cobra命令Use: "restart RESOURCE",DisableFlagsInUseLine: true,Short: i18n.T("Restart a resource"),Long: restartLong,Example: restartExample,Run: func(cmd *cobra.Command, args []string) {cmdutil.CheckErr(o.Complete(f, cmd, args))//准备cmdutil.CheckErr(o.Validate())//校验cmdutil.CheckErr(o.RunRestart())//运行},ValidArgs: validArgs,//有效参数}usage := "identifying the resource to get from a server."cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)//文件选项o.PrintFlags.AddFlags(cmd)//打印选项return cmd
}

//准备函数
func (o *RestartOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {o.Resources = args//设置资源o.Restarter = polymorphichelpers.ObjectRestarterFn//设置Restarter函数var err erroro.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()//设置namespace和enforceNamespaceif err != nil {return err}o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {//print flag 转printer函数o.PrintFlags.NamePrintFlags.Operation = operationreturn o.PrintFlags.ToPrinter()}o.Builder = f.NewBuilder//设置builderreturn nil
}//校验
func (o *RestartOptions) Validate() error {if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {//资源和文件至少有一个return fmt.Errorf("required resource not specified")}return nil
}

//运行
func (o RestartOptions) RunRestart() error {r := o.Builder().WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).NamespaceParam(o.Namespace).DefaultNamespace().FilenameParam(o.EnforceNamespace, &o.FilenameOptions).ResourceTypeOrNameArgs(true, o.Resources...).ContinueOnError().Latest().Flatten().Do()//构造result对象if err := r.Err(); err != nil {return err}allErrs := []error{}infos, err := r.Infos()//获取info对象if err != nil {// restore previous command behavior where// an error caused by retrieving infos due to// at least a single broken object did not result// in an immediate return, but rather an overall// aggregation of errors.allErrs = append(allErrs, err)}for _, patch := range set.CalculatePatches(infos, scheme.DefaultJSONEncoder(), set.PatchFn(o.Restarter)) {//计算info补丁,并遍历info := patch.Infoif patch.Err != nil {//补丁有错误,append错误继续resourceString := info.Mapping.Resource.Resourceif len(info.Mapping.Resource.Group) > 0 {resourceString = resourceString + "." + info.Mapping.Resource.Group}allErrs = append(allErrs, fmt.Errorf("error: %s %q %v", resourceString, info.Name, patch.Err))continue}if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {//如果补丁为空,则append错误allErrs = append(allErrs, fmt.Errorf("failed to create patch for %v: empty patch", info.Name))}obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch, nil)//用helper应用补丁到服务端if err != nil {allErrs = append(allErrs, fmt.Errorf("failed to patch: %v", err))continue}info.Refresh(obj, true)//刷新obj对象printer, err := o.ToPrinter("restarted")//printflag转printerif err != nil {allErrs = append(allErrs, err)continue}if err = printer.PrintObj(info.Object, o.Out); err != nil {//打印结果allErrs = append(allErrs, err)}}return utilerrors.NewAggregate(allErrs)
}






推荐阅读
author-avatar
pan0608
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有