Skip to content

Commit

Permalink
feat: support user-defined labels (#7593)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Jun 21, 2024
1 parent 8ed6732 commit 8a96fbd
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 53 deletions.
5 changes: 5 additions & 0 deletions apis/apps/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@ type ClusterComponentSpec struct {
// +optional
EnabledLogs []string `json:"enabledLogs,omitempty"`

// Specifies Labels to override or add for underlying Pods.
//
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Specifies Annotations to override or add for underlying Pods.
//
// +optional
Expand Down
5 changes: 5 additions & 0 deletions apis/apps/v1alpha1/component_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type ComponentSpec struct {
// +optional
ServiceRefs []ServiceRef `json:"serviceRefs,omitempty"`

// Specifies Labels to override or add for underlying Pods.
//
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Specifies Annotations to override or add for underlying Pods.
//
// +optional
Expand Down
14 changes: 14 additions & 0 deletions apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions config/crd/bases/apps.kubeblocks.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3814,6 +3814,12 @@ spec:
required:
- name
type: object
labels:
additionalProperties:
type: string
description: Specifies Labels to override or add for underlying
Pods.
type: object
monitor:
description: |-
Deprecated since v0.9
Expand Down Expand Up @@ -12471,6 +12477,12 @@ spec:
required:
- name
type: object
labels:
additionalProperties:
type: string
description: Specifies Labels to override or add for underlying
Pods.
type: object
monitor:
description: |-
Deprecated since v0.9
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/apps.kubeblocks.io_components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,11 @@ spec:
- name
type: object
type: array
labels:
additionalProperties:
type: string
description: Specifies Labels to override or add for underlying Pods.
type: object
offlineInstances:
description: |-
Specifies the names of instances to be transitioned to offline status.
Expand Down
1 change: 1 addition & 0 deletions controllers/apps/transformer_cluster_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func copyAndMergeComponent(oldCompObj, newCompObj *appsv1alpha1.Component) *apps
compObjCopy.Spec.CompDef = compProto.Spec.CompDef
compObjCopy.Spec.ServiceVersion = compProto.Spec.ServiceVersion
compObjCopy.Spec.ServiceRefs = compProto.Spec.ServiceRefs
compObjCopy.Spec.Labels = compProto.Spec.Labels
compObjCopy.Spec.Annotations = compProto.Spec.Annotations
compObjCopy.Spec.Env = compProto.Spec.Env
compObjCopy.Spec.Resources = compProto.Spec.Resources
Expand Down
12 changes: 12 additions & 0 deletions deploy/helm/crds/apps.kubeblocks.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3814,6 +3814,12 @@ spec:
required:
- name
type: object
labels:
additionalProperties:
type: string
description: Specifies Labels to override or add for underlying
Pods.
type: object
monitor:
description: |-
Deprecated since v0.9
Expand Down Expand Up @@ -12471,6 +12477,12 @@ spec:
required:
- name
type: object
labels:
additionalProperties:
type: string
description: Specifies Labels to override or add for underlying
Pods.
type: object
monitor:
description: |-
Deprecated since v0.9
Expand Down
5 changes: 5 additions & 0 deletions deploy/helm/crds/apps.kubeblocks.io_components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,11 @@ spec:
- name
type: object
type: array
labels:
additionalProperties:
type: string
description: Specifies Labels to override or add for underlying Pods.
type: object
offlineInstances:
description: |-
Specifies the names of instances to be transitioned to offline status.
Expand Down
36 changes: 36 additions & 0 deletions docs/developer_docs/api-reference/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@ identified using Cluster, Component and Service names.</li>
</tr>
<tr>
<td>
<code>labels</code><br/>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Specifies Labels to override or add for underlying Pods.</p>
</td>
</tr>
<tr>
<td>
<code>annotations</code><br/>
<em>
map[string]string
Expand Down Expand Up @@ -4802,6 +4814,18 @@ you can enable the collection of these logs by including their names in the <cod
</tr>
<tr>
<td>
<code>labels</code><br/>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Specifies Labels to override or add for underlying Pods.</p>
</td>
</tr>
<tr>
<td>
<code>annotations</code><br/>
<em>
map[string]string
Expand Down Expand Up @@ -8685,6 +8709,18 @@ identified using Cluster, Component and Service names.</li>
</tr>
<tr>
<td>
<code>labels</code><br/>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Specifies Labels to override or add for underlying Pods.</p>
</td>
</tr>
<tr>
<td>
<code>annotations</code><br/>
<em>
map[string]string
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/builder/builder_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (builder *ComponentBuilder) SetServiceVersion(serviceVersion string) *Compo
return builder
}

func (builder *ComponentBuilder) SetLabels(labels map[string]string) *ComponentBuilder {
builder.get().Spec.Labels = labels
return builder
}

func (builder *ComponentBuilder) SetAnnotations(annotations map[string]string) *ComponentBuilder {
builder.get().Spec.Annotations = annotations
return builder
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func BuildComponent(cluster *appsv1alpha1.Cluster, compSpec *appsv1alpha1.Cluste
AddLabelsInMap(constant.GetComponentWellKnownLabels(cluster.Name, compSpec.Name)).
AddLabels(constant.KBAppClusterUIDLabelKey, string(cluster.UID)).
SetServiceVersion(compSpec.ServiceVersion).
SetLabels(compSpec.Labels).
SetAnnotations(compSpec.Annotations).
SetEnv(compSpec.Env).
SetSchedulingPolicy(schedulingPolicy).
Expand Down
61 changes: 31 additions & 30 deletions pkg/controller/component/synthesize_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,37 @@ func buildSynthesizedComponent(reqCtx intctrlutil.RequestCtx,
}
compDefObj := compDef.DeepCopy()
synthesizeComp := &SynthesizedComponent{
Namespace: comp.Namespace,
ClusterName: clusterName,
ClusterUID: clusterUID,
Comp2CompDefs: comp2CompDef,
Name: compName,
FullCompName: comp.Name,
CompDefName: compDef.Name,
ServiceVersion: comp.Spec.ServiceVersion,
ClusterGeneration: clusterGeneration(cluster, comp),
TemplateAnnotations: comp.Spec.Annotations,
PodSpec: &compDef.Spec.Runtime,
HostNetwork: compDefObj.Spec.HostNetwork,
ComponentServices: compDefObj.Spec.Services,
LogConfigs: compDefObj.Spec.LogConfigs,
ConfigTemplates: compDefObj.Spec.Configs,
ScriptTemplates: compDefObj.Spec.Scripts,
Roles: compDefObj.Spec.Roles,
UpdateStrategy: compDefObj.Spec.UpdateStrategy,
MinReadySeconds: compDefObj.Spec.MinReadySeconds,
PolicyRules: compDefObj.Spec.PolicyRules,
LifecycleActions: compDefObj.Spec.LifecycleActions,
SystemAccounts: mergeSystemAccounts(compDefObj.Spec.SystemAccounts, comp.Spec.SystemAccounts),
Replicas: comp.Spec.Replicas,
Resources: comp.Spec.Resources,
TLSConfig: comp.Spec.TLSConfig,
ServiceAccountName: comp.Spec.ServiceAccountName,
Instances: comp.Spec.Instances,
OfflineInstances: comp.Spec.OfflineInstances,
DisableExporter: comp.Spec.DisableExporter,
PodManagementPolicy: compDef.Spec.PodManagementPolicy,
Namespace: comp.Namespace,
ClusterName: clusterName,
ClusterUID: clusterUID,
Comp2CompDefs: comp2CompDef,
Name: compName,
FullCompName: comp.Name,
CompDefName: compDef.Name,
ServiceVersion: comp.Spec.ServiceVersion,
ClusterGeneration: clusterGeneration(cluster, comp),
UserDefinedLabels: comp.Spec.Labels,
UserDefinedAnnotations: comp.Spec.Annotations,
PodSpec: &compDef.Spec.Runtime,
HostNetwork: compDefObj.Spec.HostNetwork,
ComponentServices: compDefObj.Spec.Services,
LogConfigs: compDefObj.Spec.LogConfigs,
ConfigTemplates: compDefObj.Spec.Configs,
ScriptTemplates: compDefObj.Spec.Scripts,
Roles: compDefObj.Spec.Roles,
UpdateStrategy: compDefObj.Spec.UpdateStrategy,
MinReadySeconds: compDefObj.Spec.MinReadySeconds,
PolicyRules: compDefObj.Spec.PolicyRules,
LifecycleActions: compDefObj.Spec.LifecycleActions,
SystemAccounts: mergeSystemAccounts(compDefObj.Spec.SystemAccounts, comp.Spec.SystemAccounts),
Replicas: comp.Spec.Replicas,
Resources: comp.Spec.Resources,
TLSConfig: comp.Spec.TLSConfig,
ServiceAccountName: comp.Spec.ServiceAccountName,
Instances: comp.Spec.Instances,
OfflineInstances: comp.Spec.OfflineInstances,
DisableExporter: comp.Spec.DisableExporter,
PodManagementPolicy: compDef.Spec.PodManagementPolicy,
}

// build backward compatible fields, including workload, services, componentRefEnvs, clusterDefName, clusterCompDefName, and clusterCompVer, etc.
Expand Down
45 changes: 23 additions & 22 deletions pkg/controller/component/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,29 @@ type SynthesizedComponent struct {
TLSConfig *v1alpha1.TLSConfig `json:"tlsConfig"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
// TODO: remove this later
ComponentRefEnvs []corev1.EnvVar `json:"componentRefEnvs,omitempty"`
ServiceReferences map[string]*v1alpha1.ServiceDescriptor `json:"serviceReferences,omitempty"`
TemplateAnnotations map[string]string
TemplateVars map[string]any `json:"templateVars,omitempty"`
EnvVars []corev1.EnvVar `json:"envVars,omitempty"`
EnvFromSources []corev1.EnvFromSource `json:"envFromSources,omitempty"`
Instances []v1alpha1.InstanceTemplate `json:"instances,omitempty"`
OfflineInstances []string `json:"offlineInstances,omitempty"`
Roles []v1alpha1.ReplicaRole `json:"roles,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
UpdateStrategy *v1alpha1.UpdateStrategy `json:"updateStrategy,omitempty"`
PodManagementPolicy *appsv1.PodManagementPolicyType `json:"podManagementPolicy,omitempty"`
PolicyRules []rbacv1.PolicyRule `json:"policyRules,omitempty"`
LifecycleActions *v1alpha1.ComponentLifecycleActions `json:"lifecycleActions,omitempty"`
SystemAccounts []v1alpha1.SystemAccount `json:"systemAccounts,omitempty"`
Volumes []v1alpha1.ComponentVolume `json:"volumes,omitempty"`
HostNetwork *v1alpha1.HostNetwork `json:"hostNetwork,omitempty"`
ComponentServices []v1alpha1.ComponentService `json:"componentServices,omitempty"`
MinReadySeconds int32 `json:"minReadySeconds,omitempty"`
Sidecars []string `json:"sidecars,omitempty"`
DisableExporter *bool `json:"disableExporter,omitempty"`
ComponentRefEnvs []corev1.EnvVar `json:"componentRefEnvs,omitempty"`
ServiceReferences map[string]*v1alpha1.ServiceDescriptor `json:"serviceReferences,omitempty"`
UserDefinedLabels map[string]string
UserDefinedAnnotations map[string]string
TemplateVars map[string]any `json:"templateVars,omitempty"`
EnvVars []corev1.EnvVar `json:"envVars,omitempty"`
EnvFromSources []corev1.EnvFromSource `json:"envFromSources,omitempty"`
Instances []v1alpha1.InstanceTemplate `json:"instances,omitempty"`
OfflineInstances []string `json:"offlineInstances,omitempty"`
Roles []v1alpha1.ReplicaRole `json:"roles,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
UpdateStrategy *v1alpha1.UpdateStrategy `json:"updateStrategy,omitempty"`
PodManagementPolicy *appsv1.PodManagementPolicyType `json:"podManagementPolicy,omitempty"`
PolicyRules []rbacv1.PolicyRule `json:"policyRules,omitempty"`
LifecycleActions *v1alpha1.ComponentLifecycleActions `json:"lifecycleActions,omitempty"`
SystemAccounts []v1alpha1.SystemAccount `json:"systemAccounts,omitempty"`
Volumes []v1alpha1.ComponentVolume `json:"volumes,omitempty"`
HostNetwork *v1alpha1.HostNetwork `json:"hostNetwork,omitempty"`
ComponentServices []v1alpha1.ComponentService `json:"componentServices,omitempty"`
MinReadySeconds int32 `json:"minReadySeconds,omitempty"`
Sidecars []string `json:"sidecars,omitempty"`
DisableExporter *bool `json:"disableExporter,omitempty"`

// TODO(xingran): The following fields will be deprecated after KubeBlocks version 0.8.0
ClusterDefName string `json:"clusterDefName,omitempty"` // the name of the clusterDefinition
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/factory/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ func BuildInstanceSet(synthesizedComp *component.SynthesizedComponent, component
AddLabelsInMap(labels).
AddLabelsInMap(compDefLabel).
AddLabelsInMap(constant.GetAppVersionLabel(compDefName)).
AddLabelsInMap(synthesizedComp.UserDefinedLabels).
AddAnnotations(constant.ComponentReplicasAnnotationKey, replicasStr).
AddAnnotationsInMap(synthesizedComp.TemplateAnnotations)
AddAnnotationsInMap(synthesizedComp.UserDefinedAnnotations)
template := corev1.PodTemplateSpec{
ObjectMeta: podBuilder.GetObject().ObjectMeta,
Spec: *synthesizedComp.PodSpec.DeepCopy(),
Expand Down

0 comments on commit 8a96fbd

Please sign in to comment.