-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"context" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
ctrlcli "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func getComponentPods(ctx context.Context, namespace string) map[string]corev1.Pod { | ||
podlist := corev1.PodList{} | ||
noJobPodsReq, err := labels.NewRequirement("job-name", selection.DoesNotExist, []string{}) | ||
Expect(err).Should(Succeed()) | ||
selector := labels.NewSelector() | ||
selector = selector.Add(*noJobPodsReq) | ||
err = k8sClient.List( | ||
ctx, | ||
&podlist, | ||
ctrlcli.InNamespace(namespace), | ||
ctrlcli.MatchingLabelsSelector{Selector: selector}, | ||
) | ||
Expect(err).Should(Succeed()) | ||
|
||
result := make(map[string]corev1.Pod) | ||
for _, pod := range podlist.Items { | ||
result[pod.Name] = pod | ||
} | ||
return result | ||
} | ||
|
||
type StringSet mapset.Set[string] | ||
|
||
func NewStringSet() StringSet { | ||
return mapset.NewSet[string]() | ||
} | ||
|
||
func NewStringSetFromItems(slice ...string) StringSet { | ||
set := NewStringSet() | ||
for _, item := range slice { | ||
set.Add(item) | ||
} | ||
return set | ||
} | ||
|
||
func NewStringSetFromMap[T any](data map[string]T) StringSet { | ||
set := NewStringSet() | ||
for key := range data { | ||
set.Add(key) | ||
} | ||
return set | ||
} | ||
|
||
type podsCreationDiff struct { | ||
created StringSet | ||
deleted StringSet | ||
recreated StringSet | ||
} | ||
|
||
func newPodsCreationDiff() podsCreationDiff { | ||
return podsCreationDiff{ | ||
created: NewStringSet(), | ||
deleted: NewStringSet(), | ||
recreated: NewStringSet(), | ||
} | ||
} | ||
|
||
func diffPodsCreation(before, after map[string]corev1.Pod) podsCreationDiff { | ||
diff := newPodsCreationDiff() | ||
for podName := range before { | ||
if _, existNow := after[podName]; !existNow { | ||
diff.deleted.Add(podName) | ||
} | ||
} | ||
for podName, podAfter := range after { | ||
podBefore, wasBefore := before[podName] | ||
if !wasBefore { | ||
diff.created.Add(podName) | ||
continue | ||
} | ||
if podBefore.CreationTimestamp.Before(&podAfter.CreationTimestamp) { | ||
diff.recreated.Add(podName) | ||
} | ||
} | ||
return diff | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters