Skip to content

Commit aa5a56f

Browse files
authored
Add controller for TargetAllocator CR (#3275)
1 parent f81ef33 commit aa5a56f

13 files changed

+589
-33
lines changed

apis/v1alpha1/targetallocator_types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
)
2222

2323
func init() {
24-
v1beta1.SchemeBuilder.Register(&TargetAllocator{}, &TargetAllocatorList{})
24+
SchemeBuilder.Register(&TargetAllocator{}, &TargetAllocatorList{})
2525
}
2626

2727
//+kubebuilder:object:root=true
28+
//+kubebuilder:storageversion
2829
//+kubebuilder:subresource:status
2930

3031
// TargetAllocator is the Schema for the targetallocators API.

controllers/common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func BuildCollector(params manifests.Params) ([]client.Object, error) {
6767
Recorder: params.Recorder,
6868
Log: params.Log,
6969
Config: params.Config,
70-
Collector: params.OtelCol,
70+
Collector: &params.OtelCol,
7171
TargetAllocator: *params.TargetAllocator,
7272
}
7373
taResources, err := BuildTargetAllocator(taParams)

controllers/suite_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ func TestMain(m *testing.M) {
182182
fmt.Printf("failed to SetupWebhookWithManager: %v", err)
183183
os.Exit(1)
184184
}
185+
if err = v1alpha1.SetupTargetAllocatorWebhook(mgr, config.New(), reviewer); err != nil {
186+
fmt.Printf("failed to SetupWebhookWithManager: %v", err)
187+
os.Exit(1)
188+
}
185189

186190
if err = v1alpha1.SetupOpAMPBridgeWebhook(mgr, config.New()); err != nil {
187191
fmt.Printf("failed to SetupWebhookWithManager: %v", err)
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package controllers contains the main controller, where the reconciliation starts.
16+
package controllers
17+
18+
import (
19+
"context"
20+
21+
"github.com/go-logr/logr"
22+
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
23+
appsv1 "k8s.io/api/apps/v1"
24+
corev1 "k8s.io/api/core/v1"
25+
policyV1 "k8s.io/api/policy/v1"
26+
apierrors "k8s.io/apimachinery/pkg/api/errors"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
"k8s.io/client-go/tools/record"
29+
ctrl "sigs.k8s.io/controller-runtime"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
31+
32+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
33+
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
34+
"github.com/open-telemetry/opentelemetry-operator/internal/config"
35+
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator"
36+
taStatus "github.com/open-telemetry/opentelemetry-operator/internal/status/targetallocator"
37+
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
38+
)
39+
40+
// TargetAllocatorReconciler reconciles a TargetAllocator object.
41+
type TargetAllocatorReconciler struct {
42+
client.Client
43+
recorder record.EventRecorder
44+
scheme *runtime.Scheme
45+
log logr.Logger
46+
config config.Config
47+
}
48+
49+
// TargetAllocatorReconcilerParams is the set of options to build a new TargetAllocatorReconciler.
50+
type TargetAllocatorReconcilerParams struct {
51+
client.Client
52+
Recorder record.EventRecorder
53+
Scheme *runtime.Scheme
54+
Log logr.Logger
55+
Config config.Config
56+
}
57+
58+
func (r *TargetAllocatorReconciler) getParams(instance v1alpha1.TargetAllocator) targetallocator.Params {
59+
p := targetallocator.Params{
60+
Config: r.config,
61+
Client: r.Client,
62+
Log: r.log,
63+
Scheme: r.scheme,
64+
Recorder: r.recorder,
65+
TargetAllocator: instance,
66+
}
67+
68+
return p
69+
}
70+
71+
// NewTargetAllocatorReconciler creates a new reconciler for TargetAllocator objects.
72+
func NewTargetAllocatorReconciler(
73+
client client.Client,
74+
scheme *runtime.Scheme,
75+
recorder record.EventRecorder,
76+
config config.Config,
77+
logger logr.Logger,
78+
) *TargetAllocatorReconciler {
79+
return &TargetAllocatorReconciler{
80+
Client: client,
81+
log: logger,
82+
scheme: scheme,
83+
config: config,
84+
recorder: recorder,
85+
}
86+
}
87+
88+
// TODO: Uncomment the lines below after enabling the TA controller in main.go
89+
// // +kubebuilder:rbac:groups="",resources=pods;configmaps;services;serviceaccounts;persistentvolumeclaims;persistentvolumes,verbs=get;list;watch;create;update;patch;delete
90+
// // +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
91+
// // +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
92+
// // +kubebuilder:rbac:groups=policy,resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete
93+
// // +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;podmonitors,verbs=get;list;watch;create;update;patch;delete
94+
// // +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors,verbs=get;list;watch;update;patch
95+
// // +kubebuilder:rbac:groups=opentelemetry.io,resources=targetallocators,verbs=get;list;watch;update;patch
96+
// // +kubebuilder:rbac:groups=opentelemetry.io,resources=targetallocators/status,verbs=get;update;patch
97+
98+
// Reconcile the current state of a TargetAllocator resource with the desired state.
99+
func (r *TargetAllocatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
100+
log := r.log.WithValues("targetallocator", req.NamespacedName)
101+
102+
var instance v1alpha1.TargetAllocator
103+
if err := r.Client.Get(ctx, req.NamespacedName, &instance); err != nil {
104+
if !apierrors.IsNotFound(err) {
105+
log.Error(err, "unable to fetch TargetAllocator")
106+
}
107+
108+
// we'll ignore not-found errors, since they can't be fixed by an immediate
109+
// requeue (we'll need to wait for a new notification), and we can get them
110+
// on deleted requests.
111+
return ctrl.Result{}, client.IgnoreNotFound(err)
112+
}
113+
// We have a deletion, short circuit and let the deletion happen
114+
if deletionTimestamp := instance.GetDeletionTimestamp(); deletionTimestamp != nil {
115+
return ctrl.Result{}, nil
116+
}
117+
118+
if instance.Spec.ManagementState == v1beta1.ManagementStateUnmanaged {
119+
log.Info("Skipping reconciliation for unmanaged TargetAllocator resource", "name", req.String())
120+
// Stop requeueing for unmanaged TargetAllocator custom resources
121+
return ctrl.Result{}, nil
122+
}
123+
124+
params := r.getParams(instance)
125+
desiredObjects, buildErr := BuildTargetAllocator(params)
126+
if buildErr != nil {
127+
return ctrl.Result{}, buildErr
128+
}
129+
130+
err := reconcileDesiredObjects(ctx, r.Client, log, &params.TargetAllocator, params.Scheme, desiredObjects, nil)
131+
return taStatus.HandleReconcileStatus(ctx, log, params, err)
132+
}
133+
134+
// SetupWithManager tells the manager what our controller is interested in.
135+
func (r *TargetAllocatorReconciler) SetupWithManager(mgr ctrl.Manager) error {
136+
builder := ctrl.NewControllerManagedBy(mgr).
137+
For(&v1alpha1.TargetAllocator{}).
138+
Owns(&corev1.ConfigMap{}).
139+
Owns(&corev1.ServiceAccount{}).
140+
Owns(&corev1.Service{}).
141+
Owns(&appsv1.Deployment{}).
142+
Owns(&corev1.PersistentVolume{}).
143+
Owns(&corev1.PersistentVolumeClaim{}).
144+
Owns(&policyV1.PodDisruptionBudget{})
145+
146+
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
147+
builder.Owns(&monitoringv1.ServiceMonitor{})
148+
builder.Owns(&monitoringv1.PodMonitor{})
149+
}
150+
151+
return builder.Complete(r)
152+
}

0 commit comments

Comments
 (0)