Skip to content

Commit 38af572

Browse files
authored
Merge pull request #4581 from migueleliasweb/make-go-install-able
🐛 Fix Kubebuilder Installation with go install
2 parents d72fa06 + 3269c59 commit 38af572

File tree

6 files changed

+53
-21
lines changed

6 files changed

+53
-21
lines changed

Diff for: Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ help: ## Display this help
4646
##@ Build
4747

4848
LD_FLAGS=-ldflags " \
49-
-X main.kubeBuilderVersion=$(shell git describe --tags --dirty --broken) \
50-
-X main.goos=$(shell go env GOOS) \
51-
-X main.goarch=$(shell go env GOARCH) \
52-
-X main.gitCommit=$(shell git rev-parse HEAD) \
53-
-X main.buildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') \
49+
-X sigs.k8s.io/kubebuilder/v4/cmd.kubeBuilderVersion=$(shell git describe --tags --dirty --broken) \
50+
-X sigs.k8s.io/kubebuilder/v4/cmd.goos=$(shell go env GOOS) \
51+
-X sigs.k8s.io/kubebuilder/v4/cmd.goarch=$(shell go env GOARCH) \
52+
-X sigs.k8s.io/kubebuilder/v4/cmd.gitCommit=$(shell git rev-parse HEAD) \
53+
-X sigs.k8s.io/kubebuilder/v4/cmd.buildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') \
5454
"
5555
.PHONY: build
5656
build: ## Build the project locally
57-
go build $(LD_FLAGS) -o bin/kubebuilder ./cmd
57+
go build $(LD_FLAGS) -o bin/kubebuilder
5858

5959
.PHONY: install
6060
install: build ## Build and install the binary with the current source code. Use it to test your changes locally.

Diff for: build/.goreleaser.yml

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ before:
2929
# Build a binary for each target in targets.
3030
builds:
3131
- id: kubebuilder
32-
main: ./cmd
3332
binary: kubebuilder
3433
mod_timestamp: "{{ .CommitTimestamp }}"
3534
ldflags:
36-
- -X main.kubeBuilderVersion={{ .Version }}
37-
- -X main.goos={{ .Os }}
38-
- -X main.goarch={{ .Arch }}
39-
- -X main.gitCommit={{ .Commit }}
40-
- -X main.buildDate={{ .Date }}
41-
- -X main.kubernetesVendorVersion={{ .Env.KUBERNETES_VERSION }}
35+
- -X sigs.k8s.io/kubebuilder/v4/cmd.kubeBuilderVersion={{ .Version }}
36+
- -X sigs.k8s.io/kubebuilder/v4/cmd.goos={{ .Os }}
37+
- -X sigs.k8s.io/kubebuilder/v4/cmd.goarch={{ .Arch }}
38+
- -X sigs.k8s.io/kubebuilder/v4/cmd.gitCommit={{ .Commit }}
39+
- -X sigs.k8s.io/kubebuilder/v4/cmd.buildDate={{ .Date }}
40+
- -X sigs.k8s.io/kubebuilder/v4/cmd.kubernetesVendorVersion={{ .Env.KUBERNETES_VERSION }}
4241
targets:
4342
- linux_amd64
4443
- linux_arm64

Diff for: cmd/main.go renamed to cmd/cmd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package cmd
1818

1919
import (
2020
"github.com/sirupsen/logrus"
@@ -36,7 +36,8 @@ func init() {
3636
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
3737
}
3838

39-
func main() {
39+
// Run bootstraps & runs the CLI
40+
func Run() {
4041
// Bundle plugin which built the golang projects scaffold with base.go/v4 and kustomize/v2 plugins
4142
gov4Bundle, _ := plugin.NewBundleWithOptions(plugin.WithName(golang.DefaultNameQualifier),
4243
plugin.WithVersion(plugin.Version{Number: 4}),

Diff for: cmd/version.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package cmd
1818

1919
import (
2020
"fmt"
21+
"runtime/debug"
2122
)
2223

24+
const unknown = "unknown"
25+
2326
// var needs to be used instead of const as ldflags is used to fill this
2427
// information in the release process
2528
var (
26-
kubeBuilderVersion = "unknown"
27-
kubernetesVendorVersion = "unknown"
28-
goos = "unknown"
29-
goarch = "unknown"
29+
kubeBuilderVersion = unknown
30+
kubernetesVendorVersion = unknown
31+
goos = unknown
32+
goarch = unknown
3033
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
3134

3235
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
@@ -44,6 +47,12 @@ type version struct {
4447

4548
// versionString returns the CLI version
4649
func versionString() string {
50+
if kubeBuilderVersion == unknown {
51+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
52+
kubeBuilderVersion = info.Main.Version
53+
}
54+
}
55+
4756
return fmt.Sprintf("Version: %#v", version{
4857
kubeBuilderVersion,
4958
kubernetesVendorVersion,

Diff for: main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2020 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 main
18+
19+
import "sigs.k8s.io/kubebuilder/v4/cmd"
20+
21+
func main() {
22+
cmd.Run()
23+
}

Diff for: test/common.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""}
109109
function build_kb {
110110
header_text "Building kubebuilder"
111111

112-
go build -o "${kb_root_dir}/bin/kubebuilder" ./cmd
112+
go build -o "${kb_root_dir}/bin/kubebuilder"
113113
kb="${kb_root_dir}/bin/kubebuilder"
114114
}
115115

0 commit comments

Comments
 (0)