|
| 1 | +package subroutines |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/platform-mesh/golang-commons/controller/lifecycle/runtimeobject" |
| 8 | + "github.com/platform-mesh/golang-commons/errors" |
| 9 | + "github.com/platform-mesh/golang-commons/logger" |
| 10 | + corev1alpha1 "github.com/platform-mesh/platform-mesh-operator/api/v1alpha1" |
| 11 | + "github.com/platform-mesh/platform-mesh-operator/internal/config" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | + ctrl "sigs.k8s.io/controller-runtime" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | +) |
| 16 | + |
| 17 | +const FeatureToggleSubroutineName = "FeatureToggleSubroutine" |
| 18 | + |
| 19 | +type KubeconfigBuilder interface { |
| 20 | + Build(ctx context.Context, client client.Client, kcpUrl string) (*rest.Config, error) |
| 21 | +} |
| 22 | + |
| 23 | +type defaultKubeconfigBuilder struct{} |
| 24 | + |
| 25 | +func (defaultKubeconfigBuilder) Build(ctx context.Context, client client.Client, kcpUrl string) (*rest.Config, error) { |
| 26 | + return buildKubeconfig(ctx, client, kcpUrl) |
| 27 | +} |
| 28 | + |
| 29 | +type FeatureToggleSubroutine struct { |
| 30 | + client client.Client |
| 31 | + workspaceDirectory string |
| 32 | + kcpUrl string |
| 33 | + kubeconfigBuilder KubeconfigBuilder |
| 34 | + kcpHelper KcpHelper |
| 35 | +} |
| 36 | + |
| 37 | +func NewFeatureToggleSubroutine(client client.Client, helper KcpHelper, operatorCfg *config.OperatorConfig, kcpUrl string) *FeatureToggleSubroutine { |
| 38 | + return &FeatureToggleSubroutine{ |
| 39 | + client: client, |
| 40 | + workspaceDirectory: filepath.Join(operatorCfg.WorkspaceDir, "/manifests/features/"), |
| 41 | + kcpUrl: kcpUrl, |
| 42 | + kubeconfigBuilder: defaultKubeconfigBuilder{}, |
| 43 | + kcpHelper: helper, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (r *FeatureToggleSubroutine) GetName() string { |
| 48 | + return FeatureToggleSubroutineName |
| 49 | +} |
| 50 | + |
| 51 | +func (r *FeatureToggleSubroutine) Finalize(_ context.Context, _ runtimeobject.RuntimeObject) (ctrl.Result, errors.OperatorError) { |
| 52 | + return ctrl.Result{}, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (r *FeatureToggleSubroutine) Finalizers() []string { // coverage-ignore |
| 56 | + return []string{} |
| 57 | +} |
| 58 | + |
| 59 | +func (r *FeatureToggleSubroutine) Process(ctx context.Context, runtimeObj runtimeobject.RuntimeObject) (ctrl.Result, errors.OperatorError) { |
| 60 | + log := logger.LoadLoggerFromContext(ctx).ChildLogger("subroutine", r.GetName()) |
| 61 | + |
| 62 | + inst := runtimeObj.(*corev1alpha1.PlatformMesh) |
| 63 | + for _, ft := range inst.Spec.FeatureToggles { |
| 64 | + switch ft.Name { |
| 65 | + case "feature-enable-getting-started": |
| 66 | + // Implement the logic to enable the getting started feature |
| 67 | + log.Info().Msg("Getting started feature enabled") |
| 68 | + return r.FeatureGettingStarted(ctx, inst) |
| 69 | + default: |
| 70 | + log.Warn().Str("featureToggle", ft.Name).Msg("Unknown feature toggle") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return ctrl.Result{}, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (r *FeatureToggleSubroutine) FeatureGettingStarted(ctx context.Context, inst *corev1alpha1.PlatformMesh) (ctrl.Result, errors.OperatorError) { |
| 78 | + log := logger.LoadLoggerFromContext(ctx).ChildLogger("subroutine", r.GetName()) |
| 79 | + |
| 80 | + // Implement the logic to enable the getting started feature |
| 81 | + log.Info().Msg("Getting started feature enabled") |
| 82 | + |
| 83 | + // Build kcp kubeconfig |
| 84 | + cfg, err := buildKubeconfig(ctx, r.client, r.kcpUrl) |
| 85 | + if err != nil { |
| 86 | + log.Error().Err(err).Msg("Failed to build kubeconfig") |
| 87 | + return ctrl.Result{}, errors.NewOperatorError(errors.Wrap(err, "Failed to build kubeconfig"), true, false) |
| 88 | + } |
| 89 | + |
| 90 | + dir := r.workspaceDirectory + "/feature-enable-getting-started" |
| 91 | + |
| 92 | + err = ApplyDirStructure(ctx, dir, "root", cfg, make(map[string]string), inst, r.kcpHelper) |
| 93 | + if err != nil { |
| 94 | + log.Err(err).Msg("Failed to apply dir structure") |
| 95 | + return ctrl.Result{}, errors.NewOperatorError(errors.Wrap(err, "Failed to apply dir structure"), true, false) |
| 96 | + } |
| 97 | + |
| 98 | + return ctrl.Result{}, nil |
| 99 | +} |
0 commit comments