跳到主要内容

36.serverVersion

PR #627

Add /version to server and check it in client

// Info contains versioning information.
// TODO: Add []string of api versions supported? It's still unclear
// how we'll want to distribute that information.
type Info struct {
Major string `json:"major" yaml:"major"`
Minor string `json:"minor" yaml:"minor"`
GitCommit string `json:"gitCommit" yaml:"gitCommit"`
}

// Get returns the overall codebase version. It's for detecting
// what code a binary was built from.
func Get() Info {
return Info{
Major: "0",
Minor: "1",
GitCommit: commitFromGit,
}
}

PR #690

Clean up pkg/apiserver

PR #672

Custom conversion funcs [添加自定义转换功能系统] The nice thing about this system is that you can write a conversion function that takes arbitrary types; you can make it as broad or narrow as you want. Finding the right function is a couple reflection operations and a map lookup. At scale it may be worth it to write custom broad conversion functions that short-circuit the recursive part of reflections, but IMO we can worry about that when it actually happens. If 2x reflect.TypeOf() and 1x reflect.Value.Call() is slower than the actual translation/copy code, I'd be very surprised. I pretty strongly believe that we shouldn't preclude optimizations, but we also shouldn't write them until they're needed.

这个系统的优点在于,您可以编写一个转换函数,该函数采用任意类型; 您可以根据自己的需要将其设置为宽或窄。找到正确的函数需要两个反射操作和一个映射查找。在规模上,编写定制的广义转换函数来短路反射的递归部分可能是值得的,但是我们可以在实际发生时担心这个问题。如果2倍反射。TypeOf ()和1倍反射。价值。Call ()比实际的翻译/复制代码慢,我会非常惊讶。我非常强烈地认为,我们不应该排除优化,但是我们也不应该在需要它们之前编写它们。

修改:

  1. 去除defaultCopy.go 添加了 converter.go 用来注册 source->dst 的转换函数
这个转换器用到了Go语言的反射(reflection)机制,它能在运行时检查类型和变量,例如它的类型是什么,它有哪些字段和方法等等。

以下是主要的一些部分:

typePair 结构体定义了一个从源类型到目标类型的对应关系。

Converter 结构体是转换器的核心,它保存了一个从typePair到reflect.Value的映射,reflect.Value代表一个转换函数。它还可以选择性地使用一个调试记录器来记录详细的转换过程。

NewConverter 函数创建一个新的转换器实例。

Register 方法注册一个新的转换函数到转换器。转换函数必须有两个指针参数,表示输入和输出,返回一个错误。

Convert 方法将源对象转换为目标对象。它首先检查源和目标是否都是指针类型,然后调用 convert 方法进行实际的转换。

convert 方法是转换的核心。它首先查看是否有注册的转换函数可以用,如果有,就调用它。如果没有,它会尝试复制源对象到目标对象。如果源和目标类型不同,它会报错。否则,它将尝试根据目标类型的种类(kind)来进行转换,包括处理结构体、切片、指针和映射等情况。如果遇到无法处理的情况,它将返回一个错误。

这段代码的设计充分利用了Go语言的特性,包括类型系统、反射机制和接口等,使得它能灵活而高效地进行类型转换。
  1. 去除 typeNamePath 结构体,转而使用 var typeToVersion = map[reflect.Type]string{}
// typeToVersion allows one to figure out the version for a given go object.
// The reflect.Type we index by should *not* be a pointer. If the same type
// is registered for multiple versions, the last one wins.

PR #691

Add RestartPolicy to Pod and PodTemplate [This is the first PR to add RestartPolicy to Pod. Next PRs will hook policy with kubelets.]

// RestartPolicyType represents a restart policy for a pod.
type RestartPolicyType string

// Valid restart policies defined for a PodState.RestartPolicy.
const (
RestartAlways RestartPolicyType = "RestartAlways"
RestartOnFailure RestartPolicyType = "RestartOnFailure"
RestartNever RestartPolicyType = "RestartNever"
)

type RestartPolicy struct {
// Optional: Defaults to "RestartAlways".
Type RestartPolicyType `yaml:"type,omitempty" json:"type,omitempty"`
}

PR #549

Make replication controllers use new watch interface

controller 使用新写的 watch 接口来监听变换,而不是直接监听 etcd

提示

homework: 实现一个反向代理,可以记录耗时操作,需要使用 NewSingleHostReverseProxy 并 重写 RoundTrip