Skip to content

Add ansible plugin scaffolding command into ansible-operator binary #149

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/ansible-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/operator-framework/ansible-operator-plugins/internal/cmd/ansible-operator/run"
"github.com/operator-framework/ansible-operator-plugins/internal/cmd/ansible-operator/scaffold"
"github.com/operator-framework/ansible-operator-plugins/internal/cmd/ansible-operator/version"
)

Expand All @@ -36,6 +37,7 @@ operator project's image entrypoint

root.AddCommand(run.NewCmd())
root.AddCommand(version.NewCmd())
root.AddCommand(scaffold.NewCmd())

if err := root.Execute(); err != nil {
log.Fatal(err)
Expand Down
25 changes: 2 additions & 23 deletions hack/generate/samples/ansible/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ import (
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
kustomizev2 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang"

"github.com/operator-framework/ansible-operator-plugins/hack/generate/samples/internal/pkg"
"github.com/operator-framework/ansible-operator-plugins/pkg/plugins/ansible/v1"
"github.com/operator-framework/ansible-operator-plugins/internal/cmd/ansible-operator/scaffold"
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/command"
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/e2e"
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/sample"
Expand All @@ -43,24 +39,7 @@ var memcachedGVK = schema.GroupVersionKind{
}

func getCli() *cli.CLI {
ansibleBundle, _ := plugin.NewBundleWithOptions(
plugin.WithName(golang.DefaultNameQualifier),
plugin.WithVersion(ansible.Plugin{}.Version()),
plugin.WithPlugins(kustomizev2.Plugin{}, ansible.Plugin{}),
)

c, err := cli.New(
cli.WithCommandName("cli"),
cli.WithVersion("v0.0.0"),
cli.WithPlugins(
ansibleBundle,
),
cli.WithDefaultPlugins(cfgv3.Version, ansibleBundle),
cli.WithDefaultProjectVersion(cfgv3.Version),
cli.WithCompletion(),
)
pkg.CheckError("getting cli implementation:", err)
return c
return scaffold.GetPluginsCLI()
Copy link

@camilamacedo86 camilamacedo86 Jun 20, 2025

Choose a reason for hiding this comment

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

We should not include this code here.
Instead, we must ensure that the hack generator uses the actual binary, rather than mocking the CLI and invoking it directly.
The binary should be built locally and used during generation to validate that everything functions correctly—this is the approach taken in the Operator SDK.

Reference: operator-sdk/hack/generate/samples/generate_testdata.go#L30-L37
c/c @acornett21 ^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The binary should be built locally and used during generation to validate that everything functions correctly—this is the approach taken in the Operator SDK.

I think the current approach follows a deliberate design choice made in #4 PR, after which we are now generating the testdata on the fly using the helper library.
This PR is trying to unify the testdata generation with the binary's command execution. We created a single entrypoint (GetPluginsCLI()) that is called by both the main function for the final binary and by the hack script that generates the test samples.
It ensures that our testdata is always generated using the exact same logic and configuration that the end-user's binary will have. It creates a tight coupling that prevents any possible divergence between the test suite and the released artifact.

This change is a continuation of the existing pattern. I think we are not breaking the testdata generation flow or logic.

}

func GenerateMemcachedSamples(rootPath string) []sample.Sample {
Expand Down
69 changes: 69 additions & 0 deletions internal/cmd/ansible-operator/scaffold/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2025 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scaffold

import (
"fmt"
"log"
"runtime"

"github.com/operator-framework/ansible-operator-plugins/pkg/plugins/ansible/v1"
"github.com/spf13/cobra"

ver "github.com/operator-framework/ansible-operator-plugins/internal/version"
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
kustomizev2 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2"
)

func NewCmd() *cobra.Command {
cli := GetPluginsCLI()
return cli.Command()
}

func GetPluginsCLI() *cli.CLI {
ansibleBundle, _ := plugin.NewBundleWithOptions(
plugin.WithName(ansible.Plugin{}.Name()),

Choose a reason for hiding this comment

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

We might not want to have base.ansible.sdk.operatorframework.io/v1 since it is not a base for Ansible ( only Ansible code without the config/ generated by kustomize/v2 ), so we might wish to to change it to be called ansible.operatorframework.io/v1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Updated the plugin name to reflect ansible.operatorframework.io/v1

plugin.WithVersion(ansible.Plugin{}.Version()),
plugin.WithPlugins(
kustomizev2.Plugin{},
ansible.Plugin{},
),
)

c, err := cli.New(
cli.WithCommandName("scaffold"),
cli.WithDescription("scaffolds ansible-operator"),
cli.WithVersion(makeVersionString()),
cli.WithPlugins(
ansibleBundle,
),
cli.WithDefaultPlugins(cfgv3.Version, ansibleBundle),
cli.WithDefaultProjectVersion(cfgv3.Version),
cli.WithCompletion(),
)

if err != nil {
log.Fatal(err)
}

return c
}

func makeVersionString() string {
return fmt.Sprintf("scaffold version: %q, commit: %q, kubernetes version: %q, go version: %q, GOOS: %q, GOARCH: %q",
ver.GitVersion, ver.GitCommit, ver.KubernetesVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
4 changes: 1 addition & 3 deletions pkg/plugins/ansible/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import (
"sigs.k8s.io/kubebuilder/v4/pkg/config"
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"

"github.com/operator-framework/ansible-operator-plugins/pkg/plugins"
)

const pluginName = "base.ansible" + plugins.DefaultNameQualifier
const pluginName = "ansible.operatorframework.io"

var (
pluginVersion = plugin.Version{Number: 1}
Expand Down
2 changes: 1 addition & 1 deletion testdata/memcached-molecule-operator/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: example.com
layout:
- go.kubebuilder.io/v1
- ansible.operatorframework.io/v1
multigroup: true
projectName: memcached-molecule-operator
resources:
Expand Down