Skip to content

Commit 4e41b8d

Browse files
author
Jeff Peeler
committed
fix(unit): refactor to use shared pod resource
1 parent c726141 commit 4e41b8d

File tree

6 files changed

+73
-154
lines changed

6 files changed

+73
-154
lines changed

pkg/controller/operators/catalog/operator_test.go

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,23 @@ func TestExecutePlan(t *testing.T) {
241241
Resource: v1alpha1.StepResource{
242242
CatalogSource: "catalog",
243243
CatalogSourceNamespace: namespace,
244-
Group: "",
245-
Version: "v1",
246-
Kind: "Service",
247-
Name: "service",
248-
Manifest: toManifest(service("service", namespace)),
244+
Group: "",
245+
Version: "v1",
246+
Kind: "Service",
247+
Name: "service",
248+
Manifest: toManifest(service("service", namespace)),
249249
},
250250
Status: v1alpha1.StepStatusUnknown,
251251
},
252252
{
253253
Resource: v1alpha1.StepResource{
254254
CatalogSource: "catalog",
255255
CatalogSourceNamespace: namespace,
256-
Group: "operators.coreos.com",
257-
Version: "v1alpha1",
258-
Kind: "ClusterServiceVersion",
259-
Name: "csv",
260-
Manifest: toManifest(csv("csv", namespace, nil, nil)),
256+
Group: "operators.coreos.com",
257+
Version: "v1alpha1",
258+
Kind: "ClusterServiceVersion",
259+
Name: "csv",
260+
Manifest: toManifest(csv("csv", namespace, nil, nil)),
261261
},
262262
Status: v1alpha1.StepStatusUnknown,
263263
},
@@ -330,6 +330,7 @@ func TestSyncCatalogSources(t *testing.T) {
330330
Name: "cool-catalog",
331331
Namespace: "cool-namespace",
332332
UID: types.UID("catalog-uid"),
333+
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
333334
},
334335
Spec: v1alpha1.CatalogSourceSpec{
335336
Image: "catalog-image",
@@ -493,6 +494,7 @@ func TestSyncCatalogSources(t *testing.T) {
493494
Name: "cool-catalog",
494495
Namespace: "cool-namespace",
495496
UID: types.UID("catalog-uid"),
497+
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
496498
},
497499
Spec: v1alpha1.CatalogSourceSpec{
498500
Image: "old-image",
@@ -898,50 +900,7 @@ func toManifest(obj runtime.Object) string {
898900
}
899901

900902
func pod(s v1alpha1.CatalogSource) *corev1.Pod {
901-
pod := &corev1.Pod{
902-
ObjectMeta: metav1.ObjectMeta{
903-
GenerateName: s.GetName() + "-",
904-
Namespace: s.GetNamespace(),
905-
Labels: map[string]string{
906-
"olm.catalogSource": s.GetName(),
907-
},
908-
},
909-
Spec: corev1.PodSpec{
910-
Containers: []corev1.Container{
911-
{
912-
Name: "registry-server",
913-
Image: s.Spec.Image,
914-
Ports: []corev1.ContainerPort{
915-
{
916-
Name: "grpc",
917-
ContainerPort: 50051,
918-
},
919-
},
920-
ReadinessProbe: &corev1.Probe{
921-
Handler: corev1.Handler{
922-
Exec: &corev1.ExecAction{
923-
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
924-
},
925-
},
926-
InitialDelaySeconds: 5,
927-
},
928-
LivenessProbe: &corev1.Probe{
929-
Handler: corev1.Handler{
930-
Exec: &corev1.ExecAction{
931-
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
932-
},
933-
},
934-
InitialDelaySeconds: 10,
935-
},
936-
},
937-
},
938-
Tolerations: []corev1.Toleration{
939-
{
940-
Operator: corev1.TolerationOpExists,
941-
},
942-
},
943-
},
944-
}
903+
pod := reconciler.Pod(&s, "registry-server", s.Spec.Image, s.GetLabels(), 5, 10)
945904
ownerutil.AddOwner(pod, &s, false, false)
946905
return pod
947906
}

pkg/controller/registry/reconciler/configmap.go

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/sirupsen/logrus"
99
v1 "k8s.io/api/core/v1"
1010
rbacv1 "k8s.io/api/rbac/v1"
11-
"k8s.io/apimachinery/pkg/api/resource"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"k8s.io/apimachinery/pkg/labels"
1413
"k8s.io/apimachinery/pkg/util/intstr"
@@ -90,56 +89,9 @@ func (s *configMapCatalogSourceDecorator) Service() *v1.Service {
9089
}
9190

9291
func (s *configMapCatalogSourceDecorator) Pod(image string) *v1.Pod {
93-
pod := &v1.Pod{
94-
ObjectMeta: metav1.ObjectMeta{
95-
GenerateName: s.GetName() + "-",
96-
Namespace: s.GetNamespace(),
97-
Labels: s.Labels(),
98-
},
99-
Spec: v1.PodSpec{
100-
Containers: []v1.Container{
101-
{
102-
Name: "configmap-registry-server",
103-
Image: image,
104-
Command: []string{"configmap-server", "-c", s.Spec.ConfigMap, "-n", s.GetNamespace()},
105-
Ports: []v1.ContainerPort{
106-
{
107-
Name: "grpc",
108-
ContainerPort: 50051,
109-
},
110-
},
111-
ReadinessProbe: &v1.Probe{
112-
Handler: v1.Handler{
113-
Exec: &v1.ExecAction{
114-
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
115-
},
116-
},
117-
InitialDelaySeconds: 1,
118-
},
119-
LivenessProbe: &v1.Probe{
120-
Handler: v1.Handler{
121-
Exec: &v1.ExecAction{
122-
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
123-
},
124-
},
125-
InitialDelaySeconds: 2,
126-
},
127-
Resources: v1.ResourceRequirements{
128-
Requests: v1.ResourceList{
129-
v1.ResourceCPU: resource.MustParse("10m"),
130-
v1.ResourceMemory: resource.MustParse("50Mi"),
131-
},
132-
},
133-
},
134-
},
135-
Tolerations: []v1.Toleration{
136-
{
137-
Operator: v1.TolerationOpExists,
138-
},
139-
},
140-
ServiceAccountName: s.GetName() + ConfigMapServerPostfix,
141-
},
142-
}
92+
pod := Pod(s.CatalogSource, "configmap-registry-server", image, s.Labels(), 1, 2)
93+
pod.Spec.ServiceAccountName = s.GetName() + ConfigMapServerPostfix
94+
pod.Spec.Containers[0].Command = []string{"configmap-server", "-c", s.Spec.ConfigMap, "-n", s.GetNamespace()}
14395
ownerutil.AddOwner(pod, s.CatalogSource, false, false)
14496
return pod
14597
}

pkg/controller/registry/reconciler/configmap_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func validConfigMapCatalogSource(configMap *corev1.ConfigMap) *v1alpha1.CatalogS
170170
Name: "cool-catalog",
171171
Namespace: testNamespace,
172172
UID: types.UID("catalog-uid"),
173+
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
173174
},
174175
Spec: v1alpha1.CatalogSourceSpec{
175176
ConfigMap: "cool-configmap",

pkg/controller/registry/reconciler/grpc.go

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/pkg/errors"
77
"github.com/sirupsen/logrus"
88
v1 "k8s.io/api/core/v1"
9-
"k8s.io/apimachinery/pkg/api/resource"
109
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110
"k8s.io/apimachinery/pkg/labels"
1211
"k8s.io/apimachinery/pkg/util/intstr"
@@ -56,54 +55,7 @@ func (s *grpcCatalogSourceDecorator) Service() *v1.Service {
5655
}
5756

5857
func (s *grpcCatalogSourceDecorator) Pod() *v1.Pod {
59-
pod := &v1.Pod{
60-
ObjectMeta: metav1.ObjectMeta{
61-
GenerateName: s.GetName() + "-",
62-
Namespace: s.GetNamespace(),
63-
Labels: s.Labels(),
64-
},
65-
Spec: v1.PodSpec{
66-
Containers: []v1.Container{
67-
{
68-
Name: "registry-server",
69-
Image: s.Spec.Image,
70-
Ports: []v1.ContainerPort{
71-
{
72-
Name: "grpc",
73-
ContainerPort: 50051,
74-
},
75-
},
76-
ReadinessProbe: &v1.Probe{
77-
Handler: v1.Handler{
78-
Exec: &v1.ExecAction{
79-
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
80-
},
81-
},
82-
InitialDelaySeconds: 5,
83-
},
84-
LivenessProbe: &v1.Probe{
85-
Handler: v1.Handler{
86-
Exec: &v1.ExecAction{
87-
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
88-
},
89-
},
90-
InitialDelaySeconds: 10,
91-
},
92-
Resources: v1.ResourceRequirements{
93-
Requests: v1.ResourceList{
94-
v1.ResourceCPU: resource.MustParse("10m"),
95-
v1.ResourceMemory: resource.MustParse("50Mi"),
96-
},
97-
},
98-
},
99-
},
100-
Tolerations: []v1.Toleration{
101-
{
102-
Operator: v1.TolerationOpExists,
103-
},
104-
},
105-
},
106-
}
58+
pod := Pod(s.CatalogSource, "registry-server", s.Spec.Image, s.Labels(), 5, 10)
10759
ownerutil.AddOwner(pod, s.CatalogSource, false, false)
10860
return pod
10961
}

pkg/controller/registry/reconciler/grpc_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func validGrpcCatalogSource(image, address string) *v1alpha1.CatalogSource {
2222
Name: "img-catalog",
2323
Namespace: testNamespace,
2424
UID: types.UID("catalog-uid"),
25+
Labels: map[string]string{"olm.catalogSource": "img-catalog"},
2526
},
2627
Spec: v1alpha1.CatalogSourceSpec{
2728
Image: image,

pkg/controller/registry/reconciler/reconciler.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package reconciler
33

44
import (
5+
v1 "k8s.io/api/core/v1"
6+
"k8s.io/apimachinery/pkg/api/resource"
57
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
68

79
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
@@ -83,3 +85,55 @@ func NewRegistryReconcilerFactory(lister operatorlister.OperatorLister, opClient
8385
ConfigMapServerImage: configMapServerImage,
8486
}
8587
}
88+
89+
func Pod(source *v1alpha1.CatalogSource, name string, image string, labels map[string]string, readinessDelay int32, livenessDelay int32) *v1.Pod {
90+
pod := &v1.Pod{
91+
ObjectMeta: metav1.ObjectMeta{
92+
GenerateName: source.GetName() + "-",
93+
Namespace: source.GetNamespace(),
94+
Labels: labels,
95+
},
96+
Spec: v1.PodSpec{
97+
Containers: []v1.Container{
98+
{
99+
Name: name,
100+
Image: image,
101+
Ports: []v1.ContainerPort{
102+
{
103+
Name: "grpc",
104+
ContainerPort: 50051,
105+
},
106+
},
107+
ReadinessProbe: &v1.Probe{
108+
Handler: v1.Handler{
109+
Exec: &v1.ExecAction{
110+
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
111+
},
112+
},
113+
InitialDelaySeconds: readinessDelay,
114+
},
115+
LivenessProbe: &v1.Probe{
116+
Handler: v1.Handler{
117+
Exec: &v1.ExecAction{
118+
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
119+
},
120+
},
121+
InitialDelaySeconds: livenessDelay,
122+
},
123+
Resources: v1.ResourceRequirements{
124+
Requests: v1.ResourceList{
125+
v1.ResourceCPU: resource.MustParse("10m"),
126+
v1.ResourceMemory: resource.MustParse("50Mi"),
127+
},
128+
},
129+
},
130+
},
131+
Tolerations: []v1.Toleration{
132+
{
133+
Operator: v1.TolerationOpExists,
134+
},
135+
},
136+
},
137+
}
138+
return pod
139+
}

0 commit comments

Comments
 (0)