Skip to content

Commit

Permalink
fix(unit): refactor to use shared pod resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Peeler committed Jul 18, 2019
1 parent c726141 commit 8e87032
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 154 deletions.
67 changes: 13 additions & 54 deletions pkg/controller/operators/catalog/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,23 @@ func TestExecutePlan(t *testing.T) {
Resource: v1alpha1.StepResource{
CatalogSource: "catalog",
CatalogSourceNamespace: namespace,
Group: "",
Version: "v1",
Kind: "Service",
Name: "service",
Manifest: toManifest(service("service", namespace)),
Group: "",
Version: "v1",
Kind: "Service",
Name: "service",
Manifest: toManifest(service("service", namespace)),
},
Status: v1alpha1.StepStatusUnknown,
},
{
Resource: v1alpha1.StepResource{
CatalogSource: "catalog",
CatalogSourceNamespace: namespace,
Group: "operators.coreos.com",
Version: "v1alpha1",
Kind: "ClusterServiceVersion",
Name: "csv",
Manifest: toManifest(csv("csv", namespace, nil, nil)),
Group: "operators.coreos.com",
Version: "v1alpha1",
Kind: "ClusterServiceVersion",
Name: "csv",
Manifest: toManifest(csv("csv", namespace, nil, nil)),
},
Status: v1alpha1.StepStatusUnknown,
},
Expand Down Expand Up @@ -330,6 +330,7 @@ func TestSyncCatalogSources(t *testing.T) {
Name: "cool-catalog",
Namespace: "cool-namespace",
UID: types.UID("catalog-uid"),
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
},
Spec: v1alpha1.CatalogSourceSpec{
Image: "catalog-image",
Expand Down Expand Up @@ -493,6 +494,7 @@ func TestSyncCatalogSources(t *testing.T) {
Name: "cool-catalog",
Namespace: "cool-namespace",
UID: types.UID("catalog-uid"),
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
},
Spec: v1alpha1.CatalogSourceSpec{
Image: "old-image",
Expand Down Expand Up @@ -898,50 +900,7 @@ func toManifest(obj runtime.Object) string {
}

func pod(s v1alpha1.CatalogSource) *corev1.Pod {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: s.GetName() + "-",
Namespace: s.GetNamespace(),
Labels: map[string]string{
"olm.catalogSource": s.GetName(),
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "registry-server",
Image: s.Spec.Image,
Ports: []corev1.ContainerPort{
{
Name: "grpc",
ContainerPort: 50051,
},
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: 5,
},
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: 10,
},
},
},
Tolerations: []corev1.Toleration{
{
Operator: corev1.TolerationOpExists,
},
},
},
}
pod := reconciler.Pod(&s, "registry-server", s.Spec.Image, 5, 10)
ownerutil.AddOwner(pod, &s, false, false)
return pod
}
54 changes: 3 additions & 51 deletions pkg/controller/registry/reconciler/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -90,56 +89,9 @@ func (s *configMapCatalogSourceDecorator) Service() *v1.Service {
}

func (s *configMapCatalogSourceDecorator) Pod(image string) *v1.Pod {
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: s.GetName() + "-",
Namespace: s.GetNamespace(),
Labels: s.Labels(),
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "configmap-registry-server",
Image: image,
Command: []string{"configmap-server", "-c", s.Spec.ConfigMap, "-n", s.GetNamespace()},
Ports: []v1.ContainerPort{
{
Name: "grpc",
ContainerPort: 50051,
},
},
ReadinessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: 1,
},
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: 2,
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("10m"),
v1.ResourceMemory: resource.MustParse("50Mi"),
},
},
},
},
Tolerations: []v1.Toleration{
{
Operator: v1.TolerationOpExists,
},
},
ServiceAccountName: s.GetName() + ConfigMapServerPostfix,
},
}
pod := Pod(s.CatalogSource, "configmap-registry-server", image, 1, 2)
pod.Spec.ServiceAccountName = s.GetName() + ConfigMapServerPostfix
pod.Spec.Containers[0].Command = []string{"configmap-server", "-c", s.Spec.ConfigMap, "-n", s.GetNamespace()}
ownerutil.AddOwner(pod, s.CatalogSource, false, false)
return pod
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/registry/reconciler/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func validConfigMapCatalogSource(configMap *corev1.ConfigMap) *v1alpha1.CatalogS
Name: "cool-catalog",
Namespace: testNamespace,
UID: types.UID("catalog-uid"),
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
},
Spec: v1alpha1.CatalogSourceSpec{
ConfigMap: "cool-configmap",
Expand Down
50 changes: 1 addition & 49 deletions pkg/controller/registry/reconciler/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -56,54 +55,7 @@ func (s *grpcCatalogSourceDecorator) Service() *v1.Service {
}

func (s *grpcCatalogSourceDecorator) Pod() *v1.Pod {
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: s.GetName() + "-",
Namespace: s.GetNamespace(),
Labels: s.Labels(),
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "registry-server",
Image: s.Spec.Image,
Ports: []v1.ContainerPort{
{
Name: "grpc",
ContainerPort: 50051,
},
},
ReadinessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: 5,
},
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: 10,
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("10m"),
v1.ResourceMemory: resource.MustParse("50Mi"),
},
},
},
},
Tolerations: []v1.Toleration{
{
Operator: v1.TolerationOpExists,
},
},
},
}
pod := Pod(s.CatalogSource, "registry-server", s.Spec.Image, 5, 10)
ownerutil.AddOwner(pod, s.CatalogSource, false, false)
return pod
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/registry/reconciler/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func validGrpcCatalogSource(image, address string) *v1alpha1.CatalogSource {
Name: "img-catalog",
Namespace: testNamespace,
UID: types.UID("catalog-uid"),
Labels: map[string]string{"olm.catalogSource": "img-catalog"},
},
Spec: v1alpha1.CatalogSourceSpec{
Image: image,
Expand Down
54 changes: 54 additions & 0 deletions pkg/controller/registry/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package reconciler

import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
Expand Down Expand Up @@ -83,3 +85,55 @@ func NewRegistryReconcilerFactory(lister operatorlister.OperatorLister, opClient
ConfigMapServerImage: configMapServerImage,
}
}

func Pod(source *v1alpha1.CatalogSource, name string, image string, readinessDelay int32, livenessDelay int32) *v1.Pod {
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: source.GetName() + "-",
Namespace: source.GetNamespace(),
Labels: source.GetLabels(),
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: name,
Image: image,
Ports: []v1.ContainerPort{
{
Name: "grpc",
ContainerPort: 50051,
},
},
ReadinessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: readinessDelay,
},
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{"grpc_health_probe", "-addr=localhost:50051"},
},
},
InitialDelaySeconds: livenessDelay,
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("10m"),
v1.ResourceMemory: resource.MustParse("50Mi"),
},
},
},
},
Tolerations: []v1.Toleration{
{
Operator: v1.TolerationOpExists,
},
},
},
}
return pod
}

0 comments on commit 8e87032

Please sign in to comment.