Skip to content

Commit

Permalink
refactor: use term workload instead of resource more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 29, 2024
1 parent bb9d1db commit ad1e5cd
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 190 deletions.
193 changes: 97 additions & 96 deletions internal/controller/dash0_controller.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions internal/controller/dash0_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var _ = Describe("Dash0 Controller", func() {
})).To(Succeed())
}

By("Cleanup the Dash0 resource instance")
By("Cleanup the Dash0 custom resource instance")
if dash0CustomResource := loadDash0CustomResourceIfItExists(ctx); dash0CustomResource != nil {
// We want to delete the custom resource, but we need to remove the finalizer first, otherwise the first
// reconcile of the next test case will actually run the finalizers.
Expand All @@ -101,13 +101,13 @@ var _ = Describe("Dash0 Controller", func() {
Expect(allEvents.Items).To(BeEmpty())
})

It("should successfully run the first reconcile (no modifiable resources exist)", func() {
It("should successfully run the first reconcile (no modifiable workloads exist)", func() {
By("Trigger reconcile request")
triggerReconcileRequest(ctx, reconciler, "")
verifyDash0ResourceIsAvailable(ctx)
})

It("should successfully run multiple reconciles (no modifiable resources exist)", func() {
It("should successfully run multiple reconciles (no modifiable workloads exist)", func() {
triggerReconcileRequest(ctx, reconciler, "First reconcile request")

firstAvailableStatusCondition := verifyDash0ResourceIsAvailable(ctx)
Expand Down Expand Up @@ -172,8 +172,8 @@ var _ = Describe("Dash0 Controller", func() {
clientset,
namespace,
name,
"Dash0 instrumentation of this resource by the controller has not been successful. Error message: Dash0 cannot"+
" instrument the existing job test-namespace/job1, since the this type of resource is immutable.",
"Dash0 instrumentation of this workload by the controller has not been successful. Error message: Dash0 cannot"+
" instrument the existing job test-namespace/job1, since the this type of workload is immutable.",
)
VerifyImmutableJobCouldNotBeModified(GetJob(ctx, k8sClient, namespace, name))
})
Expand Down Expand Up @@ -303,7 +303,7 @@ var _ = Describe("Dash0 Controller", func() {
clientset,
namespace,
name,
"The controller's attempt to remove the Dash0 instrumentation from this resource has not been successful. Error message: Dash0 cannot remove the instrumentation from the existing job test-namespace/job2, since the this type of resource is immutable.",
"The controller's attempt to remove the Dash0 instrumentation from this workload has not been successful. Error message: Dash0 cannot remove the instrumentation from the existing job test-namespace/job2, since the this type of workload is immutable.",
)
VerifyModifiedJob(GetJob(ctx, k8sClient, namespace, name), BasicInstrumentedPodSpecExpectations)
})
Expand All @@ -326,7 +326,7 @@ var _ = Describe("Dash0 Controller", func() {

triggerReconcileRequest(ctx, reconciler, "Trigger a reconcile request to attempt to revert the instrumented job")

VerifyAlreadyNotInstrumented(ctx, clientset, namespace, name, "Dash0 instrumentation was not present on this resource, no modification by the controller has been necessary.")
VerifyAlreadyNotInstrumented(ctx, clientset, namespace, name, "Dash0 instrumentation was not present on this workload, no modification by the controller has been necessary.")
VerifyUnmodifiedJob(GetJob(ctx, k8sClient, namespace, name))
})

Expand Down
32 changes: 16 additions & 16 deletions internal/controller/instrumentable_workloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type instrumentableWorkload interface {
getObjectMeta() *metav1.ObjectMeta
getTypeMeta() *metav1.TypeMeta
getKind() string
asRuntimeObject() runtime.Object
asClientObject() client.Object
instrument(*logr.Logger) bool
Expand All @@ -29,14 +29,14 @@ type cronJobWorkload struct {
}

func (w *cronJobWorkload) getObjectMeta() *metav1.ObjectMeta { return &w.cronJob.ObjectMeta }
func (w *cronJobWorkload) getTypeMeta() *metav1.TypeMeta { return &w.cronJob.TypeMeta }
func (w *cronJobWorkload) getKind() string { return "CronJob" }
func (w *cronJobWorkload) asRuntimeObject() runtime.Object { return w.cronJob }
func (w *cronJobWorkload) asClientObject() client.Object { return w.cronJob }
func (w *cronJobWorkload) instrument(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).ModifyCronJob(w.cronJob)
return newWorkloadModifier(w.versions, logger).ModifyCronJob(w.cronJob)
}
func (w *cronJobWorkload) revert(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).RevertCronJob(w.cronJob)
return newWorkloadModifier(w.versions, logger).RevertCronJob(w.cronJob)
}

type daemonSetWorkload struct {
Expand All @@ -45,14 +45,14 @@ type daemonSetWorkload struct {
}

func (w *daemonSetWorkload) getObjectMeta() *metav1.ObjectMeta { return &w.daemonSet.ObjectMeta }
func (w *daemonSetWorkload) getTypeMeta() *metav1.TypeMeta { return &w.daemonSet.TypeMeta }
func (w *daemonSetWorkload) getKind() string { return "DaemonSet" }
func (w *daemonSetWorkload) asRuntimeObject() runtime.Object { return w.daemonSet }
func (w *daemonSetWorkload) asClientObject() client.Object { return w.daemonSet }
func (w *daemonSetWorkload) instrument(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).ModifyDaemonSet(w.daemonSet)
return newWorkloadModifier(w.versions, logger).ModifyDaemonSet(w.daemonSet)
}
func (w *daemonSetWorkload) revert(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).RevertDaemonSet(w.daemonSet)
return newWorkloadModifier(w.versions, logger).RevertDaemonSet(w.daemonSet)
}

type deploymentWorkload struct {
Expand All @@ -61,14 +61,14 @@ type deploymentWorkload struct {
}

func (w *deploymentWorkload) getObjectMeta() *metav1.ObjectMeta { return &w.deployment.ObjectMeta }
func (w *deploymentWorkload) getTypeMeta() *metav1.TypeMeta { return &w.deployment.TypeMeta }
func (w *deploymentWorkload) getKind() string { return "Deployment" }
func (w *deploymentWorkload) asRuntimeObject() runtime.Object { return w.deployment }
func (w *deploymentWorkload) asClientObject() client.Object { return w.deployment }
func (w *deploymentWorkload) instrument(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).ModifyDeployment(w.deployment)
return newWorkloadModifier(w.versions, logger).ModifyDeployment(w.deployment)
}
func (w *deploymentWorkload) revert(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).RevertDeployment(w.deployment)
return newWorkloadModifier(w.versions, logger).RevertDeployment(w.deployment)
}

type replicaSetWorkload struct {
Expand All @@ -77,14 +77,14 @@ type replicaSetWorkload struct {
}

func (w *replicaSetWorkload) getObjectMeta() *metav1.ObjectMeta { return &w.replicaSet.ObjectMeta }
func (w *replicaSetWorkload) getTypeMeta() *metav1.TypeMeta { return &w.replicaSet.TypeMeta }
func (w *replicaSetWorkload) getKind() string { return "ReplicaSet" }
func (w *replicaSetWorkload) asRuntimeObject() runtime.Object { return w.replicaSet }
func (w *replicaSetWorkload) asClientObject() client.Object { return w.replicaSet }
func (w *replicaSetWorkload) instrument(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).ModifyReplicaSet(w.replicaSet)
return newWorkloadModifier(w.versions, logger).ModifyReplicaSet(w.replicaSet)
}
func (w *replicaSetWorkload) revert(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).RevertReplicaSet(w.replicaSet)
return newWorkloadModifier(w.versions, logger).RevertReplicaSet(w.replicaSet)
}

type statefulSetWorkload struct {
Expand All @@ -93,12 +93,12 @@ type statefulSetWorkload struct {
}

func (w *statefulSetWorkload) getObjectMeta() *metav1.ObjectMeta { return &w.statefulSet.ObjectMeta }
func (w *statefulSetWorkload) getTypeMeta() *metav1.TypeMeta { return &w.statefulSet.TypeMeta }
func (w *statefulSetWorkload) getKind() string { return "StatefulSet" }
func (w *statefulSetWorkload) asRuntimeObject() runtime.Object { return w.statefulSet }
func (w *statefulSetWorkload) asClientObject() client.Object { return w.statefulSet }
func (w *statefulSetWorkload) instrument(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).ModifyStatefulSet(w.statefulSet)
return newWorkloadModifier(w.versions, logger).ModifyStatefulSet(w.statefulSet)
}
func (w *statefulSetWorkload) revert(logger *logr.Logger) bool {
return newResourceModifier(w.versions, logger).RevertStatefulSet(w.statefulSet)
return newWorkloadModifier(w.versions, logger).RevertStatefulSet(w.statefulSet)
}
12 changes: 6 additions & 6 deletions internal/util/k8sevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func QueueSuccessfulInstrumentationEvent(eventRecorder record.EventRecorder, res
resource,
corev1.EventTypeNormal,
string(ReasonSuccessfulInstrumentation),
fmt.Sprintf("Dash0 instrumentation of this resource by the %s has been successful.", eventSource),
fmt.Sprintf("Dash0 instrumentation of this workload by the %s has been successful.", eventSource),
)
}

Expand All @@ -25,7 +25,7 @@ func QueueAlreadyInstrumentedEvent(eventRecorder record.EventRecorder, resource
resource,
corev1.EventTypeNormal,
string(ReasonAlreadyInstrumented),
fmt.Sprintf("Dash0 instrumentation was already present on this resource, no modification by the %s is necessary.", eventSource),
fmt.Sprintf("Dash0 instrumentation was already present on this workload, no modification by the %s is necessary.", eventSource),
)
}

Expand All @@ -34,7 +34,7 @@ func QueueFailedInstrumentationEvent(eventRecorder record.EventRecorder, resourc
resource,
corev1.EventTypeWarning,
string(ReasonFailedInstrumentation),
fmt.Sprintf("Dash0 instrumentation of this resource by the %s has not been successful. Error message: %s", eventSource, err.Error()),
fmt.Sprintf("Dash0 instrumentation of this workload by the %s has not been successful. Error message: %s", eventSource, err.Error()),
)
}

Expand All @@ -43,7 +43,7 @@ func QueueSuccessfulUninstrumentationEvent(eventRecorder record.EventRecorder, r
resource,
corev1.EventTypeNormal,
string(ReasonSuccessfulUninstrumentation),
fmt.Sprintf("The %s successfully removed the Dash0 instrumentation from this resource.", eventSource),
fmt.Sprintf("The %s successfully removed the Dash0 instrumentation from this workload.", eventSource),
)
}

Expand All @@ -52,7 +52,7 @@ func QueueAlreadyNotInstrumentedEvent(eventRecorder record.EventRecorder, resour
resource,
corev1.EventTypeNormal,
string(ReasonAlreadyNotInstrumented),
fmt.Sprintf("Dash0 instrumentation was not present on this resource, no modification by the %s has been necessary.", eventSource),
fmt.Sprintf("Dash0 instrumentation was not present on this workload, no modification by the %s has been necessary.", eventSource),
)
}

Expand All @@ -61,6 +61,6 @@ func QueueFailedUninstrumentationEvent(eventRecorder record.EventRecorder, resou
resource,
corev1.EventTypeWarning,
string(ReasonFailedUninstrumentation),
fmt.Sprintf("The %s's attempt to remove the Dash0 instrumentation from this resource has not been successful. Error message: %s", eventSource, err.Error()),
fmt.Sprintf("The %s's attempt to remove the Dash0 instrumentation from this workload has not been successful. Error message: %s", eventSource, err.Error()),
)
}
20 changes: 10 additions & 10 deletions internal/webhook/dash0_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/dash0hq/dash0-operator/internal/k8sresources"
"github.com/dash0hq/dash0-operator/internal/util"
"github.com/dash0hq/dash0-operator/internal/workloads"
)

type Handler struct {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (h *Handler) handleCronJob(
if failed {
return responseIfFailed
}
hasBeenModified := h.newResourceModifier(logger).ModifyCronJob(cronJob)
hasBeenModified := h.newWorkloadModifier(logger).ModifyCronJob(cronJob)
return h.postProcess(request, cronJob, hasBeenModified, logger)
}

Expand All @@ -123,7 +123,7 @@ func (h *Handler) handleDaemonSet(
if failed {
return responseIfFailed
}
hasBeenModified := h.newResourceModifier(logger).ModifyDaemonSet(daemonSet)
hasBeenModified := h.newWorkloadModifier(logger).ModifyDaemonSet(daemonSet)
return h.postProcess(request, daemonSet, hasBeenModified, logger)
}

Expand All @@ -137,7 +137,7 @@ func (h *Handler) handleDeployment(
if failed {
return responseIfFailed
}
hasBeenModified := h.newResourceModifier(logger).ModifyDeployment(deployment)
hasBeenModified := h.newWorkloadModifier(logger).ModifyDeployment(deployment)
return h.postProcess(request, deployment, hasBeenModified, logger)
}

Expand All @@ -151,7 +151,7 @@ func (h *Handler) handleJob(
if failed {
return responseIfFailed
}
hasBeenModified := h.newResourceModifier(logger).ModifyJob(job)
hasBeenModified := h.newWorkloadModifier(logger).ModifyJob(job)
return h.postProcess(request, job, hasBeenModified, logger)
}

Expand All @@ -165,7 +165,7 @@ func (h *Handler) handleReplicaSet(
if failed {
return responseIfFailed
}
hasBeenModified := h.newResourceModifier(logger).ModifyReplicaSet(replicaSet)
hasBeenModified := h.newWorkloadModifier(logger).ModifyReplicaSet(replicaSet)
return h.postProcess(request, replicaSet, hasBeenModified, logger)
}

Expand All @@ -179,7 +179,7 @@ func (h *Handler) handleStatefulSet(
if failed {
return responseIfFailed
}
hasBeenModified := h.newResourceModifier(logger).ModifyStatefulSet(statefulSet)
hasBeenModified := h.newWorkloadModifier(logger).ModifyStatefulSet(statefulSet)
return h.postProcess(request, statefulSet, hasBeenModified, logger)
}

Expand Down Expand Up @@ -214,13 +214,13 @@ func (h *Handler) postProcess(
return admission.Allowed(fmt.Errorf("error when marshalling modfied resource to JSON: %w", err).Error())
}

logger.Info("The webhook has added Dash0 instrumentation to the resource.")
logger.Info("The webhook has added Dash0 instrumentation to the workload.")
util.QueueSuccessfulInstrumentationEvent(h.Recorder, resource, "webhook")
return admission.PatchResponseFromRaw(request.Object.Raw, marshalled)
}

func (h *Handler) newResourceModifier(logger *logr.Logger) *k8sresources.ResourceModifier {
return k8sresources.NewResourceModifier(
func (h *Handler) newWorkloadModifier(logger *logr.Logger) *workloads.ResourceModifier {
return workloads.NewResourceModifier(
util.InstrumentationMetadata{
Versions: h.Versions,
InstrumentedBy: "webhook",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
// SPDX-License-Identifier: Apache-2.0

package k8sresources
package workloads

import (
"fmt"
Expand Down
Loading

0 comments on commit ad1e5cd

Please sign in to comment.