/
reconcile_tenant.go
174 lines (151 loc) · 6.1 KB
/
reconcile_tenant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2022 StreamNative
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package connection
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/streamnative/pulsar-resources-operator/pkg/feature"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
resourcev1alpha1 "github.com/streamnative/pulsar-resources-operator/api/v1alpha1"
"github.com/streamnative/pulsar-resources-operator/pkg/admin"
"github.com/streamnative/pulsar-resources-operator/pkg/reconciler"
)
// PulsarTenantReconciler reconciles a PulsarTenant object
type PulsarTenantReconciler struct {
conn *PulsarConnectionReconciler
log logr.Logger
}
func makeTenantsReconciler(r *PulsarConnectionReconciler) reconciler.Interface {
return &PulsarTenantReconciler{
conn: r,
log: makeSubResourceLog(r, "PulsarTenant"),
}
}
// Observe checks the updates of object
func (r *PulsarTenantReconciler) Observe(ctx context.Context) error {
r.log.V(1).Info("Start Observe")
tenantList := &resourcev1alpha1.PulsarTenantList{}
if err := r.conn.client.List(ctx, tenantList, client.InNamespace(r.conn.connection.Namespace),
client.MatchingFields(map[string]string{
".spec.connectionRef.name": r.conn.connection.Name,
})); err != nil {
return fmt.Errorf("list tenants [%w]", err)
}
r.log.V(1).Info("Observed tenants items", "Count", len(tenantList.Items))
r.conn.tenants = tenantList.Items
for i := range r.conn.tenants {
if !resourcev1alpha1.IsPulsarResourceReady(&r.conn.tenants[i]) {
r.conn.addUnreadyResource(&r.conn.tenants[i])
}
}
r.log.V(1).Info("Observe Done")
return nil
}
// Reconcile reconciles all tenants
func (r *PulsarTenantReconciler) Reconcile(ctx context.Context) error {
for i := range r.conn.tenants {
tenant := &r.conn.tenants[i]
if err := r.ReconcileTenant(ctx, r.conn.pulsarAdmin, tenant); err != nil {
return fmt.Errorf("reconcile tenant [%w]", err)
}
}
return nil
}
// ReconcileTenant move the current state of the toic closer to the desired state
func (r *PulsarTenantReconciler) ReconcileTenant(ctx context.Context, pulsarAdmin admin.PulsarAdmin,
tenant *resourcev1alpha1.PulsarTenant) error {
log := r.log.WithValues("name", tenant.Name, "namespace", tenant.Namespace)
log.Info("Start Reconcile")
if !tenant.DeletionTimestamp.IsZero() {
log.Info("Deleting tenant", "LifecyclePolicy", tenant.Spec.LifecyclePolicy)
if tenant.Spec.LifecyclePolicy != resourcev1alpha1.KeepAfterDeletion {
if err := pulsarAdmin.DeleteTenant(tenant.Spec.Name); err != nil && !admin.IsNotFound(err) {
log.Error(err, "Failed to delete tenant")
return err
}
}
// TODO use otelcontroller until kube-instrumentation upgrade controller-runtime version to newer
controllerutil.RemoveFinalizer(tenant, resourcev1alpha1.FinalizerName)
if err := r.conn.client.Update(ctx, tenant); err != nil {
log.Error(err, "Failed to remove finalizer")
return err
}
return nil
}
// TODO use otelcontroller until kube-instrumentation upgrade controller-runtime version to newer
controllerutil.AddFinalizer(tenant, resourcev1alpha1.FinalizerName)
if err := r.conn.client.Update(ctx, tenant); err != nil {
log.Error(err, "Failed to add finalizer")
return err
}
if resourcev1alpha1.IsPulsarResourceReady(tenant) &&
!feature.DefaultFeatureGate.Enabled(feature.AlwaysUpdatePulsarResource) {
log.Info("Skip reconcile, tenant resource is ready")
return nil
}
tenantParams := &admin.TenantParams{
AdminRoles: tenant.Spec.AdminRoles,
AllowedClusters: tenant.Spec.AllowedClusters,
}
// If ObeservedGeneration is greater than 1, means the tenant alreday been created
// So it will been updated in ApplyTenant
if tenant.Status.ObservedGeneration > 1 {
tenantParams.Changed = true
}
if refs := tenant.Spec.GeoReplicationRefs; len(refs) != 0 {
for _, ref := range refs {
geoReplication := &resourcev1alpha1.PulsarGeoReplication{}
namespacedName := types.NamespacedName{
Namespace: tenant.Namespace,
Name: ref.Name,
}
if err := r.conn.client.Get(ctx, namespacedName, geoReplication); err != nil {
return err
}
log.V(1).Info("Found geo replication", "GEO Replication", geoReplication.Name)
destConnection := &resourcev1alpha1.PulsarConnection{}
namespacedName = types.NamespacedName{
Name: geoReplication.Spec.DestinationConnectionRef.Name,
Namespace: geoReplication.Namespace,
}
if err := r.conn.client.Get(ctx, namespacedName, destConnection); err != nil {
log.Error(err, "Failed to get destination connection for geo replication")
return err
}
tenantParams.AllowedClusters = append(tenantParams.AllowedClusters, destConnection.Spec.ClusterName)
tenantParams.AllowedClusters = append(tenantParams.AllowedClusters, r.conn.connection.Spec.ClusterName)
}
log.Info("Geo Replication is enabled. Apply tenant with allowed clusters", "clusters", tenantParams.AllowedClusters)
}
if err := pulsarAdmin.ApplyTenant(tenant.Spec.Name, tenantParams); err != nil {
meta.SetStatusCondition(&tenant.Status.Conditions, *NewErrorCondition(tenant.Generation, err.Error()))
log.Error(err, "Failed to apply tenant")
if err := r.conn.client.Status().Update(ctx, tenant); err != nil {
log.Error(err, "Failed to update the tenant status")
return err
}
return err
}
tenant.Status.ObservedGeneration = tenant.Generation
meta.SetStatusCondition(&tenant.Status.Conditions, *NewReadyCondition(tenant.Generation))
if err := r.conn.client.Status().Update(ctx, tenant); err != nil {
log.Error(err, "Failed to update the tenant status")
return err
}
return nil
}