Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make .spec.http.service fully configurable by default #1004

Merged
merged 2 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 12 additions & 63 deletions operators/config/all-in-one.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,12 @@ spec:
description: Service is a template for the Kubernetes Service
properties:
metadata:
description: Metadata is metadata for the HTTP Service.
properties:
annotations:
description: 'Annotations is an unstructured key value map
stored with a resource that may be set by external tools
to store and retrieve arbitrary metadata. They are not
queryable and should be preserved when modifying objects.
More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
description: ObjectMeta is metadata for the service. The name
and namespace provided here is managed by ECK and will be
ignored.
type: object
spec:
description: Spec contains user-provided settings for the HTTP
Service.
properties:
type:
description: 'Type determines which service type to use
for this workload. The options are: `ClusterIP|LoadBalancer|NodePort`.
Defaults to ClusterIP.'
enum:
- ClusterIP
- LoadBalancer
- NodePort
type: string
description: Spec defines the behavior of the service.
type: object
type: object
tls:
Expand Down Expand Up @@ -263,29 +246,12 @@ spec:
description: Service is a template for the Kubernetes Service
properties:
metadata:
description: Metadata is metadata for the HTTP Service.
properties:
annotations:
description: 'Annotations is an unstructured key value map
stored with a resource that may be set by external tools
to store and retrieve arbitrary metadata. They are not
queryable and should be preserved when modifying objects.
More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
description: ObjectMeta is metadata for the service. The name
and namespace provided here is managed by ECK and will be
ignored.
type: object
spec:
description: Spec contains user-provided settings for the HTTP
Service.
properties:
type:
description: 'Type determines which service type to use
for this workload. The options are: `ClusterIP|LoadBalancer|NodePort`.
Defaults to ClusterIP.'
enum:
- ClusterIP
- LoadBalancer
- NodePort
type: string
description: Spec defines the behavior of the service.
type: object
type: object
tls:
Expand Down Expand Up @@ -864,29 +830,12 @@ spec:
description: Service is a template for the Kubernetes Service
properties:
metadata:
description: Metadata is metadata for the HTTP Service.
properties:
annotations:
description: 'Annotations is an unstructured key value map
stored with a resource that may be set by external tools
to store and retrieve arbitrary metadata. They are not
queryable and should be preserved when modifying objects.
More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
description: ObjectMeta is metadata for the service. The name
and namespace provided here is managed by ECK and will be
ignored.
type: object
spec:
description: Spec contains user-provided settings for the HTTP
Service.
properties:
type:
description: 'Type determines which service type to use
for this workload. The options are: `ClusterIP|LoadBalancer|NodePort`.
Defaults to ClusterIP.'
enum:
- ClusterIP
- LoadBalancer
- NodePort
type: string
description: Spec defines the behavior of the service.
type: object
type: object
tls:
Expand Down
25 changes: 4 additions & 21 deletions operators/config/crds/apm_v1alpha1_apmserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,12 @@ spec:
description: Service is a template for the Kubernetes Service
properties:
metadata:
description: Metadata is metadata for the HTTP Service.
properties:
annotations:
description: 'Annotations is an unstructured key value map
stored with a resource that may be set by external tools
to store and retrieve arbitrary metadata. They are not
queryable and should be preserved when modifying objects.
More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
description: ObjectMeta is metadata for the service. The name
and namespace provided here is managed by ECK and will be
ignored.
type: object
spec:
description: Spec contains user-provided settings for the HTTP
Service.
properties:
type:
description: 'Type determines which service type to use
for this workload. The options are: `ClusterIP|LoadBalancer|NodePort`.
Defaults to ClusterIP.'
enum:
- ClusterIP
- LoadBalancer
- NodePort
type: string
description: Spec defines the behavior of the service.
type: object
type: object
tls:
Expand Down
25 changes: 4 additions & 21 deletions operators/config/crds/elasticsearch_v1alpha1_elasticsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,12 @@ spec:
description: Service is a template for the Kubernetes Service
properties:
metadata:
description: Metadata is metadata for the HTTP Service.
properties:
annotations:
description: 'Annotations is an unstructured key value map
stored with a resource that may be set by external tools
to store and retrieve arbitrary metadata. They are not
queryable and should be preserved when modifying objects.
More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
description: ObjectMeta is metadata for the service. The name
and namespace provided here is managed by ECK and will be
ignored.
type: object
spec:
description: Spec contains user-provided settings for the HTTP
Service.
properties:
type:
description: 'Type determines which service type to use
for this workload. The options are: `ClusterIP|LoadBalancer|NodePort`.
Defaults to ClusterIP.'
enum:
- ClusterIP
- LoadBalancer
- NodePort
type: string
description: Spec defines the behavior of the service.
type: object
type: object
tls:
Expand Down
25 changes: 4 additions & 21 deletions operators/config/crds/kibana_v1alpha1_kibana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,12 @@ spec:
description: Service is a template for the Kubernetes Service
properties:
metadata:
description: Metadata is metadata for the HTTP Service.
properties:
annotations:
description: 'Annotations is an unstructured key value map
stored with a resource that may be set by external tools
to store and retrieve arbitrary metadata. They are not
queryable and should be preserved when modifying objects.
More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
description: ObjectMeta is metadata for the service. The name
and namespace provided here is managed by ECK and will be
ignored.
type: object
spec:
description: Spec contains user-provided settings for the HTTP
Service.
properties:
type:
description: 'Type determines which service type to use
for this workload. The options are: `ClusterIP|LoadBalancer|NodePort`.
Defaults to ClusterIP.'
enum:
- ClusterIP
- LoadBalancer
- NodePort
type: string
description: Spec defines the behavior of the service.
type: object
type: object
tls:
Expand Down
13 changes: 9 additions & 4 deletions operators/pkg/apis/common/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package v1alpha1

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

Expand Down Expand Up @@ -59,11 +61,14 @@ type SubjectAlternativeName struct {

// HTTPService contains defaults for a HTTP service.
type HTTPService struct {
nkvoll marked this conversation as resolved.
Show resolved Hide resolved
// Metadata is metadata for the HTTP Service.
Metadata HTTPServiceObjectMeta `json:"metadata,omitempty"`
// ObjectMeta is metadata for the service.
// The name and namespace provided here is managed by ECK and will be ignored.
// +optional
ObjectMeta metav1.ObjectMeta `json:"metadata,omitempty"`

// Spec contains user-provided settings for the HTTP Service.
Spec HTTPServiceSpec `json:"spec,omitempty"`
// Spec defines the behavior of the service.
// +optional
Spec v1.ServiceSpec `json:"spec,omitempty"`
}

// HTTPServiceObjectMeta is metadata for HTTP Service.
nkvoll marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 2 additions & 2 deletions operators/pkg/apis/common/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 16 additions & 23 deletions operators/pkg/controller/apmserver/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,27 @@
package apmserver

import (
v1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/apm/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/apis/apm/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func NewService(as v1alpha1.ApmServer) *corev1.Service {
var svc = corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: as.Namespace,
Name: PseudoNamespacedResourceName(as),
Labels: NewLabels(as.Name),
Annotations: as.Spec.HTTP.Service.Metadata.Annotations,
},
Spec: corev1.ServiceSpec{
Selector: NewLabels(as.Name),
Ports: []corev1.ServicePort{
{
Protocol: corev1.ProtocolTCP,
Port: HTTPPort,
},
},
SessionAffinity: corev1.ServiceAffinityNone,
Type: common.GetServiceType(as.Spec.HTTP.Service.Spec.Type),
},
svc := corev1.Service{
ObjectMeta: as.Spec.HTTP.Service.ObjectMeta,
Spec: as.Spec.HTTP.Service.Spec,
}
if svc.Spec.Type != corev1.ServiceTypeClusterIP {
svc.Spec.ExternalTrafficPolicy = corev1.ServiceExternalTrafficPolicyTypeCluster

svc.ObjectMeta.Namespace = as.Namespace
svc.ObjectMeta.Name = PseudoNamespacedResourceName(as)

labels := NewLabels(as.Name)
ports := []corev1.ServicePort{
{
Protocol: corev1.ProtocolTCP,
Port: HTTPPort,
},
}
return &svc

return common.SetServiceDefaults(&svc, labels, labels, ports)
}
44 changes: 25 additions & 19 deletions operators/pkg/controller/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@
package common

import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)

const (
// DefaultServiceType is used when the stack spec is empty or not valid.
DefaultServiceType = corev1.ServiceTypeClusterIP
)
// SetServiceDefaults updates the service with the provided defaults if they are not already set.
func SetServiceDefaults(
nkvoll marked this conversation as resolved.
Show resolved Hide resolved
svc *v1.Service,
defaultLabels map[string]string,
defaultSelector map[string]string,
defaultPorts []v1.ServicePort,
) *v1.Service {
if svc.ObjectMeta.Labels == nil {
svc.ObjectMeta.Labels = defaultLabels
} else {
// add our labels, but don't overwrite user labels
for k, v := range defaultLabels {
if _, ok := svc.ObjectMeta.Labels[k]; !ok {
svc.ObjectMeta.Labels[k] = v
}
}
}

// GetServiceType obtains the service type from a string.
// There's no validation here since it is assumed to happen at the API level.
func GetServiceType(s string) corev1.ServiceType {
switch corev1.ServiceType(s) {
case corev1.ServiceTypeNodePort:
return corev1.ServiceTypeNodePort
case corev1.ServiceTypeLoadBalancer:
return corev1.ServiceTypeLoadBalancer
default:
return DefaultServiceType
if svc.Spec.Selector == nil {
svc.Spec.Selector = defaultSelector
}

if svc.Spec.Ports == nil {
svc.Spec.Ports = defaultPorts
}
}

// hasNodePort returns for a given service type, if the service ports have a NodePort or not.
func hasNodePort(svcType corev1.ServiceType) bool {
return svcType == corev1.ServiceTypeNodePort || svcType == corev1.ServiceTypeLoadBalancer
return svc
}
7 changes: 6 additions & 1 deletion operators/pkg/controller/common/service_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

var log = logf.Log.WithName("stack-controller")
var log = logf.Log.WithName("common")

func ReconcileService(
c k8s.Client,
Expand Down Expand Up @@ -64,3 +64,8 @@ func needsUpdate(expected *corev1.Service, reconciled *corev1.Service) bool {
}
return !reflect.DeepEqual(expected.Spec, reconciled.Spec)
}

// hasNodePort returns for a given service type, if the service ports have a NodePort or not.
func hasNodePort(svcType corev1.ServiceType) bool {
return svcType == corev1.ServiceTypeNodePort || svcType == corev1.ServiceTypeLoadBalancer
}
Loading