-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
217 additions
and
34 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
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
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
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
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,54 @@ | ||
// Copyright 2019-2024 The Liqo 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 concurrent | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
var _ manager.Runnable = &ConcurrentRunnable{} | ||
|
||
// ConcurrentRunnable is a Runnable that manages concurrency. | ||
type ConcurrentRunnable struct { | ||
Client client.Client | ||
|
||
PodName string | ||
DeploymentName string | ||
Namespace string | ||
} | ||
|
||
// Start starts the ConcurrentRunnable. | ||
func (cr *ConcurrentRunnable) Start(ctx context.Context) error { | ||
pods, err := ListAllGatewaysReplicas(ctx, cr.Client, cr.Namespace, cr.DeploymentName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := range pods { | ||
if pods[i].GetName() == cr.PodName { | ||
if err := AddActiveGatewayLabel(ctx, cr.Client, client.ObjectKeyFromObject(&pods[i])); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := RemoveActiveGatewayLabel(ctx, cr.Client, client.ObjectKeyFromObject(&pods[i])); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,20 @@ | ||
// Copyright 2019-2024 The Liqo 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 concurrent contains the logic to manage same gateway replicas. | ||
// They are managed using an active/passive approach. | ||
// The gateway container try to acquire the active role using the controller manager lease. | ||
// Then, the active gateway is labeled with the ActiveGatewayKey and ActiveGatewayValue, and the passive gateways are unlabeled. | ||
// The gateway service target the active gateway using the ActiveGatewayKey and ActiveGatewayValue labels. | ||
package concurrent |
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,73 @@ | ||
// Copyright 2019-2024 The Liqo 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 concurrent | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// AddActiveGatewayLabel adds the active gateway label to the pod. | ||
func AddActiveGatewayLabel(ctx context.Context, cl client.Client, key client.ObjectKey) error { | ||
pod := &corev1.Pod{} | ||
if err := cl.Get(ctx, key, &corev1.Pod{}); err != nil { | ||
return err | ||
} | ||
|
||
labels := pod.GetLabels() | ||
if labels == nil { | ||
labels = map[string]string{} | ||
} | ||
labels[ActiveGatewayKey] = ActiveGatewayValue | ||
pod.SetLabels(labels) | ||
|
||
if err := cl.Update(ctx, pod); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// RemoveActiveGatewayLabel removes the active gateway label from the pod. | ||
func RemoveActiveGatewayLabel(ctx context.Context, cl client.Client, key client.ObjectKey) error { | ||
pod := &corev1.Pod{} | ||
if err := cl.Get(ctx, key, &corev1.Pod{}); err != nil { | ||
return err | ||
} | ||
|
||
labels := pod.GetLabels() | ||
if labels == nil { | ||
return nil | ||
} | ||
delete(labels, ActiveGatewayKey) | ||
pod.SetLabels(labels) | ||
|
||
if err := cl.Update(ctx, pod); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ListAllGatewaysReplicas returns the list of all the gateways replicas of the same deployment. | ||
func ListAllGatewaysReplicas(ctx context.Context, cl client.Client, namespace, deploymentName string) ([]corev1.Pod, error) { | ||
podList := &corev1.PodList{} | ||
if err := cl.List(ctx, podList, client.InNamespace(namespace), client.MatchingLabels{ | ||
"app": deploymentName, | ||
}); err != nil { | ||
return nil, err | ||
} | ||
return podList.Items, nil | ||
} |
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,22 @@ | ||
// Copyright 2019-2024 The Liqo 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 concurrent | ||
|
||
const ( | ||
// ActiveGatewayKey is the key used to label the active pod gateway. | ||
ActiveGatewayKey = "networking.liqo.io/active" | ||
// ActiveGatewayValue is the value used to label the active pod gateway. | ||
ActiveGatewayValue = "true" | ||
) |
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.