Skip to content

Commit 0ac6e5a

Browse files
Add capibmadm tool related targets (#1168)
Signed-off-by: Prajyot-Parab <[email protected]>
1 parent 340a49a commit 0ac6e5a

File tree

9 files changed

+258
-18
lines changed

9 files changed

+258
-18
lines changed

Dockerfile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
# limitations under the License.
1616

1717
# Build the manager binary
18-
FROM golang:1.20.3 as builder
18+
FROM golang:1.20.3 as toolchain
1919

20+
# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
21+
ARG goproxy=https://proxy.golang.org,direct
22+
ENV GOPROXY=$goproxy
23+
24+
FROM toolchain as builder
2025
WORKDIR /workspace
2126
# Copy the Go Modules manifests
2227
COPY go.mod go.mod
2328
COPY go.sum go.sum
2429
# cache deps before building and copying source so that we don't need to re-download as much
2530
# and so that source changes don't invalidate our downloaded layer
26-
ENV GOPROXY https://proxy.golang.org,direct
27-
RUN go mod download
31+
RUN --mount=type=cache,target=/root/.local/share/golang \
32+
--mount=type=cache,target=/go/pkg/mod \
33+
go mod download
2834

2935
# Copy the go source
3036
COPY main.go main.go
@@ -36,13 +42,17 @@ COPY util/ util/
3642

3743
# Build
3844
ARG ARCH
39-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on go build -ldflags "-extldflags '-static'" -a -o manager main.go
45+
ARG LDFLAGS
46+
RUN --mount=type=cache,target=/root/.cache/go-build \
47+
--mount=type=cache,target=/go/pkg/mod \
48+
--mount=type=cache,target=/root/.local/share/golang \
49+
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on go build -ldflags "${LDFLAGS} -extldflags '-static'" -o manager main.go
4050

4151
# Use distroless as minimal base image to package the manager binary
4252
# Refer to https://github.com/GoogleContainerTools/distroless for more details
4353
FROM gcr.io/distroless/static:nonroot
4454
WORKDIR /
4555
COPY --from=builder /workspace/manager .
46-
USER nonroot:nonroot
47-
56+
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies
57+
USER 65532
4858
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CRD_OPTIONS ?= "crd:crdVersions=v1"
2626
# Directories.
2727
REPO_ROOT := $(shell git rev-parse --show-toplevel)
2828
ARTIFACTS ?= $(REPO_ROOT)/_artifacts
29+
BIN_DIR := bin
2930
TOOLS_DIR := hack/tools
3031
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
3132
GO_INSTALL = ./scripts/go_install.sh
@@ -55,11 +56,17 @@ PULL_BASE_REF ?= $(RELEASE_TAG) # PULL_BASE_REF will be provided by Prow
5556
RELEASE_ALIAS_TAG ?= $(PULL_BASE_REF)
5657
RELEASE_DIR := out
5758

59+
# image name used to build the cmd/capibmadm
60+
TOOLCHAIN_IMAGE := toolchain
61+
5862
TAG ?= dev
5963
ARCH ?= amd64
6064
ALL_ARCH ?= amd64 ppc64le arm64
6165
PULL_POLICY ?= Always
6266

67+
# Set build time variables including version details
68+
LDFLAGS := $(shell ./hack/version.sh)
69+
6370
KUBEBUILDER_ENVTEST_KUBERNETES_VERSION ?= 1.24.1
6471

6572
# main controller
@@ -94,9 +101,16 @@ all: manager
94101

95102
##@ build:
96103

104+
.PHONY: binaries
105+
binaries: manager capibmadm ## Builds and installs all binaries
106+
107+
.PHONY: capibmadm
108+
capibmadm: ## Build the capibmadm binary into the ./bin folder
109+
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/capibmadm ./cmd/capibmadm
110+
97111
# Build manager binary
98112
manager: generate fmt vet ## Build the manager binary into the ./bin folder
99-
go build -o bin/manager main.go
113+
go build -ldflags "${LDFLAGS} -extldflags '-static'" -o $(BIN_DIR)/manager main.go
100114

101115
# Run against the configured Kubernetes cluster in ~/.kube/config
102116
run: generate fmt vet
@@ -245,6 +259,10 @@ $(RELEASE_DIR):
245259
$(ARTIFACTS):
246260
mkdir -p $@
247261

262+
.PHONY: build-toolchain
263+
build-toolchain: ## Build the toolchain
264+
docker build --target toolchain -t $(TOOLCHAIN_IMAGE) .
265+
248266
.PHONY: list-staging-releases
249267
list-staging-releases: ## List staging images for image promotion
250268
@echo $(CORE_IMAGE_NAME):
@@ -271,6 +289,7 @@ release: clean-release check-release-tag $(RELEASE_DIR) ## Build and push conta
271289
git checkout "${RELEASE_TAG}"
272290
CORE_CONTROLLER_IMG=$(PROD_REGISTRY)/$(CORE_IMAGE_NAME) $(MAKE) release-manifests
273291
$(MAKE) release-templates
292+
$(MAKE) release-binaries
274293

275294
.PHONY: release-manifests
276295
release-manifests: ## Build the manifests to publish with a release
@@ -285,6 +304,7 @@ release-staging: ## Build and push container images to the staging bucket
285304
$(MAKE) release-alias-tag
286305
$(MAKE) staging-manifests
287306
$(MAKE) release-templates
307+
$(MAKE) release-binaries
288308
$(MAKE) upload-staging-artifacts
289309

290310
.PHONY: staging-manifests
@@ -304,6 +324,31 @@ release-alias-tag: ## Add the release alias tag to the last build tag
304324
release-templates: $(RELEASE_DIR) ## Generate release templates
305325
cp templates/cluster-template*.yaml $(RELEASE_DIR)/
306326

327+
.PHONY: release-binaries
328+
release-binaries: ## Builds the binaries to publish with a release
329+
RELEASE_BINARY=./cmd/capibmadm GOOS=linux GOARCH=ppc64le $(MAKE) release-binary
330+
RELEASE_BINARY=./cmd/capibmadm GOOS=linux GOARCH=amd64 $(MAKE) release-binary
331+
RELEASE_BINARY=./cmd/capibmadm GOOS=linux GOARCH=arm64 $(MAKE) release-binary
332+
RELEASE_BINARY=./cmd/capibmadm GOOS=darwin GOARCH=amd64 $(MAKE) release-binary
333+
RELEASE_BINARY=./cmd/capibmadm GOOS=darwin GOARCH=arm64 $(MAKE) release-binary
334+
RELEASE_BINARY=./cmd/capibmadm GOOS=windows GOARCH=amd64 EXT=.exe $(MAKE) release-binary
335+
RELEASE_BINARY=./cmd/capibmadm GOOS=windows GOARCH=arm64 EXT=.exe $(MAKE) release-binary
336+
337+
.PHONY: release-binary
338+
release-binary: $(RELEASE_DIR) build-toolchain ## Release binary
339+
docker run \
340+
--rm \
341+
-e CGO_ENABLED=0 \
342+
-e GOOS=$(GOOS) \
343+
-e GOARCH=$(GOARCH) \
344+
--mount=source=gocache,target=/go/pkg/mod \
345+
--mount=source=gocache,target=/root/.cache/go-build \
346+
-v "$$(pwd):/workspace" \
347+
-w /workspace \
348+
$(TOOLCHAIN_IMAGE) \
349+
go build -ldflags '$(LDFLAGS) -extldflags "-static"' \
350+
-o $(RELEASE_DIR)/$(notdir $(RELEASE_BINARY))-$(GOOS)-$(GOARCH)$(EXT) $(RELEASE_BINARY)
351+
307352
IMAGE_PATCH_DIR := $(ARTIFACTS)/image-patch
308353

309354
$(IMAGE_PATCH_DIR): $(ARTIFACTS)
@@ -347,7 +392,7 @@ image-patch-kustomization-without-webhook: $(IMAGE_PATCH_DIR) $(GOJQ)
347392

348393
.PHONY: docker-build
349394
docker-build: docker-pull-prerequisites ## Build the docker image for controller-manager
350-
docker build --build-arg ARCH=$(ARCH) . -t $(CORE_CONTROLLER_IMG)-$(ARCH):$(TAG)
395+
docker build --build-arg ARCH=$(ARCH) --build-arg LDFLAGS="$(LDFLAGS)" . -t $(CORE_CONTROLLER_IMG)-$(ARCH):$(TAG)
351396

352397
.PHONY: docker-push
353398
docker-push: ## Push the docker image
@@ -484,6 +529,7 @@ clean: ## Remove all generated files
484529
.PHONY: clean-bin
485530
clean-bin: ## Remove all generated binaries
486531
rm -rf $(TOOLS_BIN_DIR)
532+
rm -rf $(BIN_DIR)
487533

488534
.PHONY: clean-book
489535
clean-book: ## Remove all generated GitBook files

cmd/capibmadm/cmd/powervs/powervs.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package powervs
1818

1919
import (
20+
"fmt"
21+
"os"
22+
2023
"github.com/spf13/cobra"
2124

2225
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/image"
@@ -31,6 +34,14 @@ func Commands() *cobra.Command {
3134
cmd := &cobra.Command{
3235
Use: "powervs",
3336
Short: "Commands for operations on PowerVS resources",
37+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
38+
apiKey := os.Getenv(options.IBMCloudAPIKeyEnvName)
39+
if apiKey == "" {
40+
return fmt.Errorf("ibmcloud api key is not provided, set %s environmental variable", options.IBMCloudAPIKeyEnvName)
41+
}
42+
options.GlobalOptions.IBMCloudAPIKey = apiKey
43+
return nil
44+
},
3445
}
3546

3647
cmd.PersistentFlags().StringVar(&options.GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id (Required)")

cmd/capibmadm/cmd/root.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
3030

3131
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs"
32+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/version"
3233
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc"
33-
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
3434
)
3535

3636
func init() {
@@ -43,19 +43,12 @@ func rootCommand() *cobra.Command {
4343
Use: "capibmadm",
4444
Short: "Kubernetes Cluster API Provider IBM Cloud Management Utility",
4545
Long: `capibmadm provides helpers for completing the prerequisite operations for creating IBM Cloud Power VS or VPC clusters.`,
46-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
47-
apiKey := os.Getenv(options.IBMCloudAPIKeyEnvName)
48-
if apiKey == "" {
49-
return fmt.Errorf("ibmcloud api key is not provided, set %s environmental variable", options.IBMCloudAPIKeyEnvName)
50-
}
51-
options.GlobalOptions.IBMCloudAPIKey = apiKey
52-
return nil
53-
},
5446
}
5547

5648
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
5749
cmd.AddCommand(powervs.Commands())
5850
cmd.AddCommand(vpc.Commands())
51+
cmd.AddCommand(version.Commands(os.Stdout))
5952

6053
return cmd
6154
}

cmd/capibmadm/cmd/version/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package version contains the capibmadm version command.
18+
package version

cmd/capibmadm/cmd/version/version.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"io"
23+
24+
"github.com/pkg/errors"
25+
"github.com/spf13/cobra"
26+
"gopkg.in/yaml.v2"
27+
28+
"sigs.k8s.io/cluster-api-provider-ibmcloud/version"
29+
)
30+
31+
// Version provides the version information of capibmadm.
32+
type Version struct {
33+
ClientVersion *version.Info `json:"ibmcloudProviderVersion"`
34+
}
35+
36+
// Commands provides the version information capibmadm.
37+
func Commands(out io.Writer) *cobra.Command {
38+
cmd := &cobra.Command{
39+
Use: "version",
40+
Short: "Print version of capibmadm",
41+
Args: cobra.ExactArgs(0),
42+
RunE: func(cmd *cobra.Command, args []string) error {
43+
return runVersion(out, cmd)
44+
},
45+
}
46+
47+
cmd.Flags().StringP("output", "o", "", "The output format of the result. Supported printer types: short, yaml, json")
48+
49+
return cmd
50+
}
51+
52+
func runVersion(out io.Writer, cmd *cobra.Command) error {
53+
clientVersion := version.Get()
54+
v := Version{
55+
ClientVersion: &clientVersion,
56+
}
57+
58+
const flag = "output"
59+
of, err := cmd.Flags().GetString(flag)
60+
if err != nil {
61+
return errors.Wrapf(err, "error accessing flag %s for command %s", flag, cmd.Name())
62+
}
63+
64+
switch of {
65+
case "":
66+
fmt.Fprintf(out, "capibmadm version: %#v\n", *v.ClientVersion)
67+
case "short":
68+
fmt.Fprintf(out, "%s\n", v.ClientVersion.GitVersion)
69+
case "yaml":
70+
y, err := yaml.Marshal(&v)
71+
if err != nil {
72+
return err
73+
}
74+
fmt.Fprint(out, string(y))
75+
case "json":
76+
y, err := json.MarshalIndent(&v, "", " ")
77+
if err != nil {
78+
return err
79+
}
80+
fmt.Fprintln(out, string(y))
81+
default:
82+
return errors.Errorf("invalid output format: %s", of)
83+
}
84+
85+
return nil
86+
}

cmd/capibmadm/cmd/vpc/vpc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ limitations under the License.
1818
package vpc
1919

2020
import (
21+
"fmt"
22+
"os"
23+
2124
"github.com/spf13/cobra"
2225

2326
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc/image"
@@ -30,6 +33,14 @@ func Commands() *cobra.Command {
3033
cmd := &cobra.Command{
3134
Use: "vpc",
3235
Short: "Commands for operations on VPC resources",
36+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
37+
apiKey := os.Getenv(options.IBMCloudAPIKeyEnvName)
38+
if apiKey == "" {
39+
return fmt.Errorf("ibmcloud api key is not provided, set %s environmental variable", options.IBMCloudAPIKeyEnvName)
40+
}
41+
options.GlobalOptions.IBMCloudAPIKey = apiKey
42+
return nil
43+
},
3344
}
3445

3546
cmd.PersistentFlags().StringVar(&options.GlobalOptions.VPCRegion, "region", options.GlobalOptions.VPCRegion, "IBM cloud vpc region. (Required)")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/stretchr/testify v1.8.2
2727
golang.org/x/crypto v0.8.0
2828
golang.org/x/text v0.9.0
29+
gopkg.in/yaml.v2 v2.4.0
2930
k8s.io/api v0.26.1
3031
k8s.io/apiextensions-apiserver v0.26.1
3132
k8s.io/apimachinery v0.26.1
@@ -143,7 +144,6 @@ require (
143144
google.golang.org/protobuf v1.28.1 // indirect
144145
gopkg.in/inf.v0 v0.9.1 // indirect
145146
gopkg.in/ini.v1 v1.67.0 // indirect
146-
gopkg.in/yaml.v2 v2.4.0 // indirect
147147
gopkg.in/yaml.v3 v3.0.1 // indirect
148148
k8s.io/apiserver v0.26.1 // indirect
149149
k8s.io/cluster-bootstrap v0.25.0 // indirect

0 commit comments

Comments
 (0)