forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (98 loc) · 3.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2017 The Kubernetes 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 main
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/build"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/create"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/docs"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/generate"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/initproject"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/update"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version"
)
func main() {
util.CheckInstall()
gopath := os.Getenv("GOPATH")
if len(gopath) == 0 {
log.Fatal("GOPATH not defined")
}
util.GoSrc = filepath.Join(gopath, "src")
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if !strings.HasPrefix(filepath.Dir(wd), util.GoSrc) {
log.Fatalf("kubebuilder must be run from the project root under $GOPATH/src/<package>. "+
"\nCurrent GOPATH=%s. \nCurrent directory=%s", gopath, wd)
}
util.Repo = strings.Replace(wd, util.GoSrc+string(filepath.Separator), "", 1)
build.AddBuild(cmd)
create.AddCreate(cmd)
docs.AddDocs(cmd)
generate.AddGenerate(cmd)
initproject.AddInit(cmd)
update.AddUpdate(cmd)
version.AddVersion(cmd)
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}
var cmd = &cobra.Command{
Use: "kubebuilder",
Short: "Development kit for building Kubernetes extensions and tools.",
Long: `Development kit for building Kubernetes extensions and tools.
Provides libraries and tools to create new projects, APIs and controllers.
Includes tools for packaging artifacts into an installer container.
Typical project lifecycle:
- initialize a project:
kubebuilder init --domain example.com
- create one or more a new resource APIs and add your code to them:
kubebuilder create resource --group <group> --version <version> --kind <Kind>
- run the controller as a local process (e.g. not in a container), installing APIs into the cluster if they are missing:
GOBIN=${PWD}/bin go install ${PWD#$GOPATH/src/}/cmd/controller-manager
bin/controller-manager --kubeconfig ~/.kube/config
# In another terminal create a new instance of your resource and watch the controller-manager output
kubectl apply -f hack/sample/<resource>.yaml
- build a docker container to install the API and controller into a namespace with RBAC configured:
Note: You may need to give yourself admin privs in order to install the RBAC rules
kubectl create clusterrolebinding <your-binding-name> --clusterrole=cluster-admin --user=<your-user>
docker build -f Dockerfile.controller . -t <image:tag>
docker push <image:tag>
kubebuilder create config --controller-image <image:tag> --name <project-name>
kubectl apply -f hack/install.yaml
More options:
- run tests
kubebuilder generate
export TEST_ASSET_KUBECTL=/usr/local/kubebuilder/bin/kubectl
export TEST_ASSET_KUBE_APISERVER=/usr/local/kubebuilder/bin/kube-apiserver
export TEST_ASSET_ETCD=/usr/local/kubebuilder/bin/etcd
go test ./pkg/...
- build reference documentation to docs/reference/build/index.html
kubebuilder docs
`,
Example: `# Initialize your project
kubebuilder init --domain example.com
# Initialize your project adding a go-header file to all generated files
touch hack/boilerplate.go.txt
kubebuilder init --domain example.com`,
Run: RunMain,
}
func RunMain(cmd *cobra.Command, args []string) {
cmd.Help()
}