Skip to content

Commit f81b817

Browse files
author
Ali Syed
committed
Initialize the GatewayAPI upgradeable controller with required imports and constants.
Set up predicates and watches for ConfigMap and CRD updates. Implement the operator and admingate Add e2e testing to testadminupgrade
1 parent db05f51 commit f81b817

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package gatewayapi_upgradeable
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
logf "github.com/openshift/cluster-ingress-operator/pkg/log"
8+
operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
9+
10+
corev1 "k8s.io/api/core/v1"
11+
12+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
14+
"sigs.k8s.io/controller-runtime/pkg/cache"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
"sigs.k8s.io/controller-runtime/pkg/controller"
17+
"sigs.k8s.io/controller-runtime/pkg/handler"
18+
"sigs.k8s.io/controller-runtime/pkg/manager"
19+
"sigs.k8s.io/controller-runtime/pkg/predicate"
20+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
21+
"sigs.k8s.io/controller-runtime/pkg/source"
22+
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
23+
)
24+
25+
const (
26+
controllerName = "gatewayapi_upgradeable_controller"
27+
GatewayAPIAdminKey = "ack-gateway-api-management"
28+
GatewayAPIAdminMsg = "This message was generated due to the presence of the Gateway API CRDs in the cluster. Going forward OCP will fully manage the lifecycle of these CRDs and we strongly advise against any external entity attempting to manage them, as this will be blocked and result in failure. Only the platform itself will support management of these CRDs. Any tampering will cause the cluster to enter a 'Degraded' state. The cluster administrator is responsible for the safety of existing Gateway API implementations. An admin gate will inform the administrator of their responsibilities and prevent upgrades until they acknowledge and accept them. For more information, please refer to the [link to doc here pending]."
29+
)
30+
31+
var (
32+
log = logf.Logger.WithName(controllerName)
33+
)
34+
35+
// The New function initializes the controller and sets up the watch for the ConfigMap.
36+
func New(mgr manager.Manager) (controller.Controller, error) {
37+
reconciler := &reconciler{
38+
client: mgr.GetClient(),
39+
cache: mgr.GetCache(),
40+
}
41+
42+
c, err := controller.New(controllerName, mgr, controller.Options{
43+
Reconciler: reconciler,
44+
})
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
// A predicate filter to watch for specific changes in the ConfigMap.
50+
// Verify that the ConfigMap's name and namespace match the expected values.
51+
adminGatePredicate := predicate.NewPredicateFuncs(func(o client.Object) bool {
52+
cm := o.(*corev1.ConfigMap)
53+
return cm.Name == operatorcontroller.AdminGatesConfigMapName().Name && cm.Namespace == operatorcontroller.AdminGatesConfigMapName().Namespace
54+
})
55+
56+
// Mapping the events to the admin gate ConfigMap.
57+
toAdminGateConfigMap := func(_ context.Context, _ client.Object) []reconcile.Request {
58+
return []reconcile.Request{{
59+
NamespacedName: operatorcontroller.AdminGatesConfigMapName(),
60+
}}
61+
}
62+
63+
// Defining the CRD predicate.
64+
crdPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool {
65+
return o.(*apiextensionsv1.CustomResourceDefinition).Spec.Group == gatewayapiv1.GroupName
66+
})
67+
68+
// Setting up a watch for CRD events.
69+
if err := c.Watch(source.Kind[client.Object](mgr.GetCache(), &apiextensionsv1.CustomResourceDefinition{}, handler.EnqueueRequestsFromMapFunc(toAdminGateConfigMap), crdPredicate)); err != nil {
70+
return nil, err
71+
}
72+
73+
if err := c.Watch(source.Kind[client.Object](mgr.GetCache(), &corev1.ConfigMap{}, &handler.EnqueueRequestForObject{}, adminGatePredicate)); err != nil {
74+
return nil, err
75+
}
76+
77+
return c, nil
78+
}
79+
80+
// Reconciler struct holds the client and cache attributes.
81+
type reconciler struct {
82+
client client.Client
83+
cache cache.Cache
84+
}
85+
86+
// Reconcile function implements the logic to check conditions and manage the admin gate.
87+
func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
88+
log.Info("reconciling", "request", request)
89+
90+
adminGateConditionExists, err := r.adminGateConditionExists(ctx)
91+
if err != nil {
92+
return reconcile.Result{}, fmt.Errorf("failed to determine if admin gate condition exists: %v", err)
93+
}
94+
95+
if adminGateConditionExists {
96+
if err := r.addAdminGate(ctx); err != nil {
97+
return reconcile.Result{}, fmt.Errorf("failed to add admin gate: %w", err)
98+
}
99+
} else {
100+
if err := r.removeAdminGate(ctx); err != nil {
101+
return reconcile.Result{}, fmt.Errorf("failed to remove admin gate: %w", err)
102+
}
103+
}
104+
105+
return reconcile.Result{}, nil
106+
}
107+
108+
// adminGateConditionExists checks if the admin gate condition exists based on both ConfigMap and CRDs.
109+
func (r *reconciler) adminGateConditionExists(ctx context.Context) (bool, error) {
110+
crds := &apiextensionsv1.CustomResourceDefinitionList{}
111+
if err := r.client.List(ctx, crds); err != nil {
112+
return false, fmt.Errorf("failed to list CRDs: %w", err)
113+
}
114+
115+
for _, crd := range crds.Items {
116+
if crd.Spec.Group == gatewayapiv1.GroupName {
117+
return true, nil
118+
}
119+
}
120+
121+
return false, nil
122+
}
123+
124+
// The addAdminGate function is responsible for adding the admin gate to the ConfigMap.
125+
func (r *reconciler) addAdminGate(ctx context.Context) error {
126+
adminGateConfigMap := &corev1.ConfigMap{}
127+
if err := r.cache.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGateConfigMap); err != nil {
128+
return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err)
129+
}
130+
131+
if adminGateConfigMap.Data == nil {
132+
adminGateConfigMap.Data = map[string]string{}
133+
}
134+
135+
// The function checks if the admin key exists and if it is set to the expected message.
136+
if val, ok := adminGateConfigMap.Data[GatewayAPIAdminKey]; ok && val == GatewayAPIAdminMsg {
137+
return nil
138+
}
139+
adminGateConfigMap.Data[GatewayAPIAdminKey] = GatewayAPIAdminMsg
140+
141+
log.Info("Adding admin gate for Gateway API management")
142+
if err := r.client.Update(ctx, adminGateConfigMap); err != nil {
143+
return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err)
144+
}
145+
return nil
146+
}
147+
148+
// The removeAdminGate function is responsible for removing the admin gate from the ConfigMap.
149+
func (r *reconciler) removeAdminGate(ctx context.Context) error {
150+
adminGateConfigMap := &corev1.ConfigMap{}
151+
if err := r.cache.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGateConfigMap); err != nil {
152+
return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err)
153+
}
154+
155+
// The function checks if the admin key exists in the ConfigMap.
156+
if _, ok := adminGateConfigMap.Data[GatewayAPIAdminKey]; !ok {
157+
return nil // Nothing to do if the key doesn't exist
158+
}
159+
160+
log.Info("Removing admin gate for Gateway API management")
161+
delete(adminGateConfigMap.Data, GatewayAPIAdminKey)
162+
if err := r.client.Update(ctx, adminGateConfigMap); err != nil {
163+
return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err)
164+
}
165+
return nil
166+
}

pkg/operator/controller/names.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ const (
4949
RemoteWorkerLabel = "node.openshift.io/remote-worker"
5050
)
5151

52+
func AdminGatesConfigMapName() types.NamespacedName {
53+
return types.NamespacedName{
54+
Name: "admin-gates",
55+
Namespace: GlobalMachineSpecifiedConfigNamespace,
56+
}
57+
}
58+
5259
// IngressClusterOperatorName returns the namespaced name of the ClusterOperator
5360
// resource for the operator.
5461
func IngressClusterOperatorName() types.NamespacedName {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//go:build e2e
2+
// +build e2e
3+
4+
package e2e
5+
6+
import (
7+
"context"
8+
"k8s.io/client-go/kubernetes"
9+
"testing"
10+
11+
operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
12+
gatewayapi_upgradeable "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gatewayapi-upgradeable"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
15+
16+
"k8s.io/apimachinery/pkg/api/errors"
17+
"sigs.k8s.io/controller-runtime/pkg/client/config"
18+
)
19+
20+
func TestGatewayAPIUpgradeable(t *testing.T) {
21+
t.Parallel()
22+
23+
t.Log("Starting test: Ensure the Gateway API CRDs exist")
24+
ensureCRDs(t)
25+
26+
t.Log("Starting test: Testing if admin gate works properly")
27+
testAdminGate(t)
28+
29+
t.Log("Starting test: Deleting Gateway API CRDs")
30+
deleteCRDs(t)
31+
32+
t.Log("Starting test: Testing if admin gate works properly after CRDs are deleted")
33+
testAdminGate(t)
34+
35+
// Defer the cleanup of the test gateway.
36+
t.Cleanup(func() {
37+
t.Log("Starting cleanup: Deleting test gateway")
38+
testGateway := gatewayapiv1.Gateway{ObjectMeta: metav1.ObjectMeta{Name: testGatewayName, Namespace: operatorcontroller.DefaultOperandNamespace}}
39+
if err := kclient.Delete(context.TODO(), &testGateway); err != nil {
40+
if errors.IsNotFound(err) {
41+
t.Logf("Gateway %q not found during cleanup", testGateway.Name)
42+
return
43+
}
44+
t.Errorf("failed to delete gateway %q: %v", testGateway.Name, err)
45+
} else {
46+
t.Logf("Successfully deleted gateway %q", testGateway.Name)
47+
}
48+
})
49+
}
50+
51+
func getClient() (*kubernetes.Clientset, error) {
52+
kubeConfig, err := config.GetConfig()
53+
if err != nil {
54+
return nil, err
55+
}
56+
return kubernetes.NewForConfig(kubeConfig)
57+
}
58+
59+
func testAdminGate(t *testing.T) {
60+
kclient, err := getClient()
61+
if err != nil {
62+
t.Fatalf("failed to get kube client: %v", err)
63+
}
64+
65+
t.Log("Ensuring CRDs exist")
66+
crdsExist := true
67+
68+
t.Logf("Getting configmap admin-gates in namespace openshift-config-managed")
69+
configMap, err := kclient.CoreV1().ConfigMaps("openshift-config-managed").
70+
Get(context.TODO(), "admin-gates", metav1.GetOptions{})
71+
if err != nil {
72+
t.Errorf("failed to get configmap admin-gates: %v", err)
73+
if errors.IsNotFound(err) {
74+
t.Logf("ConfigMap 'admin-gates' not found in namespace openshift-config-managed")
75+
} else {
76+
t.Logf("Error getting ConfigMap: %v", err)
77+
}
78+
return
79+
}
80+
81+
t.Log("Checking for admin gate key in the configmap")
82+
adminGateKeyExists := false
83+
if _, ok := configMap.Data[gatewayapi_upgradeable.GatewayAPIAdminKey]; ok {
84+
adminGateKeyExists = true
85+
}
86+
87+
t.Logf("CRDs exist: %v, Admin Gate Key exists: %v", crdsExist, adminGateKeyExists)
88+
89+
if crdsExist && !adminGateKeyExists {
90+
t.Errorf("expected admin gate key to exist in configmap, but it does not")
91+
} else if !crdsExist && adminGateKeyExists {
92+
t.Errorf("expected admin gate key to be removed from configmap, but it still exists")
93+
}
94+
}

0 commit comments

Comments
 (0)