|
1 | 1 | package controllers
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/openshift/sandboxed-containers-operator/internal/featuregates" |
| 4 | + "context" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 8 | + "k8s.io/apimachinery/pkg/types" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + FgConfigMapName = "osc-feature-gates" |
| 13 | + ConfidentialFeatureGate = "confidential" |
5 | 14 | )
|
6 | 15 |
|
| 16 | +var DefaultFeatureGates = map[string]bool{ |
| 17 | + ConfidentialFeatureGate: false, |
| 18 | +} |
| 19 | + |
| 20 | +type FeatureGateStatus struct { |
| 21 | + FeatureGates map[string]bool |
| 22 | +} |
| 23 | + |
7 | 24 | // Create enum to represent the state of the feature gates
|
| 25 | +// While today we just have two states, we retain the flexibility in case we want to introduce some additional states. |
8 | 26 | type FeatureGateState int
|
9 | 27 |
|
10 | 28 | const (
|
11 | 29 | Enabled FeatureGateState = iota
|
12 | 30 | Disabled
|
13 | 31 | )
|
14 | 32 |
|
| 33 | +// This method returns a new FeatureGateStatus object |
| 34 | +// that contains the status of the feature gates |
| 35 | +// defined in the ConfigMap in the namespace |
| 36 | +// Return default values if the ConfigMap is not found. |
| 37 | +// Return values from the ConfigMap if the ConfigMap is not found. Use default values for missing entries in the ConfigMap. |
| 38 | +// Return an error for any other reason, such as an API error. |
| 39 | +func (r *KataConfigOpenShiftReconciler) NewFeatureGateStatus() (*FeatureGateStatus, error) { |
| 40 | + fgStatus := &FeatureGateStatus{ |
| 41 | + FeatureGates: make(map[string]bool), |
| 42 | + } |
| 43 | + |
| 44 | + cfgMap := &corev1.ConfigMap{} |
| 45 | + err := r.Client.Get(context.TODO(), types.NamespacedName{Name: FgConfigMapName, |
| 46 | + Namespace: OperatorNamespace}, cfgMap) |
| 47 | + if err == nil { |
| 48 | + for feature, value := range cfgMap.Data { |
| 49 | + fgStatus.FeatureGates[feature] = value == "true" |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Add default values for missing feature gates |
| 54 | + for feature, defaultValue := range DefaultFeatureGates { |
| 55 | + if _, exists := fgStatus.FeatureGates[feature]; !exists { |
| 56 | + fgStatus.FeatureGates[feature] = defaultValue |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if k8serrors.IsNotFound(err) { |
| 61 | + return fgStatus, nil |
| 62 | + } else { |
| 63 | + return fgStatus, err |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func IsEnabled(fgStatus *FeatureGateStatus, feature string) bool { |
| 68 | + return fgStatus.FeatureGates[feature] |
| 69 | +} |
| 70 | + |
15 | 71 | // Function to handle the feature gates
|
16 | 72 | func (r *KataConfigOpenShiftReconciler) processFeatureGates() error {
|
17 | 73 |
|
18 |
| - fgStatus, err := featuregates.NewFeatureGateStatus(r.Client) |
| 74 | + fgStatus, err := r.NewFeatureGateStatus() |
19 | 75 | if err != nil {
|
20 | 76 | r.Log.Info("There were errors in getting feature gate status.", "err", err)
|
21 | 77 | return err
|
22 | 78 | }
|
23 | 79 |
|
24 | 80 | // Check which feature gates are enabled in the FG ConfigMap and
|
25 | 81 | // perform the necessary actions
|
26 |
| - // The feature gates are defined in internal/featuregates/featuregates.go |
27 |
| - // and are fetched from the ConfigMap in the namespace |
28 |
| - // Eg. TimeTravelFeatureGate |
29 |
| - |
30 |
| - if featuregates.IsEnabled(fgStatus, featuregates.TimeTravelFeatureGate) { |
31 |
| - r.Log.Info("Feature gate is enabled", "featuregate", featuregates.TimeTravelFeatureGate) |
32 |
| - // Perform the necessary actions |
33 |
| - r.handleTimeTravelFeature(Enabled) |
34 |
| - } else { |
35 |
| - r.Log.Info("Feature gate is disabled", "featuregate", featuregates.TimeTravelFeatureGate) |
36 |
| - // Perform the necessary actions |
37 |
| - r.handleTimeTravelFeature(Disabled) |
| 82 | + if r.kataConfig.Spec.EnablePeerPods { |
| 83 | + if IsEnabled(fgStatus, ConfidentialFeatureGate) { |
| 84 | + r.Log.Info("Feature gate is enabled", "featuregate", ConfidentialFeatureGate) |
| 85 | + // Perform the necessary actions |
| 86 | + if err := r.handleFeatureConfidential(Enabled); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + } else { |
| 90 | + r.Log.Info("Feature gate is disabled", "featuregate", ConfidentialFeatureGate) |
| 91 | + // Perform the necessary actions |
| 92 | + if err := r.handleFeatureConfidential(Disabled); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } |
38 | 96 | }
|
39 | 97 |
|
40 | 98 | return err
|
41 | 99 |
|
42 | 100 | }
|
43 |
| - |
44 |
| -// Function to handle the TimeTravel feature gate |
45 |
| -func (r *KataConfigOpenShiftReconciler) handleTimeTravelFeature(state FeatureGateState) { |
46 |
| - // Perform the necessary actions for the TimeTravel feature gate |
47 |
| - if state == Enabled { |
48 |
| - r.Log.Info("Starting TimeTravel") |
49 |
| - } else { |
50 |
| - r.Log.Info("Stopping TimeTravel") |
51 |
| - } |
52 |
| -} |
|
0 commit comments