-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine Node controller to support Gateway HA
In order to support Gateway active-standby mode HA, the Node controller has been refined to handle Node readiness change. There will be at most one Gateway in a given Namespace, and the Gateway controller is updated correspondingly. 1. Check Node readiness and create a Gateway CR if Node is ready and there is no existing Gateway. 2. Initilize active Gateway and Gateway candidate Nodes with annotation 'multicluster.antrea.io/gateway'. 3. Add Gateway webhook to allow at most one Gateway in a given Namespace. Signed-off-by: Lan Luo <[email protected]>
- Loading branch information
1 parent
36622c8
commit 204f359
Showing
12 changed files
with
697 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
multicluster/cmd/multicluster-controller/gateway_webhook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
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 main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
mcv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1" | ||
) | ||
|
||
//+kubebuilder:webhook:path=/validate-multicluster-crd-antrea-io-v1alpha1-gateway,mutating=false,failurePolicy=fail,sideEffects=None,groups=multicluster.crd.antrea.io,resources=gateways,verbs=create;update,versions=v1alpha1,name=vgateway.kb.io,admissionReviewVersions={v1,v1beta1} | ||
|
||
// Gateway validator | ||
type gatewayValidator struct { | ||
Client client.Client | ||
decoder *admission.Decoder | ||
namespace string | ||
} | ||
|
||
// Handle handles admission requests. | ||
func (v *gatewayValidator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
gateway := &mcv1alpha1.Gateway{} | ||
err := v.decoder.Decode(req, gateway) | ||
if err != nil { | ||
klog.ErrorS(err, "Error while decoding Gateway", "Gateway", req.Namespace+"/"+req.Name) | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
// Check if there is any existing Gateway. | ||
gatewayList := &mcv1alpha1.GatewayList{} | ||
if err := v.Client.List(context.TODO(), gatewayList, client.InNamespace(v.namespace)); err != nil { | ||
klog.ErrorS(err, "Error reading Gateway", "Namespace", v.namespace) | ||
return admission.Errored(http.StatusPreconditionFailed, err) | ||
} | ||
|
||
if req.Operation == admissionv1.Create && len(gatewayList.Items) > 0 { | ||
err := fmt.Errorf("multiple Gateways in a Namespace are not allowed") | ||
klog.ErrorS(err, "failed to create Gateway", "Gateway", klog.KObj(gateway), "Namespace", v.namespace) | ||
return admission.Errored(http.StatusPreconditionFailed, err) | ||
} | ||
return admission.Allowed("") | ||
} | ||
|
||
func (v *gatewayValidator) InjectDecoder(d *admission.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} |
123 changes: 123 additions & 0 deletions
123
multicluster/cmd/multicluster-controller/gateway_webhook_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
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 main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/admission/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
k8smcsv1alpha1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1" | ||
|
||
mcsv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1" | ||
) | ||
|
||
var gatewayWebhookUnderTest *gatewayValidator | ||
|
||
func TestWebhookGatewayEvents(t *testing.T) { | ||
newGateway := &mcsv1alpha1.Gateway{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "node-1", | ||
}, | ||
GatewayIP: "1.2.3.4", | ||
InternalIP: "172.168.3.4", | ||
} | ||
existingGateway := &mcsv1alpha1.Gateway{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "node-2", | ||
}, | ||
} | ||
|
||
newGW, _ := json.Marshal(newGateway) | ||
|
||
newReq := admission.Request{ | ||
AdmissionRequest: v1.AdmissionRequest{ | ||
UID: "07e52e8d-4513-11e9-a716-42010a800270", | ||
Kind: metav1.GroupVersionKind{ | ||
Group: "multicluster.crd.antrea.io", | ||
Version: "v1alpha1", | ||
Kind: "Gateway", | ||
}, | ||
Resource: metav1.GroupVersionResource{ | ||
Group: "multicluster.crd.antrea.io", | ||
Version: "v1alpha1", | ||
Resource: "Gateways", | ||
}, | ||
Name: "node-1", | ||
Namespace: "default", | ||
Operation: v1.Create, | ||
Object: runtime.RawExtension{ | ||
Raw: newGW, | ||
}, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
req admission.Request | ||
existingGateway *mcsv1alpha1.Gateway | ||
newGateway *mcsv1alpha1.Gateway | ||
isAllowed bool | ||
}{ | ||
{ | ||
name: "create a new Gateway successfully", | ||
req: newReq, | ||
isAllowed: true, | ||
}, | ||
{ | ||
name: "failed to create a Gateway when there is an existing one", | ||
existingGateway: existingGateway, | ||
req: newReq, | ||
isAllowed: false, | ||
}, | ||
} | ||
|
||
newScheme := runtime.NewScheme() | ||
utilruntime.Must(clientgoscheme.AddToScheme(newScheme)) | ||
utilruntime.Must(k8smcsv1alpha1.AddToScheme(newScheme)) | ||
utilruntime.Must(mcsv1alpha1.AddToScheme(newScheme)) | ||
decoder, err := admission.NewDecoder(newScheme) | ||
if err != nil { | ||
klog.ErrorS(err, "Error constructing a decoder") | ||
} | ||
for _, tt := range tests { | ||
fakeClient := fake.NewClientBuilder().WithScheme(newScheme).WithObjects().Build() | ||
if tt.existingGateway != nil { | ||
fakeClient = fake.NewClientBuilder().WithScheme(newScheme).WithObjects(tt.existingGateway).Build() | ||
} | ||
gatewayWebhookUnderTest = &gatewayValidator{ | ||
Client: fakeClient, | ||
namespace: "default"} | ||
gatewayWebhookUnderTest.InjectDecoder(decoder) | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
response := gatewayWebhookUnderTest.Handle(context.Background(), tt.req) | ||
assert.Equal(t, tt.isAllowed, response.Allowed) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,3 +54,4 @@ resources: | |
|
||
patchesStrategicMerge: | ||
- manager_command_patch.yaml | ||
- webhook_patch.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: ValidatingWebhookConfiguration | ||
metadata: | ||
name: validating-webhook-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
name: vgateway.kb.io | ||
$patch: delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.