Skip to content

Commit

Permalink
chore: move workload related defs to api module and remove cyclic dep…
Browse files Browse the repository at this point in the history
…endency (#2356)

This PR removes the dependency of the `api` module on the `k8sutils`
module.
Since `k8sutils` obviously needs to depend on `api`, this causes cyclic
dependency between modules and can sometimes manifest as errors when
attempting new imports in code.
  • Loading branch information
blumamir authored Feb 2, 2025
1 parent 18962fa commit 9098447
Show file tree
Hide file tree
Showing 54 changed files with 322 additions and 312 deletions.
2 changes: 1 addition & 1 deletion api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SHELL = /usr/bin/env bash -o pipefail
TOOLS := $(PWD)/../.tools

.PHONY: all
all: generate manifests generate-client sync-helm-crd
all: generate manifests generate-client sync-helm-crd docgen

##@ General

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.23.0

require (
github.com/odigos-io/odigos/common v0.0.0
github.com/odigos-io/odigos/k8sutils v0.0.0
github.com/stretchr/testify v1.10.0
k8s.io/api v0.32.1
k8s.io/apimachinery v0.32.1
Expand Down Expand Up @@ -72,7 +71,4 @@ require (
sigs.k8s.io/yaml v1.4.0
)

replace (
github.com/odigos-io/odigos/common => ../common
github.com/odigos-io/odigos/k8sutils => ../k8sutils
)
replace github.com/odigos-io/odigos/common => ../common
2 changes: 0 additions & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
35 changes: 35 additions & 0 deletions api/k8sconsts/podworkload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package k8sconsts

// 1. the pascal case representation of the workload kind
// it is used in k8s api objects as the `Kind` field.
type WorkloadKind string

const (
WorkloadKindDeployment WorkloadKind = "Deployment"
WorkloadKindDaemonSet WorkloadKind = "DaemonSet"
WorkloadKindStatefulSet WorkloadKind = "StatefulSet"
WorkloadKindNamespace WorkloadKind = "Namespace"
)

// 2. the lower case representation of the workload kind
// is used in odigos with the object name for instrumentation config and runtime details
type WorkloadKindLowerCase string

const (
WorkloadKindLowerCaseDeployment WorkloadKindLowerCase = "deployment"
WorkloadKindLowerCaseDaemonSet WorkloadKindLowerCase = "daemonset"
WorkloadKindLowerCaseStatefulSet WorkloadKindLowerCase = "statefulset"
WorkloadKindLowerCaseNamespace WorkloadKindLowerCase = "namespace"
)

// PodWorkload represents the higher-level controller managing a specific Pod within a Kubernetes cluster.
// It contains essential details about the controller such as its Name, Namespace, and Kind.
// 'Kind' refers to the type of controller, which can be a Deployment, StatefulSet, or DaemonSet.
// This struct is useful for identifying and interacting with the overarching entity
// that governs the lifecycle and behavior of a Pod, especially in contexts where
// understanding the relationship between a Pod and its controlling workload is crucial.
type PodWorkload struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Kind WorkloadKind `json:"kind"`
}
4 changes: 2 additions & 2 deletions api/odigos/v1alpha1/instrumentationrule_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package v1alpha1

import (
"github.com/odigos-io/odigos/api/k8sconsts"
"github.com/odigos-io/odigos/api/odigos/v1alpha1/instrumentationrules"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ type InstrumentationRuleSpec struct {
Disabled bool `json:"disabled,omitempty"`

// An array of workload objects (name, namespace, kind) to which the rule should be applied. If not specified, the rule will be applied to all workloads. empty array will render the rule inactive.
Workloads *[]workload.PodWorkload `json:"workloads,omitempty"`
Workloads *[]k8sconsts.PodWorkload `json:"workloads,omitempty"`

// For fine grained control, the user can specify the instrumentation library to use.
// One can specify same rule for multiple languages and libraries at the same time.
Expand Down
9 changes: 4 additions & 5 deletions api/odigos/v1alpha1/source_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/odigos-io/odigos/api/k8sconsts"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
)

var ErrorTooManySources = errors.New("too many Sources found for workload")
Expand All @@ -49,7 +48,7 @@ type SourceSpec struct {
// Workload represents the workload or namespace to be instrumented.
// This field is required upon creation and cannot be modified.
// +kubebuilder:validation:Required
Workload workload.PodWorkload `json:"workload"`
Workload k8sconsts.PodWorkload `json:"workload"`
// DisableInstrumentation excludes this workload from auto-instrumentation.
// +kubebuilder:validation:Optional
DisableInstrumentation bool `json:"disableInstrumentation,omitempty"`
Expand Down Expand Up @@ -116,11 +115,11 @@ func GetSources(ctx context.Context, kubeClient client.Client, obj client.Object
workloadSources := &WorkloadSources{}

namespace := obj.GetNamespace()
if len(namespace) == 0 && obj.GetObjectKind().GroupVersionKind().Kind == string(workload.WorkloadKindNamespace) {
if len(namespace) == 0 && obj.GetObjectKind().GroupVersionKind().Kind == string(k8sconsts.WorkloadKindNamespace) {
namespace = obj.GetName()
}

if obj.GetObjectKind().GroupVersionKind().Kind != string(workload.WorkloadKindNamespace) {
if obj.GetObjectKind().GroupVersionKind().Kind != string(k8sconsts.WorkloadKindNamespace) {
sourceList := SourceList{}
selector := labels.SelectorFromSet(labels.Set{
k8sconsts.WorkloadNameLabel: obj.GetName(),
Expand All @@ -143,7 +142,7 @@ func GetSources(ctx context.Context, kubeClient client.Client, obj client.Object
namespaceSelector := labels.SelectorFromSet(labels.Set{
k8sconsts.WorkloadNameLabel: namespace,
k8sconsts.WorkloadNamespaceLabel: namespace,
k8sconsts.WorkloadKindLabel: string(workload.WorkloadKindNamespace),
k8sconsts.WorkloadKindLabel: string(k8sconsts.WorkloadKindNamespace),
})
err = kubeClient.List(ctx, &namespaceSourceList, &client.ListOptions{LabelSelector: namespaceSelector}, client.InNamespace(namespace))
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions api/odigos/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/api-reference/odigos.io.v1alpha1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ and whether the values are considered a guaranteed API.</p>
<code>workloads</code> <B>[Required]</B>
</td>
<td>
<code>[]github.com/odigos-io/odigos/k8sutils/pkg/workload.PodWorkload</code>
<code>[]github.com/odigos-io/odigos/api/k8sconsts.PodWorkload</code>
</td>
<td>
<p>An array of workload objects (name, namespace, kind) to which the rule should be applied. If not specified, the rule will be applied to all workloads. empty array will render the rule inactive.</p>
Expand Down Expand Up @@ -2471,7 +2471,7 @@ This means the destination will receive data only from sources labeled with &quo
<code>workload</code> <B>[Required]</B>
</td>
<td>
<a href="https://pkg.go.dev/github.com/odigos-io/odigos/k8sutils/pkg/workload#PodWorkload"><code>workload.PodWorkload</code></a>
<code>k8sconsts.PodWorkload</code>
</td>
<td>
<p>Workload represents the workload or namespace to be instrumented.
Expand Down
10 changes: 5 additions & 5 deletions frontend/services/collector_metrics/node_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"sync"

"github.com/odigos-io/odigos/api/k8sconsts"
"github.com/odigos-io/odigos/frontend/services/common"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
Expand Down Expand Up @@ -187,13 +187,13 @@ func metricAttributesToSourceID(attrs pcommon.Map) (common.SourceID, error) {
return common.SourceID{}, errors.New("namespace not found")
}

var kind workload.WorkloadKind
var kind k8sconsts.WorkloadKind
if _, ok := attrs.Get(K8SDeploymentNameKey); ok {
kind = workload.WorkloadKindDeployment
kind = k8sconsts.WorkloadKindDeployment
} else if _, ok := attrs.Get(K8SStatefulSetNameKey); ok {
kind = workload.WorkloadKindStatefulSet
kind = k8sconsts.WorkloadKindStatefulSet
} else if _, ok := attrs.Get(K8SDaemonSetNameKey); ok {
kind = workload.WorkloadKindDaemonSet
kind = k8sconsts.WorkloadKindDaemonSet
} else {
return common.SourceID{}, errors.New("kind not found")
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/services/common/source.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package common

import "github.com/odigos-io/odigos/k8sutils/pkg/workload"
import "github.com/odigos-io/odigos/api/k8sconsts"

type SourceID struct {
// combination of namespace, kind and name is unique
Name string `json:"name"`
Kind workload.WorkloadKind `json:"kind"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Kind k8sconsts.WorkloadKind `json:"kind"`
Namespace string `json:"namespace"`
}
17 changes: 8 additions & 9 deletions frontend/services/instrumentationrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/odigos-io/odigos/frontend/graph/model"
"github.com/odigos-io/odigos/frontend/kube"
"github.com/odigos-io/odigos/k8sutils/pkg/env"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -79,12 +78,12 @@ func UpdateInstrumentationRule(ctx context.Context, id string, input model.Instr
existingRule.Spec.Notes = *input.Notes
existingRule.Spec.Disabled = *input.Disabled
if input.Workloads != nil {
convertedWorkloads := make([]workload.PodWorkload, len(input.Workloads))
convertedWorkloads := make([]k8sconsts.PodWorkload, len(input.Workloads))
for i, w := range input.Workloads {
convertedWorkloads[i] = workload.PodWorkload{
convertedWorkloads[i] = k8sconsts.PodWorkload{
Name: w.Name,
Namespace: w.Namespace,
Kind: workload.WorkloadKind(w.Kind),
Kind: k8sconsts.WorkloadKind(w.Kind),
}
}
existingRule.Spec.Workloads = &convertedWorkloads
Expand Down Expand Up @@ -191,14 +190,14 @@ func CreateInstrumentationRule(ctx context.Context, input model.InstrumentationR
notes := *input.Notes
disabled := *input.Disabled

var workloads *[]workload.PodWorkload
var workloads *[]k8sconsts.PodWorkload
if input.Workloads != nil {
convertedWorkloads := make([]workload.PodWorkload, len(input.Workloads))
convertedWorkloads := make([]k8sconsts.PodWorkload, len(input.Workloads))
for i, w := range input.Workloads {
convertedWorkloads[i] = workload.PodWorkload{
convertedWorkloads[i] = k8sconsts.PodWorkload{
Name: w.Name,
Namespace: w.Namespace,
Kind: workload.WorkloadKind(w.Kind),
Kind: k8sconsts.WorkloadKind(w.Kind),
}
}
workloads = &convertedWorkloads
Expand Down Expand Up @@ -293,7 +292,7 @@ func handleNotFoundError(err error, id string, entity string) error {
}

// Converts Workloads to GraphQL-compatible format
func convertWorkloads(workloads *[]workload.PodWorkload) []*model.PodWorkload {
func convertWorkloads(workloads *[]k8sconsts.PodWorkload) []*model.PodWorkload {
var gqlWorkloads []*model.PodWorkload
if workloads != nil {
for _, w := range *workloads {
Expand Down
5 changes: 2 additions & 3 deletions frontend/services/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/odigos-io/odigos/frontend/graph/model"
"github.com/odigos-io/odigos/frontend/kube"
"github.com/odigos-io/odigos/k8sutils/pkg/client"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -270,10 +269,10 @@ func CreateSourceCRD(ctx context.Context, nsName string, workloadName string, wo
GenerateName: "source-",
},
Spec: v1alpha1.SourceSpec{
Workload: workload.PodWorkload{
Workload: k8sconsts.PodWorkload{
Namespace: nsName,
Name: workloadName,
Kind: workload.WorkloadKind(workloadKind),
Kind: k8sconsts.WorkloadKind(workloadKind),
},
},
}
Expand Down
13 changes: 7 additions & 6 deletions instrumentor/controllers/deleteinstrumentationconfig/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/odigos-io/odigos/api/k8sconsts"
odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/common/consts"
sourceutils "github.com/odigos-io/odigos/k8sutils/pkg/source"
Expand Down Expand Up @@ -84,10 +85,10 @@ func removeReportedNameAnnotation(ctx context.Context, kubeClient client.Client,

func syncNamespaceWorkloads(ctx context.Context, k8sClient client.Client, req ctrl.Request) error {
var err error
for _, kind := range []workload.WorkloadKind{
workload.WorkloadKindDaemonSet,
workload.WorkloadKindDeployment,
workload.WorkloadKindStatefulSet,
for _, kind := range []k8sconsts.WorkloadKind{
k8sconsts.WorkloadKindDaemonSet,
k8sconsts.WorkloadKindDeployment,
k8sconsts.WorkloadKindStatefulSet,
} {
err = errors.Join(err, listAndSyncWorkloadList(ctx, k8sClient, req, kind))
}
Expand All @@ -97,7 +98,7 @@ func syncNamespaceWorkloads(ctx context.Context, k8sClient client.Client, req ct
func listAndSyncWorkloadList(ctx context.Context,
k8sClient client.Client,
req ctrl.Request,
kind workload.WorkloadKind) error {
kind k8sconsts.WorkloadKind) error {
logger := log.FromContext(ctx)
logger.V(2).Info("Uninstrumenting workloads for Namespace Source", "name", req.Name, "namespace", req.Namespace, "kind", kind)

Expand Down Expand Up @@ -133,7 +134,7 @@ func listAndSyncWorkloadList(ctx context.Context,
return err
}

func syncGenericWorkloadListToNs(ctx context.Context, c client.Client, kind workload.WorkloadKind, key client.ObjectKey) error {
func syncGenericWorkloadListToNs(ctx context.Context, c client.Client, kind k8sconsts.WorkloadKind, key client.ObjectKey) error {
// it is very important that we make the changes based on a fresh copy of the workload object
// if a list operation pulled in state and is now slowly iterating over it, we might be working with stale data
freshWorkloadCopy := workload.ClientObjectFromWorkloadKind(kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *SourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
"excluded", v1alpha1.IsDisabledSource(source),
"terminating", k8sutils.IsTerminating(source))

if source.Spec.Workload.Kind == workload.WorkloadKindNamespace {
if source.Spec.Workload.Kind == k8sconsts.WorkloadKindNamespace {
err = errors.Join(err, syncNamespaceWorkloads(ctx, r.Client, req))
} else {
// This is a Source for a specific workload, not an entire namespace
Expand Down
Loading

0 comments on commit 9098447

Please sign in to comment.