Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.99.x-prod][OSL-1.32.2] Propagate workflow labels to the its K8s resources (#413) #28

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions controllers/builder/openshiftbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func (o *openshiftBuilderManager) Schedule(build *operatorapi.SonataFlowBuild) e
if err = o.addExternalResources(bc, workflow); err != nil {
return err
}
workflowproj.SetDefaultLabels(workflow, is)
workflowproj.SetDefaultLabels(workflow, bc)
workflowproj.SetMergedLabels(workflow, is)
workflowproj.SetMergedLabels(workflow, bc)
if err = controllerutil.SetControllerReference(build, bc, o.buildManagerContext.client.Scheme()); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/profiles/common/object_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var (
// DeploymentCreator is an objectCreator for a base Kubernetes Deployments for profiles that need to deploy the workflow on a vanilla deployment.
// It serves as a basis for a basic Quarkus Java application, expected to listen on http 8080.
func DeploymentCreator(workflow *operatorapi.SonataFlow) (client.Object, error) {
lbl := workflowproj.GetDefaultLabels(workflow)
lbl := workflowproj.GetMergedLabels(workflow)

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -184,7 +184,7 @@ func defaultContainer(workflow *operatorapi.SonataFlow) (*corev1.Container, erro
// ServiceCreator is an objectCreator for a basic Service aiming a vanilla Kubernetes Deployment.
// It maps the default HTTP port (80) to the target Java application webserver on port 8080.
func ServiceCreator(workflow *operatorapi.SonataFlow) (client.Object, error) {
lbl := workflowproj.GetDefaultLabels(workflow)
lbl := workflowproj.GetMergedLabels(workflow)

service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down
1 change: 0 additions & 1 deletion controllers/profiles/dev/object_creators_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func workflowDefConfigMapCreator(workflow *operatorapi.SonataFlow) (client.Objec
if err != nil {
return nil, err
}
workflowproj.SetDefaultLabels(workflow, configMap)
return configMap, nil
}

Expand Down
3 changes: 3 additions & 0 deletions controllers/profiles/dev/object_creators_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ func Test_ensureWorkflowDevServiceIsExposed(t *testing.T) {
assert.NotNil(t, reflectService.Spec.Type)
assert.NotEmpty(t, reflectService.Spec.Type)
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"})
}
6 changes: 6 additions & 0 deletions controllers/profiles/prod/profile_prod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func Test_Reconciler_ProdOps(t *testing.T) {
assert.Len(t, deployment.Spec.Template.Spec.Containers, 1)
assert.Len(t, deployment.Spec.Template.Spec.InitContainers, 1)
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": "simple", "sonataflow.org/workflow-app": "simple"})
}

func Test_Reconciler_ProdCustomPod(t *testing.T) {
Expand Down Expand Up @@ -96,6 +99,9 @@ func Test_Reconciler_ProdCustomPod(t *testing.T) {
assert.Len(t, deployment.Spec.Template.Spec.Containers, 1)
assert.Len(t, deployment.Spec.Template.Spec.InitContainers, 1)
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"})
}

func Test_reconcilerProdBuildConditions(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions controllers/workflowdef/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"path"

"github.com/apache/incubator-kie-kogito-serverless-operator/workflowproj"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -47,6 +49,7 @@ func CreateNewConfigMap(workflow *operatorapi.SonataFlow) (*corev1.ConfigMap, er
ObjectMeta: metav1.ObjectMeta{
Name: workflow.Name,
Namespace: workflow.Namespace,
Labels: workflowproj.GetMergedLabels(workflow),
},
Data: map[string]string{GetWorkflowDefFileName(workflow): string(workflowDef)},
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ metadata:
annotations:
sonataflow.org/description: Simple example on k8s!
sonataflow.org/version: 0.0.1
labels:
test: test
app: not-simple
spec:
podTemplate:
container:
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/sonataflow.org_v1alpha08_sonataflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ metadata:
annotations:
sonataflow.org/description: Greeting example on k8s!
sonataflow.org/version: 0.0.1
labels:
test: test
spec:
flow:
start: ChooseOnLanguage
Expand Down
2 changes: 1 addition & 1 deletion utils/openshift/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func RouteForWorkflow(workflow *operatorapi.SonataFlow) (*v1.Route, error) {
ObjectMeta: metav1.ObjectMeta{
Name: workflow.Name,
Namespace: workflow.Namespace,
Labels: workflowproj.GetMergedLabels(workflow),
},
Spec: v1.RouteSpec{
To: v1.RouteTargetReference{
Expand All @@ -44,6 +45,5 @@ func RouteForWorkflow(workflow *operatorapi.SonataFlow) (*v1.Route, error) {
},
},
}
workflowproj.SetDefaultLabels(workflow, route)
return route, nil
}
30 changes: 21 additions & 9 deletions workflowproj/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,40 @@ func GetManagedPropertiesFileName(workflow *operatorapi.SonataFlow) string {
return fmt.Sprintf("application-%s.properties", profile)
}

// SetDefaultLabels adds the default workflow application labels to the given object.
// Overrides the defined labels.
func SetDefaultLabels(workflow *operatorapi.SonataFlow, object metav1.Object) {
object.SetLabels(GetDefaultLabels(workflow))
}

// GetDefaultLabels gets the default labels based on the given workflow.
// You can use SetDefaultLabels that essentially does the same thing, if you don't need the labels explicitly.
func GetDefaultLabels(workflow *operatorapi.SonataFlow) map[string]string {
return map[string]string{
LabelApp: workflow.Name,
LabelWorkflow: workflow.Name,
}
}

// SetMergedLabels adds the merged labels to the given object.
func SetMergedLabels(workflow *operatorapi.SonataFlow, object metav1.Object) {
object.SetLabels(GetMergedLabels(workflow))
}

// GetMergedLabels gets labels based on the given workflow, includes their own labels, merged with the default ones.
func GetMergedLabels(workflow *operatorapi.SonataFlow) map[string]string {
mergedLabels := make(map[string]string)
if labels := workflow.GetLabels(); labels != nil {
for k, v := range labels {
mergedLabels[k] = v
}
}
for k, v := range GetDefaultLabels(workflow) {
mergedLabels[k] = v
}
return mergedLabels
}

// CreateNewUserPropsConfigMap creates a new empty ConfigMap object to hold the user application properties of the workflow.
func CreateNewUserPropsConfigMap(workflow *operatorapi.SonataFlow) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: GetWorkflowUserPropertiesConfigMapName(workflow),
Namespace: workflow.Namespace,
Labels: GetDefaultLabels(workflow),
Labels: GetMergedLabels(workflow),
},
Data: map[string]string{ApplicationPropertiesFileName: ""},
}
Expand All @@ -115,7 +127,7 @@ func CreateNewManagedPropsConfigMap(workflow *operatorapi.SonataFlow, properties
ObjectMeta: metav1.ObjectMeta{
Name: GetWorkflowManagedPropertiesConfigMapName(workflow),
Namespace: workflow.Namespace,
Labels: GetDefaultLabels(workflow),
Labels: GetMergedLabels(workflow),
},
Data: map[string]string{GetManagedPropertiesFileName(workflow): properties},
}
Expand Down
2 changes: 1 addition & 1 deletion workflowproj/workflowproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (w *workflowProjectHandler) parseRawWorkflow() error {
profile = w.profile
}
SetWorkflowProfile(w.project.Workflow, profile)
SetDefaultLabels(w.project.Workflow, w.project.Workflow)
SetMergedLabels(w.project.Workflow, w.project.Workflow)
if err = SetTypeToObject(w.project.Workflow, w.scheme); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions workflowproj/workflowproj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func Test_Handler_WorkflowMinimalAndPropsAndSpec(t *testing.T) {
AsObjects()
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.NotNil(t, proj.Properties)
assert.NotEmpty(t, proj.Resources)
assert.Equal(t, "hello", proj.Workflow.Name)
Expand Down
Loading