Skip to content

Commit

Permalink
metering labels: redis labels
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed May 27, 2020
1 parent 23bd82e commit 7b2f7c0
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 83 deletions.
93 changes: 17 additions & 76 deletions pkg/3scale/amp/component/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,29 @@ func (redis *Redis) buildDeploymentConfigTypeMeta() metav1.TypeMeta {
}
}

const ()

func (redis *Redis) buildDeploymentConfigObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: backendRedisObjectMetaName,
Labels: redis.buildLabelsForDeploymentConfigObjectMeta(),
Labels: redis.Options.BackendRedisLabels,
}
}

const (
backendRedisObjectMetaName = "backend-redis"
backendRedisDCSelectorName = backendRedisObjectMetaName
backendComponentNameLabel = "backend"
backendComponentElementLabel = "redis"
backendRedisStorageVolumeName = "backend-redis-storage"
backendRedisConfigVolumeName = "redis-config"
backendRedisConfigMapKey = "redis.conf"
backendRedisContainerName = "backend-redis"
backendRedisContainerCommand = "/opt/rh/rh-redis32/root/usr/bin/redis-server"
)

func (redis *Redis) buildLabelsForDeploymentConfigObjectMeta() map[string]string {
return map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": backendComponentNameLabel,
"threescale_component_element": backendComponentElementLabel,
}
}

func (redis *Redis) buildDeploymentConfigSpec() appsv1.DeploymentConfigSpec {
return appsv1.DeploymentConfigSpec{
Template: redis.buildPodTemplateSpec(),
Strategy: redis.buildDeploymentStrategy(),
Selector: redis.buildDeploymentConfigSelector(),
Replicas: 1, //TODO make this configurable via flag
Replicas: 1,
Triggers: redis.buildDeploymentConfigTriggers(),
}
}
Expand Down Expand Up @@ -117,17 +105,8 @@ func (redis *Redis) buildPodTemplateSpec() *v1.PodTemplateSpec {
Volumes: redis.buildPodVolumes(),
Containers: redis.buildPodContainers(),
},
ObjectMeta: redis.buildPodObjectMeta(),
}
}

func (redis *Redis) buildPodObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Labels: map[string]string{
"deploymentConfig": backendRedisDCSelectorName,
"app": redis.Options.AppLabel,
"threescale_component": backendComponentNameLabel,
"threescale_component_element": backendComponentElementLabel,
ObjectMeta: metav1.ObjectMeta{
Labels: redis.Options.BackendRedisPodTemplateLabels,
},
}
}
Expand Down Expand Up @@ -249,15 +228,7 @@ func (redis *Redis) BackendService() *v1.Service {
func (redis *Redis) buildServiceObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: "backend-redis",
Labels: redis.buildLabelsForServiceObjectMeta(),
}
}

func (redis *Redis) buildLabelsForServiceObjectMeta() map[string]string {
return map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": "backend",
"threescale_component_element": "redis",
Labels: redis.Options.BackendRedisLabels,
}
}

Expand Down Expand Up @@ -302,15 +273,7 @@ func (redis *Redis) BackendConfigMap() *v1.ConfigMap {
func (redis *Redis) buildConfigMapObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: backendRedisConfigVolumeName,
Labels: redis.buildLabelsForConfigMapObjectMeta(),
}
}

func (redis *Redis) buildLabelsForConfigMapObjectMeta() map[string]string {
return map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": "system", // TODO should also be redis???
"threescale_component_element": "redis",
Labels: redis.Options.SystemRedisLabels,
}
}

Expand Down Expand Up @@ -388,15 +351,7 @@ func (redis *Redis) BackendPVC() *v1.PersistentVolumeClaim {
func (redis *Redis) buildPVCObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: backendRedisStorageVolumeName,
Labels: redis.buildLabelsForServiceObjectMeta(),
}
}

func (redis *Redis) buildLabelsForPVCObjectMeta() map[string]string {
return map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": "backend",
"threescale_component_element": "redis",
Labels: redis.Options.BackendRedisLabels,
}
}

Expand All @@ -423,11 +378,8 @@ func (redis *Redis) buildPVCSpec() v1.PersistentVolumeClaimSpec {
func (redis *Redis) BackendImageStream() *imagev1.ImageStream {
return &imagev1.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Name: "backend-redis",
Labels: map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": "backend",
},
Name: "backend-redis",
Labels: redis.Options.BackendCommonLabels,
Annotations: map[string]string{
"openshift.io/display-name": "Backend Redis",
},
Expand Down Expand Up @@ -463,7 +415,7 @@ func (redis *Redis) SystemDeploymentConfig() *appsv1.DeploymentConfig {
},
ObjectMeta: metav1.ObjectMeta{
Name: "system-redis",
Labels: map[string]string{"threescale_component": "system", "threescale_component_element": "redis", "app": redis.Options.AppLabel},
Labels: redis.Options.SystemRedisLabels,
},
Spec: appsv1.DeploymentConfigSpec{
Strategy: appsv1.DeploymentStrategy{
Expand Down Expand Up @@ -491,7 +443,7 @@ func (redis *Redis) SystemDeploymentConfig() *appsv1.DeploymentConfig {
Selector: map[string]string{"deploymentConfig": "system-redis"},
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"threescale_component": "system", "threescale_component_element": "redis", "app": redis.Options.AppLabel, "deploymentConfig": "system-redis"},
Labels: redis.Options.SystemRedisPodTemplateLabels,
},
Spec: v1.PodSpec{
ServiceAccountName: "amp", //TODO make this configurable via flag
Expand Down Expand Up @@ -568,12 +520,8 @@ func (redis *Redis) SystemService() *v1.Service {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "system-redis",
Labels: map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": "system",
"threescale_component_element": "redis",
},
Name: "system-redis",
Labels: redis.Options.SystemRedisLabels,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
Expand All @@ -596,12 +544,8 @@ func (redis *Redis) SystemPVC() *v1.PersistentVolumeClaim {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "system-redis-storage",
Labels: map[string]string{
"threescale_component": "system",
"threescale_component_element": "redis",
"app": redis.Options.AppLabel,
},
Name: "system-redis-storage",
Labels: redis.Options.SystemRedisLabels,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{
Expand All @@ -617,11 +561,8 @@ func (redis *Redis) SystemPVC() *v1.PersistentVolumeClaim {
func (redis *Redis) SystemImageStream() *imagev1.ImageStream {
return &imagev1.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Name: "system-redis",
Labels: map[string]string{
"app": redis.Options.AppLabel,
"threescale_component": "system",
},
Name: "system-redis",
Labels: redis.Options.SystemCommonLabels,
Annotations: map[string]string{
"openshift.io/display-name": "System Redis",
},
Expand Down
8 changes: 7 additions & 1 deletion pkg/3scale/amp/component/redis_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

type RedisOptions struct {
AppLabel string `validate:"required"`
AmpRelease string `validate:"required"`
BackendImage string `validate:"required"`
BackendImageTag string `validate:"required"`
Expand All @@ -16,6 +15,13 @@ type RedisOptions struct {
BackendRedisContainerResourceRequirements *v1.ResourceRequirements `validate:"required"`
SystemRedisContainerResourceRequirements *v1.ResourceRequirements `validate:"required"`
InsecureImportPolicy *bool `validate:"required"`

SystemCommonLabels map[string]string `validate:"required"`
SystemRedisLabels map[string]string `validate:"required"`
SystemRedisPodTemplateLabels map[string]string `validate:"required"`
BackendCommonLabels map[string]string `validate:"required"`
BackendRedisLabels map[string]string `validate:"required"`
BackendRedisPodTemplateLabels map[string]string `validate:"required"`
}

func NewRedisOptions() *RedisOptions {
Expand Down
61 changes: 59 additions & 2 deletions pkg/3scale/amp/operator/redis_options_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
"github.com/3scale/3scale-operator/pkg/3scale/amp/product"
appsv1alpha1 "github.com/3scale/3scale-operator/pkg/apis/apps/v1alpha1"
"github.com/3scale/3scale-operator/pkg/helper"

v1 "k8s.io/api/core/v1"
)

Expand All @@ -20,11 +22,9 @@ func NewRedisOptionsProvider(apimanager *appsv1alpha1.APIManager) *RedisOptionsP
}

func (r *RedisOptionsProvider) GetRedisOptions() (*component.RedisOptions, error) {
r.options.AppLabel = *r.apimanager.Spec.AppLabel
r.options.AmpRelease = product.ThreescaleRelease
r.options.BackendImageTag = product.ThreescaleRelease
r.options.SystemImageTag = product.ThreescaleRelease

r.options.InsecureImportPolicy = r.apimanager.Spec.ImageStreamTagImportInsecure

r.options.BackendImage = BackendRedisImageURL()
Expand All @@ -37,6 +37,13 @@ func (r *RedisOptionsProvider) GetRedisOptions() (*component.RedisOptions, error
r.options.SystemImage = *r.apimanager.Spec.System.RedisImage
}

r.options.SystemCommonLabels = r.systemCommonLabels()
r.options.SystemRedisLabels = r.systemRedisLabels()
r.options.SystemRedisPodTemplateLabels = r.systemRedisPodTemplateLabels(r.options.SystemImage)
r.options.BackendCommonLabels = r.backendCommonLabels()
r.options.BackendRedisLabels = r.backendRedisLabels()
r.options.BackendRedisPodTemplateLabels = r.backendRedisPodTemplateLabels(r.options.BackendImage)

r.setResourceRequirementsOptions()

err := r.options.Validate()
Expand All @@ -52,3 +59,53 @@ func (r *RedisOptionsProvider) setResourceRequirementsOptions() {
r.options.SystemRedisContainerResourceRequirements = &v1.ResourceRequirements{}
}
}

func (r *RedisOptionsProvider) systemCommonLabels() map[string]string {
return map[string]string{
"app": *r.apimanager.Spec.AppLabel,
"threescale_component": "system",
}
}

func (r *RedisOptionsProvider) backendCommonLabels() map[string]string {
return map[string]string{
"app": *r.apimanager.Spec.AppLabel,
"threescale_component": "backend",
}
}

func (r *RedisOptionsProvider) systemRedisLabels() map[string]string {
labels := r.systemCommonLabels()
labels["threescale_component_element"] = "redis"
return labels
}

func (r *RedisOptionsProvider) backendRedisLabels() map[string]string {
labels := r.backendCommonLabels()
labels["threescale_component_element"] = "redis"
return labels
}

func (r *RedisOptionsProvider) systemRedisPodTemplateLabels(image string) map[string]string {
labels := helper.MeteringLabels("system-redis", helper.ParseVersion(image), helper.ApplicationType)

for k, v := range r.systemRedisLabels() {
labels[k] = v
}

labels["deploymentConfig"] = "system-redis"

return labels
}

func (r *RedisOptionsProvider) backendRedisPodTemplateLabels(image string) map[string]string {
labels := helper.MeteringLabels("backend-redis", helper.ParseVersion(image), helper.ApplicationType)

for k, v := range r.backendRedisLabels() {
labels[k] = v
}

labels["deploymentConfig"] = "backend-redis"

return labels
}
Loading

0 comments on commit 7b2f7c0

Please sign in to comment.