Kubernetes 新玩法:在 yaml 中编程( 三 )


// ExecSteps executes steps. func ExecSteps(ctx context.Context, steps []v1alpha1.BeidouStep, references []v1alpha1.BeidouReference) error { logger, _ := ctx.Value(CtxLogger).(*log.Entry) var hasMonitored bool for i, step := range steps { for j, op := range step.Operations { switch op.Op { case ''DeploymentCreationEfficiency'': if !hasMonitored { defer func() { err := monitor.Output() if err != nil { logger.Errorf(''Failed to output: %s'', err) } }() } hasMonitored = true } err := ExecOperation(ctx, op, references) if err != nil { return fmt.Errorf(''failed to run operation %s: %s'', op.Name, err) } } } return nil } // ExecOperation executes operation. func ExecOperation(ctx context.Context, op v1alpha1.BeidouOperation, references []v1alpha1.BeidouReference) error { switch op.Type { case v1alpha1.BeidouTask: if !tasks.IsRegistered(op.Op) { return ErrNotRegistered } if !tasks.DoesSupportReference(op.Op) { return ExecTask(ctx, op.Op, op.Args) } return ExecTaskWithRefer(ctx, op.Op, op.Args, references) } return nil } // ExecTask executes a task. func ExecTask(ctx context.Context, opname string, args []v1alpha1.BeidouArg) error { switch opname { case tasks.CreateNamespace: var ns string for _, arg := range args { switch arg.Name { case ''NS'': ns = arg.Value } } return op.CreateNamespace(ctx, ns) // ... } // ... } // ExecTaskWithRefer executes a task with reference. func ExecTaskWithRefer(ctx context.Context, opname string, args []v1alpha1.BeidouArg, references []v1alpha1.BeidouReference) error { switch opname { case tasks.RepeatNTimes: var times int var steps []v1alpha1.BeidouStep var err error for _, arg := range args { switch arg.Name { case ''TIMES'': times, err = strconv.Atoi(arg.Value) if err != nil { return ErrParseArgs } case ''ACTION'': for _, refer := range references { if refer.ID == arg.Reference.ID { steps = refer.Steps break } } } } return RepeatNTimes(ctx, times, steps) } return ErrNotImplemented }操作原理的实现示例:
// PodAnnotations is an operation used to check whether annotations of Pod are expected. func PodAnnotations(ctx context.Context, data PodAnnotationsData) error { kclient, ok := ctx.Value(tasks.KubernetesClient).(kubernetes.Interface) if !ok { return tasks.ErrNoKubernetesClient } pods, err := kclient.CoreV1().Pods(data.Namespace).List(metav1.ListOptions{}) if err != nil { return fmt.Errorf(''failed to list pods in ns %s: %s'', data.Namespace, err) } for _, pod := range pods.Items { if pod.Annotations == nil { return fmt.Errorf(''pod %s in ns %s has no annotations'', pod.Name, data.Namespace) } for _, annotation := range data.Exists { if _, exists := pod.Annotations[annotation]; !exists { return fmt.Errorf(''annotation %s does not exist in pod %s in ns %s'', annotation, pod.Name, data.Namespace) } } for k, v := range data.Equal { if pod.Annotations[k] != v { return fmt.Errorf(''value of annotation %s is not %s in pod %s in ns %s'', k, v, pod.Name, data.Namespace) } } } return nil }后续
目前阿里云容器服务团队内部已经实现了初版 , 已用于部分云产品的内部性能测试以及常规的回归测试 , 很大程度上提升了我们的工作效率 。


推荐阅读