Skip to content

Commit

Permalink
feat(controller): set base label for managed resources
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Aug 29, 2023
1 parent 15c7ff3 commit 4ceeb2d
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 64 deletions.
74 changes: 48 additions & 26 deletions api/v1alpha1/rpaasinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,56 @@ import (
)

const (
teamOwnerLabel = "rpaas.extensions.tsuru.io/team-owner"
clusterNameLabel = "rpaas.extensions.tsuru.io/cluster-name"
DefaultLabelKeyPrefix = "rpaas.extensions.tsuru.io"
RpaasOperatorInstanceNameLabelKey = DefaultLabelKeyPrefix + "/instance-name"
RpaasOperatorServiceNameLabelKey = DefaultLabelKeyPrefix + "/service-name"
RpaasOperatorPlanNameLabelKey = DefaultLabelKeyPrefix + "/plan-name"
RpaasOperatorTeamOwnerLabelKey = DefaultLabelKeyPrefix + "/team-owner"
RpaasOperatorClusterNameLabelKey = DefaultLabelKeyPrefix + "/cluster-name"

LegacyRpaasOperatorInstanceNameLabelKey = "rpaas_instance"
LegacyRpaasOperatorServiceNameLabelKey = "rpaas_service"
)

func (i *RpaasInstance) GetBaseLabels(labels map[string]string) map[string]string {
return mergeMap(map[string]string{
LegacyRpaasOperatorInstanceNameLabelKey: i.Name,
LegacyRpaasOperatorServiceNameLabelKey: i.Labels[RpaasOperatorServiceNameLabelKey],
RpaasOperatorInstanceNameLabelKey: i.Name,
RpaasOperatorServiceNameLabelKey: i.Labels[RpaasOperatorServiceNameLabelKey],
RpaasOperatorPlanNameLabelKey: i.Spec.PlanName,
RpaasOperatorTeamOwnerLabelKey: i.Labels[RpaasOperatorTeamOwnerLabelKey],
}, labels)
}

func (i *RpaasInstance) TeamOwner() string {
return i.Labels[RpaasOperatorTeamOwnerLabelKey]
}

func (i *RpaasInstance) ClusterName() string {
return i.Labels[RpaasOperatorClusterNameLabelKey]
}

func (i *RpaasInstance) SetTeamOwner(team string) {
newLabels := map[string]string{RpaasOperatorTeamOwnerLabelKey: team}
i.appendNewLabels(newLabels)
}

func (i *RpaasInstance) SetClusterName(clusterName string) {
newLabels := map[string]string{RpaasOperatorClusterNameLabelKey: clusterName}
i.appendNewLabels(newLabels)
}

func (i *RpaasInstance) BelongsToCluster(clusterName string) bool {
instanceCluster := i.Labels[RpaasOperatorClusterNameLabelKey]

if instanceCluster == "" {
return false
}

return clusterName == instanceCluster
}

func (i *RpaasInstance) CertManagerRequests() (reqs []CertManager) {
if i == nil || i.Spec.DynamicCertificates == nil {
return
Expand Down Expand Up @@ -59,36 +105,12 @@ func (c *CertManager) dnsNames(i *RpaasInstance) (names []string) {
return
}

func (i *RpaasInstance) SetTeamOwner(team string) {
newLabels := map[string]string{teamOwnerLabel: team}
i.appendNewLabels(newLabels)
}

func (i *RpaasInstance) SetClusterName(clusterName string) {
newLabels := map[string]string{clusterNameLabel: clusterName}
i.appendNewLabels(newLabels)
}

func (i *RpaasInstance) BelongsToCluster(clusterName string) bool {
instanceCluster := i.Labels[clusterNameLabel]

if instanceCluster == "" {
return false
}

return clusterName == instanceCluster
}

func (i *RpaasInstance) appendNewLabels(newLabels map[string]string) {
i.Labels = mergeMap(i.Labels, newLabels)
i.Annotations = mergeMap(i.Annotations, newLabels)
i.Spec.PodTemplate.Labels = mergeMap(i.Spec.PodTemplate.Labels, newLabels)
}

func (i *RpaasInstance) TeamOwner() string {
return i.Labels[teamOwnerLabel]
}

func mergeMap(a, b map[string]string) map[string]string {
if a == nil {
return b
Expand Down
13 changes: 5 additions & 8 deletions api/v1alpha1/rpaasinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ import (
func Test_SetTeamOwner(t *testing.T) {
instance := &RpaasInstance{}
instance.SetTeamOwner("team-one")
expected := map[string]string{teamOwnerLabel: "team-one"}
expected := map[string]string{RpaasOperatorTeamOwnerLabelKey: "team-one"}
assert.Equal(t, expected, instance.Labels)
assert.Equal(t, expected, instance.Annotations)
assert.Equal(t, expected, instance.Spec.PodTemplate.Labels)

instance.SetTeamOwner("team-two")
expected = map[string]string{teamOwnerLabel: "team-two"}
expected = map[string]string{RpaasOperatorTeamOwnerLabelKey: "team-two"}
assert.Equal(t, expected, instance.Labels)
assert.Equal(t, expected, instance.Annotations)
assert.Equal(t, expected, instance.Spec.PodTemplate.Labels)
}

func Test_GetTeamOwner(t *testing.T) {
instance := &RpaasInstance{}
owner := instance.TeamOwner()
assert.Equal(t, "", owner)
assert.Equal(t, "", instance.TeamOwner())
instance.SetTeamOwner("team-one")
owner = instance.TeamOwner()
assert.Equal(t, "team-one", owner)
assert.Equal(t, "team-one", instance.TeamOwner())
}

func Test_BelongsToCluster(t *testing.T) {
Expand All @@ -43,7 +41,7 @@ func Test_BelongsToCluster(t *testing.T) {
instance = &RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterNameLabel: "cluster01",
RpaasOperatorClusterNameLabelKey: "cluster01",
},
},
}
Expand Down Expand Up @@ -104,5 +102,4 @@ func TestCertManagerRequests(t *testing.T) {
DNSNamesDefault: true,
},
}, instance.CertManagerRequests())

}
51 changes: 24 additions & 27 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,15 @@ func newCronJobForSessionTickets(instance *v1alpha1.RpaasInstance) *batchv1.Cron
Kind: "RpaasInstance",
}),
},
Labels: labelsForRpaasInstance(instance),
Labels: instance.GetBaseLabels(nil),
},
Spec: batchv1.CronJobSpec{
Schedule: minutesIntervalToSchedule(rotationInterval),
SuccessfulJobsHistoryLimit: &jobsHistoryLimit,
FailedJobsHistoryLimit: &jobsHistoryLimit,
JobTemplate: batchv1.JobTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Labels: labelsForRpaasInstance(instance),
Labels: instance.GetBaseLabels(nil),
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Expand Down Expand Up @@ -468,7 +467,7 @@ func newSecretForTLSSessionTickets(instance *v1alpha1.RpaasInstance) (*corev1.Se
ObjectMeta: metav1.ObjectMeta{
Name: secretNameForTLSSessionTickets(instance),
Namespace: instance.Namespace,
Labels: labelsForRpaasInstance(instance),
Labels: instance.GetBaseLabels(nil),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(instance, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Expand Down Expand Up @@ -756,7 +755,7 @@ func newKEDAScaledObject(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.
Kind: "RpaasInstance",
}),
},
Labels: labelsForRpaasInstance(instance),
Labels: instance.GetBaseLabels(nil),
Annotations: map[string]string{
// NOTE: allows the KEDA controller to take over the ownership of HPA resources.
"scaledobject.keda.sh/transfer-hpa-ownership": strconv.FormatBool(true),
Expand Down Expand Up @@ -841,7 +840,7 @@ func newPDB(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) (*poli
Kind: "RpaasInstance",
}),
},
Labels: labelsForRpaasInstance(instance),
Labels: instance.GetBaseLabels(nil),
},
Spec: policyv1.PodDisruptionBudgetSpec{
MaxUnavailable: &maxUnavailable,
Expand Down Expand Up @@ -1020,15 +1019,16 @@ func (r *RpaasInstanceReconciler) deleteOldConfig(ctx context.Context, instance

func newConfigMap(instance *v1alpha1.RpaasInstance, renderedTemplate string) *corev1.ConfigMap {
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(renderedTemplate)))
labels := labelsForRpaasInstance(instance)
labels["type"] = "config"
labels["instance"] = instance.Name

return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-config-%s", instance.Name, hash[:10]),
Namespace: instance.Namespace,
Labels: labels,
Labels: instance.GetBaseLabels(map[string]string{"type": "config", "instance": instance.Name}),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(instance, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Expand All @@ -1037,10 +1037,6 @@ func newConfigMap(instance *v1alpha1.RpaasInstance, renderedTemplate string) *co
}),
},
},
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
Data: map[string]string{
"nginx.conf": renderedTemplate,
},
Expand Down Expand Up @@ -1091,13 +1087,25 @@ func newNginx(instanceMergedWithFlavors *v1alpha1.RpaasInstance, plan *v1alpha1.

instanceMergedWithFlavors.Spec.Service = mergeServiceWithDNS(instanceMergedWithFlavors)

if s := instanceMergedWithFlavors.Spec.Service; s != nil {
s.Labels = instanceMergedWithFlavors.GetBaseLabels(s.Labels)
}

if ing := instanceMergedWithFlavors.Spec.Ingress; ing != nil {
ing.Labels = instanceMergedWithFlavors.GetBaseLabels(ing.Labels)
}

replicas := instanceMergedWithFlavors.Spec.Replicas
if isAutoscaleEnabled(instanceMergedWithFlavors.Spec.Autoscale) {
// NOTE: we should avoid changing the number of replicas as it's managed by HPA.
replicas = nil
}

n := &nginxv1alpha1.Nginx{
TypeMeta: metav1.TypeMeta{
Kind: "Nginx",
APIVersion: "nginx.tsuru.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: instanceMergedWithFlavors.Name,
Namespace: instanceMergedWithFlavors.Namespace,
Expand All @@ -1108,11 +1116,7 @@ func newNginx(instanceMergedWithFlavors *v1alpha1.RpaasInstance, plan *v1alpha1.
Kind: "RpaasInstance",
}),
},
Labels: labelsForRpaasInstance(instanceMergedWithFlavors),
},
TypeMeta: metav1.TypeMeta{
Kind: "Nginx",
APIVersion: "nginx.tsuru.io/v1alpha1",
Labels: instanceMergedWithFlavors.GetBaseLabels(nil),
},
Spec: nginxv1alpha1.NginxSpec{
Image: plan.Spec.Image,
Expand Down Expand Up @@ -1251,7 +1255,7 @@ func newHPA(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) *autos
Kind: "RpaasInstance",
}),
},
Labels: labelsForRpaasInstance(instance),
Labels: instance.GetBaseLabels(nil),
},
Spec: autoscalingv2.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{
Expand Down Expand Up @@ -1382,13 +1386,6 @@ func (_ rpaasMergoTransformers) Transformer(t reflect.Type) func(reflect.Value,
return nil
}

func labelsForRpaasInstance(instance *extensionsv1alpha1.RpaasInstance) map[string]string {
return map[string]string{
"rpaas.extensions.tsuru.io/instance-name": instance.Name,
"rpaas.extensions.tsuru.io/plan-name": instance.Spec.PlanName,
}
}

func nameForCronJob(name string) string {
const cronjobMaxChars = 52

Expand Down
Loading

0 comments on commit 4ceeb2d

Please sign in to comment.