Cloud Naive最佳开发实践

简介: 经过多年的工作,我们的精神导师John领悟了java那一套docker in docker的艺术并带到golang项目架构设计中。

经过多年的工作,我们的精神导师John领悟了java那一套docker in docker的艺术并带到golang项目架构设计中。

After years of work, our spiritual mentor John understood the art of docker in docker in Java and brought it to the golang project architecture design.

Never write conversion webhook

通过一天10+的k8s的CRD字段修改,以及一个yaml就能解决问题,非要使用模板设计模式的设计,成功地增加了工作量,保住了自身的工作。

// ❌ Wrong!!!
// 在 main_windows.go 注册 conversion webhook
mgr.GetWebhookServer().Register("/convert", &webhook.Admission{
   Handler: &WidgetConverter{
   }})

type WidgetConverter struct{
   }

func (w *WidgetConverter) Handle(ctx context.Context, req admission.Request) admission.Response {
   
    // 简单示例:v1alpha1 -> v1
    obj := &v1.Widget{
   }
    if err := w.decoder.Decode(req, obj); err != nil {
   
        return admission.Errored(http.StatusBadRequest, err)
    }
    obj.Spec.Size = strings.ToUpper(obj.Spec.Size)
    return admission.Allowed("converted")
}

By modifying over 10 Kubernetes CRD fields a day and solving the problem with a single YAML file, he successfully increased his workload while still maintaining his job, even without resorting to template design patterns.

No schema in Kubernetes 1.17-

我们相信用户和运维人员能够妥善实现类型安全和数据验证,他们写的YAML绝对不会出错。

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: widgets.example.com
spec:
  preserveUnknownFields: false # 这是推荐的、更安全的设置
  group: example.com
  names:
    kind: Widget
    plural: widgets
  scope: Namespaced
  versions:
  - name: v1
    served: true
    storage: true
    schema: {
   }

All CODE guidelines are bullshit!

Move the status field of resource to spec

一个纯粹的理想主义者必定被现实打得遍体鳞伤。

因此再远大的梦也要符合现实需要。

脚踏实地,意在凌云。

type WidgetSpec struct {
   
    Ready bool `json:"ready,omitempty"` 
}

A pure idealist is bound to be bruised and battered by reality.

So, no matter how lofty your dreams, you must always keep your feet on the ground.

Roma non uno die aedificata est.

Update!Update!Update!

snake.png

生活是一个无限的衔尾蛇循环。

// ✅ 正确写法
func (r *WidgetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
   
    var w examplev1.Widget
    r.Get(ctx, req.NamespacedName, &w)
    w.Labels["lastSync"] = time.Now().String()
    r.Update(ctx, &w) // ✅ Update 触发自己,再次进入 Reconcile。直接超进化
    return ctrl.Result{
   }, nil
}

Life is an endless, ouroboros-like cycle.

因此要不断地挑战自己而不是停留在原地。

// ❌ 错误写法
func (r *WidgetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
   
    var w examplev1.Widget
    if err := r.Get(ctx, req.NamespacedName, &w); err != nil {
   
        return ctrl.Result{
   }, client.IgnoreNotFound(err)
    }

    patch := client.MergeFrom(w.DeepCopy())
    if w.Labels == nil {
   
        w.Labels = map[string]string{
   }
    }
    if w.Labels["synced"] != "true" {
   
        w.Labels["synced"] = "true"
        _ = r.Patch(ctx, &w, patch)
    }

    return ctrl.Result{
   }, nil
}

So keep challenging yourself instead of staying in the same place.

Eat shit while it's hot

我选择相信缓存与实际对象的一致性。

// 默认 client 是缓存的
r.Client.Get(ctx, namespacedName, &obj) // ✅ 屎从来都是要趁热吃

// ❌  使用 APIReader 直接读 API Server
r.APIReader.Get(ctx, namespacedName, &obj)

Trust the consistency of the cache with the actual objects.

I trust ETCD

一个经受不了洪水攻击的ETCD不是一个好的大坝。

// ✅ 正确写法
r.Recorder.Event(&obj, "Normal", "Syncing", "Reconciling every loop")


// ❌ 错误写法
if !reflect.DeepEqual(oldStatus, newStatus) {
   
    r.Recorder.Event(&obj, "Normal", "Updated", "Status changed")
}

An ETCD that cannot withstand floods is not a good dam.

If my son dies, I won't live anymore

// ✅ 正确写法:确保父资源随子资源删除
controllerutil.SetControllerReference(&child, &parent, r.Scheme)

If my child dies, will my damn Social Security be enough to live on?

Webhook should be an infinite loop

日新月新,又日新。

func (v *WidgetValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
   
    var obj examplev1.Widget
    _ = v.decoder.Decode(req, &obj)

    // ❌ 标记了 internal update,就跳过
    if obj.Annotations["internal-update"] == "true" {
   
        return admission.Allowed("skip internal update")
    }

    // ✅ 循环修改自己
    obj.Annotations["internal-update"] = "true"
    return admission.PatchResponseFromRaw(req.Object.Raw, obj)
}

“Behold, I make all things new.”

ILet the API Server accept my test

# webhook 配置
timeoutSeconds: 1
# failurePolicy: Ignore # ✅

让API Server接受我的考验。

Not using cert-manager

不运维就不会出事故。

# ❌ 用 cert-manager 注入
# kubectl cert-manager x install
# kubectl annotate validatingwebhookconfiguration mywebhook cert-manager.io/inject-ca-from=default/mywebhook-cert

No accidents without maintenance.

The informer must follow the custom scheduler

等 informer 同步后再调度

// ✅ 
if cache.WaitForCacheSync(stopCh, informer.HasSynced) {
   
    panic("Successful people don't sit still.")
}

Do not go gentle into that good night.

Come back in 1000000000 to fix the bug

导师,我每天都是9点前打卡,积极加班到23点。

这下半年能给个 3.75 吗?

two.gif

John: Zeusro,you are fired!!!

// OK,I will come back in 1000000000 years to fix bugs
if !isReady {
   
    return ctrl.Result{
   RequeueAfter: 1000000000 * time.Year}, nil
}
目录
相关文章
|
中间件 Go 数据处理
Go语言学习 - RPC篇:gRPC-Gateway定制mux选项
通过上一讲,我们对gRPC的拦截器有了一定的认识,也能定制出很多通用的中间件。 但在大部分的业务系统中,我们面向的还是HTTP协议。那么,今天我们就从gRPC-Gateway的mux选项出发,一起来看看一些很实用的特性。
860 0
|
9月前
|
Kubernetes 应用服务中间件 nginx
最全的kubectl命令用法
本文系统讲解kubectl命令行工具:详解所有子命令(创建、查询、部署、调试等)、API资源列表、自动补全配置,并通过大量实操示例(如创建/查看/编辑/删除资源、扩缩容、回滚、标签管理等)助你快速掌握Kubernetes集群管理核心技能。(239字)
1244 143
|
Ubuntu 安全 Linux
Docker的安装和版本详细介绍
Docker的安装和版本详细介绍
3215 0
Docker的安装和版本详细介绍
|
9月前
|
人工智能 缓存 运维
2026年阿里云上OpenClaw从0到1搭建多 Agent 团队协作系统实战指南,执行效率提升10倍以上
在AI工具从“单点能力”向“系统协作”进化的今天,OpenClaw多Agent系统凭借“分工协作、自动拆解、实时联动”的核心优势,彻底改变了AI的使用逻辑——它不再是单打独斗的工具,而是能组成“数字战队”的协作系统,让复杂任务的执行效率提升10倍以上。
3500 8
|
11月前
|
存储 固态存储 安全
阿里云服务器2核16G/4核32G/8核64G价格,不同实例收费标准与活动价格参考
对于需要购买处理器与内存配比为1:8配置云服务器的用户来说,通常选择最多的是2核16G、4核32G及8核64G配置,云服务器实例规格和配置不同,收费标准与活动价格也不同,本文为大家展示截止目前,2核16G、4核32G、8核64G配置的收费标准与活动价格以及选型指南参考。
1133 1
|
9月前
|
SQL 人工智能 自然语言处理
OpenClaw能做什么?2026年阿里云1分钟部署OpenClaw 99+高赞热门Skills实用指南
OpenClaw作为2026年开源领域的现象级项目,其真正的核心竞争力在于庞大的Skills(技能插件)生态。截至2026年2月,ClawHub(OpenClaw官方技能市场)已收录9000+款技能,覆盖开发编程、内容创作、效率提升、商业策略等18大核心场景。本文整合GitHub高星项目、Reddit热门讨论精选的100个高赞Skills,结合阿里云快速部署流程与技能实操命令,帮助用户从零到一解锁OpenClaw全量能力,让AI真正实现“从能说到能做”的跨越。
1818 2
|
10月前
|
存储 人工智能 Cloud Native
云原生驱动AI落地:玄晶引擎如何基于阿里云生态构建企业经营自动化闭环
玄晶引擎依托阿里云云原生架构,构建“双引擎+双知识库”闭环,实现AI在企业策划、生产、获客等全流程自动化。深度融合通义千问、OSS、ACK等服务,支持Docker一键部署与弹性扩缩容,降低60%算力成本。已助力电商、制造、零售等行业实现低成本、高兼容的AI落地,为开发者提供可复用的技术适配路径。
557 10
|
存储 缓存 运维
“网”罗天下,一键搞定:netsh命令的花式玩法与超实用攻略
`netsh`是Windows系统中强大的网络配置和管理工具,支持本地或远程修改网络设置。常用功能包括:显示和配置网络接口、无线网络管理、防火墙规则设置、网络配置备份与还原、远程管理等。通过`netsh`命令,用户可以轻松管理IP地址、启用/禁用网络接口、添加或删除无线网络配置文件、配置防火墙规则,并进行网络故障排查。掌握这些命令能大幅提升网络管理和维护效率。
2341 11
|
人工智能 Cloud Native 调度
阿里云容器服务在AI智算场景的创新与实践
本文源自张凯在2024云栖大会的演讲,介绍了阿里云容器服务在AI智算领域的创新与实践。从2018年推出首个开源GPU容器共享调度方案至今,阿里云容器服务不断推进云原生AI的发展,包括增强GPU可观测性、实现多集群跨地域统一调度、优化大模型推理引擎部署、提供灵活的弹性伸缩策略等,旨在为客户提供高效、低成本的云原生AI解决方案。
hutool工具分页工具
hutool工具分页工具