generated from openmcp-project/repository-template
    
        
        - 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
feat: add deploy-eso #91
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
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      94e81cb
              
                feat: add deploy-eso command, read eso resources from ocm root compon…
              
              
                rdksap 6f82a07
              
                feat: add ExternalSecrets structure to bootstrapper config to allow s…
              
              
                rdksap 05aacdd
              
                feat: use pointer ref
              
              
                rdksap dd5fbed
              
                feat: add deploy-eso part to readme
              
              
                rdksap 4e1faf1
              
                Merge branch 'main' into feat/deploy-eso
              
              
                rdksap e9fdc44
              
                fix: revert go version change
              
              
                rdksap 36358fc
              
                Update cmd/deploy_eso.go
              
              
                rdksap ef267a8
              
                fix: re-add indentation
              
              
                rdksap 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
    
  
  
    
              
  
    
      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,54 @@ | ||
| package cmd | ||
| 
     | 
||
| import ( | ||
| "fmt" | ||
| 
     | 
||
| "github.com/spf13/cobra" | ||
| 
     | 
||
| cfg "github.com/openmcp-project/bootstrapper/internal/config" | ||
| 
     | 
||
| esodeployer "github.com/openmcp-project/bootstrapper/internal/eso-deployer" | ||
| logging "github.com/openmcp-project/bootstrapper/internal/log" | ||
| "github.com/openmcp-project/bootstrapper/internal/scheme" | ||
| "github.com/openmcp-project/bootstrapper/internal/util" | ||
| ) | ||
| 
     | 
||
| // deployEsoCmd represents the deploy-eso command | ||
| var deployEsoCmd = &cobra.Command{ | ||
| Use: "deploy-eso", | ||
| Short: "Deploys External Secrets Operator controllers on the target cluster", | ||
| Long: "Deploys External Secrets Operator controllers on the target cluster", | ||
| Args: cobra.ExactArgs(1), | ||
| ArgAliases: []string{ | ||
| "configFile", | ||
| }, | ||
| Example: ` openmcp-bootstrapper deploy-eso "./config.yaml"`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| configFilePath := args[0] | ||
| config := &cfg.BootstrapperConfig{} | ||
| err := config.ReadFromFile(configFilePath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read config file: %w", err) | ||
| } | ||
| log := logging.GetLogger() | ||
| log.Info("Starting deployment of external secrets operator controllers.") | ||
| 
     | 
||
| targetCluster, err := util.GetCluster(cmd.Flag(FlagKubeConfig).Value.String(), "target-cluster", scheme.NewFluxScheme()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get platform cluster: %w", err) | ||
| } | ||
| 
     | 
||
| if err = esodeployer.NewEsoDeployer(config, cmd.Flag(FlagOcmConfig).Value.String(), targetCluster, log).Deploy(cmd.Context()); err != nil { | ||
| return fmt.Errorf("failed deploying eso: %w", err) | ||
| } | ||
| 
     | 
||
| return nil | ||
| }, | ||
| } | ||
| 
     | 
||
| func init() { | ||
| RootCmd.AddCommand(deployEsoCmd) | ||
| deployEsoCmd.Flags().SortFlags = false | ||
| deployEsoCmd.Flags().String(FlagOcmConfig, "", "OCM configuration file") | ||
| deployEsoCmd.Flags().String(FlagKubeConfig, "", "Kubernetes configuration file") | ||
| } | ||
  
    
      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
    
  
  
    
              
  
    
      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,8 @@ | ||
| package eso_deployer | ||
| 
     | 
||
| const ( | ||
| esoNamespace = "external-secrets" | ||
| esoImageRepoName = "external-secrets-image" | ||
| esoChartRepoName = "external-secrets-chart" | ||
| esoHelmReleaseName = "external-secrets-operator" | ||
| ) | 
  
    
      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,149 @@ | ||
| package eso_deployer | ||
| 
     | 
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
| 
     | 
||
| helmv2 "github.com/fluxcd/helm-controller/api/v2" | ||
| sourcev1 "github.com/fluxcd/source-controller/api/v1" | ||
| "github.com/openmcp-project/controller-utils/pkg/clusters" | ||
| "github.com/sirupsen/logrus" | ||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| 
     | 
||
| "github.com/openmcp-project/bootstrapper/internal/component" | ||
| cfg "github.com/openmcp-project/bootstrapper/internal/config" | ||
| "github.com/openmcp-project/bootstrapper/internal/flux_deployer" | ||
| ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli" | ||
| "github.com/openmcp-project/bootstrapper/internal/util" | ||
| ) | ||
| 
     | 
||
| type EsoDeployer struct { | ||
| Config *cfg.BootstrapperConfig | ||
| 
     | 
||
| // OcmConfigPath is the path to the OCM configuration file | ||
| OcmConfigPath string | ||
| 
     | 
||
| platformCluster *clusters.Cluster | ||
| log *logrus.Logger | ||
| } | ||
| 
     | 
||
| func NewEsoDeployer(config *cfg.BootstrapperConfig, ocmConfigPath string, platformCluster *clusters.Cluster, log *logrus.Logger) *EsoDeployer { | ||
| return &EsoDeployer{ | ||
| Config: config, | ||
| OcmConfigPath: ocmConfigPath, | ||
| platformCluster: platformCluster, | ||
| log: log, | ||
| } | ||
| } | ||
| 
     | 
||
| func (d *EsoDeployer) Deploy(ctx context.Context) error { | ||
| componentManager, err := component.NewComponentManager(ctx, d.Config, d.OcmConfigPath) | ||
| if err != nil { | ||
| return fmt.Errorf("error creating component manager: %w", err) | ||
| } | ||
| 
     | 
||
| return d.DeployWithComponentManager(ctx, componentManager) | ||
| } | ||
| 
     | 
||
| func (d *EsoDeployer) DeployWithComponentManager(ctx context.Context, componentManager component.ComponentManager) error { | ||
| d.log.Info("Getting OCM component containing ESO resources.") | ||
| esoComponent, err := componentManager.GetComponentWithImageResources(ctx, "external-secrets-operator-image") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get external-secrets-operator-image component: %w", err) | ||
| } | ||
| 
     | 
||
| esoChartRes, err := esoComponent.GetResource("external-secrets-operator-chart") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get external-secrets-operator-chart resource: %w", err) | ||
| } | ||
| d.log.Info("Deploying OCIRepo for ESO chart.") | ||
| if err = d.deployRepo(ctx, esoChartRes, esoChartRepoName); err != nil { | ||
| return fmt.Errorf("failed to create helm chart repo: %w", err) | ||
| } | ||
| 
     | 
||
| esoImageRes, err := esoComponent.GetResource("external-secrets-operator-image") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get external-secrets-operator-image resource: %w", err) | ||
| } | ||
| d.log.Info("Deploying OCIRepo for ESO image.") | ||
| if err = d.deployRepo(ctx, esoImageRes, esoImageRepoName); err != nil { | ||
| return fmt.Errorf("failed to create helm image repo: %w", err) | ||
| } | ||
| 
     | 
||
| d.log.Info("Deploying HelmRelease for ESO.") | ||
| if err = d.deployHelmRelease(ctx, esoImageRes); err != nil { | ||
| return fmt.Errorf("failed to deploy helm release: %w", err) | ||
| } | ||
| 
     | 
||
| d.log.Info("Done.") | ||
| return nil | ||
| } | ||
| 
     | 
||
| func (d *EsoDeployer) deployHelmRelease(ctx context.Context, res *ocmcli.Resource) error { | ||
| name, tag, _, err := util.ParseImageVersionAndTag(*res.Access.ImageReference) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse image resource: %w", err) | ||
| } | ||
| 
     | 
||
| values := map[string]any{ | ||
| "image": map[string]any{ | ||
| "repository": name, | ||
| "tag": tag, | ||
| }, | ||
| } | ||
| values["imagePullSecrets"] = d.Config.ExternalSecrets.ImagePullSecrets | ||
| 
     | 
||
| encoded, err := json.Marshal(values) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal ESO Helm values: %w", err) | ||
| } | ||
| jsonVals := &apiextensionsv1.JSON{Raw: encoded} | ||
| 
     | 
||
| helmRelease := &helmv2.HelmRelease{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: esoHelmReleaseName, | ||
| Namespace: flux_deployer.FluxSystemNamespace, | ||
| }, | ||
| Spec: helmv2.HelmReleaseSpec{ | ||
| ChartRef: &helmv2.CrossNamespaceSourceReference{ | ||
| Kind: "OCIRepository", | ||
| Name: esoChartRepoName, | ||
| Namespace: flux_deployer.FluxSystemNamespace, | ||
| }, | ||
| ReleaseName: "eso", | ||
| TargetNamespace: esoNamespace, | ||
| Install: &helmv2.Install{ | ||
| CreateNamespace: true, | ||
| }, | ||
| Values: jsonVals, | ||
| }, | ||
| } | ||
| return util.CreateOrUpdate(ctx, d.platformCluster, helmRelease) | ||
| } | ||
| 
     | 
||
| func (d *EsoDeployer) deployRepo(ctx context.Context, res *ocmcli.Resource, repoName string) error { | ||
| name, tag, digest, err := util.ParseImageVersionAndTag(*res.Access.ImageReference) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| 
     | 
||
| ociRepo := &sourcev1.OCIRepository{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: repoName, | ||
| Namespace: flux_deployer.FluxSystemNamespace, | ||
| }, | ||
| Spec: sourcev1.OCIRepositorySpec{ | ||
| URL: fmt.Sprintf("oci://%s", name), | ||
| Reference: &sourcev1.OCIRepositoryRef{ | ||
| Tag: tag, | ||
| Digest: digest, | ||
| }, | ||
| Timeout: &metav1.Duration{Duration: 1 * time.Minute}, | ||
| SecretRef: d.Config.ExternalSecrets.RepositorySecretRef, | ||
| }, | ||
| } | ||
| return util.CreateOrUpdate(ctx, d.platformCluster, ociRepo) | ||
| } | 
      
      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.