add prober plugin for elasticsearch (#562)

Co-authored-by: lynxcat <lynxcatdeng@gmail.com>
master
lynxcat 4 years ago committed by GitHub
parent 9e07f1924c
commit 3df2536bb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1 @@
mode: all # whitelist(default),all

@ -370,6 +370,7 @@ github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:9wScpmSP5A3Bk
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:56xuuqnHyryaerycW3BfssRdxQstACi0Epw/yC5E2xM=
github.com/goburrow/modbus v0.1.0/go.mod h1:Kx552D5rLIS8E7TyUwQ/UdHEqvX5T8tyiGBTlzMcZBg=
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gofrs/uuid v2.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
@ -969,8 +970,11 @@ github.com/subosito/gotenv v1.2.1-0.20190917103637-de67a6614a4d/go.mod h1:GVSeM7
github.com/tbrandon/mbserver v0.0.0-20170611213546-993e1772cc62/go.mod h1:qUzPVlSj2UgxJkVbH0ZwuuiR46U8RBMDT5KLY78Ifpw=
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
github.com/tedsuo/ifrit v0.0.0-20191009134036-9a97d0632f00/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=

@ -10,6 +10,7 @@ import (
_ "github.com/didi/nightingale/src/modules/monapi/plugins/prometheus"
_ "github.com/didi/nightingale/src/modules/monapi/plugins/redis"
_ "github.com/didi/nightingale/src/modules/monapi/plugins/nginx"
_ "github.com/didi/nightingale/src/modules/monapi/plugins/elasticsearch"
// local
_ "github.com/didi/nightingale/src/modules/monapi/plugins/log"

@ -0,0 +1,95 @@
package elasticsearch
import (
"fmt"
"github.com/didi/nightingale/src/modules/monapi/collector"
"github.com/didi/nightingale/src/modules/monapi/plugins"
"github.com/didi/nightingale/src/toolkits/i18n"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs/elasticsearch"
"reflect"
"time"
)
func init() {
collector.CollectorRegister(NewCollector()) // for monapi
i18n.DictRegister(langDict)
}
type Collector struct {
*collector.BaseCollector
}
func NewCollector() *Collector {
return &Collector{BaseCollector: collector.NewBaseCollector(
"elasticsearch",
collector.RemoteCategory,
func() collector.TelegrafPlugin { return &Rule{} },
)}
}
var (
langDict = map[string]map[string]string{
"zh": map[string]string{
"Servers": "服务",
"specify a list of one or more Elasticsearch servers <br /> you can add username and password to your url to use basic authentication: <br /> servers = [http://user:pass@localhost:9200]": "通过URL设置指定服务器<br />可以配置用户名和密码,格式如[http://user:pass@localhost:9200]<br />配置详情可参考 https://github.com/influxdata/telegraf/tree/master/plugins/inputs/elasticsearch",
"Local": "是否本地",
"When local is true (the default), the node will read only its own stats.<br /> Set local to false when you want to read the node stats from all nodes<br /> of the cluster.": "默认为true,如要监控整个集群需设置为false",
"HTTP timeout": "请求超时时间",
"Timeout for HTTP requests": "http请求超时时间, 单位: 秒",
"ClusterHealth": "集群健康状态",
"Set cluster_health to true when you want to obtain cluster health stats": "是否获取集群健康状况统计信息",
"ClusterHealthLevel": "健康状况等级",
"Adjust cluster_health_level when you want to obtain detailed health stats <br />The options are<br /> - indices (default)<br /> - cluster":"统计健康状况等级。可选(indices, cluster)",
"ClusterStats": "集群运行状态",
"Set cluster_stats to true when you want to obtain cluster stats.": "是否收集集群运行状态",
"ClusterStatsOnlyFromMaster": "是否只收集主服务器",
"Only gather cluster_stats from the master node. To work this require local = true": "当设置为ture时是否本地需要为ture才能生效",
"Indices to collect; can be one or more indices names or _all": "可以配置一个或多个指标,默认为全部(_all)",
"NodeStats": "子指标",
"node_stats is a list of sub-stats that you want to have gathered. Valid options<br /> are \"indices\", \"os\", \"process\", \"jvm\", \"thread_pool\", \"fs\", \"transport\", \"http\", \"breaker\". <br />Per default, all stats are gathered.":"需要收集的子指标<br />可选项有:\"indices\", \"os\", \"process\", \"jvm\", \"thread_pool\", \"fs\", \"transport\", \"http\", \"breaker\"<br />不配置则全部收集",
},
}
)
type Rule struct {
Servers []string `label:"Servers" json:"servers,required" description:"specify a list of one or more Elasticsearch servers <br /> you can add username and password to your url to use basic authentication: <br /> servers = [http://user:pass@localhost:9200]" example:"http://user:pass@localhost:9200"`
Local bool `label:"Local" json:"local,required" description:"When local is true (the default), the node will read only its own stats.<br /> Set local to false when you want to read the node stats from all nodes<br /> of the cluster." default:"true"`
HTTPTimeout int `label:"HTTP timeout" json:"http_timeout" default:"5" description:"Timeout for HTTP requests"`
ClusterHealth bool `label:"ClusterHealth" json:"cluster_health,required" description:"Set cluster_health to true when you want to obtain cluster health stats" default:"false"`
ClusterHealthLevel string `label:"ClusterHealthLevel" json:"cluster_health_level,required" description:"Adjust cluster_health_level when you want to obtain detailed health stats <br />The options are<br /> - indices (default)<br /> - cluster" default:"\"indices\""`
ClusterStats bool `label:"ClusterStats" json:"cluster_stats,required" description:"Set cluster_stats to true when you want to obtain cluster stats." default:"false"`
ClusterStatsOnlyFromMaster bool `label:"ClusterStatsOnlyFromMaster" json:"cluster_stats_only_from_master,required" description:"Only gather cluster_stats from the master node. To work this require local = true" default:"true"`
IndicesInclude []string `label:"IndicesInclude" json:"indices_include,required" description:"Indices to collect; can be one or more indices names or _all" default:"[\"_all\"]"`
NodeStats []string `label:"NodeStats" json:"node_stats" description:"node_stats is a list of sub-stats that you want to have gathered. Valid options<br /> are \"indices\", \"os\", \"process\", \"jvm\", \"thread_pool\", \"fs\", \"transport\", \"http\", \"breaker\". <br />Per default, all stats are gathered."`
plugins.ClientConfig
//ssl
}
func (p *Rule) Validate() error {
if len(p.Servers) == 0 || p.Servers[0] == "" {
return fmt.Errorf("elasticsearch.rule.servers must be set")
}
return nil
}
func (p *Rule) TelegrafInput() (telegraf.Input, error) {
es := &elasticsearch.Elasticsearch{
Local: p.Local,
Servers: p.Servers,
ClusterHealth: p.ClusterHealth,
ClusterHealthLevel: p.ClusterHealthLevel,
ClusterStats: p.ClusterStats,
ClusterStatsOnlyFromMaster: p.ClusterStatsOnlyFromMaster,
IndicesInclude: p.IndicesInclude,
IndicesLevel: "shards",
NodeStats: p.NodeStats,
ClientConfig: p.ClientConfig.TlsClientConfig(),
}
v := reflect.ValueOf(&(es.HTTPTimeout.Duration)).Elem()
v.Set(reflect.ValueOf(time.Second * time.Duration(p.HTTPTimeout)))
return es, nil
}

@ -0,0 +1,2 @@
assets
_output/

@ -0,0 +1,36 @@
language: go
sudo: required
go:
- 1.8
- 1.9
- '1.10'
services:
- docker
env:
- K8S_CLIENT_TEST=1 KUBECONFIG=scripts/kubeconfig
install:
- go get -v ./...
- go get -v github.com/ghodss/yaml # Required for examples.
- ./scripts/run-kube.sh
- curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.9.1/bin/linux/amd64/kubectl
- chmod +x kubectl
- mv kubectl $GOPATH/bin
script:
- make
- make test
- make test-examples
- make verify-generate
notifications:
email: false
branches:
only:
- master

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,9 @@
package v1beta1
import "github.com/ericchiang/k8s"
func init() {
k8s.Register("apiextensions.k8s.io", "v1beta1", "customresourcedefinitions", false, &CustomResourceDefinition{})
k8s.RegisterList("apiextensions.k8s.io", "v1beta1", "customresourcedefinitions", false, &CustomResourceDefinitionList{})
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,35 @@
package v1
import "github.com/ericchiang/k8s"
func init() {
k8s.Register("", "v1", "componentstatuses", false, &ComponentStatus{})
k8s.Register("", "v1", "configmaps", true, &ConfigMap{})
k8s.Register("", "v1", "endpoints", true, &Endpoints{})
k8s.Register("", "v1", "limitranges", true, &LimitRange{})
k8s.Register("", "v1", "namespaces", false, &Namespace{})
k8s.Register("", "v1", "nodes", false, &Node{})
k8s.Register("", "v1", "persistentvolumeclaims", true, &PersistentVolumeClaim{})
k8s.Register("", "v1", "persistentvolumes", false, &PersistentVolume{})
k8s.Register("", "v1", "pods", true, &Pod{})
k8s.Register("", "v1", "replicationcontrollers", true, &ReplicationController{})
k8s.Register("", "v1", "resourcequotas", true, &ResourceQuota{})
k8s.Register("", "v1", "secrets", true, &Secret{})
k8s.Register("", "v1", "services", true, &Service{})
k8s.Register("", "v1", "serviceaccounts", true, &ServiceAccount{})
k8s.RegisterList("", "v1", "componentstatuses", false, &ComponentStatusList{})
k8s.RegisterList("", "v1", "configmaps", true, &ConfigMapList{})
k8s.RegisterList("", "v1", "endpoints", true, &EndpointsList{})
k8s.RegisterList("", "v1", "limitranges", true, &LimitRangeList{})
k8s.RegisterList("", "v1", "namespaces", false, &NamespaceList{})
k8s.RegisterList("", "v1", "nodes", false, &NodeList{})
k8s.RegisterList("", "v1", "persistentvolumeclaims", true, &PersistentVolumeClaimList{})
k8s.RegisterList("", "v1", "persistentvolumes", false, &PersistentVolumeList{})
k8s.RegisterList("", "v1", "pods", true, &PodList{})
k8s.RegisterList("", "v1", "replicationcontrollers", true, &ReplicationControllerList{})
k8s.RegisterList("", "v1", "resourcequotas", true, &ResourceQuotaList{})
k8s.RegisterList("", "v1", "secrets", true, &SecretList{})
k8s.RegisterList("", "v1", "services", true, &ServiceList{})
k8s.RegisterList("", "v1", "serviceaccounts", true, &ServiceAccountList{})
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,48 @@
package v1
import (
"encoding/json"
"time"
)
// JSON marshaling logic for the Time type so it can be used for custom
// resources, which serialize to JSON.
func (t Time) MarshalJSON() ([]byte, error) {
var seconds, nanos int64
if t.Seconds != nil {
seconds = *t.Seconds
}
if t.Nanos != nil {
nanos = int64(*t.Nanos)
}
return json.Marshal(time.Unix(seconds, nanos))
}
func (t *Time) UnmarshalJSON(p []byte) error {
var t1 time.Time
if err := json.Unmarshal(p, &t1); err != nil {
return err
}
seconds := t1.Unix()
nanos := int32(t1.UnixNano())
t.Seconds = &seconds
t.Nanos = &nanos
return nil
}
// Status must implement json.Unmarshaler for the codec to deserialize a JSON
// payload into it.
//
// See https://github.com/ericchiang/k8s/issues/82
type jsonStatus Status
func (s *Status) UnmarshalJSON(data []byte) error {
var j jsonStatus
if err := json.Unmarshal(data, &j); err != nil {
return err
}
*s = Status(j)
return nil
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,98 @@
package k8s
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/ericchiang/k8s/runtime"
"github.com/golang/protobuf/proto"
)
const (
contentTypePB = "application/vnd.kubernetes.protobuf"
contentTypeJSON = "application/json"
)
func contentTypeFor(i interface{}) string {
if _, ok := i.(proto.Message); ok {
return contentTypePB
}
return contentTypeJSON
}
// marshal encodes an object and returns the content type of that resource
// and the marshaled representation.
//
// marshal prefers protobuf encoding, but falls back to JSON.
func marshal(i interface{}) (string, []byte, error) {
if _, ok := i.(proto.Message); ok {
data, err := marshalPB(i)
return contentTypePB, data, err
}
data, err := json.Marshal(i)
return contentTypeJSON, data, err
}
// unmarshal decoded an object given the content type of the encoded form.
func unmarshal(data []byte, contentType string, i interface{}) error {
msg, isPBMsg := i.(proto.Message)
if contentType == contentTypePB && isPBMsg {
if err := unmarshalPB(data, msg); err != nil {
return fmt.Errorf("decode protobuf: %v", err)
}
return nil
}
if isPBMsg {
// only decode into JSON of a protobuf message if the type
// explicitly implements json.Unmarshaler
if _, ok := i.(json.Unmarshaler); !ok {
return fmt.Errorf("cannot decode json payload into protobuf object %T", i)
}
}
if err := json.Unmarshal(data, i); err != nil {
return fmt.Errorf("decode json: %v", err)
}
return nil
}
var magicBytes = []byte{0x6b, 0x38, 0x73, 0x00}
func unmarshalPB(b []byte, msg proto.Message) error {
if len(b) < len(magicBytes) {
return errors.New("payload is not a kubernetes protobuf object")
}
if !bytes.Equal(b[:len(magicBytes)], magicBytes) {
return errors.New("payload is not a kubernetes protobuf object")
}
u := new(runtime.Unknown)
if err := u.Unmarshal(b[len(magicBytes):]); err != nil {
return fmt.Errorf("unmarshal unknown: %v", err)
}
return proto.Unmarshal(u.Raw, msg)
}
func marshalPB(obj interface{}) ([]byte, error) {
message, ok := obj.(proto.Message)
if !ok {
return nil, fmt.Errorf("expected obj of type proto.Message, got %T", obj)
}
payload, err := proto.Marshal(message)
if err != nil {
return nil, err
}
// The URL path informs the API server what the API group, version, and resource
// of the object. We don't need to specify it here to talk to the API server.
body, err := (&runtime.Unknown{Raw: payload}).Marshal()
if err != nil {
return nil, err
}
d := make([]byte, len(magicBytes)+len(body))
copy(d[:len(magicBytes)], magicBytes)
copy(d[len(magicBytes):], body)
return d, nil
}

@ -0,0 +1,170 @@
/*
Copyright 2014 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 k8s
import (
"github.com/ericchiang/k8s/runtime"
)
// Where possible, json tags match the cli argument names.
// Top level config objects and all values required for proper functioning are not "omitempty". Any truly optional piece of config is allowed to be omitted.
// Config holds the information needed to build connect to remote kubernetes clusters as a given user
type Config struct {
// Legacy field from pkg/api/types.go TypeMeta.
// TODO(jlowdermilk): remove this after eliminating downstream dependencies.
// +optional
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
// DEPRECATED: APIVersion is the preferred api version for communicating with the kubernetes cluster (v1, v2, etc).
// Because a cluster can run multiple API groups and potentially multiple versions of each, it no longer makes sense to specify
// a single value for the cluster version.
// This field isn't really needed anyway, so we are deprecating it without replacement.
// It will be ignored if it is present.
// +optional
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
// Preferences holds general information to be use for cli interactions
Preferences Preferences `json:"preferences" yaml:"preferences"`
// Clusters is a map of referencable names to cluster configs
Clusters []NamedCluster `json:"clusters" yaml:"clusters"`
// AuthInfos is a map of referencable names to user configs
AuthInfos []NamedAuthInfo `json:"users" yaml:"users"`
// Contexts is a map of referencable names to context configs
Contexts []NamedContext `json:"contexts" yaml:"contexts"`
// CurrentContext is the name of the context that you would like to use by default
CurrentContext string `json:"current-context" yaml:"current-context"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
// +optional
Extensions []NamedExtension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
type Preferences struct {
// +optional
Colors bool `json:"colors,omitempty" yaml:"colors,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
// +optional
Extensions []NamedExtension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
// Cluster contains information about how to communicate with a kubernetes cluster
type Cluster struct {
// Server is the address of the kubernetes cluster (https://hostname:port).
Server string `json:"server" yaml:"server"`
// APIVersion is the preferred api version for communicating with the kubernetes cluster (v1, v2, etc).
// +optional
APIVersion string `json:"api-version,omitempty" yaml:"api-version,omitempty"`
// InsecureSkipTLSVerify skips the validity check for the server's certificate. This will make your HTTPS connections insecure.
// +optional
InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify,omitempty" yaml:"insecure-skip-tls-verify,omitempty"`
// CertificateAuthority is the path to a cert file for the certificate authority.
// +optional
CertificateAuthority string `json:"certificate-authority,omitempty" yaml:"certificate-authority,omitempty"`
// CertificateAuthorityData contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority
// +optional
CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty" yaml:"certificate-authority-data,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
// +optional
Extensions []NamedExtension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
// AuthInfo contains information that describes identity information. This is use to tell the kubernetes cluster who you are.
type AuthInfo struct {
// ClientCertificate is the path to a client cert file for TLS.
// +optional
ClientCertificate string `json:"client-certificate,omitempty" yaml:"client-certificate,omitempty"`
// ClientCertificateData contains PEM-encoded data from a client cert file for TLS. Overrides ClientCertificate
// +optional
ClientCertificateData []byte `json:"client-certificate-data,omitempty" yaml:"client-certificate-data,omitempty"`
// ClientKey is the path to a client key file for TLS.
// +optional
ClientKey string `json:"client-key,omitempty" yaml:"client-key,omitempty"`
// ClientKeyData contains PEM-encoded data from a client key file for TLS. Overrides ClientKey
// +optional
ClientKeyData []byte `json:"client-key-data,omitempty" yaml:"client-key-data,omitempty"`
// Token is the bearer token for authentication to the kubernetes cluster.
// +optional
Token string `json:"token,omitempty" yaml:"token,omitempty"`
// TokenFile is a pointer to a file that contains a bearer token (as described above). If both Token and TokenFile are present, Token takes precedence.
// +optional
TokenFile string `json:"tokenFile,omitempty" yaml:"tokenFile,omitempty"`
// Impersonate is the username to imperonate. The name matches the flag.
// +optional
Impersonate string `json:"as,omitempty" yaml:"as,omitempty"`
// Username is the username for basic authentication to the kubernetes cluster.
// +optional
Username string `json:"username,omitempty" yaml:"username,omitempty"`
// Password is the password for basic authentication to the kubernetes cluster.
// +optional
Password string `json:"password,omitempty" yaml:"password,omitempty"`
// AuthProvider specifies a custom authentication plugin for the kubernetes cluster.
// +optional
AuthProvider *AuthProviderConfig `json:"auth-provider,omitempty" yaml:"auth-provider,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
// +optional
Extensions []NamedExtension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
// Context is a tuple of references to a cluster (how do I communicate with a kubernetes cluster), a user (how do I identify myself), and a namespace (what subset of resources do I want to work with)
type Context struct {
// Cluster is the name of the cluster for this context
Cluster string `json:"cluster" yaml:"cluster"`
// AuthInfo is the name of the authInfo for this context
AuthInfo string `json:"user" yaml:"user"`
// Namespace is the default namespace to use on unspecified requests
// +optional
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
// +optional
Extensions []NamedExtension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
// NamedCluster relates nicknames to cluster information
type NamedCluster struct {
// Name is the nickname for this Cluster
Name string `json:"name" yaml:"name"`
// Cluster holds the cluster information
Cluster Cluster `json:"cluster" yaml:"cluster"`
}
// NamedContext relates nicknames to context information
type NamedContext struct {
// Name is the nickname for this Context
Name string `json:"name" yaml:"name"`
// Context holds the context information
Context Context `json:"context" yaml:"context"`
}
// NamedAuthInfo relates nicknames to auth information
type NamedAuthInfo struct {
// Name is the nickname for this AuthInfo
Name string `json:"name" yaml:"name"`
// AuthInfo holds the auth information
AuthInfo AuthInfo `json:"user" yaml:"user"`
}
// NamedExtension relates nicknames to extension information
type NamedExtension struct {
// Name is the nickname for this Extension
Name string `json:"name" yaml:"name"`
// Extension holds the extension information
Extension runtime.RawExtension `json:"extension" yaml:"extension"`
}
// AuthProviderConfig holds the configuration for a specified auth provider.
type AuthProviderConfig struct {
Name string `json:"name" yaml:"name"`
Config map[string]string `json:"config" yaml:"config"`
}

@ -0,0 +1,66 @@
package k8s
import (
"context"
"path"
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
)
type Version struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}
// Discovery is a client used to determine the API version and supported
// resources of the server.
type Discovery struct {
client *Client
}
func NewDiscoveryClient(c *Client) *Discovery {
return &Discovery{c}
}
func (d *Discovery) get(ctx context.Context, path string, resp interface{}) error {
return d.client.do(ctx, "GET", urlForPath(d.client.Endpoint, path), nil, resp)
}
func (d *Discovery) Version(ctx context.Context) (*Version, error) {
var v Version
if err := d.get(ctx, "version", &v); err != nil {
return nil, err
}
return &v, nil
}
func (d *Discovery) APIGroups(ctx context.Context) (*metav1.APIGroupList, error) {
var groups metav1.APIGroupList
if err := d.get(ctx, "apis", &groups); err != nil {
return nil, err
}
return &groups, nil
}
func (d *Discovery) APIGroup(ctx context.Context, name string) (*metav1.APIGroup, error) {
var group metav1.APIGroup
if err := d.get(ctx, path.Join("apis", name), &group); err != nil {
return nil, err
}
return &group, nil
}
func (d *Discovery) APIResources(ctx context.Context, groupName, groupVersion string) (*metav1.APIResourceList, error) {
var list metav1.APIResourceList
if err := d.get(ctx, path.Join("apis", groupName, groupVersion), &list); err != nil {
return nil, err
}
return &list, nil
}

@ -0,0 +1,87 @@
package k8s
import (
"regexp"
"strings"
)
const (
qnameCharFmt string = "[A-Za-z0-9]"
qnameExtCharFmt string = "[-A-Za-z0-9_./]"
qualifiedNameFmt string = "(" + qnameCharFmt + qnameExtCharFmt + "*)?" + qnameCharFmt
qualifiedNameMaxLength int = 63
labelValueFmt string = "(" + qualifiedNameFmt + ")?"
)
var labelValueRegexp = regexp.MustCompile("^" + labelValueFmt + "$")
// LabelSelector represents a Kubernetes label selector.
//
// Any values that don't conform to Kubernetes label value restrictions
// will be silently dropped.
//
// l := new(k8s.LabelSelector)
// l.Eq("component", "frontend")
// l.In("type", "prod", "staging")
//
type LabelSelector struct {
stmts []string
}
func (l *LabelSelector) Selector() Option {
return QueryParam("labelSelector", l.String())
}
func (l *LabelSelector) String() string {
return strings.Join(l.stmts, ",")
}
func validLabelValue(s string) bool {
if len(s) > 63 || len(s) == 0 {
return false
}
return labelValueRegexp.MatchString(s)
}
// Eq selects labels which have the key and the key has the provide value.
func (l *LabelSelector) Eq(key, val string) {
if !validLabelValue(key) || !validLabelValue(val) {
return
}
l.stmts = append(l.stmts, key+"="+val)
}
// NotEq selects labels where the key is present and has a different value
// than the value provided.
func (l *LabelSelector) NotEq(key, val string) {
if !validLabelValue(key) || !validLabelValue(val) {
return
}
l.stmts = append(l.stmts, key+"!="+val)
}
// In selects labels which have the key and the key has one of the provided values.
func (l *LabelSelector) In(key string, vals ...string) {
if !validLabelValue(key) || len(vals) == 0 {
return
}
for _, val := range vals {
if !validLabelValue(val) {
return
}
}
l.stmts = append(l.stmts, key+" in ("+strings.Join(vals, ", ")+")")
}
// NotIn selects labels which have the key and the key is not one of the provided values.
func (l *LabelSelector) NotIn(key string, vals ...string) {
if !validLabelValue(key) || len(vals) == 0 {
return
}
for _, val := range vals {
if !validLabelValue(val) {
return
}
}
l.stmts = append(l.stmts, key+" notin ("+strings.Join(vals, ", ")+")")
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,45 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: k8s.io/apimachinery/pkg/runtime/schema/generated.proto
/*
Package schema is a generated protocol buffer package.
It is generated from these files:
k8s.io/apimachinery/pkg/runtime/schema/generated.proto
It has these top-level messages:
*/
package schema
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import _ "github.com/ericchiang/k8s/util/intstr"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
func init() {
proto.RegisterFile("k8s.io/apimachinery/pkg/runtime/schema/generated.proto", fileDescriptorGenerated)
}
var fileDescriptorGenerated = []byte{
// 138 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x32, 0xcb, 0xb6, 0x28, 0xd6,
0xcb, 0xcc, 0xd7, 0x4f, 0x2c, 0xc8, 0xcc, 0x4d, 0x4c, 0xce, 0xc8, 0xcc, 0x4b, 0x2d, 0xaa, 0xd4,
0x2f, 0xc8, 0x4e, 0xd7, 0x2f, 0x2a, 0xcd, 0x2b, 0xc9, 0xcc, 0x4d, 0xd5, 0x2f, 0x4e, 0xce, 0x48,
0xcd, 0x4d, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0x4a, 0x2c, 0x49, 0x4d, 0xd1, 0x2b, 0x28, 0xca,
0x2f, 0xc9, 0x17, 0x52, 0x83, 0xe8, 0xd3, 0x43, 0xd6, 0xa7, 0x57, 0x90, 0x9d, 0xae, 0x07, 0xd5,
0xa7, 0x07, 0xd1, 0x27, 0x65, 0x8c, 0xcb, 0xfc, 0xd2, 0x92, 0xcc, 0x1c, 0xfd, 0xcc, 0xbc, 0x92,
0xe2, 0x92, 0x22, 0x74, 0xc3, 0x9d, 0x24, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1,
0xc1, 0x23, 0x39, 0xc6, 0x19, 0x8f, 0xe5, 0x18, 0xa2, 0xd8, 0x20, 0xc6, 0x01, 0x02, 0x00, 0x00,
0xff, 0xff, 0xf9, 0x8e, 0xdb, 0x42, 0xaf, 0x00, 0x00, 0x00,
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,199 @@
package k8s
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"github.com/ericchiang/k8s/runtime"
"github.com/ericchiang/k8s/watch/versioned"
"github.com/golang/protobuf/proto"
)
// Decode events from a watch stream.
//
// See: https://github.com/kubernetes/community/blob/master/contributors/design-proposals/protobuf.md#streaming-wire-format
// Watcher receives a stream of events tracking a particular resource within
// a namespace or across all namespaces.
//
// Watcher does not automatically reconnect. If a watch fails, a new watch must
// be initialized.
type Watcher struct {
watcher interface {
Next(Resource) (string, error)
Close() error
}
}
// Next decodes the next event from the watch stream. Errors are fatal, and
// indicate that the watcher should no longer be used, and must be recreated.
func (w *Watcher) Next(r Resource) (string, error) {
return w.watcher.Next(r)
}
// Close closes the active connection with the API server being used for
// the watch.
func (w *Watcher) Close() error {
return w.watcher.Close()
}
type watcherJSON struct {
d *json.Decoder
c io.Closer
}
func (w *watcherJSON) Close() error {
return w.c.Close()
}
func (w *watcherJSON) Next(r Resource) (string, error) {
var event struct {
Type string `json:"type"`
Object json.RawMessage `json:"object"`
}
if err := w.d.Decode(&event); err != nil {
return "", fmt.Errorf("decode event: %v", err)
}
if event.Type == "" {
return "", errors.New("wwatch event had no type field")
}
if err := json.Unmarshal([]byte(event.Object), r); err != nil {
return "", fmt.Errorf("decode resource: %v", err)
}
return event.Type, nil
}
type watcherPB struct {
r io.ReadCloser
}
func (w *watcherPB) Next(r Resource) (string, error) {
msg, ok := r.(proto.Message)
if !ok {
return "", errors.New("object was not a protobuf message")
}
event, unknown, err := w.next()
if err != nil {
return "", err
}
if event.Type == nil || *event.Type == "" {
return "", errors.New("watch event had no type field")
}
if err := proto.Unmarshal(unknown.Raw, msg); err != nil {
return "", err
}
return *event.Type, nil
}
func (w *watcherPB) Close() error {
return w.r.Close()
}
func (w *watcherPB) next() (*versioned.Event, *runtime.Unknown, error) {
length := make([]byte, 4)
if _, err := io.ReadFull(w.r, length); err != nil {
return nil, nil, err
}
body := make([]byte, int(binary.BigEndian.Uint32(length)))
if _, err := io.ReadFull(w.r, body); err != nil {
return nil, nil, fmt.Errorf("read frame body: %v", err)
}
var event versioned.Event
if err := proto.Unmarshal(body, &event); err != nil {
return nil, nil, err
}
if event.Object == nil {
return nil, nil, fmt.Errorf("event had no underlying object")
}
unknown, err := parseUnknown(event.Object.Raw)
if err != nil {
return nil, nil, err
}
return &event, unknown, nil
}
var unknownPrefix = []byte{0x6b, 0x38, 0x73, 0x00}
func parseUnknown(b []byte) (*runtime.Unknown, error) {
if !bytes.HasPrefix(b, unknownPrefix) {
return nil, errors.New("bytes did not start with expected prefix")
}
var u runtime.Unknown
if err := proto.Unmarshal(b[len(unknownPrefix):], &u); err != nil {
return nil, err
}
return &u, nil
}
// Watch creates a watch on a resource. It takes an example Resource to
// determine what endpoint to watch.
//
// Watch does not automatically reconnect. If a watch fails, a new watch must
// be initialized.
//
// // Watch configmaps in the "kube-system" namespace
// var configMap corev1.ConfigMap
// watcher, err := client.Watch(ctx, "kube-system", &configMap)
// if err != nil {
// // handle error
// }
// defer watcher.Close() // Always close the returned watcher.
//
// for {
// cm := new(corev1.ConfigMap)
// eventType, err := watcher.Next(cm)
// if err != nil {
// // watcher encountered and error, exit or create a new watcher
// }
// fmt.Println(eventType, *cm.Metadata.Name)
// }
//
func (c *Client) Watch(ctx context.Context, namespace string, r Resource, options ...Option) (*Watcher, error) {
url, err := resourceWatchURL(c.Endpoint, namespace, r, options...)
if err != nil {
return nil, err
}
ct := contentTypeFor(r)
req, err := c.newRequest(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", ct)
resp, err := c.client().Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode/100 != 2 {
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return nil, newAPIError(resp.Header.Get("Content-Type"), resp.StatusCode, body)
}
if ct == contentTypePB {
return &Watcher{&watcherPB{r: resp.Body}}, nil
}
return &Watcher{&watcherJSON{
d: json.NewDecoder(resp.Body),
c: resp.Body,
}}, nil
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save