Skip to content

Commit

Permalink
Centralize labels definition in settings package
Browse files Browse the repository at this point in the history
ref: #1025
[noissue]
  • Loading branch information
git-hyagi committed Oct 9, 2023
1 parent 5068719 commit fbf041e
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 214 deletions.
3 changes: 1 addition & 2 deletions .ci/scripts/backup_and_restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ kubectl delete -f config/samples/$CUSTOM_RESOURCE
# deleting resources that have no operator owerReference to better validate that
# restore controller will recreate them instead of "reusing" the older ones
kubectl delete secrets --all
kubectl delete pvc -l "app.kubernetes.io/component=storage"
kubectl delete pvc -l "owner=pulp-dev"
kubectl delete pvc -l pulp_cr=galaxy-example
kubectl wait --for=delete --timeout=300s -f config/samples/$CUSTOM_RESOURCE

kubectl apply -f config/samples/$RESTORE_RESOURCE
Expand Down
13 changes: 1 addition & 12 deletions controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,11 @@ func (d *CommonDeployment) setReplicas(pulp repomanagerpulpprojectorgv1beta2.Pul

// setLabels defines the pod and deployment labels
func (d *CommonDeployment) setLabels(pulp repomanagerpulpprojectorgv1beta2.Pulp, pulpcoreType settings.PulpcoreType) {
pulpType := strings.ToLower(string(pulpcoreType))
d.podLabels = map[string]string{
"app.kubernetes.io/name": pulp.Spec.DeploymentType + "-" + pulpType,
"app.kubernetes.io/instance": pulp.Spec.DeploymentType + "-" + pulpType + "-" + pulp.Name,
"app.kubernetes.io/component": pulpType,
"app.kubernetes.io/part-of": pulp.Spec.DeploymentType,
"app.kubernetes.io/managed-by": pulp.Spec.DeploymentType + "-operator",
"app": "pulp-" + pulpType,
"pulp_cr": pulp.Name,
}

d.podLabels = settings.PulpcoreLabels(pulp, strings.ToLower(string(pulpcoreType)))
d.deploymentLabels = make(map[string]string)
for k, v := range d.podLabels {
d.deploymentLabels[k] = v
}
d.deploymentLabels["owner"] = "pulp-dev"
}

// setAffinity defines the affinity rules
Expand Down
10 changes: 1 addition & 9 deletions controllers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,7 @@ func IngressDefaults(resources any, plugins []IngressPlugin) (*netv1.Ingress, er
},
}
}
labels := map[string]string{
"app.kubernetes.io/name": "ingress",
"app.kubernetes.io/instance": "ingress-" + pulp.Name,
"app.kubernetes.io/component": "ingress",
"app.kubernetes.io/part-of": pulp.Spec.DeploymentType,
"app.kubernetes.io/managed-by": pulp.Spec.DeploymentType + "-operator",
"pulp_cr": pulp.Name,
"owner": "pulp-dev",
}
labels := settings.CommonLabels(*pulp)

ingress := &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Expand Down
11 changes: 2 additions & 9 deletions controllers/ocp/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ func PulpRouteController(resources controllers.FunctionResources, restClient res
conditionType := cases.Title(language.English, cases.Compact).String(pulp.Spec.DeploymentType) + "-Route-Ready"

podList := &corev1.PodList{}
labels := map[string]string{
"app.kubernetes.io/part-of": pulp.Spec.DeploymentType,
"app.kubernetes.io/managed-by": pulp.Spec.DeploymentType + "-operator",
"app.kubernetes.io/instance": pulp.Spec.DeploymentType + "-worker-" + pulp.Name,
"app.kubernetes.io/component": "worker",
}
labels := settings.PulpcoreLabels(*pulp, "worker")
listOpts := []client.ListOption{
client.InNamespace(pulp.Namespace),
client.MatchingLabels(labels),
Expand Down Expand Up @@ -227,9 +222,7 @@ func PulpRouteObject(ctx context.Context, resources controllers.FunctionResource
annotation["haproxy.router.openshift.io/rewrite-target"] = p.Rewrite
}

labels := map[string]string{}
labels["pulp_cr"] = resources.Pulp.Name
labels["owner"] = "pulp-dev"
labels := settings.CommonLabels(*resources.Pulp)
for k, v := range resources.Pulp.Spec.RouteLabels {
labels[k] = v
}
Expand Down
12 changes: 8 additions & 4 deletions controllers/ocp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ import (
)

// CreateRHOperatorPullSecret creates a default secret called redhat-operators-pull-secret
func CreateRHOperatorPullSecret(r client.Client, ctx context.Context, namespace, pulpName string) error {
func CreateRHOperatorPullSecret(r client.Client, ctx context.Context, pulp repomanagerpulpprojectorgv1beta2.Pulp) error {
log := logr.Logger{}

pulpName := pulp.Name
namespace := pulp.Namespace

secretName := settings.RedHatOperatorPullSecret(pulpName)
// Get redhat-operators-pull-secret
defaultSecret := &corev1.Secret{}
Expand All @@ -47,6 +50,7 @@ func CreateRHOperatorPullSecret(r client.Client, ctx context.Context, namespace,
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: namespace,
Labels: settings.CommonLabels(pulp),
},
StringData: map[string]string{
"operator": "pulp",
Expand All @@ -68,13 +72,13 @@ func CreateEmptyConfigMap(r client.Client, scheme *runtime.Scheme, ctx context.C
configMap := &corev1.ConfigMap{}
err := r.Get(ctx, types.NamespacedName{Name: configMapName, Namespace: pulp.Namespace}, configMap)

labels := settings.CommonLabels(*pulp)
labels["config.openshift.io/inject-trusted-cabundle"] = "true"
expected_cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: pulp.Namespace,
Labels: map[string]string{
"config.openshift.io/inject-trusted-cabundle": "true",
},
Labels: labels,
},
Data: map[string]string{},
}
Expand Down
41 changes: 12 additions & 29 deletions controllers/repo_manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,14 @@ func (r *RepoManagerReconciler) pulpApiController(ctx context.Context, pulp *rep
func fileStoragePVC(resources controllers.FunctionResources) client.Object {

pulp := resources.Pulp
labels := settings.CommonLabels(*pulp)
labels["app.kubernetes.io/component"] = "storage"
// Define the new PVC
pvc := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: settings.DefaultPulpFileStorage(pulp.Name),
Namespace: pulp.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": pulp.Spec.DeploymentType + "-storage",
"app.kubernetes.io/instance": pulp.Spec.DeploymentType + "-storage-" + pulp.Name,
"app.kubernetes.io/component": "storage",
"app.kubernetes.io/part-of": pulp.Spec.DeploymentType,
"app.kubernetes.io/managed-by": pulp.Spec.DeploymentType + "-operator",
},
Labels: labels,
},
Spec: corev1.PersistentVolumeClaimSpec{
Resources: corev1.ResourceRequirements{
Expand All @@ -249,34 +245,29 @@ func fileStoragePVC(resources controllers.FunctionResources) client.Object {
// serviceForAPI returns a service object for pulp-api
func serviceForAPI(resources controllers.FunctionResources) client.Object {
pulp := resources.Pulp
svc := serviceAPIObject(pulp.Name, pulp.Namespace, pulp.Spec.DeploymentType)
svc := serviceAPIObject(*pulp)

// Set Pulp instance as the owner and controller
ctrl.SetControllerReference(pulp, svc, resources.Scheme)
return svc
}

func serviceAPIObject(name, namespace, deployment_type string) *corev1.Service {
func serviceAPIObject(pulp repomanagerpulpprojectorgv1beta2.Pulp) *corev1.Service {
name := pulp.Name
namespace := pulp.Namespace

return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: settings.ApiService(name),
Namespace: namespace,
Labels: map[string]string{
"app.kubernetes.io/name": deployment_type + "-api",
"app.kubernetes.io/instance": deployment_type + "-api-" + name,
"app.kubernetes.io/component": "api",
"app.kubernetes.io/part-of": deployment_type,
"app.kubernetes.io/managed-by": deployment_type + "-operator",
"app": "pulp-api",
"pulp_cr": name,
},
Labels: settings.PulpcoreLabels(pulp, "api"),
},
Spec: serviceAPISpec(name, namespace, deployment_type),
Spec: serviceAPISpec(pulp),
}
}

// api service spec
func serviceAPISpec(name, namespace, deployment_type string) corev1.ServiceSpec {
func serviceAPISpec(pulp repomanagerpulpprojectorgv1beta2.Pulp) corev1.ServiceSpec {

serviceInternalTrafficPolicyCluster := corev1.ServiceInternalTrafficPolicyType("Cluster")
ipFamilyPolicyType := corev1.IPFamilyPolicyType("SingleStack")
Expand All @@ -295,15 +286,7 @@ func serviceAPISpec(name, namespace, deployment_type string) corev1.ServiceSpec
Protocol: servicePortProto,
TargetPort: targetPort,
}},
Selector: map[string]string{
"app.kubernetes.io/name": deployment_type + "-api",
"app.kubernetes.io/instance": deployment_type + "-api-" + name,
"app.kubernetes.io/component": "api",
"app.kubernetes.io/part-of": deployment_type,
"app.kubernetes.io/managed-by": deployment_type + "-operator",
"app": "pulp-api",
"pulp_cr": name,
},
Selector: settings.PulpcoreLabels(pulp, "api"),
SessionAffinity: serviceAffinity,
Type: serviceType,
}
Expand Down
30 changes: 8 additions & 22 deletions controllers/repo_manager/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,28 @@ func (r *RepoManagerReconciler) pulpContentController(ctx context.Context, pulp
func serviceForContent(resources controllers.FunctionResources) client.Object {

pulp := resources.Pulp
svc := serviceContentObject(pulp.Name, pulp.Namespace, pulp.Spec.DeploymentType)
svc := serviceContentObject(*pulp)

// Set Pulp instance as the owner and controller
ctrl.SetControllerReference(pulp, svc, resources.Scheme)
return svc
}

func serviceContentObject(name, namespace, deployment_type string) *corev1.Service {
func serviceContentObject(pulp repomanagerpulpprojectorgv1beta2.Pulp) *corev1.Service {
name := pulp.Name
namespace := pulp.Namespace
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: settings.ContentService(name),
Namespace: namespace,
Labels: map[string]string{
"app.kubernetes.io/name": deployment_type + "-content",
"app.kubernetes.io/instance": deployment_type + "-content-" + name,
"app.kubernetes.io/component": "content",
"app.kubernetes.io/part-of": deployment_type,
"app.kubernetes.io/managed-by": deployment_type + "-operator",
"app": "pulp-content",
"pulp_cr": name,
},
Labels: settings.PulpcoreLabels(pulp, "content"),
},
Spec: serviceContentSpec(name, namespace, deployment_type),
Spec: serviceContentSpec(pulp),
}
}

// content service spec
func serviceContentSpec(name, namespace, deployment_type string) corev1.ServiceSpec {
func serviceContentSpec(pulp repomanagerpulpprojectorgv1beta2.Pulp) corev1.ServiceSpec {

serviceInternalTrafficPolicyCluster := corev1.ServiceInternalTrafficPolicyType("Cluster")
ipFamilyPolicyType := corev1.IPFamilyPolicyType("SingleStack")
Expand All @@ -139,15 +133,7 @@ func serviceContentSpec(name, namespace, deployment_type string) corev1.ServiceS
Protocol: servicePortProto,
TargetPort: targetPort,
}},
Selector: map[string]string{
"app.kubernetes.io/name": deployment_type + "-content",
"app.kubernetes.io/instance": deployment_type + "-content-" + name,
"app.kubernetes.io/component": "content",
"app.kubernetes.io/part-of": deployment_type,
"app.kubernetes.io/managed-by": deployment_type + "-operator",
"app": "pulp-content",
"pulp_cr": name,
},
Selector: settings.PulpcoreLabels(pulp, "content"),
SessionAffinity: serviceAffinity,
Type: serviceType,
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/repo_manager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (r *RepoManagerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
isOpenShift, _ := controllers.IsOpenShift()
if isOpenShift {
log.V(1).Info("Running on OpenShift cluster")
if err := pulp_ocp.CreateRHOperatorPullSecret(r.Client, ctx, req.NamespacedName.Namespace, pulp.Name); err != nil {
if err := pulp_ocp.CreateRHOperatorPullSecret(r.Client, ctx, *pulp); err != nil {
return ctrl.Result{}, err
}
}
Expand Down
18 changes: 9 additions & 9 deletions controllers/repo_manager/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ var _ = Describe("Pulp controller", Ordered, func() {
format.MaxLength = 0

labelsSts := map[string]string{
"app.kubernetes.io/name": "postgres",
"app.kubernetes.io/instance": "postgres-" + PulpName,
"app.kubernetes.io/name": OperatorType + "-database",
"app.kubernetes.io/instance": OperatorType + "-database-" + PulpName,
"app.kubernetes.io/component": "database",
"app.kubernetes.io/part-of": OperatorType,
"app.kubernetes.io/managed-by": PulpName,
"owner": "pulp-dev",
"app": "postgresql",
"app.kubernetes.io/managed-by": OperatorType + "-operator",
"app": "pulp-database",
"pulp_cr": PulpName,
}

Expand Down Expand Up @@ -529,12 +528,13 @@ var _ = Describe("Pulp controller", Ordered, func() {
Name: StsName,
Namespace: PulpNamespace,
Labels: map[string]string{
"app.kubernetes.io/name": "postgres",
"app.kubernetes.io/instance": "postgres-" + PulpName,
"app.kubernetes.io/name": OperatorType + "-database",
"app.kubernetes.io/instance": OperatorType + "-database-" + PulpName,
"app.kubernetes.io/component": "database",
"app.kubernetes.io/part-of": OperatorType,
"app.kubernetes.io/managed-by": PulpName,
"owner": "pulp-dev",
"app.kubernetes.io/managed-by": OperatorType + "-operator",
"app": "pulp-database",
"pulp_cr": PulpName,
},
},
Spec: appsv1.StatefulSetSpec{
Expand Down
27 changes: 5 additions & 22 deletions controllers/repo_manager/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,7 @@ func statefulSetForDatabase(m *repomanagerpulpprojectorgv1beta2.Pulp) *appsv1.St
ObjectMeta: metav1.ObjectMeta{
Name: settings.DefaultDBStatefulSet(m.Name),
Namespace: m.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "postgres",
"app.kubernetes.io/instance": "postgres-" + m.Name,
"app.kubernetes.io/component": "database",
"app.kubernetes.io/part-of": m.Spec.DeploymentType,
"app.kubernetes.io/managed-by": m.Spec.DeploymentType + "-operator",
"owner": "pulp-dev",
},
Labels: ls,
},
Spec: appsv1.StatefulSetSpec{
Replicas: &replicas,
Expand Down Expand Up @@ -469,16 +462,7 @@ func statefulSetForDatabase(m *repomanagerpulpprojectorgv1beta2.Pulp) *appsv1.St
// labelsForDatabase returns the labels for selecting the resources
// belonging to the given pulp CR name.
func labelsForDatabase(m *repomanagerpulpprojectorgv1beta2.Pulp) map[string]string {
return map[string]string{
"app.kubernetes.io/name": "postgres",
"app.kubernetes.io/instance": "postgres-" + m.Name,
"app.kubernetes.io/component": "database",
"app.kubernetes.io/part-of": m.Spec.DeploymentType,
"app.kubernetes.io/managed-by": m.Spec.DeploymentType + "-operator",
"owner": "pulp-dev",
"app": "postgresql",
"pulp_cr": m.Name,
}
return settings.PulpcoreLabels(*m, "database")
}

// serviceForDatabase returns a service object for postgres pods
Expand All @@ -495,6 +479,7 @@ func serviceForDatabase(m *repomanagerpulpprojectorgv1beta2.Pulp) *corev1.Servic
ObjectMeta: metav1.ObjectMeta{
Name: settings.DBService(m.Name),
Namespace: m.Namespace,
Labels: labelsForDatabase(m),
},
Spec: corev1.ServiceSpec{
ClusterIP: "None",
Expand All @@ -507,10 +492,7 @@ func serviceForDatabase(m *repomanagerpulpprojectorgv1beta2.Pulp) *corev1.Servic
Protocol: servicePortProto,
TargetPort: targetPort,
}},
Selector: map[string]string{
"app": "postgresql",
"pulp_cr": m.Name,
},
Selector: labelsForDatabase(m),
SessionAffinity: serviceAffinity,
Type: serviceType,
},
Expand All @@ -531,6 +513,7 @@ func databaseConfigSecret(m *repomanagerpulpprojectorgv1beta2.Pulp) *corev1.Secr
ObjectMeta: metav1.ObjectMeta{
Name: settings.DefaultDBSecret(m.Name),
Namespace: m.Namespace,
Labels: settings.CommonLabels(*m),
},
StringData: map[string]string{
"password": createPwd(32),
Expand Down
7 changes: 1 addition & 6 deletions controllers/repo_manager/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ func (r *RepoManagerReconciler) pulpIngressController(ctx context.Context, pulp
conditionType := cases.Title(language.English, cases.Compact).String(pulp.Spec.DeploymentType) + "-Ingress-Ready"

podList := &corev1.PodList{}
labels := map[string]string{
"app.kubernetes.io/part-of": pulp.Spec.DeploymentType,
"app.kubernetes.io/managed-by": pulp.Spec.DeploymentType + "-operator",
"app.kubernetes.io/instance": pulp.Spec.DeploymentType + "-content-" + pulp.Name,
"app.kubernetes.io/component": "content",
}
labels := settings.PulpcoreLabels(*pulp, "content")
listOpts := []client.ListOption{
client.InNamespace(pulp.Namespace),
client.MatchingLabels(labels),
Expand Down
Loading

0 comments on commit fbf041e

Please sign in to comment.