Skip to content

Commit

Permalink
feat: support user-defined labels
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Jun 21, 2024
1 parent 1e0521c commit 331c2e8
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 54 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 @@ -668,6 +668,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 @@ -75,6 +75,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 @@ -2850,6 +2850,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 Determines whether metrics
exporter information is annotated on the Component's headless
Expand Down Expand Up @@ -8598,6 +8604,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 Determines whether metrics
exporter information is annotated on the Component's headless
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 @@ -2591,6 +2591,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. \n Marking an instance as offline results in
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 @@ -207,6 +207,7 @@ func copyAndMergeComponent(oldCompObj, newCompObj *appsv1alpha1.Component) *apps
compObjCopy.Spec.ServiceVersion = compProto.Spec.ServiceVersion
compObjCopy.Spec.ClassDefRef = compProto.Spec.ClassDefRef
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 @@ -2850,6 +2850,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 Determines whether metrics
exporter information is annotated on the Component's headless
Expand Down Expand Up @@ -8598,6 +8604,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 Determines whether metrics
exporter information is annotated on the Component's headless
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 @@ -2591,6 +2591,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. \n Marking an instance as offline results in
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 @@ -836,6 +836,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 @@ -5059,6 +5071,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 @@ -9327,6 +9351,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 @@ -85,6 +85,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).
SetAffinity(affinities).
Expand Down
63 changes: 32 additions & 31 deletions pkg/controller/component/synthesize_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,37 +149,38 @@ 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),
RoleArbitrator: compDefObj.Spec.RoleArbitrator,
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),
RoleArbitrator: compDefObj.Spec.RoleArbitrator,
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"`
RoleArbitrator *v1alpha1.RoleArbitrator `json:"roleArbitrator,omitempty"`
Volumes []v1alpha1.ComponentVolume `json:"volumes,omitempty"`
HostNetwork *v1alpha1.HostNetwork `json:"hostNetwork,omitempty"`
ComponentServices []v1alpha1.ComponentService `json:"componentServices,omitempty"`
MinReadySeconds int32 `json:"minReadySeconds,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"`
RoleArbitrator *v1alpha1.RoleArbitrator `json:"roleArbitrator,omitempty"`
Volumes []v1alpha1.ComponentVolume `json:"volumes,omitempty"`
HostNetwork *v1alpha1.HostNetwork `json:"hostNetwork,omitempty"`
ComponentServices []v1alpha1.ComponentService `json:"componentServices,omitempty"`
MinReadySeconds int32 `json:"minReadySeconds,omitempty"`
DisableExporter *bool `json:"disableExporter,omitempty"`

// TODO(xingran): The following fields will be deprecated after version 0.8.0 and will be replaced with a new data structure.
Probes *v1alpha1.ClusterDefinitionProbes `json:"probes,omitempty"` // The Probes will be replaced with LifecycleActions.RoleProbe in the future.
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 331c2e8

Please sign in to comment.