-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
controller: dispatch host policy (#67)
TODO: write the policy to k8s --------- Signed-off-by: spacewander <[email protected]>
- Loading branch information
1 parent
53bc547
commit 9af1a7f
Showing
24 changed files
with
940 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package config | ||
|
||
func GoSoPath() string { | ||
return "/etc/libgolang.so" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package istio | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"google.golang.org/protobuf/types/known/structpb" | ||
istioapi "istio.io/api/networking/v1alpha3" | ||
istiov1a3 "istio.io/client-go/pkg/apis/networking/v1alpha3" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
ctrlcfg "mosn.io/moe/controller/internal/config" | ||
"mosn.io/moe/controller/internal/model" | ||
"mosn.io/moe/pkg/filtermanager" | ||
) | ||
|
||
const ( | ||
DefaultHttpFilter = "htnn-http-filter" | ||
) | ||
|
||
func MustNewStruct(fields map[string]interface{}) *structpb.Struct { | ||
st, err := structpb.NewStruct(fields) | ||
if err != nil { | ||
// NewStruct returns error only when the fields contain non-standard type | ||
panic(err) | ||
} | ||
return st | ||
} | ||
|
||
func DefaultEnvoyFilters() map[string]*istiov1a3.EnvoyFilter { | ||
efs := map[string]*istiov1a3.EnvoyFilter{} | ||
efs[DefaultHttpFilter] = &istiov1a3.EnvoyFilter{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: DefaultHttpFilter, | ||
}, | ||
Spec: istioapi.EnvoyFilter{ | ||
ConfigPatches: []*istioapi.EnvoyFilter_EnvoyConfigObjectPatch{ | ||
{ | ||
ApplyTo: istioapi.EnvoyFilter_HTTP_FILTER, | ||
Match: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch{ | ||
ObjectTypes: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch_Listener{ | ||
Listener: &istioapi.EnvoyFilter_ListenerMatch{ | ||
FilterChain: &istioapi.EnvoyFilter_ListenerMatch_FilterChainMatch{ | ||
Filter: &istioapi.EnvoyFilter_ListenerMatch_FilterMatch{ | ||
Name: "envoy.filters.network.http_connection_manager", | ||
SubFilter: &istioapi.EnvoyFilter_ListenerMatch_SubFilterMatch{ | ||
Name: "envoy.filters.http.router", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Patch: &istioapi.EnvoyFilter_Patch{ | ||
Operation: istioapi.EnvoyFilter_Patch_INSERT_BEFORE, | ||
Value: MustNewStruct(map[string]interface{}{ | ||
"name": "envoy.filters.http.golang", | ||
"typed_config": map[string]interface{}{ | ||
"@type": "type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.Config", | ||
"library_id": "fm", | ||
"library_path": ctrlcfg.GoSoPath(), | ||
"plugin_name": "fm", | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return efs | ||
} | ||
|
||
func GenerateHostFilter(host *model.VirtualHost, config *filtermanager.FilterManagerConfig) *istiov1a3.EnvoyFilter { | ||
v := map[string]interface{}{} | ||
// This Marshal/Unmarshal trick works around the type check in MustNewStruct | ||
data, _ := json.Marshal(config) | ||
json.Unmarshal(data, &v) | ||
return &istiov1a3.EnvoyFilter{ | ||
Spec: istioapi.EnvoyFilter{ | ||
ConfigPatches: []*istioapi.EnvoyFilter_EnvoyConfigObjectPatch{ | ||
{ | ||
ApplyTo: istioapi.EnvoyFilter_VIRTUAL_HOST, | ||
Match: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch{ | ||
ObjectTypes: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch_RouteConfiguration{ | ||
RouteConfiguration: &istioapi.EnvoyFilter_RouteConfigurationMatch{ | ||
Vhost: &istioapi.EnvoyFilter_RouteConfigurationMatch_VirtualHostMatch{ | ||
Name: host.Name, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Patch: &istioapi.EnvoyFilter_Patch{ | ||
Operation: istioapi.EnvoyFilter_Patch_MERGE, | ||
Value: MustNewStruct(map[string]interface{}{ | ||
"typed_per_filter_config": map[string]interface{}{ | ||
"envoy.filters.http.golang": map[string]interface{}{ | ||
"@type": "type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.ConfigsPerRoute", | ||
"plugins_config": map[string]interface{}{ | ||
"fm": map[string]interface{}{ | ||
"config": map[string]interface{}{ | ||
"@type": "type.googleapis.com/xds.type.v3.TypedStruct", | ||
"value": v, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package istio | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
istiov1a3 "istio.io/client-go/pkg/apis/networking/v1alpha3" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func TestDefaultFilters(t *testing.T) { | ||
out := []*istiov1a3.EnvoyFilter{} | ||
for _, ef := range DefaultEnvoyFilters() { | ||
out = append(out, ef) | ||
} | ||
d, _ := yaml.Marshal(out) | ||
actual := string(d) | ||
expFile := filepath.Join("testdata", "default_filters.yml") | ||
d, _ = os.ReadFile(expFile) | ||
want := string(d) | ||
require.Equal(t, want, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
- metadata: | ||
creationTimestamp: null | ||
name: htnn-http-filter | ||
spec: | ||
configPatches: | ||
- applyTo: HTTP_FILTER | ||
match: | ||
listener: | ||
filterChain: | ||
filter: | ||
name: envoy.filters.network.http_connection_manager | ||
subFilter: | ||
name: envoy.filters.http.router | ||
patch: | ||
operation: INSERT_BEFORE | ||
value: | ||
name: envoy.filters.http.golang | ||
typed_config: | ||
'@type': type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.Config | ||
library_id: fm | ||
library_path: /etc/libgolang.so | ||
plugin_name: fm | ||
status: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package model | ||
|
||
import "k8s.io/apimachinery/pkg/types" | ||
|
||
type Gateway struct { | ||
NsName *types.NamespacedName | ||
Port uint32 | ||
} | ||
|
||
type VirtualHost struct { | ||
Gateway *Gateway | ||
NsName *types.NamespacedName | ||
Name string | ||
} |
Oops, something went wrong.