Commit cb86a5d1 authored by André Carvalho's avatar André Carvalho

create ClusterIP service for nginx

parent 987e6807
......@@ -59,6 +59,18 @@ func reconcile(ctx context.Context, event sdk.Event, nginx *v1alpha1.Nginx, logg
return nil
}
if err := reconcileDeployment(ctx, nginx, logger); err != nil {
return err
}
if err := reconcileService(ctx, nginx, logger); err != nil {
return err
}
return nil
}
func reconcileDeployment(ctx context.Context, nginx *v1alpha1.Nginx, logger *logrus.Entry) error {
deployment := k8s.NewDeployment(nginx)
err := sdk.Create(deployment)
......@@ -92,6 +104,21 @@ func reconcile(ctx context.Context, event sdk.Event, nginx *v1alpha1.Nginx, logg
return nil
}
func reconcileService(ctx context.Context, nginx *v1alpha1.Nginx, logger *logrus.Entry) error {
service := k8s.NewService(nginx)
err := sdk.Create(service)
if errors.IsAlreadyExists(err) {
return nil
}
if err != nil {
logger.Errorf("Failed to create service: %v", err)
}
return err
}
func refreshStatus(ctx context.Context, event sdk.Event, nginx *v1alpha1.Nginx, logger *logrus.Entry) error {
if event.Deleted {
logger.Debug("nginx deleted, skipping status update")
......
......@@ -3,6 +3,8 @@ package k8s
import (
"fmt"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/tsuru/nginx-operator/pkg/apis/nginx/v1alpha1"
appv1 "k8s.io/api/apps/v1"
......@@ -67,6 +69,39 @@ func NewDeployment(n *v1alpha1.Nginx) *appv1.Deployment {
return &deployment
}
// NewService assembles the ClusterIP service for the Nginx
func NewService(n *v1alpha1.Nginx) *corev1.Service {
return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: n.Name + "-service",
Namespace: n.Namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(n, schema.GroupVersionKind{
Group: v1alpha1.SchemeGroupVersion.Group,
Version: v1alpha1.SchemeGroupVersion.Version,
Kind: "Nginx",
}),
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString("http"),
Port: int32(80),
},
},
Selector: LabelsForNginx(n.Name),
Type: corev1.ServiceTypeClusterIP,
},
}
}
// LabelsForNginx returns the labels for a Nginx CR with the given name
func LabelsForNginx(name string) map[string]string {
return map[string]string{
......
......@@ -9,6 +9,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
)
func baseNginx() v1alpha1.Nginx {
......@@ -201,3 +202,53 @@ func assertDeployment(t *testing.T, want, got *appv1.Deployment) {
assert.Equal(t, want.Spec, got.Spec)
assert.Equal(t, want, got)
}
func TestNewService(t *testing.T) {
tests := []struct {
name string
nginx v1alpha1.Nginx
want *corev1.Service
}{
{
name: "base",
nginx: baseNginx(),
want: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-nginx-service",
Namespace: "default",
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString("http"),
Port: int32(80),
},
},
Selector: map[string]string{
"nginx_cr": "my-nginx",
"app": "nginx",
},
Type: corev1.ServiceTypeClusterIP,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.want.OwnerReferences = []metav1.OwnerReference{
*metav1.NewControllerRef(&tt.nginx, schema.GroupVersionKind{
Group: v1alpha1.SchemeGroupVersion.Group,
Version: v1alpha1.SchemeGroupVersion.Version,
Kind: "Nginx",
}),
}
assert.Equal(t, tt.want, NewService(&tt.nginx))
})
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment