-
Notifications
You must be signed in to change notification settings - Fork 359
/
labelidentity_import_controller.go
156 lines (141 loc) · 6.16 KB
/
labelidentity_import_controller.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
/*
Copyright 2022 Antrea Authors.
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 member
import (
"context"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"antrea.io/antrea/multicluster/apis/multicluster/constants"
multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
"antrea.io/antrea/multicluster/controllers/multicluster/common"
"antrea.io/antrea/multicluster/controllers/multicluster/commonarea"
)
// LabelIdentityResourceImportReconciler reconciles a LabelIdentity kind of ResourceImport object in the member cluster.
type LabelIdentityResourceImportReconciler struct {
client.Client
Scheme *runtime.Scheme
localClusterClient client.Client
localClusterID string
namespace string
remoteCommonArea commonarea.RemoteCommonArea
// Saved Manager to indicate SetupWithManager() is done or not.
manager ctrl.Manager
}
func newLabelIdentityResourceImportReconciler(client client.Client, scheme *runtime.Scheme, localClusterClient client.Client,
localClusterID string, namespace string, remoteCommonArea commonarea.RemoteCommonArea) *LabelIdentityResourceImportReconciler {
return &LabelIdentityResourceImportReconciler{
Client: client,
Scheme: scheme,
localClusterClient: localClusterClient,
localClusterID: localClusterID,
namespace: namespace,
remoteCommonArea: remoteCommonArea,
}
}
func (r *LabelIdentityResourceImportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.V(2).InfoS("Reconciling LabelIdentity kind of ResourceImport", "resourceImport", req.NamespacedName)
var labelIdentityResImport multiclusterv1alpha1.ResourceImport
err := r.remoteCommonArea.Get(ctx, req.NamespacedName, &labelIdentityResImport)
if err != nil {
if !apierrors.IsNotFound(err) {
klog.ErrorS(err, "Unable to fetch LabelIdentity kind of ResourceImport", "resourceImport", req.NamespacedName)
return ctrl.Result{}, err
}
return r.handleLabelIdentityImpDelete(ctx, req.NamespacedName)
}
return r.handleLabelIdentityImpCreateOrUpdate(ctx, &labelIdentityResImport)
}
func (r *LabelIdentityResourceImportReconciler) handleLabelIdentityImpCreateOrUpdate(ctx context.Context,
labelResImp *multiclusterv1alpha1.ResourceImport) (ctrl.Result, error) {
labelIdentityName := types.NamespacedName{
Name: labelResImp.Name,
}
labelIdentity := &multiclusterv1alpha1.LabelIdentity{}
err := r.localClusterClient.Get(ctx, labelIdentityName, labelIdentity)
labelIdentityNotFound := apierrors.IsNotFound(err)
if err != nil && !labelIdentityNotFound {
return ctrl.Result{}, err
}
if labelIdentityNotFound {
newLabelIdentity := &multiclusterv1alpha1.LabelIdentity{
ObjectMeta: metav1.ObjectMeta{
Name: labelResImp.Name,
},
Spec: multiclusterv1alpha1.LabelIdentitySpec{
Label: labelResImp.Spec.LabelIdentity.Label,
ID: labelResImp.Spec.LabelIdentity.ID,
},
}
if err = r.localClusterClient.Create(ctx, newLabelIdentity, &client.CreateOptions{}); err != nil {
klog.ErrorS(err, "Failed to create LabelIdentity", "clusterID", r.localClusterID, "label", labelResImp.Spec.LabelIdentity.Label)
return ctrl.Result{}, err
}
} else if labelIdentity.Spec.ID != labelResImp.Spec.LabelIdentity.ID {
labelIdentity.Spec.ID = labelResImp.Spec.LabelIdentity.ID
if err = r.localClusterClient.Update(ctx, labelIdentity, &client.UpdateOptions{}); err != nil {
klog.ErrorS(err, "Failed to update LabelIdentity", "clusterID", r.localClusterID, "label", labelResImp.Spec.LabelIdentity.Label)
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
func (r *LabelIdentityResourceImportReconciler) handleLabelIdentityImpDelete(ctx context.Context,
resImpName types.NamespacedName) (ctrl.Result, error) {
klog.V(2).InfoS("Deleting LabelIdentity corresponding to LabelIdentity kind of ResourceImport", "resourceImport", resImpName.String())
labelIdentity := &multiclusterv1alpha1.LabelIdentity{
ObjectMeta: metav1.ObjectMeta{
Name: resImpName.Name,
},
}
if err := r.localClusterClient.Delete(ctx, labelIdentity, &client.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
klog.ErrorS(err, "Failed to delete LabelIdentity", "clusterID", r.localClusterID, "labelIdentity", labelIdentity.Name)
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *LabelIdentityResourceImportReconciler) SetupWithManager(mgr ctrl.Manager) error {
if r.manager == mgr {
// SetupWithManager was called by a previous RemoteManager.StartWatch() call and already
// completed with no error.
return nil
}
// Ignore status update event via GenerationChangedPredicate
generationPredicate := predicate.GenerationChangedPredicate{}
// Only register this controller to reconcile LabelIdentity kind of ResourceImport
labelIdentityResImportFilter := func(object client.Object) bool {
if resImport, ok := object.(*multiclusterv1alpha1.ResourceImport); ok {
return resImport.Spec.Kind == constants.LabelIdentityKind
}
return false
}
labelIdentityResImportPredicate := predicate.NewPredicateFuncs(labelIdentityResImportFilter)
instance := predicate.And(generationPredicate, labelIdentityResImportPredicate)
err := ctrl.NewControllerManagedBy(mgr).
For(&multiclusterv1alpha1.ResourceImport{}).
WithEventFilter(instance).
WithOptions(controller.Options{
MaxConcurrentReconciles: common.DefaultWorkerCount,
}).
Complete(r)
if err == nil {
r.manager = mgr
}
return err
}