Newer
Older
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 kubernetes
import (
"context"
"sync"
)
import (
perrors "github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
)
import (
"github.com/apache/dubbo-go/common/logger"
)
const (
// kubernetes inject the var
podNameKey = "HOSTNAME"
nameSpaceKey = "NAMESPACE"
// all pod annotation key
DubboIOAnnotationKey = "dubbo.io/annotation"
DubboIOLabelKey = "dubbo.io/label"
DubboIOLabelValue = "dubbo.io-value"
)
var (
ErrDubboLabelAlreadyExist = perrors.New("dubbo label already exist")
lock sync.RWMutex
// manage the client lifecycle
ctx context.Context
cancel context.CancelFunc
// newClient
// new a client for registry
func newClient(namespace string) (*Client, error) {
ctx, cancel := context.WithCancel(context.Background())
return nil, perrors.WithMessage(err, "new dubbo-registry controller")
c := &Client{
ctx: ctx,
cancel: cancel,
controller: controller,
}
return c, nil
}
// Create
// create k/v pair in watcher-set
func (c *Client) Create(k, v string) error {
// the read current pod must be lock, protect every
// create operation can be atomic
c.lock.Lock()
defer c.lock.Unlock()
if err := c.controller.addAnnotationForCurrentPod(k, v); err != nil {
return perrors.WithMessagef(err, "add annotation @key = %s @value = %s", k, v)
logger.Debugf("put the @key = %s @value = %s success", k, v)
return nil
}
// GetChildren
func (c *Client) GetChildren(k string) ([]string, []string, error) {
return nil, nil, perrors.WithMessagef(err, "get children from watcherSet on (%s)", k)
}
var kList []string
var vList []string
for _, o := range objectList {
kList = append(kList, o.Key)
vList = append(vList, o.Value)
}
return kList, vList, nil
}
// Watch
// watch on spec key
func (c *Client) Watch(k string) (<-chan *WatcherEvent, <-chan struct{}, error) {
return nil, nil, perrors.WithMessagef(err, "watch on (%s)", k)
}
// Watch
// watch on spec prefix
func (c *Client) WatchWithPrefix(prefix string) (<-chan *WatcherEvent, <-chan struct{}, error) {
return nil, nil, perrors.WithMessagef(err, "watch on prefix (%s)", prefix)
}
// Valid
// Valid the client
// if return false, the client is die
func (c *Client) Valid() bool {
select {
case <-c.Done():
return false
default:
}
}
// Done
// read the client status
func (c *Client) Done() <-chan struct{} {
return c.ctx.Done()
}
// Stop
// read the client status
func (c *Client) Close() {
select {
case <-c.ctx.Done():
//already stopped
return
default:
}
c.cancel()
// the client ctx be canceled
// so, just wait
}
// ValidateClient
// validate the kubernetes client
func ValidateClient(container clientFacade) error {
if client == nil || client.Valid() {
//ns, err := getCurrentNameSpace()
//if err != nil {
// return perrors.WithMessage(err, "get current namespace")
//}
//newClient, err := newClient(ns)
newClient, err := newClient("")
logger.Warnf("new kubernetes client (namespace{%s}: %v)", "", err)
return perrors.WithMessagef(err, "new kubernetes client (:%+v)", "")
}
container.SetClient(newClient)
}
return nil
}
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// NewMockClient
// export for registry package test
func NewMockClient(namespace string, mockClientGenerator func() (kubernetes.Interface, error)) (*Client, error) {
return nil, nil
//return newMockClient(namespace, mockClientGenerator)
}
// newMockClient
// new a client for test
//func newMockClient(namespace string, mockClientGenerator func() (kubernetes.Interface, error)) (*Client, error) {
//
// rawClient, err := mockClientGenerator()
// if err != nil {
// return nil, perrors.WithMessage(err, "call mock generator")
// }
//
// currentPodName, err := getCurrentPodName()
// if err != nil {
// return nil, perrors.WithMessage(err, "get pod name")
// }
//
// ctx, cancel := context.WithCancel(context.Background())
//
// c := &Client{
// currentPodName: currentPodName,
// ns: namespace,
// rawClient: rawClient,
// ctx: ctx,
// watcherSet: newWatcherSet(ctx),
// cancel: cancel,
// }
//
// currentPod, err := c.initCurrentPod()
// if err != nil {
// return nil, perrors.WithMessage(err, "init current pod")
// }
//
// // record current status
// c.currentPod = currentPod
//
// // init the watcherSet by current pods
// if err := c.initWatchSet(); err != nil {
// return nil, perrors.WithMessage(err, "init watcherSet")
// }
//
// c.lastResourceVersion = c.currentPod.GetResourceVersion()
//
// // start kubernetes watch loop
// if err := c.watchPods(); err != nil {
// return nil, perrors.WithMessage(err, "watch pods")
// }
//
// logger.Infof("init kubernetes registry client success @namespace = %q @Podname = %q", namespace, c.currentPod.Name)
// return c, nil
//}