Skip to content

Commit 9d45e6d

Browse files
authored
Merge pull request #3154 from sttts/sttts-create-workspace-plugin
✨ add kubectl-create-workspace command supported with latest kubectl
2 parents bbe3354 + 4511369 commit 9d45e6d

File tree

8 files changed

+139
-64
lines changed

8 files changed

+139
-64
lines changed

.goreleaser.yaml

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ builds:
1818
goarch: ppc64le
1919
env:
2020
- CGO_ENABLED=0
21+
2122
# plugin builds
22-
# kubectl-kcp
2323
- id: "kubectl-kcp"
2424
main: ./cmd/kubectl-kcp
2525
dir: cli
@@ -41,11 +41,11 @@ builds:
4141
goarch: ppc64le
4242
env:
4343
- CGO_ENABLED=0
44-
# kubectl-workspaces
45-
- id: "kubectl-workspaces"
46-
main: ./cmd/kubectl-workspace
44+
45+
- id: "kubectl-ws"
46+
main: ./cmd/kubectl-ws
4747
dir: cli
48-
binary: bin/kubectl-workspaces
48+
binary: bin/kubectl-ws
4949
ldflags:
5050
- "{{ .Env.LDFLAGS }}"
5151
goos:
@@ -63,11 +63,11 @@ builds:
6363
goarch: ppc64le
6464
env:
6565
- CGO_ENABLED=0
66-
# kubectl-ws
67-
- id: "kubectl-ws"
68-
main: ./cmd/kubectl-workspace
66+
67+
- id: "kubectl-create-workspace"
68+
main: ./cmd/kubectl-create-workspace
6969
dir: cli
70-
binary: bin/kubectl-ws
70+
binary: bin/kubectl-create-workspace
7171
ldflags:
7272
- "{{ .Env.LDFLAGS }}"
7373
goos:
@@ -85,6 +85,7 @@ builds:
8585
goarch: ppc64le
8686
env:
8787
- CGO_ENABLED=0
88+
8889
archives:
8990
- id: kcp
9091
builds:
@@ -97,48 +98,49 @@ archives:
9798
builds:
9899
- kubectl-ws
99100
name_template: "kubectl-ws-plugin_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
100-
- id: kubectl-workspaces-plugin
101+
- id: kubectl-create-workspace-plugin-krew
101102
builds:
102-
- kubectl-workspaces
103-
name_template: "kubectl-workspaces-plugin_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
103+
- kubectl-create-workspace
104+
name_template: "kubectl-create-workspace-plugin_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
105+
104106
release:
105107
draft: true
106108
mode: keep-existing
107109

108110
krews:
109-
- name: ws
111+
- name: kcp
110112
ids:
111-
- kubectl-ws-plugin
113+
- kubectl-kcp-plugin
112114
repository:
113115
owner: kcp-dev
114116
name: krew-index
115117
token: "{{ .Env.KREW_GITHUB_TOKEN }}"
116118
homepage: "https://kcp.io/"
117119
description: |
118-
KCP workspace cli plugin for kubectl. Enables you to manage your kcp workspaces.
119-
short_description: "KCP workspace cli plugin for kubectl."
120+
KCP cli plugin for kubectl. Enables you to work with KCP.
121+
short_description: "KCP cli plugin for kubectl."
120122
skip_upload: auto
121-
- name: workspaces
123+
- name: ws
122124
ids:
123-
- kubectl-workspaces-plugin
125+
- kubectl-ws-plugin
124126
repository:
125127
owner: kcp-dev
126128
name: krew-index
127129
token: "{{ .Env.KREW_GITHUB_TOKEN }}"
128130
homepage: "https://kcp.io/"
129131
description: |
130-
KCP workspace cli plugin for kubectl. Enables you to manage your kcp workspaces.
132+
KCP workspace cli plugin for kubectl. Enables you to manage your KCP workspaces.
131133
short_description: "KCP workspace cli plugin for kubectl."
132134
skip_upload: auto
133-
- name: kcp
135+
- name: create-workspace
134136
ids:
135-
- kubectl-kcp-plugin
137+
- kubectl-create-workspace-plugin-krew
136138
repository:
137139
owner: kcp-dev
138140
name: krew-index
139141
token: "{{ .Env.KREW_GITHUB_TOKEN }}"
140142
homepage: "https://kcp.io/"
141143
description: |
142-
KCP cli plugin for kubectl. Enables you to manage your kcp.
143-
short_description: "KCP cli plugin for kubectl."
144-
skip_upload: auto
144+
KCP create workspace cli plugin for kubectl. Enables you to create KCP workspaces.
145+
short_description: "KCP create workspace cli plugin for kubectl."
146+
skip_upload: auto

FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ In kcp an application should be able to describe the constraints it needs in its
6565

6666
Shards in kcp represent a single apiserver and etcd/db instance. This is how kcp would like to split workspaces across many kcp instances since etcd will have storage limits.
6767

68-
## Where can I get the kubectl workspace plugin?
68+
## Where can I get the kubectl ws plugin?
6969

7070
You're in the right place. Clone this repo and run `make install WHAT=./cli/cmd/kubectl-kcp`.
7171

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ build: require-jq require-go require-git verify-go-versions ## Build the project
122122
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(BUILDFLAGS) -ldflags="$(LDFLAGS)" -o $(ROOT_DIR)/bin ./...; \
123123
popd; \
124124
done
125-
ln -sf kubectl-workspace bin/kubectl-workspaces
126-
ln -sf kubectl-workspace bin/kubectl-ws
127125
.PHONY: build
128126

129127
.PHONY: build-all
@@ -136,8 +134,6 @@ install: require-jq require-go require-git verify-go-versions ## Install the pro
136134
W=$$(echo "$${W}" | sed 's,^\./,github.com/kcp-dev/kcp/,') && \
137135
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go install -ldflags="$(LDFLAGS)" $${W}; \
138136
done
139-
ln -sf $(INSTALL_GOBIN)/kubectl-workspace $(INSTALL_GOBIN)/kubectl-ws
140-
ln -sf $(INSTALL_GOBIN)/kubectl-workspace $(INSTALL_GOBIN)/kubectl-workspaces
141137
.PHONY: install
142138

143139
$(GOLANGCI_LINT):
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2022 The KCP 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 (
20+
goflags "flag"
21+
"fmt"
22+
"os"
23+
24+
"github.com/spf13/pflag"
25+
26+
"k8s.io/cli-runtime/pkg/genericclioptions"
27+
"k8s.io/klog/v2"
28+
29+
"github.com/kcp-dev/kcp/cli/pkg/workspace/cmd"
30+
)
31+
32+
func main() {
33+
flags := pflag.NewFlagSet("kubectl-create-workspace", pflag.ExitOnError)
34+
pflag.CommandLine = flags
35+
36+
createWorkspaceCmd, err := cmd.NewCreate("kubectl create workspace", "", genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
37+
if err != nil {
38+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
39+
os.Exit(1)
40+
}
41+
42+
// setup klog
43+
fs := goflags.NewFlagSet("klog", goflags.PanicOnError)
44+
klog.InitFlags(fs)
45+
createWorkspaceCmd.PersistentFlags().AddGoFlagSet(fs)
46+
47+
if err := createWorkspaceCmd.Execute(); err != nil {
48+
os.Exit(1)
49+
}
50+
}

cli/cmd/kubectl-workspace/main.go renamed to cli/cmd/kubectl-ws/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
func main() {
33-
flags := pflag.NewFlagSet("kubectl-workspace", pflag.ExitOnError)
33+
flags := pflag.NewFlagSet("kubectl-ws", pflag.ExitOnError)
3434
pflag.CommandLine = flags
3535

3636
workspaceCmd, err := cmd.New(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})

cli/pkg/workspace/cmd/cmd.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,10 @@ func New(streams genericclioptions.IOStreams) (*cobra.Command, error) {
150150
}
151151
currentWorkspaceOpts.BindFlags(currentCmd)
152152

153-
createWorkspaceOpts := plugin.NewCreateWorkspaceOptions(streams)
154-
createCmd := &cobra.Command{
155-
Use: "create",
156-
Short: "Creates a new workspace",
157-
Example: "kcp workspace create <workspace name> [--type=<type>] [--enter [--ignore-not-ready]] --ignore-existing",
158-
SilenceUsage: true,
159-
Args: cobra.ExactArgs(1),
160-
RunE: func(cmd *cobra.Command, args []string) error {
161-
if len(args) == 0 {
162-
return cmd.Help()
163-
}
164-
if err := createWorkspaceOpts.Validate(); err != nil {
165-
return err
166-
}
167-
if err := createWorkspaceOpts.Complete(args); err != nil {
168-
return err
169-
}
170-
return createWorkspaceOpts.Run(cmd.Context())
171-
},
153+
createCmd, err := NewCreate(cliName+" workspace create", "install the 'kubectl create workspace' plugin instead, compare https://docs.kcp.io/kcp/latest/setup/kubectl-plugin/.", streams)
154+
if err != nil {
155+
return nil, err
172156
}
173-
createWorkspaceOpts.BindFlags(createCmd)
174157

175158
createContextOpts := plugin.NewCreateContextOptions(streams)
176159
createContextCmd := &cobra.Command{
@@ -221,3 +204,31 @@ func New(streams genericclioptions.IOStreams) (*cobra.Command, error) {
221204
cmd.AddCommand(createContextCmd)
222205
return cmd, nil
223206
}
207+
208+
// NewCreate returns a cobra.Command for workspace create action.
209+
func NewCreate(prefix string, deprecation string, streams genericclioptions.IOStreams) (*cobra.Command, error) {
210+
createWorkspaceOpts := plugin.NewCreateWorkspaceOptions(streams)
211+
cmd := &cobra.Command{
212+
Use: "create",
213+
Short: "Creates a new workspace",
214+
Example: prefix + " <workspace name> [--type=<type>] [--enter [--ignore-not-ready]] --ignore-existing",
215+
Deprecated: deprecation,
216+
SilenceUsage: true,
217+
Args: cobra.ExactArgs(1),
218+
RunE: func(cmd *cobra.Command, args []string) error {
219+
if len(args) == 0 {
220+
return cmd.Help()
221+
}
222+
if err := createWorkspaceOpts.Validate(); err != nil {
223+
return err
224+
}
225+
if err := createWorkspaceOpts.Complete(args); err != nil {
226+
return err
227+
}
228+
return createWorkspaceOpts.Run(cmd.Context())
229+
},
230+
}
231+
createWorkspaceOpts.BindFlags(cmd)
232+
233+
return cmd, nil
234+
}

contrib/kcp.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
],
9797
},
9898
{
99-
"name": "Manage workspaces through the kubectl workspace plugin",
99+
"name": "Manage workspaces through the 'kubectl ws' plugin",
100100
"type": "go",
101101
"request": "launch",
102102
"mode": "debug",

docs/content/setup/kubectl-plugin.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@ description: >
33
How to install and use the kubectl kcp plugin.
44
---
55

6-
# kubectl plugin
6+
# kubectl plugins
77

8-
kcp provides a kubectl plugin that simplifies the operations with the kcp server.
8+
kcp provides kubectl plugins that simplify the operations with the kcp server.
99

10-
You can install the plugin from the current repo:
10+
You can install the plugins from the current repo:
1111

1212
```sh
1313
$ make install
1414
go install ./cmd/...
1515
```
1616

17-
The plugin will be [automatically discovered by your current `kubectl` binary](https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/):
17+
or use [krew](https://krew.sigs.k8s.io/):
1818

1919
```sh
20-
$ kubectl-kcp
20+
$ kubectl krew index add kcp-dev https://github.com/kcp-dev/krew-index.git
21+
$ kubectl krew install kcp-dev/kcp
22+
$ kubectl krew install kcp-dev/ws
23+
$ kubectl krew install kcp-dev/create-workspace
24+
```
25+
26+
The plugins will be [automatically discovered by your current `kubectl` binary](https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/):
27+
28+
```sh
29+
$ kubectl kcp
2130
KCP is the easiest way to manage Kubernetes applications against one or more clusters, by giving you a personal control plane that schedules your workloads onto one or many clusters, and making it simple to pick up and move. Advanced use cases including spreading your apps across clusters for resiliency, scheduling batch workloads onto clusters with free capacity, and enabling collaboration for individual teams without having access to the underlying clusters.
2231

2332
This command provides KCP specific sub-command for kubectl.
@@ -26,25 +35,32 @@ Usage:
2635
kcp [command]
2736

2837
Available Commands:
29-
completion generate the autocompletion script for the specified shell
38+
bind Bind different types into current workspace.
39+
claims Operations related to viewing or updating permission claims
40+
completion Generate the autocompletion script for the specified shell
41+
crd CRD related operations
3042
help Help about any command
3143
workspace Manages KCP workspaces
3244

3345
Flags:
3446
--add_dir_header If true, adds the file directory to the header of the log messages
35-
--alsologtostderr log to standard error as well as files
47+
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
3648
-h, --help help for kcp
3749
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
38-
--log_dir string If non-empty, write log files in this directory
39-
--log_file string If non-empty, use this log file
40-
--log_file_max_size uint Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
50+
--log_dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
51+
--log_file string If non-empty, use this log file (no effect when -logtostderr=true)
52+
--log_file_max_size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
4153
--logtostderr log to standard error instead of files (default true)
42-
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
54+
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
4355
--skip_headers If true, avoid header prefixes in the log messages
44-
--skip_log_headers If true, avoid headers when opening log files
45-
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
56+
--skip_log_headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
57+
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=true) (default 2)
4658
-v, --v Level number for the log level verbosity
59+
--version version for kcp
4760
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
4861

4962
Use "kcp [command] --help" for more information about a command.
63+
64+
$ kubectl ws . # a short-cut for kubectl kcp workspace
65+
$ kubectl create workspace my-workspace # a short-cut for kubectl kcp workspace create
5066
```

0 commit comments

Comments
 (0)