Skip to content

Commit

Permalink
Set recommended kubernetes labels on services and workflows
Browse files Browse the repository at this point in the history
Motivation:
There's a need to select efficiently workflows and their respective
services. Having the common kubernetes labels allow a single selector:

   podSelector:
     matchExpressions:
       - { key: app.kubernetes.io/component, operator: In, values:
         ["data-index-service", "jobs-service", "serverless-workflow"] }

Modification:
Make the v1.Deployment for services and the deployment or knative
services to contain at common labels

Result:
A workflow deployment or knative serving labels:
    app.kubernetes.io/name: ${workflow name}
    app.kubernetes.io/component: serverless-workflow
    app.kubernetes.io/part-of: ${platform url set by status}
    app.kubernetes.io/managed-by: sonataflow-operator

Data index or Jobs services Deployment.v1 labels:

    app.kubernetes.io/name: ${service name}
    app.kubernetes.io/component: data-index-service|jobs-service
    app.kubernetes.io/part-of: ${platform name}
    app.kubernetes.io/managed-by: sonataflow-operator

Reference: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels

Signed-off-by: Roy Golan <[email protected]>
  • Loading branch information
rgolangh committed Jun 25, 2024
1 parent e9fce09 commit ea018f5
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 17 deletions.
7 changes: 5 additions & 2 deletions controllers/platform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,11 @@ func createOrUpdateService(ctx context.Context, client client.Client, platform *

func getLabels(platform *operatorapi.SonataFlowPlatform, psh services.PlatformServiceHandler) (map[string]string, map[string]string) {
lbl := map[string]string{
workflowproj.LabelApp: platform.Name,
workflowproj.LabelService: psh.GetServiceName(),
workflowproj.LabelService: psh.GetServiceName(),
workflowproj.LabelK8SName: psh.GetContainerName(),
workflowproj.LabelK8SComponent: psh.GetServiceName(),
workflowproj.LabelK8SPartOF: platform.Name,
workflowproj.LabelK8SManagedBy: "sonataflow-operator",
}
selectorLbl := map[string]string{
workflowproj.LabelService: psh.GetServiceName(),
Expand Down
2 changes: 1 addition & 1 deletion controllers/profiles/common/object_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func DeploymentCreator(workflow *operatorapi.SonataFlow, plf *operatorapi.Sonata
Spec: appsv1.DeploymentSpec{
Replicas: getReplicasOrDefault(workflow),
Selector: &metav1.LabelSelector{
MatchLabels: lbl,
MatchLabels: workflowproj.GetSelectorLabels(workflow),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
16 changes: 14 additions & 2 deletions controllers/profiles/common/object_creators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"github.com/apache/incubator-kie-kogito-serverless-operator/workflowproj"
)

const platformName = "test-platform"

func Test_ensureWorkflowPropertiesConfigMapMutator(t *testing.T) {
workflow := test.GetBaseSonataFlowWithDevProfile(t.Name())
platform := test.GetBasePlatform()
Expand Down Expand Up @@ -192,7 +194,12 @@ func Test_ensureWorkflowSinkBindingIsCreated(t *testing.T) {
assert.NotEmpty(t, reflectSinkBinding.Spec.Sink)
assert.Equal(t, reflectSinkBinding.Spec.Sink.Ref.Kind, "Broker")
assert.NotNil(t, reflectSinkBinding.GetLabels())
assert.Equal(t, reflectSinkBinding.ObjectMeta.Labels, map[string]string{"app": "vet", "sonataflow.org/workflow-app": "vet"})
assert.Equal(t, reflectSinkBinding.ObjectMeta.Labels, map[string]string{
"sonataflow.org/workflow-app": "vet",
"app.kubernetes.io/name": "vet",
"app.kubernetes.io/component": "serverless-workflow",
"app.kubernetes.io/managed-by": "sonataflow-operator",
})
}

func Test_ensureWorkflowTriggersAreCreated(t *testing.T) {
Expand All @@ -206,7 +213,12 @@ func Test_ensureWorkflowTriggersAreCreated(t *testing.T) {
for _, trigger := range triggers {
assert.Contains(t, []string{"vet-vetappointmentrequestreceived-trigger", "vet-vetappointmentinfo-trigger"}, trigger.GetName())
assert.NotNil(t, trigger.GetLabels())
assert.Equal(t, trigger.GetLabels(), map[string]string{"app": "vet", "sonataflow.org/workflow-app": "vet"})
assert.Equal(t, trigger.GetLabels(), map[string]string{
"sonataflow.org/workflow-app": "vet",
"app.kubernetes.io/name": "vet",
"app.kubernetes.io/component": "serverless-workflow",
"app.kubernetes.io/managed-by": "sonataflow-operator",
})
}
}

Expand Down
8 changes: 7 additions & 1 deletion controllers/profiles/dev/object_creators_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ func Test_ensureWorkflowDevServiceIsExposed(t *testing.T) {
assert.Equal(t, reflectService.Spec.Type, v1.ServiceTypeNodePort)
assert.NotNil(t, reflectService.ObjectMeta)
assert.NotNil(t, reflectService.ObjectMeta.Labels)
assert.Equal(t, reflectService.ObjectMeta.Labels, map[string]string{"test": "test", "app": "greeting", "sonataflow.org/workflow-app": "greeting"})
assert.Equal(t, reflectService.ObjectMeta.Labels, map[string]string{
"test": "test",
"sonataflow.org/workflow-app": "greeting",
"app.kubernetes.io/name": "greeting",
"app.kubernetes.io/component": "serverless-workflow",
"app.kubernetes.io/managed-by": "sonataflow-operator",
})
}
2 changes: 1 addition & 1 deletion controllers/profiles/dev/status_enricher_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func statusEnricher(ctx context.Context, c client.Client, workflow *operatorapi.
podList := &v1.PodList{}
opts := []client.ListOption{
client.InNamespace(workflow.Namespace),
client.MatchingLabels{workflowproj.LabelApp: labels[workflowproj.LabelApp]},
client.MatchingLabels{workflowproj.LabelK8SName: labels[workflowproj.LabelK8SName]},
}
err := c.List(ctx, podList, opts...)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion controllers/profiles/gitops/profile_gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@ func Test_Reconciler_ProdOps(t *testing.T) {

assert.NotNil(t, deployment.ObjectMeta)
assert.NotNil(t, deployment.ObjectMeta.Labels)
assert.Equal(t, deployment.ObjectMeta.Labels, map[string]string{"test": "test", "app": "simple", "sonataflow.org/workflow-app": "simple"})
assert.Equal(t, deployment.ObjectMeta.Labels, map[string]string{
"test": "test",
"sonataflow.org/workflow-app": "simple",
"app.kubernetes.io/name": "simple",
"app.kubernetes.io/component": "serverless-workflow",
"app.kubernetes.io/managed-by": "sonataflow-operator",
"app.kubernetes.io/part-of": "sonataflow-platform",
})
}
8 changes: 7 additions & 1 deletion controllers/profiles/preview/profile_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ func Test_Reconciler_ProdCustomPod(t *testing.T) {
assert.Len(t, deployment.Spec.Template.Spec.Containers[0].VolumeMounts, 1)
assert.NotNil(t, deployment.ObjectMeta)
assert.NotNil(t, deployment.ObjectMeta.Labels)
assert.Equal(t, deployment.ObjectMeta.Labels, map[string]string{"test": "test", "app": "greeting", "sonataflow.org/workflow-app": "greeting"})
assert.Equal(t, deployment.ObjectMeta.Labels, map[string]string{
"test": "test",
"sonataflow.org/workflow-app": "greeting",
"app.kubernetes.io/name": "greeting",
"app.kubernetes.io/component": "serverless-workflow",
"app.kubernetes.io/managed-by": "sonataflow-operator",
})
}

func Test_reconcilerProdBuildConditions(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ metadata:
sonataflow.org/version: 0.0.1
labels:
test: test
app: not-simple
spec:
podTemplate:
container:
Expand Down
26 changes: 20 additions & 6 deletions workflowproj/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const (
// ApplicationPropertiesFileName is the default application properties file name holding user properties
ApplicationPropertiesFileName = "application.properties"
workflowManagedConfigMapNameSuffix = "-managed-props"
// LabelApp key to use among object selectors, "app" is used among k8s applications to group objects in some UI consoles
LabelApp = "app"
// LabelService key to use among object selectors
LabelService = metadata.Domain + "/service"
// LabelWorkflow specialized label managed by the controller
LabelWorkflow = metadata.Domain + "/workflow-app"
LabelWorkflow = metadata.Domain + "/workflow-app"
LabelK8SName = "app.kubernetes.io/name"
LabelK8SComponent = "app.kubernetes.io/component"
LabelK8SPartOF = "app.kubernetes.io/part-of"
LabelK8SManagedBy = "app.kubernetes.io/managed-by"
)

// SetTypeToObject sets the Kind and ApiVersion to a given object since the default constructor won't do it.
Expand Down Expand Up @@ -84,10 +86,22 @@ func GetManagedPropertiesFileName(workflow *operatorapi.SonataFlow) string {

// GetDefaultLabels gets the default labels based on the given workflow.
func GetDefaultLabels(workflow *operatorapi.SonataFlow) map[string]string {
return map[string]string{
LabelApp: workflow.Name,
LabelWorkflow: workflow.Name,
labels := map[string]string{
LabelWorkflow: workflow.Name,
LabelK8SName: workflow.Name,
LabelK8SComponent: "serverless-workflow",
LabelK8SManagedBy: "sonataflow-operator",
}
if workflow.Status.Platform != nil {
labels[LabelK8SPartOF] = workflow.Status.Platform.Name
}
return labels

}
func GetSelectorLabels(workflow *operatorapi.SonataFlow) map[string]string {
labels := GetDefaultLabels(workflow)
delete(labels, LabelK8SPartOF)
return labels
}

// SetMergedLabels adds the merged labels to the given object.
Expand Down
8 changes: 7 additions & 1 deletion workflowproj/workflowproj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ func Test_Handler_WorkflowMinimalAndPropsAndSpec(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, proj.Workflow)
assert.NotNil(t, proj.Workflow.ObjectMeta)
assert.Equal(t, proj.Workflow.ObjectMeta.Labels, map[string]string{"app": "hello", "sonataflow.org/workflow-app": "hello"})
assert.Equal(t, proj.Workflow.ObjectMeta.Labels, map[string]string{
"app": "hello",
"sonataflow.org/workflow-app": "hello",
"app.kubernetes.io/name": "hello",
"app.kubernetes.io/component": "serverless-workflow",
"app.kubernetes.io/managed-by": "sonataflow-operator",
})
assert.NotNil(t, proj.Properties)
assert.NotEmpty(t, proj.Resources)
assert.Equal(t, "hello", proj.Workflow.Name)
Expand Down

0 comments on commit ea018f5

Please sign in to comment.