Skip to content

Commit

Permalink
Moved label creation to function (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
isala404 committed Jul 17, 2021
1 parent b655784 commit fccf4bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
29 changes: 6 additions & 23 deletions operator/controllers/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ func (r *FlowTestReconciler) provisionResource(ctx context.Context) error {
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-configmap", flowTest.Spec.ReferencePod.Name),
Name: fmt.Sprintf("%s-configmap", flowTest.ObjectMeta.UID),
Namespace: flowTest.Spec.ReferencePod.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "pod-simulation",
"app.kubernetes.io/managed-by": "rancher-logging-explorer",
"app.kubernetes.io/created-by": "logging-plumber",
"loggingplumber.isala.me/flowtest-uuid": string(flowTest.ObjectMeta.UID),
"loggingplumber.isala.me/flowtest": flowTest.ObjectMeta.Name,
},
Labels: GetLabels("pod-simulation", &flowTest),
},
Immutable: &Immutable,
BinaryData: map[string][]byte{"simulation.log": logOutput.Bytes()},
Expand All @@ -51,7 +45,6 @@ func (r *FlowTestReconciler) provisionResource(ctx context.Context) error {

logger.V(1).Info("deployed config map with simulation.log", "uuid", configMap.ObjectMeta.UID)

// TODO: set the name based on flowtest uuid
var referencePod v1.Pod
if err := r.Get(ctx, types.NamespacedName{
Namespace: flowTest.Spec.ReferencePod.Namespace,
Expand Down Expand Up @@ -82,7 +75,7 @@ func (r *FlowTestReconciler) provisionResource(ctx context.Context) error {
Name: "config-volume",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{Name: fmt.Sprintf("%s-configmap", flowTest.Spec.ReferencePod.Name)},
LocalObjectReference: v1.LocalObjectReference{Name: fmt.Sprintf("%s-configmap", flowTest.ObjectMeta.UID)},
},
},
},
Expand All @@ -91,13 +84,7 @@ func (r *FlowTestReconciler) provisionResource(ctx context.Context) error {
},
}

extraLabels := map[string]string{}

extraLabels["app.kubernetes.io/name"] = "pod-simulation"
extraLabels["app.kubernetes.io/managed-by"] = "rancher-logging-explorer"
extraLabels["app.kubernetes.io/created-by"] = "logging-plumber"
extraLabels["loggingplumber.isala.me/flowtest-uuid"] = string(flowTest.ObjectMeta.UID)
extraLabels["loggingplumber.isala.me/flowtest"] = flowTest.ObjectMeta.Name
extraLabels := GetLabels("pod-simulation", &flowTest)

simulationPod.ObjectMeta.Labels = referencePod.ObjectMeta.Labels

Expand All @@ -123,12 +110,8 @@ func (r *FlowTestReconciler) provisionResource(ctx context.Context) error {
ObjectMeta: metav1.ObjectMeta{
Name: "logging-plumber-log-aggregator",
Namespace: flowTest.ObjectMeta.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "logging-plumber-log-aggregator",
"app.kubernetes.io/managed-by": "rancher-logging-explorer",
"app.kubernetes.io/created-by": "logging-plumber",
"loggingplumber.isala.me/component": "log-aggregator",
},
Labels: GetLabels("logging-plumber-log-aggregator", nil,
map[string]string{"loggingplumber.isala.me/component": "log-aggregator"}),
},
Spec: v1.PodSpec{
Containers: []v1.Container{{
Expand Down
51 changes: 23 additions & 28 deletions operator/controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ func flowTemplates(flow flowv1beta1.Flow, flowTest loggingplumberv1alpha1.FlowTe
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-test", flow.ObjectMeta.Name),
Namespace: flow.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": flow.ObjectMeta.Name,
"app.kubernetes.io/managed-by": "rancher-logging-explorer",
"app.kubernetes.io/created-by": "logging-plumber",
"loggingplumber.isala.me/flowtest-uuid": string(flowTest.ObjectMeta.UID),
"loggingplumber.isala.me/flowtest": flowTest.ObjectMeta.Name,
},
Labels: GetLabels(flow.ObjectMeta.Name, &flowTest),
},
Spec: flowv1beta1.FlowSpec{
LocalOutputRefs: nil,
Expand All @@ -46,13 +40,7 @@ func flowTemplates(flow flowv1beta1.Flow, flowTest loggingplumberv1alpha1.FlowTe
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-test", flow.ObjectMeta.Name),
Namespace: flow.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": flow.ObjectMeta.Name,
"app.kubernetes.io/managed-by": "rancher-logging-explorer",
"app.kubernetes.io/created-by": "logging-plumber",
"loggingplumber.isala.me/flowtest-uuid": string(flowTest.ObjectMeta.UID),
"loggingplumber.isala.me/flowtest": flowTest.ObjectMeta.Name,
},
Labels: GetLabels(flow.ObjectMeta.Name, &flowTest),
},
Spec: flowv1beta1.OutputSpec{
HTTPOutput: &output.HTTPOutputConfig{
Expand All @@ -77,13 +65,7 @@ func clusterFlowTemplates(flow flowv1beta1.ClusterFlow, flowTest loggingplumberv
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-test", flow.ObjectMeta.Name),
Namespace: flowTest.Spec.ReferenceFlow.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": flow.ObjectMeta.Name,
"app.kubernetes.io/managed-by": "rancher-logging-explorer",
"app.kubernetes.io/created-by": "logging-plumber",
"loggingplumber.isala.me/flowtest-uuid": string(flowTest.ObjectMeta.UID),
"loggingplumber.isala.me/flowtest": flowTest.ObjectMeta.Name,
},
Labels: GetLabels(flow.ObjectMeta.Name, &flowTest),
},
Spec: flowv1beta1.ClusterFlowSpec{
GlobalOutputRefs: nil,
Expand All @@ -101,13 +83,7 @@ func clusterFlowTemplates(flow flowv1beta1.ClusterFlow, flowTest loggingplumberv
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-test", flow.ObjectMeta.Name),
Namespace: flowTest.Spec.ReferenceFlow.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": flow.ObjectMeta.Name,
"app.kubernetes.io/managed-by": "rancher-logging-explorer",
"app.kubernetes.io/created-by": "logging-plumber",
"loggingplumber.isala.me/flowtest-uuid": string(flowTest.ObjectMeta.UID),
"loggingplumber.isala.me/flowtest": flowTest.ObjectMeta.Name,
},
Labels: GetLabels(flow.ObjectMeta.Name, &flowTest),
},
Spec: flowv1beta1.ClusterOutputSpec{
OutputSpec: flowv1beta1.OutputSpec{
Expand Down Expand Up @@ -182,3 +158,22 @@ func CheckIndex(ctx context.Context, indexName string) (bool, error) {

return false, nil
}

func GetLabels(name string, flowTest *loggingplumberv1alpha1.FlowTest, labelsMaps ...map[string]string) map[string]string {
labels := map[string]string{}

for _, labelsMap := range labelsMaps {
for k, v := range labelsMap {
labels[k] = v
}
}

labels["app.kubernetes.io/name"] = name
if flowTest != nil {
labels["loggingplumber.isala.me/flowtest-uuid"] = string(flowTest.ObjectMeta.UID)
labels["loggingplumber.isala.me/flowtest"] = flowTest.ObjectMeta.Name
}
labels["app.kubernetes.io/created-by"] = "logging-plumber"
labels["app.kubernetes.io/managed-by"] = "rancher-logging-explorer"
return labels
}

0 comments on commit fccf4bd

Please sign in to comment.