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

Set commonLabels managed-by flag #1425

Merged
merged 3 commits into from
Feb 3, 2025
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
36 changes: 35 additions & 1 deletion components/operator/internal/chart/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type FlagsBuilder interface {
WithRegistryCredentials(username string, password string) *flagsBuilder
WithRegistryEnableInternal(enableInternal bool) *flagsBuilder
WithRegistryHttpSecret(httpSecret string) *flagsBuilder
WithManagedByLabel(string) *flagsBuilder
WithNodePort(nodePort int64) *flagsBuilder
WithLogLevel(logLevel string) *flagsBuilder
WithLogFormat(logFormat string) *flagsBuilder
Expand All @@ -32,12 +33,40 @@ func NewFlagsBuilder() FlagsBuilder {
func (fb *flagsBuilder) Build() map[string]interface{} {
flags := map[string]interface{}{}
for key, value := range fb.flags {
flagPath := strings.Split(key, ".")
flagPath := strings.FieldsFunc(key, fieldsFuncWithExtrudes(key))
flagPath = removeEscapesFromFlagPath(flagPath)
appendFlag(flags, flagPath, value)
}
return flags
}

func fieldsFuncWithExtrudes(flag string) func(rune) bool {
index := 0
return func(r rune) bool {
split := shouldBeSplit(flag, index, r)

// increase index to know on which rune we are right now
index++

return split
}
}

func shouldBeSplit(flag string, index int, r rune) bool {
if r != '.' {
return false
}

return index > 0 && flag[index-1] != '\\'
}

func removeEscapesFromFlagPath(flagPath []string) []string {
for i := range flagPath {
flagPath[i] = strings.ReplaceAll(flagPath[i], "\\", "")
}
return flagPath
}

func appendFlag(flags map[string]interface{}, flagPath []string, value interface{}) {
currentFlag := flags
for i, pathPart := range flagPath {
Expand Down Expand Up @@ -129,6 +158,11 @@ func (fb *flagsBuilder) WithDefaultPresetFlags(defaultBuildJobPreset, defaultRun
return fb
}

func (fb *flagsBuilder) WithManagedByLabel(managedBy string) *flagsBuilder {
fb.flags["global.commonLabels.app\\.kubernetes\\.io/managed-by"] = managedBy
return fb
}

func (fb *flagsBuilder) WithNodePort(nodePort int64) *flagsBuilder {
fb.flags["global.registryNodePort"] = nodePort
return fb
Expand Down
6 changes: 5 additions & 1 deletion components/operator/internal/chart/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func Test_flagsBuilder_Build(t *testing.T) {
"username": "testUsername",
},
"global": map[string]interface{}{
"commonLabels": map[string]interface{}{
"app.kubernetes.io/managed-by": "test-runner",
},
"registryNodePort": int64(1234),
},
}
Expand All @@ -79,7 +82,8 @@ func Test_flagsBuilder_Build(t *testing.T) {
"testHealthzLivenessTimeout",
).
WithLogFormat("testLogFormat").
WithLogLevel("testLogLevel").Build()
WithLogLevel("testLogLevel").
WithManagedByLabel("test-runner").Build()

require.Equal(t, expectedFlags, flags)
})
Expand Down
5 changes: 5 additions & 0 deletions components/operator/internal/state/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package state

import (
"context"
"fmt"

"github.com/kyma-project/serverless/components/operator/api/v1alpha1"
"github.com/kyma-project/serverless/components/operator/internal/chart"
Expand All @@ -18,9 +19,13 @@ func sFnApplyResources(_ context.Context, r *reconciler, s *systemState) (stateF
"Installing for configuration")
}

// update common labels for all rendered resources
s.flagsBuilder.WithManagedByLabel("serverless-operator")

// install component
err := chart.Install(s.chartConfig, s.flagsBuilder.Build())
if err != nil {
fmt.Println(err)
r.log.Warnf("error while installing resource %s: %s",
client.ObjectKeyFromObject(&s.instance), err.Error())
s.setState(v1alpha1.StateError)
Expand Down
9 changes: 9 additions & 0 deletions components/operator/internal/state/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func Test_buildSFnApplyResources(t *testing.T) {
require.Nil(t, result)
requireEqualFunc(t, sFnVerifyResources, next)

expectedFlags := map[string]interface{}{
"global": map[string]interface{}{
"commonLabels": map[string]interface{}{
"app.kubernetes.io/managed-by": "serverless-operator",
},
},
}
require.Equal(t, expectedFlags, s.flagsBuilder.Build())

status := s.instance.Status
require.Equal(t, v1alpha1.StateProcessing, status.State)
requireContainsCondition(t, status,
Expand Down
8 changes: 7 additions & 1 deletion components/operator/internal/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ func fixManifestCache(manifest string) chart.ManifestCache {
_ = cache.Set(context.Background(), types.NamespacedName{
Name: testInstalledServerless.GetName(),
Namespace: testInstalledServerless.GetNamespace(),
}, chart.ServerlessSpecManifest{Manifest: manifest, CustomFlags: map[string]interface{}{}})
}, chart.ServerlessSpecManifest{Manifest: manifest, CustomFlags: map[string]interface{}{
"global": map[string]interface{}{
"commonLabels": map[string]interface{}{
"app.kubernetes.io/managed-by": "serverless-operator",
},
},
}})

return cache
}
Expand Down
Loading