mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 20:57:32 +08:00
Add ingress (#18)
This commit is contained in:
84
ingress/kube/util/util.go
Normal file
84
ingress/kube/util/util.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"istio.io/istio/pilot/pkg/model"
|
||||
)
|
||||
|
||||
const DefaultDomainSuffix = "cluster.local"
|
||||
|
||||
type ClusterNamespacedName struct {
|
||||
model.NamespacedName
|
||||
ClusterId string
|
||||
}
|
||||
|
||||
func (c ClusterNamespacedName) String() string {
|
||||
return c.ClusterId + "/" + c.NamespacedName.String()
|
||||
}
|
||||
|
||||
func SplitNamespacedName(name string) model.NamespacedName {
|
||||
nsName := strings.Split(name, "/")
|
||||
if len(nsName) == 2 {
|
||||
return model.NamespacedName{
|
||||
Namespace: nsName[0],
|
||||
Name: nsName[1],
|
||||
}
|
||||
}
|
||||
|
||||
return model.NamespacedName{
|
||||
Name: nsName[0],
|
||||
}
|
||||
}
|
||||
|
||||
// CreateDestinationRuleName create the same format of DR name with ops.
|
||||
func CreateDestinationRuleName(istioCluster, namespace, name string) string {
|
||||
format := path.Join(istioCluster, namespace, name)
|
||||
hash := md5.Sum([]byte(format))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func MessageToGoGoStruct(msg proto.Message) (*types.Struct, error) {
|
||||
if msg == nil {
|
||||
return nil, errors.New("nil message")
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := (&jsonpb.Marshaler{OrigName: true}).Marshal(buf, msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbs := &types.Struct{}
|
||||
if err := jsonpb.Unmarshal(buf, pbs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pbs, nil
|
||||
}
|
||||
|
||||
func CreateServiceFQDN(namespace, name string) string {
|
||||
return fmt.Sprintf("%s.%s.svc.%s", name, namespace, DefaultDomainSuffix)
|
||||
}
|
||||
108
ingress/kube/util/util_test.go
Normal file
108
ingress/kube/util/util_test.go
Normal file
@@ -0,0 +1,108 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
||||
wasm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/wasm/v3"
|
||||
v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/wasm/v3"
|
||||
any "google.golang.org/protobuf/types/known/anypb"
|
||||
"istio.io/istio/pilot/pkg/model"
|
||||
)
|
||||
|
||||
func TestSplitNamespacedName(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expect model.NamespacedName
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
},
|
||||
{
|
||||
input: "a/",
|
||||
expect: model.NamespacedName{
|
||||
Namespace: "a",
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "a/b",
|
||||
expect: model.NamespacedName{
|
||||
Namespace: "a",
|
||||
Name: "b",
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "/b",
|
||||
expect: model.NamespacedName{
|
||||
Name: "b",
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "b",
|
||||
expect: model.NamespacedName{
|
||||
Name: "b",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run("", func(t *testing.T) {
|
||||
result := SplitNamespacedName(testCase.input)
|
||||
if result != testCase.expect {
|
||||
t.Fatalf("expect is %v, but actual is %v", testCase.expect, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateDestinationRuleName(t *testing.T) {
|
||||
istioCluster := "gw-123-istio"
|
||||
namespace := "default"
|
||||
serviceName := "go-httpbin-v1"
|
||||
t.Log(CreateDestinationRuleName(istioCluster, namespace, serviceName))
|
||||
}
|
||||
|
||||
func TestMessageToGoGoStruct(t *testing.T) {
|
||||
bytes := []byte("test")
|
||||
wasm := &wasm.Wasm{
|
||||
Config: &v3.PluginConfig{
|
||||
Name: "basic-auth",
|
||||
FailOpen: true,
|
||||
Vm: &v3.PluginConfig_VmConfig{
|
||||
VmConfig: &v3.VmConfig{
|
||||
Runtime: "envoy.wasm.runtime.null",
|
||||
Code: &corev3.AsyncDataSource{
|
||||
Specifier: &corev3.AsyncDataSource_Local{
|
||||
Local: &corev3.DataSource{
|
||||
Specifier: &corev3.DataSource_InlineString{
|
||||
InlineString: "envoy.wasm.basic_auth",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Configuration: &any.Any{
|
||||
TypeUrl: "type.googleapis.com/google.protobuf.StringValue",
|
||||
Value: bytes,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
gogoStruct, _ := MessageToGoGoStruct(wasm)
|
||||
t.Log(gogoStruct)
|
||||
}
|
||||
Reference in New Issue
Block a user