Skip to content

Commit f250720

Browse files
committed
Add standalone ansible-cli command and release binaries
Signed-off-by: chiragkyal <ckyal@redhat.com>
1 parent 87d6d1f commit f250720

File tree

8 files changed

+134
-31
lines changed

8 files changed

+134
-31
lines changed

.github/workflows/test-ansible.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ jobs:
3636
pip3 install --user --upgrade setuptools pip
3737
pip3 install --user ansible-core~=2.17.4
3838
make test-e2e-ansible-molecule
39+
build-bin:
40+
name: build-bin
41+
runs-on: ubuntu-22.04
42+
steps:
43+
- name: Check out code
44+
uses: actions/checkout@v4
45+
with:
46+
fetch-depth: 0
47+
- name: Set up Go
48+
uses: actions/setup-go@v5
49+
with:
50+
go-version-file: "go.mod"
51+
- name: Run make build
52+
run: make build

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/dist
77
**/bin/
88
ansible-operator
9+
ansible-cli
910

1011
# Test artifacts
1112
**/testbin/

.goreleaser.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ before:
33
- go mod tidy
44
- go mod download
55
builds:
6-
- id: binary
6+
- id: ansible-operator
77
main: ./cmd/ansible-operator/
88
binary: ansible-operator
99
asmflags: "{{ .Env.GO_BUILD_ASMFLAGS }}"
@@ -17,6 +17,20 @@ builds:
1717
- arm64
1818
- ppc64le
1919
- s390x
20+
- id: ansible-cli
21+
main: ./cmd/ansible-cli/
22+
binary: ansible-cli
23+
asmflags: "{{ .Env.GO_BUILD_ASMFLAGS }}"
24+
gcflags: "{{ .Env.GO_BUILD_GCFLAGS }}"
25+
ldflags: "{{ .Env.GO_BUILD_LDFLAGS }}"
26+
mod_timestamp: "{{ .CommitTimestamp }}"
27+
goos:
28+
- linux
29+
goarch:
30+
- amd64
31+
- arm64
32+
- ppc64le
33+
- s390x
2034
dockers:
2135
- image_templates:
2236
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ clean: ## Cleanup build artifacts and tool binaries.
7171
##@ Build
7272

7373
.PHONY: install
74-
install: ## Install ansible-operator
75-
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go install $(GO_BUILD_ARGS) ./cmd/ansible-operator
74+
install: ## Install ansible-operator and ansible-cli.
75+
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go install $(GO_BUILD_ARGS) ./cmd/{ansible-operator,ansible-cli}
7676

7777
.PHONY: build
78-
build: ## Build ansible-operator
78+
build: ## Build ansible-operator and ansible-cli.
7979
@mkdir -p $(BUILD_DIR)
80-
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go build $(GO_BUILD_ARGS) -o $(BUILD_DIR) ./cmd/ansible-operator
80+
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go build $(GO_BUILD_ARGS) -o $(BUILD_DIR) ./cmd/{ansible-operator,ansible-cli}
8181

82-
.PHONY: build/ansible-operator
83-
build/ansible-operator:
82+
.PHONY: build/ansible-operator build/ansible-cli
83+
build/ansible-operator build/ansible-cli:
8484
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go build $(GO_BUILD_ARGS) -o $(BUILD_DIR)/$(@F) ./cmd/$(@F)
8585

8686
##@ Dev image build

cmd/ansible-cli/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"log"
19+
20+
"github.com/operator-framework/ansible-operator-plugins/internal/cmd/ansible-cli/cli"
21+
_ "k8s.io/client-go/plugin/pkg/client/auth"
22+
)
23+
24+
func main() {
25+
if err := cli.Run(); err != nil {
26+
log.Fatal(err)
27+
}
28+
}

hack/generate/samples/ansible/generate.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ import (
2121
log "github.com/sirupsen/logrus"
2222
"k8s.io/apimachinery/pkg/runtime/schema"
2323
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
24-
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
25-
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
26-
kustomizev2 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2"
27-
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang"
2824

2925
"github.com/operator-framework/ansible-operator-plugins/hack/generate/samples/internal/pkg"
30-
"github.com/operator-framework/ansible-operator-plugins/pkg/plugins/ansible/v1"
26+
ansiblecli "github.com/operator-framework/ansible-operator-plugins/internal/cmd/ansible-cli/cli"
3127
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/command"
3228
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/e2e"
3329
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/sample"
@@ -43,24 +39,7 @@ var memcachedGVK = schema.GroupVersionKind{
4339
}
4440

4541
func getCli() *cli.CLI {
46-
ansibleBundle, _ := plugin.NewBundleWithOptions(
47-
plugin.WithName(golang.DefaultNameQualifier),
48-
plugin.WithVersion(ansible.Plugin{}.Version()),
49-
plugin.WithPlugins(kustomizev2.Plugin{}, ansible.Plugin{}),
50-
)
51-
52-
c, err := cli.New(
53-
cli.WithCommandName("cli"),
54-
cli.WithVersion("v0.0.0"),
55-
cli.WithPlugins(
56-
ansibleBundle,
57-
),
58-
cli.WithDefaultPlugins(cfgv3.Version, ansibleBundle),
59-
cli.WithDefaultProjectVersion(cfgv3.Version),
60-
cli.WithCompletion(),
61-
)
62-
pkg.CheckError("getting cli implementation:", err)
63-
return c
42+
return ansiblecli.GetPluginsCLI()
6443
}
6544

6645
func GenerateMemcachedSamples(rootPath string) []sample.Sample {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2025 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"fmt"
19+
"log"
20+
"runtime"
21+
22+
"github.com/operator-framework/ansible-operator-plugins/pkg/plugins/ansible/v1"
23+
24+
ver "github.com/operator-framework/ansible-operator-plugins/internal/version"
25+
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
26+
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
27+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
28+
kustomizev2 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2"
29+
)
30+
31+
func Run() error {
32+
c := GetPluginsCLI()
33+
return c.Run()
34+
}
35+
36+
func GetPluginsCLI() *cli.CLI {
37+
ansibleBundle, _ := plugin.NewBundleWithOptions(
38+
plugin.WithName(ansible.Plugin{}.Name()),
39+
plugin.WithVersion(ansible.Plugin{}.Version()),
40+
plugin.WithPlugins(
41+
kustomizev2.Plugin{},
42+
ansible.Plugin{},
43+
),
44+
)
45+
46+
c, err := cli.New(
47+
cli.WithCommandName("ansible-cli"),
48+
cli.WithVersion(makeVersionString()),
49+
cli.WithPlugins(
50+
ansibleBundle,
51+
),
52+
cli.WithDefaultPlugins(cfgv3.Version, ansibleBundle),
53+
cli.WithDefaultProjectVersion(cfgv3.Version),
54+
cli.WithCompletion(),
55+
)
56+
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
return c
62+
}
63+
64+
func makeVersionString() string {
65+
return fmt.Sprintf("ansible-cli version: %q, commit: %q, kubernetes version: %q, go version: %q, GOOS: %q, GOARCH: %q",
66+
ver.GitVersion, ver.GitCommit, ver.KubernetesVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH)
67+
}

testdata/memcached-molecule-operator/PROJECT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: example.com
66
layout:
7-
- go.kubebuilder.io/v1
7+
- base.ansible.sdk.operatorframework.io/v1
88
multigroup: true
99
projectName: memcached-molecule-operator
1010
resources:

0 commit comments

Comments
 (0)