// Generate a pod name that is unique among nodes by appending the nodeName. funcgeneratePodName(name string, nodeName types.NodeName)string { return fmt.Sprintf("%s-%s", name, strings.ToLower(string(nodeName))) }
// getPodName gets the name of set's child Pod with an ordinal index of ordinal funcgetPodName(set *apps.StatefulSet, ordinal int)string { return fmt.Sprintf("%s-%d", set.Name, ordinal) }
// BeforeCreate ensures that common operations for all resources are performed on creation. It only returns // errors that can be converted to api.Status. It invokes PrepareForCreate, then GenerateName, then Validate. // It returns nil if the object should be created. funcBeforeCreate(strategy RESTCreateStrategy, ctx context.Context, obj runtime.Object)error { objectMeta, kind, kerr := objectMetaAndKind(strategy, obj) if kerr != nil { return kerr }
// ensure that system-critical metadata has been populated if !metav1.HasObjectMetaSystemFieldValues(objectMeta) { return errors.NewInternalError(fmt.Errorf("system metadata was not initialized")) }
// ensure namespace on the object is correct, or error if a conflicting namespace was set in the object requestNamespace, ok := genericapirequest.NamespaceFrom(ctx) if !ok { return errors.NewInternalError(fmt.Errorf("no namespace information found in request context")) } if err := EnsureObjectNamespaceMatchesRequestNamespace(ExpectedNamespaceForScope(requestNamespace, strategy.NamespaceScoped()), objectMeta); err != nil { return err }
// PrepareForCreate clears fields that are not allowed to be set by end users on creation. func(deploymentStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { deployment := obj.(*apps.Deployment) deployment.Status = apps.DeploymentStatus{} deployment.Generation = 1
// Ref:https://github.com/kubernetes/kubernetes/blob/88e994f6bf8fc88114c5b733e09afea339bea66d/pkg/registry/apps/deployment/strategy.go#L118 // PrepareForUpdate clears fields that are not allowed to be set by end users on update. func(deploymentStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { newDeployment := obj.(*apps.Deployment) oldDeployment := old.(*apps.Deployment) newDeployment.Status = oldDeployment.Status
// Spec updates bump the generation so that we can distinguish between // scaling events and template changes, annotation updates bump the generation // because annotations are copied from deployments to their replica sets. if !apiequality.Semantic.DeepEqual(newDeployment.Spec, oldDeployment.Spec) || !apiequality.Semantic.DeepEqual(newDeployment.Annotations, oldDeployment.Annotations) { newDeployment.Generation = oldDeployment.Generation + 1 } }
// OwnerReference contains enough information to let you identify an owning // object. An owning object must be in the same namespace as the dependent, or // be cluster-scoped, so there is no namespace field. // +structType=atomic type OwnerReference struct { // API version of the referent. // 属主对象的APIVersion APIVersion string`json:"apiVersion" protobuf:"bytes,5,opt,name=apiVersion"` // Kind of the referent. // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds // 属主对象的Kind Kind string`json:"kind" protobuf:"bytes,1,opt,name=kind"` // Name of the referent. // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names // 属主对象的Name Name string`json:"name" protobuf:"bytes,3,opt,name=name"` // UID of the referent. // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#uids // 属主对象的UID UID types.UID `json:"uid" protobuf:"bytes,4,opt,name=uid,casttype=k8s.io/apimachinery/pkg/types.UID"` // If true, this reference points to the managing controller. // +optional // 属主对象的关系的控制器,没有被控制器自动控制,默认为false Controller *bool`json:"controller,omitempty" protobuf:"varint,6,opt,name=controller"` // If true, AND if the owner has the "foregroundDeletion" finalizer, then // the owner cannot be deleted from the key-value store until this // reference is removed. // See https://kubernetes.io/docs/concepts/architecture/garbage-collection/#foreground-deletion // for how the garbage collector interacts with this field and enforces the foreground deletion. // Defaults to false. // To set this field, a user needs "delete" permission of the owner, // otherwise 422 (Unprocessable Entity) will be returned. // +optional // 设置是否自动被GC,默认为true BlockOwnerDeletion *bool`json:"blockOwnerDeletion,omitempty" protobuf:"varint,7,opt,name=blockOwnerDeletion"` }
// ManagedFieldsEntry is a workflow-id, a FieldSet and the group version of the resource // that the fieldset applies to. type ManagedFieldsEntry struct { // Manager is an identifier of the workflow managing these fields. // 执行操作的 Manager string`json:"manager,omitempty" protobuf:"bytes,1,opt,name=manager"` // Operation is the type of operation which lead to this ManagedFieldsEntry being created. // The only valid values for this field are 'Apply' and 'Update'. // 操作的类型,包括Apply和Update Operation ManagedFieldsOperationType `json:"operation,omitempty" protobuf:"bytes,2,opt,name=operation,casttype=ManagedFieldsOperationType"` // APIVersion defines the version of this resource that this field set // applies to. The format is "group/version" just like the top-level // APIVersion field. It is necessary to track the version of a field // set because it cannot be automatically converted. // 定义这个资源的字段集使用的版本 APIVersion string`json:"apiVersion,omitempty" protobuf:"bytes,3,opt,name=apiVersion"` // Time is the timestamp of when the ManagedFields entry was added. The // timestamp will also be updated if a field is added, the manager // changes any of the owned fields value or removes a field. The // timestamp does not update when a field is removed from the entry // because another manager took it over. // +optional // 操作时间 Time *Time `json:"time,omitempty" protobuf:"bytes,4,opt,name=time"`
// Fields is tombstoned to show why 5 is a reserved protobuf tag. //Fields *Fields `json:"fields,omitempty" protobuf:"bytes,5,opt,name=fields,casttype=Fields"`
// FieldsType is the discriminator for the different fields format and version. // There is currently only one possible value: "FieldsV1" // 不同字段格式和版本的鉴别器,目前都是Fields V1 FieldsType string`json:"fieldsType,omitempty" protobuf:"bytes,6,opt,name=fieldsType"` // FieldsV1 holds the first JSON version format as described in the "FieldsV1" type. // +optional // 以JSON展示类似Trie的数据结构中,要么以'.'要么是子字段 // f:<name> <name>是结构中的字段的名称或者映射中的键 // v:<value> <value>列表项的确切的JSON格式的值 // i:<index> <index>是列表项的位置 // k:<key> <key>是列表项关键字段与唯一值的映射,如果映射的为空字段值,则该键代表的字段是集合的一部分 FieldsV1 *FieldsV1 `json:"fieldsV1,omitempty" protobuf:"bytes,7,opt,name=fieldsV1"`
// Subresource is the name of the subresource used to update that object, or // empty string if the object was updated through the main resource. The // value of this field is used to distinguish between managers, even if they // share the same name. For example, a status update will be distinct from a // regular update using the same manager name. // Note that the APIVersion field is not related to the Subresource field and // it always corresponds to the version of the main resource. // 更新字段的子资源的名称,如果更新资源通过主资源更新,则为空 Subresource string`json:"subresource,omitempty" protobuf:"bytes,8,opt,name=subresource"` }
// Manager updates the managed fields and merges applied configurations. type Manager interface { // Update is used when the object has already been merged (non-apply // use-case), and simply updates the managed fields in the output // object. // * `liveObj` is not mutated by this function // * `newObj` may be mutated by this function // Returns the new object with managedFields removed, and the object's new // proposed managedFields separately. // Update在对象已经被合并的时候使用,并且在输出的对象中更新MangedFields,返回删除了ManagedFields的新对象,以及新对象单独提出的ManagedFields Update(liveObj, newObj runtime.Object, managed Managed, manager string) (runtime.Object, Managed, error)
// Apply is used when server-side apply is called, as it merges the // object and updates the managed fields. // * `liveObj` is not mutated by this function // * `newObj` may be mutated by this function // Returns the new object with managedFields removed, and the object's new // proposed managedFields separately. // 在调用服务端应用的时候使用,因为他合并了对象并更新了ManagedFields Apply(liveObj, appliedObj runtime.Object, managed Managed, fieldManager string, force bool) (runtime.Object, Managed, error) }