MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / ComparePodSpecs

Function ComparePodSpecs

pkg/specs/podspec_diff.go:36–114  ·  view source on GitHub ↗

ComparePodSpecs compares two pod specs, returns true iff they are equivalent, and if they are not, points out the first discrepancy.

(
	currentPodSpec, targetPodSpec corev1.PodSpec,
)

Source from the content-addressed store, hash-verified

34// ComparePodSpecs compares two pod specs, returns true iff they are equivalent, and
35// if they are not, points out the first discrepancy.
36func ComparePodSpecs(
37 currentPodSpec, targetPodSpec corev1.PodSpec,
38) (bool, string) {
39 comparisons := map[string]func() (bool, string){
40 "volumes": func() (bool, string) {
41 return compareVolumes(currentPodSpec.Volumes, targetPodSpec.Volumes)
42 },
43 "containers": func() (bool, string) {
44 return compareContainers(currentPodSpec.Containers, targetPodSpec.Containers)
45 },
46 "init-containers": func() (bool, string) {
47 extractContainersForComparison := func(passedContainers []corev1.Container) []corev1.Container {
48 var containers []corev1.Container
49 for _, container := range passedContainers {
50 if container.Name == BootstrapControllerContainerName {
51 // ignore the bootstrap controller init container. We handle it inside checkPodSpecIsOutdated.
52 continue
53 }
54 containers = append(containers, container)
55 }
56 return containers
57 }
58 return compareContainers(
59 extractContainersForComparison(currentPodSpec.InitContainers),
60 extractContainersForComparison(targetPodSpec.InitContainers),
61 )
62 },
63 }
64
65 for comp, f := range comparisons {
66 areEqual, diff := f()
67 if areEqual {
68 continue
69 }
70 return false, fmt.Sprintf("%s: %s", comp, diff)
71 }
72
73 genericComparisons := map[string]func() bool{
74 "security-context": func() bool {
75 return reflect.DeepEqual(currentPodSpec.SecurityContext, targetPodSpec.SecurityContext)
76 },
77 "affinity": func() bool {
78 return reflect.DeepEqual(currentPodSpec.Affinity, targetPodSpec.Affinity)
79 },
80 "tolerations": func() bool {
81 return reflect.DeepEqual(currentPodSpec.Tolerations, targetPodSpec.Tolerations)
82 },
83 "node-selector": func() bool {
84 return reflect.DeepEqual(currentPodSpec.NodeSelector, targetPodSpec.NodeSelector)
85 },
86 "topology-spread-constraints": func() bool {
87 return reflect.DeepEqual(currentPodSpec.TopologySpreadConstraints, targetPodSpec.TopologySpreadConstraints)
88 },
89 "service-account-name": func() bool {
90 return currentPodSpec.ServiceAccountName == targetPodSpec.ServiceAccountName
91 },
92 "scheduler-name": func() bool {
93 return currentPodSpec.SchedulerName == targetPodSpec.SchedulerName

Callers 2

checkPodSpecIsOutdatedFunction · 0.92
pods_test.goFile · 0.85

Calls 2

compareVolumesFunction · 0.85
compareContainersFunction · 0.85

Tested by

no test coverage detected