From 1d6f431609b6115a2ab933fcec1c866704460d1f Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Fri, 3 Aug 2018 15:40:59 +0800 Subject: access k8s Issue-ID: MSB-254 Change-Id: Ic93bfe9a7a6616cffa8175ed3590c0d882ff678b Signed-off-by: Lvbo163 --- msb2pilot/src/msb2pilot/conf/k8s.yml | 14 +++++ msb2pilot/src/msb2pilot/pilot/controller.go | 66 ++++++++++++++++++++++++ msb2pilot/src/msb2pilot/pilot/controller_test.go | 26 ++++++++++ 3 files changed, 106 insertions(+) create mode 100644 msb2pilot/src/msb2pilot/conf/k8s.yml create mode 100644 msb2pilot/src/msb2pilot/pilot/controller.go create mode 100644 msb2pilot/src/msb2pilot/pilot/controller_test.go (limited to 'msb2pilot') diff --git a/msb2pilot/src/msb2pilot/conf/k8s.yml b/msb2pilot/src/msb2pilot/conf/k8s.yml new file mode 100644 index 0000000..07f512e --- /dev/null +++ b/msb2pilot/src/msb2pilot/conf/k8s.yml @@ -0,0 +1,14 @@ +apiVersion: v1 +clusters: +- cluster: + server: http://10.74.148.106:28003 + name: istio +contexts: +- context: + cluster: istio + user: "" + name: istio +current-context: istio +kind: Config +preferences: {} +users: [] diff --git a/msb2pilot/src/msb2pilot/pilot/controller.go b/msb2pilot/src/msb2pilot/pilot/controller.go new file mode 100644 index 0000000..d931605 --- /dev/null +++ b/msb2pilot/src/msb2pilot/pilot/controller.go @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package pilot + +import ( + "errors" + "msb2pilot/log" + + "istio.io/istio/pilot/pkg/config/kube/crd" + "istio.io/istio/pilot/pkg/model" +) + +type Operation string + +var ( + client *crd.Client +) + +func init() { + var err error + client, err = crd.NewClient("k8s.yml", model.ConfigDescriptor{ + model.RouteRule, + model.DestinationPolicy, + model.DestinationRule, + }, "") + + if err != nil { + log.Log.Error("fail to init crd", err) + } +} + +func Get(typ, namespace, name string) (*model.Config, bool) { + proto, err := protoSchema(typ) + if err != nil { + log.Log.Informational("get resource error", err) + return &model.Config{}, false + } + return client.Get(proto.Type, name, namespace) +} + +func protoSchema(typ string) (model.ProtoSchema, error) { + for _, desc := range client.ConfigDescriptor() { + switch typ { + case crd.ResourceName(desc.Type), crd.ResourceName(desc.Plural): + return desc, nil + } + } + return model.ProtoSchema{}, errors.New("can not find this kind of resources:[" + typ + "]") +} + +func List(typ, namespace string) ([]model.Config, error) { + proto, err := protoSchema(typ) + if err != nil { + return nil, err + } + return client.List(proto.Type, namespace) +} diff --git a/msb2pilot/src/msb2pilot/pilot/controller_test.go b/msb2pilot/src/msb2pilot/pilot/controller_test.go new file mode 100644 index 0000000..6dcea5b --- /dev/null +++ b/msb2pilot/src/msb2pilot/pilot/controller_test.go @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package pilot + +import ( + "fmt" + "testing" +) + +func TestList(t *testing.T) { + res, err := List("routerules", "default") + if err != nil { + t.Errorf("List() => got %v", err) + } else { + fmt.Print(res) + } +} -- cgit 1.2.3-korg