Skip to content

Commit 0617ee8

Browse files
committed
Add new cli for ansible plugin
Signed-off-by: chiragkyal <ckyal@redhat.com>
1 parent 87d6d1f commit 0617ee8

File tree

5 files changed

+197
-69
lines changed

5 files changed

+197
-69
lines changed

.github/workflows/test-ansible.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,24 @@ jobs:
2929
- run: sudo rm -rf /usr/local/bin/kustomize
3030
- uses: actions/setup-python@v5
3131
with:
32-
python-version: '3.12'
32+
python-version: "3.12"
3333
- name: Run test e2e ansible molecule
3434
run: |
3535
env
3636
pip3 install --user --upgrade setuptools pip
3737
pip3 install --user ansible-core~=2.17.4
3838
make test-e2e-ansible-molecule
39+
build-ansible-cli:
40+
name: build-ansible-cli
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 ansible-cli
52+
run: make ansible-cli

.goreleaser.yml

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,98 @@ 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 }}"
1010
gcflags: "{{ .Env.GO_BUILD_GCFLAGS }}"
1111
ldflags: "{{ .Env.GO_BUILD_LDFLAGS }}"
1212
mod_timestamp: "{{ .CommitTimestamp }}"
1313
goos:
14-
- linux
14+
- linux
1515
goarch:
16-
- amd64
17-
- arm64
18-
- ppc64le
19-
- s390x
16+
- amd64
17+
- arm64
18+
- ppc64le
19+
- 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:
21-
- image_templates:
22-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
23-
dockerfile: images/ansible-operator/Dockerfile
24-
goos: linux
25-
goarch: amd64
26-
use: buildx
27-
build_flag_templates:
28-
- "--platform=linux/amd64"
29-
- "--cache-from"
30-
- "type=gha,scope=amd64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
31-
- "--builder={{ .Env.BUILDX_BUILDER }}"
32-
extra_files:
33-
- "images/ansible-operator/Pipfile"
34-
- "images/ansible-operator/Pipfile.lock"
35-
- image_templates:
36-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
37-
dockerfile: images/ansible-operator/Dockerfile
38-
goos: linux
39-
goarch: arm64
40-
use: buildx
41-
build_flag_templates:
42-
- "--platform=linux/arm64"
43-
- "--cache-from"
44-
- "type=gha,scope=arm64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
45-
- "--builder={{ .Env.BUILDX_BUILDER }}"
46-
extra_files:
47-
- "images/ansible-operator/Pipfile"
48-
- "images/ansible-operator/Pipfile.lock"
49-
- image_templates:
50-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
51-
dockerfile: images/ansible-operator/Dockerfile
52-
goos: linux
53-
goarch: ppc64le
54-
use: buildx
55-
build_flag_templates:
56-
- "--platform=linux/ppc64le"
57-
- "--cache-from"
58-
- "type=gha,scope=ppc64le,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
59-
- "--builder={{ .Env.BUILDX_BUILDER }}"
60-
extra_files:
61-
- "images/ansible-operator/Pipfile"
62-
- "images/ansible-operator/Pipfile.lock"
63-
- image_templates:
64-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
65-
dockerfile: images/ansible-operator/Dockerfile
66-
goos: linux
67-
goarch: s390x
68-
use: buildx
69-
build_flag_templates:
70-
- "--platform=linux/s390x"
71-
- "--cache-from"
72-
- "type=gha,scope=s390x,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
73-
- "--builder={{ .Env.BUILDX_BUILDER }}"
74-
extra_files:
75-
- "images/ansible-operator/Pipfile"
76-
- "images/ansible-operator/Pipfile.lock"
35+
- image_templates:
36+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
37+
dockerfile: images/ansible-operator/Dockerfile
38+
goos: linux
39+
goarch: amd64
40+
use: buildx
41+
build_flag_templates:
42+
- "--platform=linux/amd64"
43+
- "--cache-from"
44+
- "type=gha,scope=amd64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
45+
- "--builder={{ .Env.BUILDX_BUILDER }}"
46+
extra_files:
47+
- "images/ansible-operator/Pipfile"
48+
- "images/ansible-operator/Pipfile.lock"
49+
- image_templates:
50+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
51+
dockerfile: images/ansible-operator/Dockerfile
52+
goos: linux
53+
goarch: arm64
54+
use: buildx
55+
build_flag_templates:
56+
- "--platform=linux/arm64"
57+
- "--cache-from"
58+
- "type=gha,scope=arm64,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
59+
- "--builder={{ .Env.BUILDX_BUILDER }}"
60+
extra_files:
61+
- "images/ansible-operator/Pipfile"
62+
- "images/ansible-operator/Pipfile.lock"
63+
- image_templates:
64+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
65+
dockerfile: images/ansible-operator/Dockerfile
66+
goos: linux
67+
goarch: ppc64le
68+
use: buildx
69+
build_flag_templates:
70+
- "--platform=linux/ppc64le"
71+
- "--cache-from"
72+
- "type=gha,scope=ppc64le,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
73+
- "--builder={{ .Env.BUILDX_BUILDER }}"
74+
extra_files:
75+
- "images/ansible-operator/Pipfile"
76+
- "images/ansible-operator/Pipfile.lock"
77+
- image_templates:
78+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
79+
dockerfile: images/ansible-operator/Dockerfile
80+
goos: linux
81+
goarch: s390x
82+
use: buildx
83+
build_flag_templates:
84+
- "--platform=linux/s390x"
85+
- "--cache-from"
86+
- "type=gha,scope=s390x,token={{ .Env.ACTIONS_RUNTIME_TOKEN }},url={{ .Env.ACTIONS_CACHE_URL }}"
87+
- "--builder={{ .Env.BUILDX_BUILDER }}"
88+
extra_files:
89+
- "images/ansible-operator/Pipfile"
90+
- "images/ansible-operator/Pipfile.lock"
7791
docker_manifests:
78-
- name_template: "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}"
79-
image_templates:
80-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
81-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
82-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
83-
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
92+
- name_template: "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}"
93+
image_templates:
94+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
95+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
96+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
97+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
8498
archives:
8599
- format: binary
86100
name_template: "{{ .Binary }}_{{ .Os }}_{{ .Arch }}"

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ build: ## Build ansible-operator
8383
build/ansible-operator:
8484
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go build $(GO_BUILD_ARGS) -o $(BUILD_DIR)/$(@F) ./cmd/$(@F)
8585

86+
.PHONY: ansible-cli
87+
ansible-cli:
88+
@mkdir -p $(BUILD_DIR)
89+
GOOS=$(BUILD_GOOS) GOARCH=$(BUILD_GOARCH) go build $(GO_BUILD_ARGS) -o $(BUILD_DIR)/ansible-cli ./cmd/ansible-cli
90+
8691
##@ Dev image build
8792

8893
# Convenience wrapper for building all remotely hosted images.

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+
}

internal/cmd/ansible-cli/cli/cli.go

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+
}

0 commit comments

Comments
 (0)