Skip to content

Incorporate convert command#5275

Merged
phisco merged 2 commits into
crossplane:masterfrom
lsviben:migrate-cmd
Jan 23, 2024
Merged

Incorporate convert command#5275
phisco merged 2 commits into
crossplane:masterfrom
lsviben:migrate-cmd

Conversation

@lsviben

@lsviben lsviben commented Jan 22, 2024

Copy link
Copy Markdown
Contributor

Description of your changes

Transfers the migrate command from https://github.com/crossplane-contrib/crossplane-migrator to
the Crossplane CLI as convert

The PR mostly copies over the commands into the Crossplane CLI. Some notable changes:

  • renamed the sub-commands from new-pipeline-composition/new-deployment-runtime to pipeline-composition/deployment-runtime
  • renamed most of the functions in the converters (mostly it was to make them private)
  • simplified some parts to reduce complexity + pass lint
  • extracted the input reading and output yaml writing to a separate package (perhaps we have something similar that we can use already?)
  • added missing ServiceAccountName to ServiceAccountTemplate
  • expanded on the help info

Big thanks to @stevendborrelli for the original implementation!

Fixes #4922

I have:

Need help with this checklist? See the cheat sheet.

How the command works

crossplane beta --help

_output/bin/darwin_arm64/crank beta --help
Usage: crossplane beta <command>

Beta commands.

WARNING: These commands may be changed or removed in a future release.

Commands:
  beta convert deployment-runtime
                    Convert a ControllerConfig to a DeploymentRuntimeConfig.
  beta convert pipeline-composition
                    Convert a Patch-and-Transform Composition to a Function Pipeline
                    Composition.
  beta render       Render a composite resource (XR).
  beta trace        Trace a Crossplane resource to get a detailed output of its
                    relationships, helpful for troubleshooting.
  beta xpkg init    Initialize a new package from a template.

Flags:
  -h, --help       Show context-sensitive help.
      --verbose    Print verbose logging statements.
  -v, --version    Print version and quit.

crossplane beta convert --help

_output/bin/darwin_arm64/crank beta convert --help
Usage: crossplane beta convert <command>

Convert a Crossplane resource to a newer version or kind.

This command converts a Crossplane resource to a newer version or a different
kind.

Currently supported conversions: * ControllerConfig -> DeploymentRuntimeConfig *
Classic Compositions -> Function Pipeline Compositions

Examples:

    # Write out a DeploymentRuntimeConfigFile from a ControllerConfig
    crossplane beta convert deployment-runtime cc.yaml -o drc.yaml

    # Convert an existing Composition to use Pipelines
    crossplane beta convert pipeline-composition composition.yaml -o pipeline-composition.yaml

Commands:
  beta convert deployment-runtime
      Convert a ControllerConfig to a DeploymentRuntimeConfig.
  beta convert pipeline-composition
      Convert a Patch-and-Transform Composition to a Function Pipeline
      Composition.

Flags:
  -h, --help       Show context-sensitive help.
      --verbose    Print verbose logging statements.
  -v, --version    Print version and quit.

ControllerConfig -> DeploymentRuntimeConfig

~/go/crossplane/_output/bin/darwin_arm64/crank beta convert deployment-runtime examples/controllerconfig-1.yaml
apiVersion: pkg.crossplane.io/v1beta1
kind: DeploymentRuntimeConfig
metadata:
  creationTimestamp: "2024-01-22T20:20:11Z"
  name: controllerconfig-example-1
spec:
  deploymentTemplate:
    spec:
      selector: {}
      strategy: {}
      template:
        metadata:
          creationTimestamp: "2024-01-22T20:20:11Z"
        spec:
          containers:
          - args:
            - -d
            - --enable-external-secret-stores
            name: package-runtime
            resources: {}
          serviceAccountName: bob
  serviceAccountTemplate:
    metadata:
      name: bob

Patch-and-Transform Composition to Function Pipeline Composition

 ~/go/crossplane/_output/bin/darwin_arm64/crank beta convert pipeline-composition examples/in-composition/composition.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  creationTimestamp: "2024-01-22T20:42:36Z"
  name: argocdclusters.example.crossplane.io
spec:
  compositeTypeRef:
    apiVersion: example.crossplane.io/v1alpha1
    kind: ArgocdCluster
  mode: Pipeline
  pipeline:
  - functionRef:
      name: function-patch-and-transform
    input:
      apiVersion: pt.fn.crossplane.io/v1beta1
      environment: null
      kind: Resources
      patchSets: []
      resources:
      - base:
          apiVersion: compute.gcp.crossplane.io/v1beta1
          kind: Network
          spec:
            forProvider:
              autoCreateSubnetworks: false
              routingConfig:
                routingMode: REGIONAL
        name: resource-0
      - base:
          apiVersion: container.gcp.crossplane.io/v1beta1
          kind: GKECluster
          spec:
            forProvider:
              addonsConfig:
                kubernetesDashboard:
...

@lsviben

lsviben commented Jan 22, 2024

Copy link
Copy Markdown
Contributor Author

While writing comments/help I noticed that I am using convert more then migrate to explain the behaviour. The original command was from crossplane-migrator so I kinda went with crossplane beta migrate but I wonder should the command be crossplane beta convert ? IMO that makes more sense as it does not actually migrate anything anywhere, but convert from one kind/version to another? WDYT?

Maybe I also got into my head because the issue this fixes was to "Migrate a command" 😕 , so from migrating a command I started to think migrate command 🕵️

@stevendborrelli

Copy link
Copy Markdown
Member

I will put my vote in for convert.

Comment thread cmd/crank/beta/migrate/deploymentruntime/cmd.go Outdated
Comment thread cmd/crank/beta/beta.go Outdated
Comment thread cmd/crank/beta/migrate/deploymentruntime/cmd.go Outdated
Comment thread cmd/crank/beta/migrate/pipelinecomposition/cmd.go Outdated
Comment thread cmd/crank/beta/migrate/deploymentruntime/converter.go
Comment thread cmd/crank/beta/migrate/deploymentruntime/converter.go
Comment thread cmd/crank/beta/migrate/pipelinecomposition/cmd.go
Comment thread cmd/crank/beta/migrate/pipelinecomposition/converter.go
Comment thread cmd/crank/beta/migrate/deploymentruntime/cmd.go Outdated
@lsviben

lsviben commented Jan 23, 2024

Copy link
Copy Markdown
Contributor Author

Renamed the command to convert

@lsviben lsviben changed the title Incorporate migrate command Incorporate convert command Jan 23, 2024

@phisco phisco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! thanks @lsviben and @stevendborrelli for the original implementation!

A nice future addition would be to avoid outputting all the [], {} and nulls, but that's just cosmetic.

Would be nice to use this to convert all the compositions we have in the e2es and run them all with those 🤔

Comment thread cmd/crank/beta/convert/convert.go

@Piotr1215 Piotr1215 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @stevendborrelli and @lsviben for this awesome functionality. This will be very helpful, especially when converting compositions. Did a quick test and functionality works as expected, converted sample GCP composition creating a storage bucket and applied without issues.

Comment thread cmd/crank/beta/beta.go
Render render.Cmd `cmd:"" help:"Render a composite resource (XR)."`
Trace trace.Cmd `cmd:"" help:"Trace a Crossplane resource to get a detailed output of its relationships, helpful for troubleshooting."`
XPKG xpkg.Cmd `cmd:"" help:"Manage Crossplane packages."`
Convert convert.Cmd `cmd:"" help:"Convert a Crossplane resource to a newer version or kind."`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help message is very generic, is it intended to capture potential future implementations?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, convert <something> is supposed to be the place where we put the conversion helpers when we need

)

// Cmd converts a Crossplane resource to a newer version or a different kind.
type Cmd struct {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a small suggestion for future improvement. Since we know the type we are converting from (composition, controllerconfig, etc), could the command infer it from the kind rather than require user to provide the type? It would simplify the options for conversion and wouldn't require parameter changes when we add more conversion pairs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about something like this. But then got discouraged when I saw that pipelinecomposition has an additional flag. So its seems better to have separate sub-commands that can decide on their flags. Having it all under one cmd and infering, then applying flags seemed messy. But not saying its not the way to go in the future if we decide so

Signed-off-by: lsviben <sviben.lovro@gmail.com>
- add serviceAccountName to template
- lots of naming changes
- fixing lint issues
- change name of command to convert
- fix nil pointer in name gen for pipeline
- modify help info
- change input file flags to args
- extract common io code to separate package

Signed-off-by: lsviben <sviben.lovro@gmail.com>
@phisco phisco merged commit 310185e into crossplane:master Jan 23, 2024
@haarchri

Copy link
Copy Markdown
Member

@Piotr1215 we migrated all upbound/platform-ref-aws/azure/gcp compositions ;) all good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Migration tools to the Crossplane CLI

6 participants