Skip to content

Commit 439edeb

Browse files
ryantxucharandas
andauthored
K8s: fix standalone command and add hack scripts (grafana#79052)
Co-authored-by: Charandas Batra <[email protected]>
1 parent 66df178 commit 439edeb

File tree

21 files changed

+307
-81
lines changed

21 files changed

+307
-81
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
/.golangci.toml @grafana/backend-platform
6666
/build.go @grafana/backend-platform
6767
/scripts/modowners/ @grafana/backend-platform
68+
/hack/ @grafana/grafana-app-platform-squad
6869

6970
/pkg/api/ @grafana/backend-platform
7071
/pkg/apis/ @grafana/grafana-app-platform-squad

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public/css/*.min.css
8080
apiserver.local.config/
8181
default.etcd/
8282

83+
# kubeconfig path used by example apiserver
84+
example-apiserver/
85+
8386
# devenv
8487
/devenv/docker-compose.yaml
8588
/devenv/docker-compose.override.yaml

.vscode/launch.json

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"cwd": "${workspaceFolder}",
1212
"args": ["server", "--homepath", "${workspaceFolder}", "--packaging", "dev"]
1313
},
14+
{
15+
"name": "Run API Server (k8s)",
16+
"type": "go",
17+
"request": "launch",
18+
"mode": "auto",
19+
"program": "${workspaceFolder}/pkg/cmd/grafana/",
20+
"env": {},
21+
"cwd": "${workspaceFolder}",
22+
"args": ["apiserver", "example.grafana.app"]
23+
},
1424
{
1525
"name": "Attach to Chrome",
1626
"port": 9222,

docs/sources/setup-grafana/configure-grafana/feature-toggles/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Experimental features might be changed or removed without prior notice.
143143
| `enableNativeHTTPHistogram` | Enables native HTTP Histograms |
144144
| `formatString` | Enable format string transformer |
145145
| `kubernetesPlaylists` | Use the kubernetes API in the frontend for playlists, and route /api/playlist requests to k8s |
146+
| `kubernetesSnapshots` | Use the kubernetes API in the frontend to support playlists |
146147
| `recoveryThreshold` | Enables feature recovery threshold (aka hysteresis) for threshold server-side expression |
147148
| `lokiStructuredMetadata` | Enables the loki data source to request structured metadata from the Loki server |
148149
| `teamHttpHeaders` | Enables datasources to apply team headers to the client requests |

hack/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Kubernetes HACK Alert
2+
3+
This is a hack folder for kubernetes codegen scripts. Oddly, a /hack/ folder seems to be standard kubernetes development practice ¯\_(ツ)\_
4+
5+
The workflow is a WIP, however we are trying to leverage as many off-the-shelf patterns as possible.
6+
7+
For these scripts to work, your local GOROOT/src/grafana/grafana must point to this git checkout. For my setup this is:
8+
9+
```
10+
❯ pwd
11+
/Users/ryan/go/src/github.com/grafana
12+
❯ ls -l
13+
total 0
14+
lrwxr-xr-x 1 ryan staff 37 Oct 5 09:34 grafana -> /Users/ryan/workspace/grafana/grafana
15+
```
16+
17+
The current workflow (sorry!) is to:
18+
19+
1. update the script to point to the group+version you want
20+
2. run the `update-codegen.sh` script. This will produce a bunch of new files
21+
3. move `pkg/generated/openapi/zz_generated.openapi.go` to `pkg/apis/{group/version}/zz_generated.openapi.go`.
22+
4. edit the package name so it is {version} and remove the boilerplate k8s kinds
23+
5. `rm -rf pkg/generated` -- we are not yet using most of the generated client stuff
24+
25+
Once we are more comfortable with the outputs and process, we will build these steps into a more standard codegen pattern, but until then... happy hacking!

hack/boilerplate.go.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// SPDX-License-Identifier: AGPL-3.0-only
2+

hack/update-codegen.sh

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2017 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
22+
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo $GOPATH/pkg/mod/k8s.io/code-generator@v0.27.1)}
23+
24+
OUTDIR="${HOME}/go/src"
25+
26+
echo $OUTDIR
27+
28+
CLIENTSET_NAME_VERSIONED=clientset \
29+
CLIENTSET_PKG_NAME=clientset \
30+
"${CODEGEN_PKG}/generate-groups.sh" "all" \
31+
github.com/grafana/grafana/pkg/generated \
32+
github.com/grafana/grafana/pkg/apis \
33+
"snapshots:v0alpha1" \
34+
--output-base "${OUTDIR}" \
35+
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
36+
37+
CLIENTSET_NAME_VERSIONED=clientset \
38+
CLIENTSET_PKG_NAME=clientset \
39+
"${CODEGEN_PKG}/generate-internal-groups.sh" "deepcopy,defaulter,conversion,openapi" \
40+
github.com/grafana/grafana/pkg/generated \
41+
github.com/grafana/grafana/pkg/apis \
42+
github.com/grafana/grafana/pkg/apis \
43+
"snapshots:v0alpha1" \
44+
--output-base "${OUTDIR}" \
45+
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"

packages/grafana-data/src/types/featureToggles.gen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export interface FeatureToggles {
135135
formatString?: boolean;
136136
transformationsVariableSupport?: boolean;
137137
kubernetesPlaylists?: boolean;
138+
kubernetesSnapshots?: boolean;
138139
cloudWatchBatchQueries?: boolean;
139140
recoveryThreshold?: boolean;
140141
lokiStructuredMetadata?: boolean;

pkg/cmd/grafana/apiserver/README.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,45 @@ The example-apiserver closely resembles the
44
[sample-apiserver](https://github.com/kubernetes/sample-apiserver/tree/master) project in code and thus
55
allows the same
66
[CLI flags](https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/) as kube-apiserver.
7-
It is currently used for testing our deployment pipelines for aggregated servers.
7+
It is currently used for testing our deployment pipelines for aggregated servers. You can optionally omit the
8+
aggregation path altogether and just run this example apiserver as a standalone process.
89

9-
## Prerequisites:
10+
## Standalone Mode
11+
12+
### Usage
13+
14+
```shell
15+
go run ./pkg/cmd/grafana apiserver example.grafana.app \
16+
--secure-port 8443
17+
```
18+
19+
### Verify that all works
20+
21+
```shell
22+
export KUBECONFIG=./example-apiserver/kubeconfig
23+
24+
kubectl api-resources
25+
NAME SHORTNAMES APIVERSION NAMESPACED KIND
26+
dummy example.grafana.app/v0alpha1 true DummyResource
27+
runtime example.grafana.app/v0alpha1 false RuntimeInfo
28+
```
29+
30+
## Aggregated Mode
31+
32+
### Prerequisites:
1033
1. kind: you will need kind (or another local K8s setup) if you want to test aggregation.
1134
```
1235
go install sigs.k8s.io/[email protected] && kind create cluster
1336
```
1437

15-
## Usage
38+
### Usage
1639

1740
You can start the example-apiserver with an invocation as shown below. The Authn / Authz flags are set up so that the kind cluster
1841
can be used as a root server for this example-apiserver (in aggregated mode). Here, it's assumed that you have a local
1942
kind cluster and that you can provide its kubeconfig in the parameters to the example-apiserver.
2043

2144
```shell
22-
go run ./pkg/cmd/grafana apiserver example.grafana.app\
45+
go run ./pkg/cmd/grafana apiserver example.grafana.app \
2346
--authentication-kubeconfig ~/.kube/config \
2447
--authorization-kubeconfig ~/.kube/config \
2548
--kubeconfig ~/.kube/config \
@@ -35,7 +58,7 @@ kubectl deploy -k ./deploy/darwin # or /linux
3558
```
3659

3760

38-
## Verify that all works
61+
### Verify that all works
3962

4063
With kubectl configured against `kind-kind` context, you can run the following:
4164

pkg/cmd/grafana/apiserver/cmd.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package apiserver
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/spf13/cobra"
@@ -10,32 +9,22 @@ import (
109
"k8s.io/component-base/cli"
1110
)
1211

13-
func newCommandStartExampleAPIServer(o *ExampleServerOptions, stopCh <-chan struct{}) *cobra.Command {
14-
// While this exists as an experimental feature, we require adding the scarry looking command line
15-
devAcknowledgementFlag := "grafana-enable-experimental-apiserver"
12+
func newCommandStartExampleAPIServer(o *APIServerOptions, stopCh <-chan struct{}) *cobra.Command {
1613
devAcknowledgementNotice := "The apiserver command is in heavy development. The entire setup is subject to change without notice"
1714

1815
cmd := &cobra.Command{
1916
Use: "apiserver [api group(s)]",
2017
Short: "Run the grafana apiserver",
2118
Long: "Run a standalone kubernetes based apiserver that can be aggregated by a root apiserver. " +
2219
devAcknowledgementNotice,
23-
Example: fmt.Sprintf("grafana apiserver example.grafana.app --%s", devAcknowledgementFlag),
24-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
25-
ok, err := cmd.Flags().GetBool(devAcknowledgementFlag)
26-
if !ok || err != nil {
27-
fmt.Printf("requires running with the flag: --%s\n\n%s\n\n",
28-
devAcknowledgementFlag, devAcknowledgementNotice)
29-
os.Exit(1)
30-
}
31-
},
20+
Example: "grafana apiserver example.grafana.app",
3221
RunE: func(c *cobra.Command, args []string) error {
3322
// Load each group from the args
3423
if err := o.LoadAPIGroupBuilders(args[1:]); err != nil {
3524
return err
3625
}
3726

38-
// Finish the config (applies all defaults)
27+
// Finish the config (a noop for now)
3928
if err := o.Complete(); err != nil {
4029
return err
4130
}
@@ -45,16 +34,13 @@ func newCommandStartExampleAPIServer(o *ExampleServerOptions, stopCh <-chan stru
4534
return err
4635
}
4736

48-
if err := o.RunExampleServer(config, stopCh); err != nil {
37+
if err := o.RunAPIServer(config, stopCh); err != nil {
4938
return err
5039
}
5140
return nil
5241
},
5342
}
5443

55-
// Register grafana flags
56-
cmd.PersistentFlags().Bool(devAcknowledgementFlag, false, devAcknowledgementNotice)
57-
5844
// Register standard k8s flags with the command line
5945
o.RecommendedOptions = options.NewRecommendedOptions(
6046
defaultEtcdPathPrefix,
@@ -68,7 +54,7 @@ func newCommandStartExampleAPIServer(o *ExampleServerOptions, stopCh <-chan stru
6854
func RunCLI() int {
6955
stopCh := genericapiserver.SetupSignalHandler()
7056

71-
options := newExampleServerOptions(os.Stdout, os.Stderr)
57+
options := newAPIServerOptions(os.Stdout, os.Stderr)
7258
cmd := newCommandStartExampleAPIServer(options, stopCh)
7359

7460
return cli.Run(cmd)

0 commit comments

Comments
 (0)