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

Improve ifology #565

Merged
merged 2 commits into from
May 10, 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
31 changes: 19 additions & 12 deletions components/serverless/internal/controllers/serverless/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,33 @@ func mergeMapWithNewValues(existing, newValues map[string]string) {
}
}

// TODO refactor to make this code more readable
func equalDeployments(existing appsv1.Deployment, expected appsv1.Deployment) bool {
result := true
result = result && len(existing.Spec.Template.Spec.Containers) == 1
result = result && len(existing.Spec.Template.Spec.Containers) == len(expected.Spec.Template.Spec.Containers)
result := equalFnContainer(existing, expected)
result = result && equalMetadata(existing, expected)

result = result && existing.Spec.Template.Spec.Containers[0].Image == expected.Spec.Template.Spec.Containers[0].Image
result = result && envsEqual(existing.Spec.Template.Spec.Containers[0].Env, expected.Spec.Template.Spec.Containers[0].Env)
result = result && equalResources(existing.Spec.Template.Spec.Containers[0].Resources, expected.Spec.Template.Spec.Containers[0].Resources)

result = result && mapsEqual(existing.GetLabels(), expected.GetLabels())
result = result && mapsEqual(existing.Spec.Template.GetLabels(), expected.Spec.Template.GetLabels())
result = result && equalInt32Pointer(existing.Spec.Replicas, expected.Spec.Replicas)

result = result && mapsEqual(existing.Spec.Template.GetAnnotations(), expected.Spec.Template.GetAnnotations())
result = result && equalSecretMounts(existing.Spec.Template.Spec, expected.Spec.Template.Spec)
result = result && equalInt32Pointer(existing.Spec.RevisionHistoryLimit, expected.Spec.RevisionHistoryLimit)
return result
}

func equalMetadata(existing appsv1.Deployment, expected appsv1.Deployment) bool {
result := mapsEqual(existing.GetLabels(), expected.GetLabels())
result = result && mapsEqual(existing.Spec.Template.GetLabels(), expected.Spec.Template.GetLabels())
result = result && mapsEqual(existing.Spec.Template.GetAnnotations(), expected.Spec.Template.GetAnnotations())
return result
}

func equalFnContainer(existing appsv1.Deployment, expected appsv1.Deployment) bool {
if !(len(existing.Spec.Template.Spec.Containers) == 1 && len(expected.Spec.Template.Spec.Containers) == 1) {
return false
}
result := existing.Spec.Template.Spec.Containers[0].Image == expected.Spec.Template.Spec.Containers[0].Image
result = result && envsEqual(existing.Spec.Template.Spec.Containers[0].Env, expected.Spec.Template.Spec.Containers[0].Env)
result = result && equalResources(existing.Spec.Template.Spec.Containers[0].Resources, expected.Spec.Template.Spec.Containers[0].Resources)
return result
}

func equalServices(existing corev1.Service, expected corev1.Service) bool {
return mapsEqual(existing.Spec.Selector, expected.Spec.Selector) &&
mapsContains(existing.Annotations, prometheusSvcAnnotations()) &&
Expand Down
18 changes: 10 additions & 8 deletions components/serverless/internal/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
func Start(t *testing.T) (cl client.Client, env *envtest.Environment) {
wdPath, err := os.Getwd()
require.NoError(t, err)
crdPath := buildCrdPath(wdPath)
crdPath := buildPathFromProjectRoot(wdPath, "components", "serverless", "config", "crd", "bases")
envtestBinsPath := buildPathFromProjectRoot(wdPath, "bin", "k8s", "kubebuilder_assets")

testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{crdPath},
ErrorIfCRDPathMissing: true,
BinaryAssetsDirectory: envtestBinsPath,
}
cfg, err := testEnv.Start()
require.NoError(t, err)
Expand All @@ -39,16 +41,16 @@ func Stop(t *testing.T, testEnv *envtest.Environment) {
require.NoError(t, testEnv.Stop())
}

func buildCrdPath(wd string) string {
func buildPathFromProjectRoot(wd string, dirs ...string) string {
wdPath := strings.Split(wd, "/")

crdPath := []string{"/"}
for _, path := range wdPath {
crdPath = append(crdPath, path)
if path == "components" {
path := []string{"/"}
for _, dir := range wdPath {
if dir == "components" {
break
}
path = append(path, dir)
}
crdPath = append(crdPath, "serverless", "config", "crd", "bases")
return filepath.Join(crdPath...)
path = append(path, dirs...)
return filepath.Join(path...)
}
Loading