Skip to content

Commit

Permalink
Adopt aggregated apiserver
Browse files Browse the repository at this point in the history
Signed-off-by: Aylei <rayingecho@gmail.com>
  • Loading branch information
aylei committed Oct 24, 2019
1 parent 302f763 commit 4509425
Show file tree
Hide file tree
Showing 37 changed files with 2,351 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ vendor
tests/e2e/e2e.test
.orig
tkc

apiserver.local.config/
36 changes: 36 additions & 0 deletions cmd/apiserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2016 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 (
_ "github.com/go-openapi/loads"
_ "github.com/ugorji/go/codec"
"k8s.io/client-go/rest"

"github.com/pingcap/tidb-operator/pkg/apiserver/cmd"
"github.com/pingcap/tidb-operator/pkg/version"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Enable cloud provider auth
)

func main() {
restConfig, err := rest.InClusterConfig()
if err != nil {
panic(err)
}

cmd.StartApiServer(restConfig, "default", nil, nil, "Api", version.Get().GitVersion)
}
90 changes: 90 additions & 0 deletions docs/adding-resource-in-aa.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Adding New Resources in TiDB Aggregated Apiserver

> This guide shows an example about how to add new resources in TiDB Aggregated Apiserver (AA)
## Project Structure

```shell
pkg/apiserver # package root of AA
pkg/apiserver/apis # all apis of AA
pkg/apiserver/apis/{group}/ # un-versioned apis of a group
pkg/apiserver/apis/{group}/{version} # versiond apis of a group
pkg/apiserver/apis/{group}/{version}/{kind}_types.go # type definition of a certain kind
pkg/apiserver/client # generated go client
pkg/apiserver/cmd # AA entrypoint
pkg/apiserver/openapi # generated openapi definition
pkg/apiserver/storage # kube-apiserver based storage implementation
```

We will create the type definition of a versioned resource in `pkg/apiserver/apis/{group}/{version}/{kind}_types.go`.

The defaults function, conversion from un-versioned to versioned, OpenAPI definition and client of the versioned resource will be generated automatically.

The type definition, REST storage, defaults function, conversion from versioned to un-versioned and client of the un-versioned resources will also be generated automatically.

## Foo Example

Let's add a new resource `foos.v1alpha1.tidb.pingcap.com`:

- `kind`: foos
- `version`: v1alpha1
- `group`: tidb.pingcap.com

1. Create packages:

```shell
$ mkdir -p pkg/apiserver/apis/tidb/v1alpha1
```

**By convention, we take the sub domain of the group name as the package name.**

2. Write group name and code-generation markers for all resources under `v1alpha.tidb.pingcap.com`:

`pkg/apiserver/apis/tidb/v1alpha1/doc.go`

```
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=github.com/pingcap/tidb-operator/pkg/apiserver/apis/tidb
// +k8s:defaulter-gen=TypeMeta
// +groupName=tidb.pingcap.com
package v1alpha1 // import "github.com/pingcap/tidb-operator/pkg/apiserver/apis/tidb/v1alpha1"
```

3. Write the type definition and code generation markers for Foo:

`pkg/apiserver/apis/tidb/v1alpha1/foo_types.go`:

```go
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Foo
// +k8s:openapi-gen=true
// +resource:path=foos
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec FooSpec `json:"spec,omitempty"`
Status FooStatus `json:"status,omitempty"`
}

// FooSpec defines the desired state of Foo
type FooSpec struct {
Replicas int `json:"replicas,omitempty"`
}

// FooStatus defines the observed state of Foo
type FooStatus struct {
CurrentReplicas int `json:"currentReplicas,omitempty"`
}
```

4. Run code generation:

```
./hack/code-gen.sh
```

61 changes: 26 additions & 35 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
module github.com/pingcap/tidb-operator

require (
bitbucket.org/ww/goautoneg v0.0.0-20120707110453-75cd24fc2f2c // indirect
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Azure/go-autorest v10.14.0+incompatible // indirect
github.com/MakeNowJust/heredoc v0.0.0-20171113091838-e9091a26100e // indirect
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/ant31/crd-validation v0.0.0-20180702145049-30f8a35d0ac2
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/chai2010/gettext-go v0.0.0-20170215093142-bf70f2a70fb1 // indirect
github.com/cockroachdb/cmux v0.0.0-20170110192607-30d10be49292 // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/coreos/bbolt v1.3.1-coreos.6 // indirect
github.com/coreos/etcd v0.0.0-20180530235116-2b3aa7e1d49d // indirect
github.com/coreos/go-semver v0.2.0 // indirect
github.com/coreos/etcd v3.2.24+incompatible // indirect
github.com/coreos/go-semver v0.2.0
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7 // indirect
github.com/coreos/pkg v0.0.0-20180108230652-97fdf19511ea // indirect
github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920 // indirect
Expand All @@ -24,62 +27,59 @@ require (
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
github.com/elazarl/go-bindata-assetfs v1.0.0 // indirect
github.com/elazarl/goproxy v0.0.0-20190421051319-9d40249d3c2f // indirect
github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect
github.com/emicklei/go-restful v2.9.5+incompatible
github.com/emicklei/go-restful-swagger12 v0.0.0-20170926063155-7524189396c6 // indirect
github.com/evanphx/json-patch v4.1.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/spec v0.19.2
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-openapi/loads v0.19.4
github.com/go-openapi/spec v0.19.3
github.com/go-sql-driver/mysql v1.4.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 // indirect
github.com/golang/groupcache v0.0.0-20181024230925-c65c006176ff // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/golangplus/bytes v0.0.0-20160111154220-45c989fe5450 // indirect
github.com/golangplus/fmt v0.0.0-20150411045040-2a5d6d7d2995 // indirect
github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e // indirect
github.com/google/btree v0.0.0-20180124185431-e89373fe6b4a // indirect
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gophercloud/gophercloud v0.3.0 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2 // indirect
github.com/gorilla/websocket v1.2.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.4.1 // indirect
github.com/hashicorp/golang-lru v0.0.0-20180201235237-0fb14efe8c47 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/json-iterator/go v1.1.7 // indirect
github.com/juju/errors v0.0.0-20180806074554-22422dad46e1
github.com/juju/loggo v0.0.0-20180524022052-584905176618 // indirect
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
github.com/kubernetes-incubator/apiserver-builder-alpha v0.0.0-20190715093516-b948d3dd40b6
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mholt/archiver v3.1.1+incompatible
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/onsi/ginkgo v1.6.0 // indirect
github.com/onsi/gomega v1.4.1
github.com/onsi/gomega v1.4.2
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pingcap/check v0.0.0-20171206051426-1c287c953996 // indirect
github.com/pingcap/errors v0.11.0
github.com/pingcap/kvproto v0.0.0-20180606093822-b7ba8ea1c0b4
github.com/pingcap/pd v2.1.0-beta+incompatible
github.com/pingcap/kvproto v0.0.0-20190516013202-4cf58ad90b6c
github.com/pingcap/log v0.0.0-20190715063458-479153f07ebd
github.com/pingcap/pd v2.1.17+incompatible
github.com/pingcap/tidb v2.1.0-beta+incompatible
github.com/pkg/errors v0.8.0 // indirect
github.com/prometheus/client_golang v0.8.0
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect
Expand All @@ -88,35 +88,25 @@ require (
github.com/robfig/cron v1.1.0
github.com/russross/blackfriday v1.5.2+incompatible // indirect
github.com/sirupsen/logrus v1.0.6
github.com/soheilhy/cmux v0.1.4 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6 // indirect
github.com/uber-go/atomic v1.4.0 // indirect
github.com/uber/jaeger-client-go v2.16.0+incompatible // indirect
github.com/uber/jaeger-lib v2.0.0+incompatible // indirect
github.com/ugorji/go v1.1.1 // indirect
github.com/unrolled/render v0.0.0-20180807193321-4206df6ff701 // indirect
github.com/ugorji/go v0.0.0-20170107133203-ded73eae5db7
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1 // indirect
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
google.golang.org/genproto v0.0.0-20180731170733-daca94659cb5 // indirect
google.golang.org/grpc v1.12.0 // indirect
google.golang.org/grpc v1.14.0 // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 // indirect
gopkg.in/square/go-jose.v2 v2.3.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.2.4
k8s.io/api v0.0.0-00010101000000-000000000000
k8s.io/apiextensions-apiserver v0.0.0-00010101000000-000000000000
Expand All @@ -125,8 +115,9 @@ require (
k8s.io/cli-runtime v0.0.0-00010101000000-000000000000
k8s.io/client-go v0.0.0-00010101000000-000000000000
k8s.io/code-generator v0.0.0-00010101000000-000000000000
k8s.io/klog v1.0.0 // indirect
k8s.io/kube-openapi v0.0.0-20190918143330-0270cf2f1c1d
k8s.io/gengo v0.0.0-20191010091904-7fa3014cb28f // indirect
k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20190208205540-d7c86cdc46e3
k8s.io/kubernetes v1.12.5
k8s.io/metrics v0.0.0-20190118124808-33c1aed8dc65 // indirect
k8s.io/utils v0.0.0-20190308190857-21c4ce38f2a7 // indirect
Expand Down
Loading

0 comments on commit 4509425

Please sign in to comment.