|
| 1 | +package eso_deployer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + helmv2 "github.com/fluxcd/helm-controller/api/v2" |
| 10 | + sourcev1 "github.com/fluxcd/source-controller/api/v1" |
| 11 | + "github.com/openmcp-project/controller-utils/pkg/clusters" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + |
| 16 | + "github.com/openmcp-project/bootstrapper/internal/component" |
| 17 | + cfg "github.com/openmcp-project/bootstrapper/internal/config" |
| 18 | + "github.com/openmcp-project/bootstrapper/internal/flux_deployer" |
| 19 | + ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli" |
| 20 | + "github.com/openmcp-project/bootstrapper/internal/util" |
| 21 | +) |
| 22 | + |
| 23 | +type EsoDeployer struct { |
| 24 | + Config *cfg.BootstrapperConfig |
| 25 | + |
| 26 | + // OcmConfigPath is the path to the OCM configuration file |
| 27 | + OcmConfigPath string |
| 28 | + |
| 29 | + platformCluster *clusters.Cluster |
| 30 | + log *logrus.Logger |
| 31 | +} |
| 32 | + |
| 33 | +func NewEsoDeployer(config *cfg.BootstrapperConfig, ocmConfigPath string, platformCluster *clusters.Cluster, log *logrus.Logger) *EsoDeployer { |
| 34 | + return &EsoDeployer{ |
| 35 | + Config: config, |
| 36 | + OcmConfigPath: ocmConfigPath, |
| 37 | + platformCluster: platformCluster, |
| 38 | + log: log, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (d *EsoDeployer) Deploy(ctx context.Context) error { |
| 43 | + componentManager, err := component.NewComponentManager(ctx, d.Config, d.OcmConfigPath) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("error creating component manager: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + return d.DeployWithComponentManager(ctx, componentManager) |
| 49 | +} |
| 50 | + |
| 51 | +func (d *EsoDeployer) DeployWithComponentManager(ctx context.Context, componentManager component.ComponentManager) error { |
| 52 | + d.log.Info("Getting OCM component containing ESO resources.") |
| 53 | + esoComponent, err := componentManager.GetComponentWithImageResources(ctx, "external-secrets-operator-image") |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get external-secrets-operator-image component: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + esoChartRes, err := esoComponent.GetResource("external-secrets-operator-chart") |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to get external-secrets-operator-chart resource: %w", err) |
| 61 | + } |
| 62 | + d.log.Info("Deploying OCIRepo for ESO chart.") |
| 63 | + if err = d.deployRepo(ctx, esoChartRes, esoChartRepoName); err != nil { |
| 64 | + return fmt.Errorf("failed to create helm chart repo: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + esoImageRes, err := esoComponent.GetResource("external-secrets-operator-image") |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to get external-secrets-operator-image resource: %w", err) |
| 70 | + } |
| 71 | + d.log.Info("Deploying OCIRepo for ESO image.") |
| 72 | + if err = d.deployRepo(ctx, esoImageRes, esoImageRepoName); err != nil { |
| 73 | + return fmt.Errorf("failed to create helm image repo: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + d.log.Info("Deploying HelmRelease for ESO.") |
| 77 | + if err = d.deployHelmRelease(ctx, esoImageRes); err != nil { |
| 78 | + return fmt.Errorf("failed to deploy helm release: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + d.log.Info("Done.") |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (d *EsoDeployer) deployHelmRelease(ctx context.Context, res *ocmcli.Resource) error { |
| 86 | + name, tag, _, err := util.ParseImageVersionAndTag(*res.Access.ImageReference) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to parse image resource: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + values := map[string]any{ |
| 92 | + "image": map[string]any{ |
| 93 | + "repository": name, |
| 94 | + "tag": tag, |
| 95 | + }, |
| 96 | + } |
| 97 | + values["imagePullSecrets"] = d.Config.ExternalSecrets.ImagePullSecrets |
| 98 | + |
| 99 | + encoded, err := json.Marshal(values) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("failed to marshal ESO Helm values: %w", err) |
| 102 | + } |
| 103 | + jsonVals := &apiextensionsv1.JSON{Raw: encoded} |
| 104 | + |
| 105 | + helmRelease := &helmv2.HelmRelease{ |
| 106 | + ObjectMeta: metav1.ObjectMeta{ |
| 107 | + Name: esoHelmReleaseName, |
| 108 | + Namespace: flux_deployer.FluxSystemNamespace, |
| 109 | + }, |
| 110 | + Spec: helmv2.HelmReleaseSpec{ |
| 111 | + ChartRef: &helmv2.CrossNamespaceSourceReference{ |
| 112 | + Kind: "OCIRepository", |
| 113 | + Name: esoChartRepoName, |
| 114 | + Namespace: flux_deployer.FluxSystemNamespace, |
| 115 | + }, |
| 116 | + ReleaseName: "eso", |
| 117 | + TargetNamespace: esoNamespace, |
| 118 | + Install: &helmv2.Install{ |
| 119 | + CreateNamespace: true, |
| 120 | + }, |
| 121 | + Values: jsonVals, |
| 122 | + }, |
| 123 | + } |
| 124 | + return util.CreateOrUpdate(ctx, d.platformCluster, helmRelease) |
| 125 | +} |
| 126 | + |
| 127 | +func (d *EsoDeployer) deployRepo(ctx context.Context, res *ocmcli.Resource, repoName string) error { |
| 128 | + name, tag, digest, err := util.ParseImageVersionAndTag(*res.Access.ImageReference) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + ociRepo := &sourcev1.OCIRepository{ |
| 134 | + ObjectMeta: metav1.ObjectMeta{ |
| 135 | + Name: repoName, |
| 136 | + Namespace: flux_deployer.FluxSystemNamespace, |
| 137 | + }, |
| 138 | + Spec: sourcev1.OCIRepositorySpec{ |
| 139 | + URL: fmt.Sprintf("oci://%s", name), |
| 140 | + Reference: &sourcev1.OCIRepositoryRef{ |
| 141 | + Tag: tag, |
| 142 | + Digest: digest, |
| 143 | + }, |
| 144 | + Timeout: &metav1.Duration{Duration: 1 * time.Minute}, |
| 145 | + SecretRef: d.Config.ExternalSecrets.RepositorySecretRef, |
| 146 | + }, |
| 147 | + } |
| 148 | + return util.CreateOrUpdate(ctx, d.platformCluster, ociRepo) |
| 149 | +} |
0 commit comments