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

feat: Add metadata and status helpers to Kyma type #2179

Merged
merged 6 commits into from
Jan 20, 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
3 changes: 3 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.23.4

require (
github.com/Masterminds/semver/v3 v3.3.1
github.com/stretchr/testify v1.10.0
k8s.io/apimachinery v0.32.1
sigs.k8s.io/controller-runtime v0.19.4
)

require github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
lindnerby marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
9 changes: 8 additions & 1 deletion api/shared/operator_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package shared
import "strings"

const (
OperatorGroup = "operator.kyma-project.io"
KymaGroup = "kyma-project.io"
OperatorGroup = "operator." + KymaGroup
Separator = "/"
ControllerName = OperatorGroup + Separator + "controller-name"
ChannelLabel = OperatorGroup + Separator + "channel"
Expand Down Expand Up @@ -43,6 +44,12 @@ const (
// If put on a single ModuleTemplate, allows to disable sync just for this object.
SyncLabel = OperatorGroup + Separator + "sync"

GlobalAccountIDLabel = KymaGroup + Separator + "global-account-id"
RegionLabel = KymaGroup + Separator + "region"
PlatformRegionLabel = KymaGroup + Separator + "platform-region"
// to be confirmed https://github.com/kyma-project/kyma/issues/18611#issuecomment-2441158676
PlanLabel = KymaGroup + Separator + "broker-plan-name"

EnableLabelValue = "true"
DisableLabelValue = "false"
)
Expand Down
26 changes: 26 additions & 0 deletions api/v1beta2/kyma_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ type KymaStatus struct {
shared.LastOperation `json:"lastOperation,omitempty"`
}

func (status *KymaStatus) GetModuleStatus(moduleName string) *ModuleStatus {
for _, moduleStatus := range status.Modules {
if moduleStatus.Name == moduleName {
return &moduleStatus
}
}
return nil
}

type ModuleStatus struct {
// Name defines the name of the Module in the Spec that the status is used for.
// It can be any kind of Reference format supported by Module.Name.
Expand Down Expand Up @@ -430,3 +439,20 @@ func (kyma *Kyma) GetNamespacedName() types.NamespacedName {
Name: kyma.GetName(),
}
}

func (kyma *Kyma) GetGlobalAccount() string {
return kyma.Labels[shared.GlobalAccountIDLabel]
}

func (kyma *Kyma) GetRegion() string {
return kyma.Labels[shared.RegionLabel]
}

func (kyma *Kyma) GetPlatformRegion() string {
return kyma.Labels[shared.PlatformRegionLabel]
}

// to be confirmed https://github.com/kyma-project/kyma/issues/18611#issuecomment-2441158676
func (kyma *Kyma) GetPlan() string {
return kyma.Labels[shared.PlanLabel]
}
208 changes: 208 additions & 0 deletions api/v1beta2/kyma_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package v1beta2_test
lindnerby marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

"github.com/stretchr/testify/assert"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
)

func Test_GetModuleStatus(t *testing.T) {
tests := []struct {
name string
kymaStatus *v1beta2.KymaStatus
moduleName string
expectSuccess bool
}{
{
name: "Test GetModuleStatus() with existing module",
kymaStatus: &v1beta2.KymaStatus{
Modules: []v1beta2.ModuleStatus{
{
Name: "module1",
},
},
},
moduleName: "module1",
expectSuccess: true,
},
{
name: "Test GetModuleStatus() with non-existing module",
kymaStatus: &v1beta2.KymaStatus{
Modules: []v1beta2.ModuleStatus{
{
Name: "module1",
},
},
},
moduleName: "module2",
expectSuccess: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
moduleStatus := testCase.kymaStatus.GetModuleStatus(testCase.moduleName)
if testCase.expectSuccess {
assert.NotNil(t, moduleStatus)
assert.Equal(t, testCase.moduleName, moduleStatus.Name)
} else {
assert.Nil(t, moduleStatus)
}
})
}
}

func Test_GetGlobalAccountID(t *testing.T) {
tests := []struct {
name string
labels map[string]string
expectSuccess bool
}{
{
name: "Test GetGlobalAccountID() with existing label",
labels: map[string]string{
shared.GlobalAccountIDLabel: "1234",
},
expectSuccess: true,
},
{
name: "Test GetGlobalAccountID() with non-existing label",
labels: map[string]string{},
expectSuccess: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
kyma := &v1beta2.Kyma{
ObjectMeta: apimetav1.ObjectMeta{
Labels: testCase.labels,
Name: "test-kyma",
},
}

globalAccountID := kyma.GetGlobalAccount()
if testCase.expectSuccess {
assert.Equal(t, testCase.labels[shared.GlobalAccountIDLabel], globalAccountID)
} else {
assert.Empty(t, globalAccountID)
}
})
}
}

func Test_GetRegion(t *testing.T) {
tests := []struct {
name string
labels map[string]string
expectSuccess bool
}{
{
name: "Test GetRegion() with existing label",
labels: map[string]string{
shared.RegionLabel: "eu-central-1",
},
expectSuccess: true,
},
{
name: "Test GetRegion() with non-existing label",
labels: map[string]string{},
expectSuccess: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
kyma := &v1beta2.Kyma{
ObjectMeta: apimetav1.ObjectMeta{
Labels: testCase.labels,
Name: "test-kyma",
},
}

region := kyma.GetRegion()
if testCase.expectSuccess {
assert.Equal(t, testCase.labels[shared.RegionLabel], region)
} else {
assert.Empty(t, region)
}
})
}
}

func Test_GetPlatformRegion(t *testing.T) {
tests := []struct {
name string
labels map[string]string
expectSuccess bool
}{
{
name: "Test GetPlatformRegion() with existing label",
labels: map[string]string{
shared.PlatformRegionLabel: "cf-us10-staging",
},
expectSuccess: true,
},
{
name: "Test GetPlatformRegion() with non-existing label",
labels: map[string]string{},
expectSuccess: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
kyma := &v1beta2.Kyma{
ObjectMeta: apimetav1.ObjectMeta{
Labels: testCase.labels,
Name: "test-kyma",
},
}

platformRegion := kyma.GetPlatformRegion()
if testCase.expectSuccess {
assert.Equal(t, testCase.labels[shared.PlatformRegionLabel], platformRegion)
} else {
assert.Empty(t, platformRegion)
}
})
}
}

func Test_GetPlan(t *testing.T) {
tests := []struct {
name string
labels map[string]string
expectSuccess bool
}{
{
name: "Test GetPlan() with existing label",
labels: map[string]string{
shared.PlanLabel: "aws",
},
expectSuccess: true,
},
{
name: "Test GetPlan() with non-existing label",
labels: map[string]string{},
expectSuccess: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
kyma := &v1beta2.Kyma{
ObjectMeta: apimetav1.ObjectMeta{
Labels: testCase.labels,
Name: "test-kyma",
},
}

plan := kyma.GetPlan()
if testCase.expectSuccess {
assert.Equal(t, testCase.labels[shared.PlanLabel], plan)
} else {
assert.Empty(t, plan)
}
})
}
}
Loading