Skip to content

Commit

Permalink
Add in types for v1beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
dbudziwojskiNR committed Jan 9, 2025
1 parent d1eee49 commit 5c689ea
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha2/propagators.go → api/common/propagators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1alpha2
package common

type (
// Propagator represents the propagation type.
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha2/samplers.go → api/common/samplers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1alpha2
package common

type (
// SamplerType represents sampler type.
Expand Down
5 changes: 3 additions & 2 deletions api/v1alpha2/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha2

import (
"github.com/newrelic/k8s-agents-operator/api/common"
"reflect"

corev1 "k8s.io/api/core/v1"
Expand All @@ -41,7 +42,7 @@ type InstrumentationSpec struct {
// Values in this list will be set in the OTEL_PROPAGATORS env var.
// Enum=tracecontext;none
// +optional
Propagators []Propagator `json:"propagators,omitempty"`
Propagators []common.Propagator `json:"propagators,omitempty"`

// Sampler defines sampling configuration.
// +optional
Expand Down Expand Up @@ -94,7 +95,7 @@ type Sampler struct {
// The value will be set in the OTEL_TRACES_SAMPLER env var.
// The value can be for instance parentbased_always_on, parentbased_always_off, parentbased_traceidratio...
// +optional
Type SamplerType `json:"type,omitempty"`
Type common.SamplerType `json:"type,omitempty"`

// Argument defines sampler argument.
// The value depends on the sampler type.
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha2/zz_generated.deepcopy.go

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

182 changes: 175 additions & 7 deletions api/v1beta1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,197 @@ limitations under the License.
package v1beta1

import (
"github.com/newrelic/k8s-agents-operator/api/common"
"reflect"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// InstrumentationSpec defines the desired state of Instrumentation
type InstrumentationSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Instrumentation. Edit instrumentation_types.go to remove/update
Foo string `json:"foo,omitempty"`
// Exporter defines exporter configuration.
// +optional
Exporter `json:"exporter,omitempty"`

// Resource defines the configuration for the resource attributes, as defined by the OpenTelemetry specification.
// +optional
Resource Resource `json:"resource,omitempty"`

// Propagators defines inter-process context propagation configuration.
// Values in this list will be set in the OTEL_PROPAGATORS env var.
// Enum=tracecontext;none
// +optional
Propagators []common.Propagator `json:"propagators,omitempty"`

// Sampler defines sampling configuration.
// +optional
Sampler `json:"sampler,omitempty"`

// PodLabelSelector defines to which pods the config should be applied.
// +optional
PodLabelSelector metav1.LabelSelector `json:"podLabelSelector"`

// PodLabelSelector defines to which pods the config should be applied.
// +optional
NamespaceLabelSelector metav1.LabelSelector `json:"namespaceLabelSelector"`

// LicenseKeySecret defines where to take the licenseKeySecret from.
// it should be present in the operator namespace.
// +optional
LicenseKeySecret string `json:"licenseKeySecret,omitempty"`

// AgentConfigMap defines where to take the agent configuration from.
// it should be present in the operator namespace.
// +optional
AgentConfigMap string `json:"agentConfigMap,omitempty"`

// Agent defines configuration for agent instrumentation.
Agent Agent `json:"agent,omitempty"`

// HealthAgent defines configuration for healthAgent instrumentation.
HealthAgent HealthAgent `json:"healthAgent,omitempty"`
}

// Resource is the attributes that are added to the resource
type Resource struct {
// Attributes defines attributes that are added to the resource.
// For example environment: dev
// +optional
Attributes map[string]string `json:"resourceAttributes,omitempty"`

// AddK8sUIDAttributes defines whether K8s UID attributes should be collected (e.g. k8s.deployment.uid).
// +optional
AddK8sUIDAttributes bool `json:"addK8sUIDAttributes,omitempty"`
}

// IsEmpty is used to check if the resource is empty
func (r Resource) IsEmpty() bool {
return !r.AddK8sUIDAttributes && len(r.Attributes) == 0
}

// Exporter defines OTLP exporter configuration.
type Exporter struct {
// Endpoint is address of the collector with OTLP endpoint.
// +optional
Endpoint string `json:"endpoint,omitempty"`
}

// Sampler defines sampling configuration.
type Sampler struct {
// Type defines sampler type.
// The value will be set in the OTEL_TRACES_SAMPLER env var.
// The value can be for instance parentbased_always_on, parentbased_always_off, parentbased_traceidratio...
// +optional
Type common.SamplerType `json:"type,omitempty"`

// Argument defines sampler argument.
// The value depends on the sampler type.
// For instance for parentbased_traceidratio sampler type it is a number in range [0..1] e.g. 0.25.
// The value will be set in the OTEL_TRACES_SAMPLER_ARG env var.
// +optional
Argument string `json:"argument,omitempty"`
}

// Agent is the configuration for the agent
type Agent struct {
// Language is the language that will be instrumented.
Language string `json:"language,omitempty"`

// Image is a container image with Go SDK and auto-instrumentation.
Image string `json:"image,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
// +optional
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`

// Env defines Go specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`

// Resources describes the compute resource requirements.
// +optional
Resources corev1.ResourceRequirements `json:"resourceRequirements,omitempty"`
}

// IsEmpty is used to check if the agent is empty, excluding `.Language`
func (a *Agent) IsEmpty() bool {
return a.Image == "" &&
len(a.Env) == 0 &&
a.VolumeSizeLimit == nil &&
len(a.Resources.Limits) == 0 &&
len(a.Resources.Requests) == 0 &&
len(a.Resources.Claims) == 0
}

// IsEqual is used to compare if an agent is equal to another, excluding `.Language`
func (a *Agent) IsEqual(b Agent) bool {
return a.Image == b.Image && reflect.DeepEqual(a.Env, b.Env) && reflect.DeepEqual(a.VolumeSizeLimit, b.VolumeSizeLimit) && reflect.DeepEqual(a.Resources, b.Resources)
}

// HealthAgent is the configuration for the healthAgent
type HealthAgent struct {
// Image is a container image with Go SDK and auto-instrumentation.
Image string `json:"image,omitempty"`

// Env defines Go specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// IsEmpty is used to check if the health agent is empty
func (a *HealthAgent) IsEmpty() bool {
return a.Image == "" &&
len(a.Env) == 0
}

// IsEqual is used to compare if a health agent is equal to another
func (a *HealthAgent) IsEqual(b HealthAgent) bool {
return a.Image == b.Image && reflect.DeepEqual(a.Env, b.Env)
}

type UnhealthyPodError struct {
Pod string `json:"pod,omitempty"`
LastError string `json:"lastError,omitempty"`
}

// InstrumentationStatus defines the observed state of Instrumentation
type InstrumentationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
PodsMatching int64 `json:"podsMatching,omitempty"`
PodsInjected int64 `json:"podsInjected,omitempty"`
PodsNotReady int64 `json:"podsNotReady,omitempty"`
PodsOutdated int64 `json:"podsOutdated,omitempty"`
PodsHealthy int64 `json:"podsHealthy,omitempty"`
PodsUnhealthy int64 `json:"podsUnhealthy,omitempty"`
UnhealthyPodsErrors []UnhealthyPodError `json:"unhealthyPodsErrors,omitempty"`
LastUpdated metav1.Time `json:"lastUpdated,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:shortName=nragent;nragents
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="PodsMatching",type="integer",JSONPath=".status.podsMatching"
// +kubebuilder:printcolumn:name="PodsInjected",type="integer",JSONPath=".status.podsInjected"
// +kubebuilder:printcolumn:name="PodsNotReady",type="integer",JSONPath=".status.podsNotReady"
// +kubebuilder:printcolumn:name="PodsOutdated",type="integer",JSONPath=".status.podsOutdated"
// +kubebuilder:printcolumn:name="PodsHealthy",type="integer",JSONPath=".status.podsHealthy"
// +kubebuilder:printcolumn:name="PodsUnhealthy",type="integer",JSONPath=".status.podsUnhealthy"
// +operator-sdk:csv:customresourcedefinitions:displayName="New Relic Instrumentation"
// +operator-sdk:csv:customresourcedefinitions:displayName="New Relic Instrumentation"
// +operator-sdk:csv:customresourcedefinitions:resources={{Pod,v1}}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:object:resource:scope=Namespaced

// Instrumentation is the Schema for the instrumentations API
type Instrumentation struct {
Expand Down
Loading

0 comments on commit 5c689ea

Please sign in to comment.