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

Enable resources to be defined at top level and overridden at compone… #110

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions pkg/apis/io/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ type JaegerSpec struct {

// JaegerCommonSpec defines the common elements used in multiple other spec structs
type JaegerCommonSpec struct {
Volumes []v1.Volume `json:"volumes"`
VolumeMounts []v1.VolumeMount `json:"volumeMounts"`
Annotations map[string]string `json:"annotations,omitempty"`
Volumes []v1.Volume `json:"volumes"`
VolumeMounts []v1.VolumeMount `json:"volumeMounts"`
Annotations map[string]string `json:"annotations,omitempty"`
Resources v1.ResourceRequirements `json:"resources,omitempty"`
}

// JaegerStatus defines what is to be returned from a status query
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/io/v1alpha1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions pkg/deployment/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (a *Agent) Get() *appsv1.DaemonSet {
},
InitialDelaySeconds: 1,
},
Resources: commonSpec.Resources,
}},
},
},
Expand Down
37 changes: 37 additions & 0 deletions pkg/deployment/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)
Expand Down Expand Up @@ -81,3 +83,38 @@ func TestDaemonSetAgentAnnotations(t *testing.T) {
assert.Equal(t, "world", dep.Spec.Template.Annotations["hello"])
assert.Equal(t, "false", dep.Spec.Template.Annotations["prometheus.io/scrape"])
}

func TestDaemonSetAgentResources(t *testing.T) {
jaeger := v1alpha1.NewJaeger("TestDaemonSetAgentResources")
jaeger.Spec.Agent.Strategy = "daemonset"
jaeger.Spec.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceLimitsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceRequestsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
}
jaeger.Spec.Agent.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceLimitsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceRequestsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
}

agent := NewAgent(jaeger)
dep := agent.Get()

assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsCPU])
assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsCPU])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsMemory])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsMemory])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsEphemeralStorage])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsEphemeralStorage])
}
1 change: 1 addition & 0 deletions pkg/deployment/all-in-one.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (a *AllInOne) Get() *appsv1.Deployment {
},
InitialDelaySeconds: 1,
},
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
},
Expand Down
35 changes: 35 additions & 0 deletions pkg/deployment/all-in-one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)
Expand Down Expand Up @@ -201,3 +202,37 @@ func TestAllInOneVolumeWithSameName(t *testing.T) {
// allInOne volume is mounted
assert.Equal(t, podSpec.Volumes[0].VolumeSource.HostPath.Path, "/data2")
}

func TestAllInOneResources(t *testing.T) {
jaeger := v1alpha1.NewJaeger("TestAllInOneResources")
jaeger.Spec.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceLimitsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceRequestsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
}
jaeger.Spec.AllInOne.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceLimitsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceRequestsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
}

allinone := NewAllInOne(jaeger)
dep := allinone.Get()

assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsCPU])
assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsCPU])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsMemory])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsMemory])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsEphemeralStorage])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsEphemeralStorage])
}
1 change: 1 addition & 0 deletions pkg/deployment/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (c *Collector) Get() *appsv1.Deployment {
},
InitialDelaySeconds: 1,
},
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
},
Expand Down
35 changes: 35 additions & 0 deletions pkg/deployment/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)
Expand Down Expand Up @@ -216,3 +217,37 @@ func TestCollectorVolumeWithSameName(t *testing.T) {
// collector volume is mounted
assert.Equal(t, podSpec.Volumes[0].VolumeSource.HostPath.Path, "/data2")
}

func TestCollectorResources(t *testing.T) {
jaeger := v1alpha1.NewJaeger("TestCollectorResources")
jaeger.Spec.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceLimitsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceRequestsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
}
jaeger.Spec.Collector.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceLimitsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceRequestsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
}

collector := NewCollector(jaeger)
dep := collector.Get()

assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsCPU])
assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsCPU])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsMemory])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsMemory])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsEphemeralStorage])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsEphemeralStorage])
}
1 change: 1 addition & 0 deletions pkg/deployment/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (q *Query) Get() *appsv1.Deployment {
},
InitialDelaySeconds: 1,
},
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
},
Expand Down
35 changes: 35 additions & 0 deletions pkg/deployment/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)
Expand Down Expand Up @@ -208,3 +209,37 @@ func TestQueryVolumeWithSameName(t *testing.T) {
// query volume is mounted
assert.Equal(t, podSpec.Volumes[0].VolumeSource.HostPath.Path, "/data2")
}

func TestQueryResources(t *testing.T) {
jaeger := v1alpha1.NewJaeger("TestQueryResources")
jaeger.Spec.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceLimitsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceRequestsEphemeralStorage: *resource.NewQuantity(512, resource.DecimalSI),
},
}
jaeger.Spec.Query.Resources = v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceLimitsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceRequestsMemory: *resource.NewQuantity(123, resource.DecimalSI),
},
}

query := NewQuery(jaeger)
dep := query.Get()

assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsCPU])
assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsCPU])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsMemory])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsMemory])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Limits[v1.ResourceLimitsEphemeralStorage])
assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[v1.ResourceRequestsEphemeralStorage])
}
26 changes: 26 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Merge(commonSpecs []v1alpha1.JaegerCommonSpec) *v1alpha1.JaegerCommonSpec {
annotations := make(map[string]string)
var volumeMounts []v1.VolumeMount
var volumes []v1.Volume
resources := &v1.ResourceRequirements{}

for _, commonSpec := range commonSpecs {
// Merge annotations
Expand All @@ -53,11 +54,36 @@ func Merge(commonSpecs []v1alpha1.JaegerCommonSpec) *v1alpha1.JaegerCommonSpec {
}
volumeMounts = append(volumeMounts, commonSpec.VolumeMounts...)
volumes = append(volumes, commonSpec.Volumes...)

// Merge resources
mergeResources(resources, commonSpec.Resources)
}

return &v1alpha1.JaegerCommonSpec{
Annotations: annotations,
VolumeMounts: removeDuplicatedVolumeMounts(volumeMounts),
Volumes: removeDuplicatedVolumes(volumes),
Resources: *resources,
}
}

func mergeResources(resources *v1.ResourceRequirements, res v1.ResourceRequirements) {

for k, v := range res.Limits {
if _, ok := resources.Limits[k]; !ok {
if resources.Limits == nil {
resources.Limits = make(v1.ResourceList)
}
resources.Limits[k] = v
}
}

for k, v := range res.Requests {
if _, ok := resources.Requests[k]; !ok {
if resources.Requests == nil {
resources.Requests = make(v1.ResourceList)
}
resources.Requests[k] = v
}
}
}
51 changes: 51 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)
Expand Down Expand Up @@ -131,3 +132,53 @@ func TestMergeVolumes(t *testing.T) {
assert.Equal(t, "/data1", merged.Volumes[0].VolumeSource.HostPath.Path)
assert.Equal(t, "volume2", merged.Volumes[1].Name)
}

func TestMergeResourceLimits(t *testing.T) {
generalSpec := v1alpha1.JaegerCommonSpec{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceLimitsEphemeralStorage: *resource.NewQuantity(123, resource.DecimalSI),
},
},
}
specificSpec := v1alpha1.JaegerCommonSpec{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceLimitsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceLimitsMemory: *resource.NewQuantity(1024, resource.BinarySI),
},
},
}

merged := Merge([]v1alpha1.JaegerCommonSpec{specificSpec, generalSpec})

assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), merged.Resources.Limits[v1.ResourceLimitsCPU])
assert.Equal(t, *resource.NewQuantity(1024, resource.BinarySI), merged.Resources.Limits[v1.ResourceLimitsMemory])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), merged.Resources.Limits[v1.ResourceLimitsEphemeralStorage])
}

func TestMergeResourceRequests(t *testing.T) {
generalSpec := v1alpha1.JaegerCommonSpec{
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(1024, resource.BinarySI),
v1.ResourceRequestsEphemeralStorage: *resource.NewQuantity(123, resource.DecimalSI),
},
},
}
specificSpec := v1alpha1.JaegerCommonSpec{
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceRequestsCPU: *resource.NewQuantity(2048, resource.BinarySI),
v1.ResourceRequestsMemory: *resource.NewQuantity(1024, resource.BinarySI),
},
},
}

merged := Merge([]v1alpha1.JaegerCommonSpec{specificSpec, generalSpec})

assert.Equal(t, *resource.NewQuantity(2048, resource.BinarySI), merged.Resources.Requests[v1.ResourceRequestsCPU])
assert.Equal(t, *resource.NewQuantity(1024, resource.BinarySI), merged.Resources.Requests[v1.ResourceRequestsMemory])
assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), merged.Resources.Requests[v1.ResourceRequestsEphemeralStorage])
}