Skip to content

Commit

Permalink
metering labels: apicast labels
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed May 27, 2020
1 parent ced020a commit adfaffb
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 54 deletions.
64 changes: 15 additions & 49 deletions pkg/3scale/amp/component/apicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ func (apicast *Apicast) StagingService() *v1.Service {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-staging",
Labels: map[string]string{
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "staging",
},
Name: "apicast-staging",
Labels: apicast.Options.CommonStagingLabels,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
Expand Down Expand Up @@ -61,12 +57,8 @@ func (apicast *Apicast) ProductionService() *v1.Service {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-production",
Labels: map[string]string{
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "production",
},
Name: "apicast-production",
Labels: apicast.Options.CommonProductionLabels,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
Expand All @@ -92,12 +84,8 @@ func (apicast *Apicast) StagingDeploymentConfig() *appsv1.DeploymentConfig {
return &appsv1.DeploymentConfig{
TypeMeta: metav1.TypeMeta{APIVersion: "apps.openshift.io/v1", Kind: "DeploymentConfig"},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-staging",
Labels: map[string]string{
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "staging",
},
Name: "apicast-staging",
Labels: apicast.Options.CommonStagingLabels,
},
Spec: appsv1.DeploymentConfigSpec{
Replicas: apicast.Options.StagingReplicas,
Expand Down Expand Up @@ -140,12 +128,7 @@ func (apicast *Apicast) StagingDeploymentConfig() *appsv1.DeploymentConfig {
},
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"deploymentConfig": "apicast-staging",
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "staging",
},
Labels: apicast.Options.StagingPodTemplateLabels,
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "9421",
Expand Down Expand Up @@ -205,12 +188,8 @@ func (apicast *Apicast) ProductionDeploymentConfig() *appsv1.DeploymentConfig {
return &appsv1.DeploymentConfig{
TypeMeta: metav1.TypeMeta{APIVersion: "apps.openshift.io/v1", Kind: "DeploymentConfig"},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-production",
Labels: map[string]string{
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "production",
},
Name: "apicast-production",
Labels: apicast.Options.CommonProductionLabels,
},
Spec: appsv1.DeploymentConfigSpec{
Replicas: apicast.Options.ProductionReplicas,
Expand Down Expand Up @@ -254,12 +233,7 @@ func (apicast *Apicast) ProductionDeploymentConfig() *appsv1.DeploymentConfig {
},
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"deploymentConfig": "apicast-production",
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "production",
},
Labels: apicast.Options.ProductionPodTemplateLabels,
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "9421",
Expand Down Expand Up @@ -368,7 +342,7 @@ func (apicast *Apicast) EnvironmentConfigMap() *v1.ConfigMap {
},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-environment",
Labels: map[string]string{"threescale_component": "apicast", "app": apicast.Options.AppLabel},
Labels: apicast.Options.CommonLabels,
},
Data: map[string]string{
"APICAST_MANAGEMENT_API": apicast.Options.ManagementAPI,
Expand All @@ -385,12 +359,8 @@ func (apicast *Apicast) StagingPodDisruptionBudget() *v1beta1.PodDisruptionBudge
APIVersion: "policy/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-staging",
Labels: map[string]string{
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "staging",
},
Name: "apicast-staging",
Labels: apicast.Options.CommonStagingLabels,
},
Spec: v1beta1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
Expand All @@ -408,12 +378,8 @@ func (apicast *Apicast) ProductionPodDisruptionBudget() *v1beta1.PodDisruptionBu
APIVersion: "policy/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-production",
Labels: map[string]string{
"app": apicast.Options.AppLabel,
"threescale_component": "apicast",
"threescale_component_element": "production",
},
Name: "apicast-production",
Labels: apicast.Options.CommonProductionLabels,
},
Spec: v1beta1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
Expand Down
6 changes: 5 additions & 1 deletion pkg/3scale/amp/component/apicast_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

type ApicastOptions struct {
AppLabel string `validate:"required"`
ManagementAPI string `validate:"required"`
OpenSSLVerify string `validate:"required"`
ResponseCodes string `validate:"required"`
Expand All @@ -16,6 +15,11 @@ type ApicastOptions struct {
StagingResourceRequirements v1.ResourceRequirements `validate:"-"`
ProductionReplicas int32
StagingReplicas int32
CommonLabels map[string]string `validate:"required"`
CommonStagingLabels map[string]string `validate:"required"`
CommonProductionLabels map[string]string `validate:"required"`
StagingPodTemplateLabels map[string]string `validate:"required"`
ProductionPodTemplateLabels map[string]string `validate:"required"`
}

func NewApicastOptions() *ApicastOptions {
Expand Down
58 changes: 56 additions & 2 deletions pkg/3scale/amp/operator/apicast_options_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
"github.com/3scale/3scale-operator/pkg/3scale/amp/product"
"github.com/3scale/3scale-operator/pkg/helper"

appsv1alpha1 "github.com/3scale/3scale-operator/pkg/apis/apps/v1alpha1"
v1 "k8s.io/api/core/v1"
)
Expand All @@ -22,16 +24,25 @@ func NewApicastOptionsProvider(apimanager *appsv1alpha1.APIManager) *ApicastOpti
}

func (a *ApicastOptionsProvider) GetApicastOptions() (*component.ApicastOptions, error) {
a.apicastOptions.AppLabel = *a.apimanager.Spec.AppLabel
imageOpts, err := NewAmpImagesOptionsProvider(a.apimanager).GetAmpImagesOptions()
if err != nil {
return nil, err
}

a.apicastOptions.ManagementAPI = *a.apimanager.Spec.Apicast.ApicastManagementAPI
a.apicastOptions.ImageTag = product.ThreescaleRelease
a.apicastOptions.OpenSSLVerify = strconv.FormatBool(*a.apimanager.Spec.Apicast.OpenSSLVerify)
a.apicastOptions.ResponseCodes = strconv.FormatBool(*a.apimanager.Spec.Apicast.IncludeResponseCodes)
a.apicastOptions.CommonLabels = a.commonLabels()
a.apicastOptions.CommonStagingLabels = a.commonStagingLabels()
a.apicastOptions.CommonProductionLabels = a.commonProductionLabels()
a.apicastOptions.StagingPodTemplateLabels = a.stagingPodTemplateLabels(imageOpts.ApicastImage)
a.apicastOptions.ProductionPodTemplateLabels = a.productionPodTemplateLabels(imageOpts.ApicastImage)

a.setResourceRequirementsOptions()
a.setReplicas()

err := a.apicastOptions.Validate()
err = a.apicastOptions.Validate()
return a.apicastOptions, err
}

Expand All @@ -49,3 +60,46 @@ func (a *ApicastOptionsProvider) setReplicas() {
a.apicastOptions.ProductionReplicas = int32(*a.apimanager.Spec.Apicast.ProductionSpec.Replicas)
a.apicastOptions.StagingReplicas = int32(*a.apimanager.Spec.Apicast.StagingSpec.Replicas)
}

func (a *ApicastOptionsProvider) commonLabels() map[string]string {
return map[string]string{
"app": *a.apimanager.Spec.AppLabel,
"threescale_component": "apicast",
}
}

func (a *ApicastOptionsProvider) commonStagingLabels() map[string]string {
labels := a.commonLabels()
labels["threescale_component_element"] = "staging"
return labels
}

func (a *ApicastOptionsProvider) commonProductionLabels() map[string]string {
labels := a.commonLabels()
labels["threescale_component_element"] = "production"
return labels
}

func (a *ApicastOptionsProvider) stagingPodTemplateLabels(image string) map[string]string {
labels := helper.MeteringLabels("apicast-staging", helper.ParseVersion(image), helper.ApplicationType)

for k, v := range a.commonStagingLabels() {
labels[k] = v
}

labels["deploymentConfig"] = "apicast-staging"

return labels
}

func (a *ApicastOptionsProvider) productionPodTemplateLabels(image string) map[string]string {
labels := helper.MeteringLabels("apicast-production", helper.ParseVersion(image), helper.ApplicationType)

for k, v := range a.commonProductionLabels() {
labels[k] = v
}

labels["deploymentConfig"] = "apicast-production"

return labels
}
45 changes: 44 additions & 1 deletion pkg/3scale/amp/operator/apicast_options_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@ const (
responseCodes = true
)

var (
testCommonLabels = map[string]string{
"app": appLabel,
"threescale_component": "apicast",
}
testStagingLabels = map[string]string{
"app": appLabel,
"threescale_component": "apicast",
"threescale_component_element": "staging",
}
testProductionLabels = map[string]string{
"app": appLabel,
"threescale_component": "apicast",
"threescale_component_element": "production",
}
testStagingPodLabels = map[string]string{
"app": appLabel,
"threescale_component": "apicast",
"threescale_component_element": "staging",
"com.redhat.component-name": "apicast-staging",
"com.redhat.component-type": "application",
"com.redhat.component-version": "nightly",
"com.redhat.product-name": "3scale",
"com.redhat.product-version": "master",
"deploymentConfig": "apicast-staging",
}
testProductionPodLabels = map[string]string{
"app": appLabel,
"threescale_component": "apicast",
"threescale_component_element": "production",
"com.redhat.component-name": "apicast-production",
"com.redhat.component-type": "application",
"com.redhat.component-version": "nightly",
"com.redhat.product-name": "3scale",
"com.redhat.product-version": "master",
"deploymentConfig": "apicast-production",
}
)

func basicApimanagerTestApicastOptions() *appsv1alpha1.APIManager {
tmpApicastManagementAPI := apicastManagementAPI
tmpOpenSSLVerify := openSSLVerify
Expand All @@ -46,7 +85,6 @@ func basicApimanagerTestApicastOptions() *appsv1alpha1.APIManager {

func defaultApicastOptions() *component.ApicastOptions {
return &component.ApicastOptions{
AppLabel: appLabel,
ManagementAPI: apicastManagementAPI,
OpenSSLVerify: strconv.FormatBool(openSSLVerify),
ResponseCodes: strconv.FormatBool(responseCodes),
Expand All @@ -55,6 +93,11 @@ func defaultApicastOptions() *component.ApicastOptions {
StagingResourceRequirements: component.DefaultStagingResourceRequirements(),
ProductionReplicas: int32(productionReplicaCount),
StagingReplicas: int32(stagingReplicaCount),
CommonLabels: testCommonLabels,
CommonStagingLabels: testStagingLabels,
CommonProductionLabels: testProductionLabels,
StagingPodTemplateLabels: testStagingPodLabels,
ProductionPodTemplateLabels: testProductionPodLabels,
}
}

Expand Down
38 changes: 37 additions & 1 deletion pkg/3scale/amp/template/adapters/apicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func (a *Apicast) componentPDBObjects(c *component.Apicast) []common.KubernetesO

func (a *Apicast) options() (*component.ApicastOptions, error) {
ao := component.NewApicastOptions()
ao.AppLabel = "${APP_LABEL}"
ao.ManagementAPI = "${APICAST_MANAGEMENT_API}"
ao.OpenSSLVerify = "${APICAST_OPENSSL_VERIFY}"
ao.ResponseCodes = "${APICAST_RESPONSE_CODES}"
Expand All @@ -106,6 +105,43 @@ func (a *Apicast) options() (*component.ApicastOptions, error) {
ao.ProductionReplicas = 1
ao.StagingReplicas = 1

ao.CommonLabels = a.commonLabels()
ao.CommonStagingLabels = a.commonStagingLabels()
ao.CommonProductionLabels = a.commonProductionLabels()
ao.StagingPodTemplateLabels = a.stagingPodTemplateLabels()
ao.ProductionPodTemplateLabels = a.productionPodTemplateLabels()

err := ao.Validate()
return ao, err
}

func (a *Apicast) commonLabels() map[string]string {
return map[string]string{
"app": "${APP_LABEL}",
"threescale_component": "apicast",
}
}

func (a *Apicast) commonStagingLabels() map[string]string {
labels := a.commonLabels()
labels["threescale_component_element"] = "staging"
return labels
}

func (a *Apicast) commonProductionLabels() map[string]string {
labels := a.commonLabels()
labels["threescale_component_element"] = "production"
return labels
}

func (a *Apicast) stagingPodTemplateLabels() map[string]string {
labels := a.commonStagingLabels()
labels["deploymentConfig"] = "apicast-staging"
return labels
}

func (a *Apicast) productionPodTemplateLabels() map[string]string {
labels := a.commonProductionLabels()
labels["deploymentConfig"] = "apicast-production"
return labels
}
22 changes: 22 additions & 0 deletions pkg/helper/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package helper

import (
"github.com/3scale/3scale-operator/pkg/3scale/amp/product"
)

type ComponentType string

const (
ApplicationType ComponentType = "application"
InfrastructureType ComponentType = "infrastructure"
)

func MeteringLabels(componentName, componentVersion string, componentType ComponentType) map[string]string {
return map[string]string{
"com.redhat.product-name": "3scale",
"com.redhat.component-name": componentName,
"com.redhat.product-version": product.ThreescaleRelease,
"com.redhat.component-version": componentVersion,
"com.redhat.component-type": string(componentType),
}
}
Loading

0 comments on commit adfaffb

Please sign in to comment.