-
Notifications
You must be signed in to change notification settings - Fork 346
/
acnp_resourceimport_controller.go
172 lines (160 loc) · 6.92 KB
/
acnp_resourceimport_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
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 commonarea
import (
"context"
"errors"
"fmt"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
"antrea.io/antrea/multicluster/controllers/multicluster/common"
"antrea.io/antrea/pkg/apis/crd/v1alpha1"
)
const acnpImportFailed string = "ACNPImportFailed"
var (
resourceImportAPIVersion = "multicluster.crd.antrea.io/v1alpha1"
resourceImportKind = "ResourceImport"
acnpEventReportingController = "resourceimport-controller"
// TODO(yang): add run-time pod suffix
acnpEventReportingInstance = "antrea-mc-controller"
)
func (r *ResourceImportReconciler) handleResImpUpdateForClusterNetworkPolicy(ctx context.Context, resImp *multiclusterv1alpha1.ResourceImport) (ctrl.Result, error) {
if resImp.Spec.ClusterNetworkPolicy == nil {
klog.V(2).InfoS("Skip reconciling ResourceImport for ClusterNetworkPolicy since it has no valid spec", "resourceimport", klog.KObj(resImp))
return ctrl.Result{}, nil
}
acnpName := types.NamespacedName{
Namespace: "",
Name: common.ToMCResourceName(resImp.Spec.Name),
}
klog.InfoS("Updating ACNP corresponding to ResourceImport",
"acnp", acnpName.String(), "resourceimport", klog.KObj(resImp))
acnp := &v1alpha1.ClusterNetworkPolicy{}
err := r.localClusterClient.Get(ctx, acnpName, acnp)
acnpNotFound := apierrors.IsNotFound(err)
if err != nil && !acnpNotFound {
return ctrl.Result{}, err
}
if !acnpNotFound {
if _, ok := acnp.Annotations[common.AntreaMCACNPAnnotation]; !ok {
msg := "Unable to import Antrea ClusterNetworkPolicy which conflicts with existing one in cluster " + r.localClusterID
err := errors.New(msg)
klog.ErrorS(err, "", "acnp", klog.KObj(acnp))
return ctrl.Result{}, r.reportStatusEvent(msg, ctx, resImp)
}
}
acnpObj := getMCAntreaClusterPolicy(resImp)
tierObj, tierName := &v1alpha1.Tier{}, acnpObj.Spec.Tier
err = r.localClusterClient.Get(ctx, types.NamespacedName{Namespace: "", Name: tierName}, tierObj)
tierNotFound := apierrors.IsNotFound(err)
if err != nil && !tierNotFound {
msg := fmt.Sprintf("Failed to get Tier %s in member cluster %s", tierName, r.localClusterID)
return ctrl.Result{}, r.reportStatusEvent(msg, ctx, resImp)
}
tierNotFoundMsg := fmt.Sprintf("ACNP Tier %s does not exist in importing cluster %s", tierName, r.localClusterID)
if !tierNotFound {
// If the ACNP Tier exists in the importing member cluster, then the policy is realizable.
// Create or update the ACNP if necessary.
if acnpNotFound {
if err = r.localClusterClient.Create(ctx, acnpObj, &client.CreateOptions{}); err != nil {
msg := "Failed to create imported Antrea ClusterNetworkPolicy in cluster " + r.localClusterID
klog.ErrorS(err, msg, "acnp", klog.KObj(acnpObj))
return ctrl.Result{}, r.reportStatusEvent(msg, ctx, resImp)
}
r.installedResImports.Add(*resImp)
} else if !apiequality.Semantic.DeepEqual(acnp.Spec, acnpObj.Spec) {
acnp.Spec = acnpObj.Spec
if err = r.localClusterClient.Update(ctx, acnp, &client.UpdateOptions{}); err != nil {
msg := "Failed to update imported Antrea ClusterNetworkPolicy in cluster " + r.localClusterID
klog.ErrorS(err, msg, "acnp", klog.KObj(acnpObj))
return ctrl.Result{}, r.reportStatusEvent(msg, ctx, resImp)
}
}
} else if !acnpNotFound {
// The ACNP Tier does not exist, and the policy cannot be realized in this particular importing member cluster.
// If there is an ACNP previously created via import (which has a valid Tier by then), it should be cleaned up.
if err = r.localClusterClient.Delete(ctx, acnpObj, &client.DeleteOptions{}); err != nil {
msg := "Failed to delete imported Antrea ClusterNetworkPolicy that no longer has a valid Tier for cluster " + r.localClusterID
klog.ErrorS(err, msg, "acnp", klog.KObj(acnpObj))
return ctrl.Result{}, r.reportStatusEvent(msg, ctx, resImp)
}
return ctrl.Result{}, r.reportStatusEvent(tierNotFoundMsg, ctx, resImp)
} else {
return ctrl.Result{}, r.reportStatusEvent(tierNotFoundMsg, ctx, resImp)
}
return ctrl.Result{}, nil
}
func (r *ResourceImportReconciler) handleResImpDeleteForClusterNetworkPolicy(ctx context.Context, resImp *multiclusterv1alpha1.ResourceImport) (ctrl.Result, error) {
acnpName := common.ToMCResourceName(resImp.Spec.Name)
klog.InfoS("Deleting ACNP corresponding to ResourceImport",
"acnp", acnpName, "resourceimport", klog.KObj(resImp))
acnp := &v1alpha1.ClusterNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: acnpName,
},
}
err := client.IgnoreNotFound(r.localClusterClient.Delete(ctx, acnp, &client.DeleteOptions{}))
if err != nil {
klog.ErrorS(err, "Failed to delete imported ACNP", "acnp", acnpName)
return ctrl.Result{}, err
}
r.installedResImports.Delete(*resImp)
return ctrl.Result{}, nil
}
func getMCAntreaClusterPolicy(resImp *multiclusterv1alpha1.ResourceImport) *v1alpha1.ClusterNetworkPolicy {
return &v1alpha1.ClusterNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: common.ToMCResourceName(resImp.Spec.Name),
Annotations: map[string]string{
common.AntreaMCACNPAnnotation: "true",
},
},
Spec: *resImp.Spec.ClusterNetworkPolicy,
}
}
func (r *ResourceImportReconciler) reportStatusEvent(errMsg string, ctx context.Context, resImp *multiclusterv1alpha1.ResourceImport) error {
t := metav1.Now()
statusEvent := &corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v.%x", resImp.Name, t.UnixNano()),
Namespace: resImp.Namespace,
},
Type: corev1.EventTypeWarning,
Reason: acnpImportFailed,
Message: errMsg,
InvolvedObject: corev1.ObjectReference{
APIVersion: resourceImportAPIVersion,
Kind: resourceImportKind,
Name: resImp.Name,
Namespace: resImp.Namespace,
UID: resImp.GetUID(),
},
FirstTimestamp: metav1.Now(),
LastTimestamp: metav1.Now(),
ReportingController: acnpEventReportingController,
ReportingInstance: acnpEventReportingInstance,
Action: "synced",
}
if err := r.remoteCommonArea.Create(ctx, statusEvent, &client.CreateOptions{}); err != nil {
klog.ErrorS(err, "Failed to create ACNP import event for ResourceImport", "resImp", klog.KObj(resImp))
return err
}
return nil
}