-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[ws-manager-mk2] Maintenance mode #16702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b91dc86
[ws-manager-mk2] Maintenance mode reconciler
WVerlaek 6ed5f0f
[ws-manager-mk2] Check for maintenance mode
WVerlaek 67d1836
[ws-manager-mk2] Default to maintenance mode on startup
WVerlaek a3f928f
[ws-manager-mk2] Disable maintenance on unmarshal failure
WVerlaek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
104 changes: 104 additions & 0 deletions
104
components/ws-manager-mk2/controllers/maintenance_controller.go
This file contains hidden or 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,104 @@ | ||
// Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/gitpod-io/gitpod/ws-manager/api/config" | ||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
const ( | ||
LabelMaintenance = "gitpod.io/maintenanceConfig" | ||
configMapName = "ws-manager-mk2-maintenance-mode" | ||
) | ||
|
||
func NewMaintenanceReconciler(c client.Client) (*MaintenanceReconciler, error) { | ||
return &MaintenanceReconciler{ | ||
Client: c, | ||
// Enable by default, until we observe the ConfigMap with the actual value. | ||
// Prevents a race on startup where the workspace reconciler might run before | ||
// we observe the maintenance mode ConfigMap. Better be safe and prevent | ||
// reconciliation of that workspace until it's certain maintenance mode is | ||
// not enabled. | ||
enabled: true, | ||
}, nil | ||
} | ||
|
||
type MaintenanceReconciler struct { | ||
client.Client | ||
|
||
enabled bool | ||
} | ||
|
||
func (r *MaintenanceReconciler) IsEnabled() bool { | ||
return r.enabled | ||
} | ||
|
||
//+kubebuilder:rbac:groups=core,resources=configmap,verbs=get;list;watch | ||
|
||
func (r *MaintenanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := log.FromContext(ctx).WithValues("configMap", req.NamespacedName) | ||
|
||
if req.Name != configMapName { | ||
log.Info("ignoring unexpected ConfigMap") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
var cm corev1.ConfigMap | ||
if err := r.Get(ctx, req.NamespacedName, &cm); err != nil { | ||
if errors.IsNotFound(err) { | ||
// ConfigMap does not exist, disable maintenance mode. | ||
r.setEnabled(log, false) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
log.Error(err, "unable to fetch configmap") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
configJson, ok := cm.Data["config.json"] | ||
if !ok { | ||
log.Info("missing config.json, setting maintenance mode as disabled") | ||
r.setEnabled(log, false) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
var cfg config.MaintenanceConfig | ||
if err := json.Unmarshal([]byte(configJson), &cfg); err != nil { | ||
log.Error(err, "failed to unmarshal maintenance config, setting maintenance mode as disabled") | ||
r.setEnabled(log, false) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
r.setEnabled(log, cfg.Enabled) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *MaintenanceReconciler) setEnabled(log logr.Logger, enabled bool) { | ||
if enabled == r.enabled { | ||
// Nothing to do. | ||
return | ||
} | ||
|
||
r.enabled = enabled | ||
log.Info("maintenance mode state change", "enabled", enabled) | ||
} | ||
|
||
func (r *MaintenanceReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
Named("maintenance"). | ||
// The controller manager filters watch events only to ConfigMaps with the LabelMaintenance label set to "true". | ||
// See components/ws-manager-mk2/main.go's NewCache function in the manager options. | ||
For(&corev1.ConfigMap{}). | ||
Complete(r) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
// Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package maintenance | ||
|
||
// Maintenance is used to check whether ws-manager-mk2 is in maintenance mode, | ||
// which prevents pod creation/deletion and snapshots being taken, such that | ||
// the cluster can be updated in-place. | ||
type Maintenance interface { | ||
IsEnabled() bool | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.