summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHuabing Zhao <zhaohuabing@gmail.com>2018-08-04 02:04:25 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-04 02:04:25 +0000
commitea2e95fdb43c0996c5ba58c8648d3a74c956d5e1 (patch)
tree7638cad0e746c1d3fcd3fb6f40fe307325788d8e
parent29857ff4528481a89e065a5518fd38790c39f33d (diff)
parent1d6f431609b6115a2ab933fcec1c866704460d1f (diff)
Merge "access k8s"
-rw-r--r--msb2pilot/src/msb2pilot/conf/k8s.yml14
-rw-r--r--msb2pilot/src/msb2pilot/pilot/controller.go66
-rw-r--r--msb2pilot/src/msb2pilot/pilot/controller_test.go26
3 files changed, 106 insertions, 0 deletions
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)
+ }
+}