Skip to content

Commit

Permalink
Changes to enable passing labels and annotations to service exposing …
Browse files Browse the repository at this point in the history
…sensor through eventProtocol (#440)
  • Loading branch information
Shreyam authored and VaibhavPage committed Dec 25, 2019
1 parent cc8914d commit 78e79e0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
18 changes: 13 additions & 5 deletions controllers/sensor/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ import (

// generateServiceSpec returns a K8s service spec for the sensor
func (ctx *sensorContext) generateServiceSpec() *corev1.Service {
return &corev1.Service{
serviceSpec := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
common.LabelSensorName: ctx.sensor.Name,
LabelControllerInstanceID: ctx.controller.Config.InstanceID,
},
Labels: ctx.sensor.Spec.EventProtocol.Http.Labels,
Annotations: ctx.sensor.Spec.EventProtocol.Http.Annotations,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
Expand All @@ -51,6 +49,16 @@ func (ctx *sensorContext) generateServiceSpec() *corev1.Service {
},
},
}

// Non-overrideable labels required by sensor service
if serviceSpec.ObjectMeta.Labels == nil {
serviceSpec.ObjectMeta.Labels = map[string]string{}
}

serviceSpec.ObjectMeta.Labels[common.LabelSensorName] = ctx.sensor.Name
serviceSpec.ObjectMeta.Labels[LabelControllerInstanceID] = ctx.controller.Config.InstanceID

return serviceSpec
}

// serviceBuilder builds a new service that exposes sensor.
Expand Down
20 changes: 20 additions & 0 deletions controllers/sensor/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ func TestResource_BuildService(t *testing.T) {
assert.NotEmpty(t, service.Annotations[common.AnnotationResourceSpecHash])
}

func TestResource_BuildServiceWithLabelsAnnotations(t *testing.T) {
controller := getController()
sensorCopy := sensorObj.DeepCopy()

sensorCopy.Spec.EventProtocol.Http.Labels = map[string]string{}
sensorCopy.Spec.EventProtocol.Http.Labels["test-label"] = "label1"
sensorCopy.Spec.EventProtocol.Http.Annotations = map[string]string{}
sensorCopy.Spec.EventProtocol.Http.Annotations["test-annotation"] = "annotation1"

opctx := newSensorContext(sensorCopy, controller)
service, err := opctx.serviceBuilder()
assert.Nil(t, err)
assert.NotNil(t, service)
assert.NotEmpty(t, service.Annotations[common.AnnotationResourceSpecHash])
assert.NotNil(t, service.ObjectMeta.Labels)
assert.NotNil(t, service.ObjectMeta.Annotations)
assert.Equal(t, service.ObjectMeta.Labels["test-label"], "label1")
assert.Equal(t, service.ObjectMeta.Annotations["test-annotation"], "annotation1")
}

func TestResource_BuildDeployment(t *testing.T) {
controller := getController()
opctx := newSensorContext(sensorObj.DeepCopy(), controller)
Expand Down
16 changes: 15 additions & 1 deletion pkg/apis/common/deepcopy_generated.go

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

6 changes: 6 additions & 0 deletions pkg/apis/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ type EventProtocol struct {
type Http struct {
// Port on which server will run
Port string `json:"port" protobuf:"bytes,1,opt,name=port"`

// Labels to be set for the service generated
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`

// Annotations to be set for the service generated
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
}

// Nats contains the information required to connect to nats server and get subscriptions
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/gateway/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion pkg/apis/sensor/v1alpha1/zz_generated.deepcopy.go

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

0 comments on commit 78e79e0

Please sign in to comment.