Skip to content

Commit

Permalink
feat: Add options for 'generator installer', remove unused -o/-f opti…
Browse files Browse the repository at this point in the history
…ons from 'generator' base command help, and change some wording (#596)

Made a few changes to the 'generator installer' command to make it
easier to script with. The command now has `--image-name` and
`--image-tag` options so users can specify these values when the
kustomization YAML is generated, instead of manually editing the file
after the fact. Some of the other options for this command were not
utilizing the McMaster CLI default value feature, so those values have
been added in-line to make it clearer to users what the defaults are if
they do not provide them. I also reworded some of the option
descriptions to be more unambiguous about what is expected to be in
those folders.

Additionally, the `Generator` class no longer inherits from
`GeneratorBase`, and `GeneratorBase` has been renamed to `OutputBase`.
This is because the `--out` and `--format` options were shown under the
`generator` command, but it seems they didn't actually do anything for
_that_ command. Instead, it seems those options are only used by the
sub-commands themselves (`docker`, `crds`, `rbac`, etc) when the
YAML/JSON is generated. So now, only the `crd`, `rbac`, `docker`,
`installer`, and `operator` commands inherit from `OutputBase`. This
will only be a breaking change for users who are unwittingly using the
options on the `generator` command that go unused by the SDK.

If that shouldn't be a breaking change, some dummy options can be
created and hidden under the `generator` command to prevent CLI parsing
errors. However, this will result in unexpected behavior for new users
if they accidentally place their -o/-f options on the wrong command
(resulting in the contents being output to the console), which is
somewhat similar to how it behaved before this PR.

---------

Co-authored-by: Christoph Bühler <[email protected]>
  • Loading branch information
ian-buse and buehler authored Sep 13, 2023
1 parent c804b59 commit 7812f42
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/KubeOps/Operator/Commands/Generators/CrdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace KubeOps.Operator.Commands.Generators;

[Command("crd", "crds", Description = "Generates the needed CRD for kubernetes.")]
internal class CrdGenerator : GeneratorBase
internal class CrdGenerator : OutputBase
{
private readonly ICrdBuilder _crdBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace KubeOps.Operator.Commands.Generators;

[Command("docker", Description = "Generates the docker file for building.")]
internal class DockerGenerator : GeneratorBase
internal class DockerGenerator : OutputBase
{
private readonly OperatorSettings _settings;
private readonly bool _hasWebhooks;
Expand Down
2 changes: 1 addition & 1 deletion src/KubeOps/Operator/Commands/Generators/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace KubeOps.Operator.Commands.Generators;
[Subcommand(typeof(InstallerGenerator))]
[Subcommand(typeof(OperatorGenerator))]
[Subcommand(typeof(RbacGenerator))]
internal class Generator : GeneratorBase
internal class Generator
{
public Task<int> OnExecuteAsync(CommandLineApplication app)
{
Expand Down
36 changes: 21 additions & 15 deletions src/KubeOps/Operator/Commands/Generators/InstallerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@

namespace KubeOps.Operator.Commands.Generators;

[Command("installer", Description = "Generates kustomization yaml for the whole installation of the operator.")]
internal class InstallerGenerator : GeneratorBase
[Command("installer", Description = "Generates kustomization YAML for installing the entire operator.")]
internal class InstallerGenerator : OutputBase
{
private readonly OperatorSettings _settings;

public InstallerGenerator(OperatorSettings settings) => _settings = settings;

[Option("--crds-dir", Description = "The path where the crds are located.")]
public string? CrdsPath { get; set; }
[Option("--crds-dir", Description = "The path where the CRD YAML files are located.")]
public string CrdsPath { get; set; } = "../crds";

[Option("--rbac-dir", Description = "The path where the rbac yamls are located.")]
public string? RbacPath { get; set; }
[Option("--rbac-dir", Description = "The path where the RBAC YAML files are located.")]
public string RbacPath { get; set; } = "../rbac";

[Option("--operator-dir", Description = "The path where the operator yamls are located.")]
public string? OperatorPath { get; set; }
[Option("--operator-dir", Description = "The path where the operator YAML files are located.")]
public string OperatorPath { get; set; } = "../operator";

[Option("--image-name", Description = "The name of the operator's Docker image.")]
public string ImageName { get; set; } = "public-docker-image-path";

[Option("--image-tag", Description = "The tag for the Docker image.")]
public string ImageTag { get; set; } = "latest";

public async Task<int> OnExecuteAsync(CommandLineApplication app)
{
Expand All @@ -44,19 +50,19 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
Resources = new List<string>
{
$"./namespace.{Format.ToString().ToLower()}",
CrdsPath == null || OutputPath == null
? "../crds"
OutputPath == null
? CrdsPath
: Path.GetRelativePath(OutputPath, CrdsPath).Replace('\\', '/'),
RbacPath == null || OutputPath == null
? "../rbac"
OutputPath == null
? RbacPath
: Path.GetRelativePath(OutputPath, RbacPath).Replace('\\', '/'),
OperatorPath == null || OutputPath == null
? "../operator"
OutputPath == null
? OperatorPath
: Path.GetRelativePath(OutputPath, OperatorPath).Replace('\\', '/'),
},
Images = new List<KustomizationImage>
{
new() { Name = "operator", NewName = "public-docker-image-path", NewTag = "latest", },
new() { Name = "operator", NewName = ImageName, NewTag = ImageTag, },
},
},
Format));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace KubeOps.Operator.Commands.Generators;

[Command("operator", "op", Description = "Generates the needed yamls to run the operator.")]
internal class OperatorGenerator : GeneratorBase
internal class OperatorGenerator : OutputBase
{
private readonly OperatorSettings _settings;
private readonly bool _hasWebhooks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace KubeOps.Operator.Commands.Generators;

internal abstract class GeneratorBase
internal abstract class OutputBase
{
[Option(CommandOptionType.SingleValue, Description = "Determines the output format for the generator.")]
[Option(
CommandOptionType.SingleValue,
Description = "Sets the output format for the generator.")]
public SerializerOutputFormat Format { get; set; }

[Option(
Description = @"The ""root"" path for the generator to put files in - if empty, prints to console.",
Description = @"The path the command will write the files to. If empty, prints output to console.",
LongName = "out")]
public string? OutputPath { get; set; }
}
2 changes: 1 addition & 1 deletion src/KubeOps/Operator/Commands/Generators/RbacGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace KubeOps.Operator.Commands.Generators;

[Command("rbac", Description = "Generates the needed rbac roles for the operator.")]
internal class RbacGenerator : GeneratorBase
internal class RbacGenerator : OutputBase
{
private readonly IRbacBuilder _rbacBuilder;

Expand Down

0 comments on commit 7812f42

Please sign in to comment.