access k8s 57/58857/1
authorLvbo163 <lv.bo163@zte.com.cn>
Fri, 3 Aug 2018 07:40:59 +0000 (15:40 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Fri, 3 Aug 2018 07:40:59 +0000 (15:40 +0800)
Issue-ID: MSB-254

Change-Id: Ic93bfe9a7a6616cffa8175ed3590c0d882ff678b
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/conf/k8s.yml [new file with mode: 0644]
msb2pilot/src/msb2pilot/pilot/controller.go [new file with mode: 0644]
msb2pilot/src/msb2pilot/pilot/controller_test.go [new file with mode: 0644]

diff --git a/msb2pilot/src/msb2pilot/conf/k8s.yml b/msb2pilot/src/msb2pilot/conf/k8s.yml
new file mode 100644 (file)
index 0000000..07f512e
--- /dev/null
@@ -0,0 +1,14 @@
+apiVersion: v1\r
+clusters:\r
+- cluster:\r
+    server: http://10.74.148.106:28003\r
+  name: istio\r
+contexts:\r
+- context:\r
+    cluster: istio\r
+    user: ""\r
+  name: istio\r
+current-context: istio\r
+kind: Config\r
+preferences: {}\r
+users: []\r
diff --git a/msb2pilot/src/msb2pilot/pilot/controller.go b/msb2pilot/src/msb2pilot/pilot/controller.go
new file mode 100644 (file)
index 0000000..d931605
--- /dev/null
@@ -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 (file)
index 0000000..6dcea5b
--- /dev/null
@@ -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)
+       }
+}