Skip to content

make pilot-agent support mosn by configure #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: feature-mosn_adapter
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion install/kubernetes/helm/istio/files/injection-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ containers:
- $(POD_NAMESPACE).svc.{{ .Values.global.proxy.clusterDomain }}
- --configPath
- "{{ .ProxyConfig.ConfigPath }}"
{{- if .Values.global.sidecar.binaryPath }}
- --binaryPath
- "{{ .ProxyConfig.BinaryPath }}"
- "{{ .Values.global.sidecar.binaryPath }}"
{{- end}}
{{- if .Values.global.sidecar.implement }}
- --proxyImplement
- "{{ .Values.global.sidecar.implement }}"
{{- end}}
- --serviceCluster
{{ if ne "" (index .ObjectMeta.Labels "app") -}}
- "{{ index .ObjectMeta.Labels `app` }}.$(POD_NAMESPACE)"
Expand Down
6 changes: 5 additions & 1 deletion install/kubernetes/helm/istio/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ global:
# binaryPath is sidecar binary path
# image is sidecar and pilot-agent image, by make docker.proxyv2 .
sidecar:
binaryPath: /usr/local/bin/mosn
image: proxyv2
# mosn/envoy
implement: mosn
# binaryPath value can be set at the same time
# binaryPath: /usr/local/bin/mosn


# Comma-separated minimum per-scope logging level of messages to output, in the form of <scope>:<level>,<scope>:<level>
# The control plane has different scopes depending on component, but can configure default log level across all components
Expand Down
18 changes: 14 additions & 4 deletions pilot/cmd/pilot-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/gogo/protobuf/types"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
isitoProxy "istio.io/istio/pkg/proxy"

meshconfig "istio.io/api/mesh/v1alpha1"
networking "istio.io/api/networking/v1alpha3"
Expand All @@ -51,6 +52,7 @@ import (
"istio.io/istio/pkg/config/mesh"
"istio.io/istio/pkg/config/validation"
"istio.io/istio/pkg/envoy"
_ "istio.io/istio/pkg/mosn"
"istio.io/istio/pkg/spiffe"
"istio.io/istio/pkg/util/gogoprotomarshal"
)
Expand Down Expand Up @@ -96,6 +98,7 @@ var (
disableInternalTelemetry bool
tlsCertsToWatch []string
loggingOptions = log.DefaultOptions()
proxyImplementFlag string

wg sync.WaitGroup

Expand Down Expand Up @@ -444,7 +447,7 @@ var (

log.Infof("PilotSAN %#v", pilotSAN)

envoyProxy := envoy.NewProxy(envoy.ProxyConfig{
pilotProxyConfig := isitoProxy.ProxyConfig{
Config: proxyConfig,
Node: role.ServiceNode(),
LogLevel: proxyLogLevel,
Expand All @@ -460,12 +463,17 @@ var (
SDSTokenPath: sdsTokenPath,
ControlPlaneAuth: controlPlaneAuthEnabled,
DisableReportCalls: disableInternalTelemetry,
})
ProxyImplement: constants.ProxyImplement(proxyImplementFlag),
}

proxy, err := isitoProxy.NewProxy(pilotProxyConfig)
if err != nil {
return err
}

agent := envoy.NewAgent(envoyProxy, features.TerminationDrainDuration())
agent := isitoProxy.NewAgent(proxy, features.TerminationDrainDuration())

watcher := envoy.NewWatcher(tlsCertsToWatch, agent.Restart)

go watcher.Run(ctx)

// On SIGINT or SIGTERM, cancel the context, triggering a graceful shutdown
Expand Down Expand Up @@ -661,6 +669,8 @@ func init() {

// Flags for proxy configuration
values := mesh.DefaultProxyConfig()
proxyCmd.PersistentFlags().StringVar(&proxyImplementFlag, "proxyImplement", string(constants.IstioProxyEnvoyImplement),
"Prooxy implement, value: [envoy/mosn], default: envoy")
proxyCmd.PersistentFlags().StringVar(&configPath, "configPath", values.ConfigPath,
"Path to the generated configuration file directory")
proxyCmd.PersistentFlags().StringVar(&binaryPath, "binaryPath", values.BinaryPath,
Expand Down
8 changes: 4 additions & 4 deletions pkg/bootstrap/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ type Instance interface {

// New creates a new Instance of an Envoy bootstrap writer.
func New(cfg Config) Instance {
return &instance{
return &EnvoyBootstrap{
Config: cfg,
}
}

type instance struct {
type EnvoyBootstrap struct {
Config
}

func (i *instance) WriteTo(w io.Writer) error {
func (i *EnvoyBootstrap) WriteTo(w io.Writer) error {
// Get the input bootstrap template.
t, err := newTemplate(i.Proxy)
if err != nil {
Expand All @@ -74,7 +74,7 @@ func (i *instance) WriteTo(w io.Writer) error {
return t.Execute(w, templateParams)
}

func (i *instance) CreateFileForEpoch(epoch int) (string, error) {
func (i *EnvoyBootstrap) CreateFileForEpoch(epoch int) (string, error) {
// Create the output file.
if err := os.MkdirAll(i.Proxy.ConfigPath, 0700); err != nil {
return "", err
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package constants

type ProxyImplement string

const (
// UnspecifiedIP constant for empty IP address
UnspecifiedIP = "0.0.0.0"
Expand Down Expand Up @@ -89,4 +91,11 @@ const (

// IstioMeshGateway is the built in gateway for all sidecars
IstioMeshGateway = "mesh"

// IstioProxyImplement: envoy
IstioProxyEnvoyImplement = ProxyImplement("envoy")

// IstioProxyImplement: mosn
IstioProxyMosnImplement = ProxyImplement("mosn")

)
39 changes: 3 additions & 36 deletions pkg/envoy/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package envoy
import (
"bytes"
"fmt"
"io"
"net/http"
"istio.io/istio/pkg/proxy"
"strings"

envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
Expand Down Expand Up @@ -63,7 +62,7 @@ func GetConfigDump(adminPort uint32) (*envoyAdmin.ConfigDump, error) {

func doEnvoyGet(path string, adminPort uint32) (*bytes.Buffer, error) {
requestURL := fmt.Sprintf("http://127.0.0.1:%d/%s", adminPort, path)
buffer, err := doHTTPGet(requestURL)
buffer, err := proxy.DoHTTPGet(requestURL)
if err != nil {
return nil, err
}
Expand All @@ -72,45 +71,13 @@ func doEnvoyGet(path string, adminPort uint32) (*bytes.Buffer, error) {

func doEnvoyPost(path, contentType, body string, adminPort uint32) (*bytes.Buffer, error) {
requestURL := fmt.Sprintf("http://127.0.0.1:%d/%s", adminPort, path)
buffer, err := doHTTPPost(requestURL, contentType, body)
buffer, err := proxy.DoHTTPPost(requestURL, contentType, body)
if err != nil {
return nil, err
}
return buffer, nil
}

func doHTTPGet(requestURL string) (*bytes.Buffer, error) {
response, err := http.Get(requestURL)
if err != nil {
return nil, err
}
defer func() { _ = response.Body.Close() }()

if response.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status %d", response.StatusCode)
}

var b bytes.Buffer
if _, err := io.Copy(&b, response.Body); err != nil {
return nil, err
}
return &b, nil
}

func doHTTPPost(requestURL, contentType, body string) (*bytes.Buffer, error) {
response, err := http.Post(requestURL, contentType, strings.NewReader(body))
if err != nil {
return nil, err
}
defer func() { _ = response.Body.Close() }()

var b bytes.Buffer
if _, err := io.Copy(&b, response.Body); err != nil {
return nil, err
}
return &b, nil
}

func unmarshal(jsonString string, msg proto.Message) error {
u := jsonpb.Unmarshaler{
AllowUnknownFields: true,
Expand Down
43 changes: 22 additions & 21 deletions pkg/envoy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
"github.com/gogo/protobuf/types"
"istio.io/istio/pkg/proxy"

meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/pkg/env"
"istio.io/pkg/log"

Expand All @@ -36,6 +36,7 @@ import (
const (
// epochFileTemplate is a template for the root config JSON
epochFileTemplate = "envoy-rev%d.json"
defaultBinaryPath = "/usr/local/bin/envoy"

// drainFile is the location of the bootstrap config used for draining on istio-proxy termination
drainFile = "/var/lib/istio/envoy/envoy_bootstrap_drain.json"
Expand All @@ -46,30 +47,30 @@ const (
)

type envoy struct {
ProxyConfig
proxy.ProxyConfig
extraArgs []string
}

type ProxyConfig struct {
Config meshconfig.ProxyConfig
Node string
LogLevel string
ComponentLogLevel string
PilotSubjectAltName []string
MixerSubjectAltName []string
NodeIPs []string
DNSRefreshRate string
PodName string
PodNamespace string
PodIP net.IP
SDSUDSPath string
SDSTokenPath string
ControlPlaneAuth bool
DisableReportCalls bool

func init(){
// no need to register envoy, for envoy is the default implement for pilot-agent
proxy.RegisterDefaultProxyFactory(NewEnvoy)
}

// NewProxy creates an instance of the proxy control commands
func NewProxy(cfg ProxyConfig) Proxy {
func NewEnvoy(cfg proxy.ProxyConfig) (proxy.Proxy, error) {
binaryPath := cfg.Config.BinaryPath
if binaryPath == "" {
log.Infof("binary path from flag is empty, try using %s as binary path", defaultBinaryPath)
binaryPath = defaultBinaryPath
}

if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
return nil, err
}

cfg.Config.BinaryPath = binaryPath

// inject tracing flag for higher levels
var args []string
if cfg.LogLevel != "" {
Expand All @@ -82,7 +83,7 @@ func NewProxy(cfg ProxyConfig) Proxy {
return &envoy{
ProxyConfig: cfg,
extraArgs: args,
}
}, nil
}

func (e *envoy) IsLive() bool {
Expand Down Expand Up @@ -152,7 +153,7 @@ func (e *envoy) Run(config interface{}, epoch int, abort <-chan error) error {
// Note: the cert checking still works, the generated file is updated if certs are changed.
// We just don't save the generated file, but use a custom one instead. Pilot will keep
// monitoring the certs and restart if the content of the certs changes.
if _, ok := config.(DrainConfig); ok {
if _, ok := config.(proxy.DrainConfig); ok {
// We are doing a graceful termination, apply an empty config to drain all connections
fname = drainFile
} else if len(e.Config.CustomConfigFile) > 0 {
Expand Down
86 changes: 86 additions & 0 deletions pkg/mosn/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2018 Istio Authors
//
// 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 mosn

import (
"bytes"
"fmt"
"istio.io/istio/pkg/proxy"
"strings"

envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

// Shutdown initiates a graceful shutdown of Envoy.
func Shutdown(adminPort uint32) error {
_, err := doMosnPost("quitquitquit", "", "", adminPort)
return err
}

// GetServerInfo returns a structure representing a call to /server_info
func GetServerInfo(adminPort uint32) (*envoyAdmin.ServerInfo, error) {
buffer, err := doMosnGet("server_info", adminPort)
if err != nil {
return nil, err
}

msg := &envoyAdmin.ServerInfo{}
if err := unmarshal(buffer.String(), msg); err != nil {
return nil, err
}

return msg, nil
}

// GetConfigDump polls Envoy admin port for the config dump and returns the response.
func GetConfigDump(adminPort uint32) (*envoyAdmin.ConfigDump, error) {
buffer, err := doMosnGet("config_dump", adminPort)
if err != nil {
return nil, err
}

msg := &envoyAdmin.ConfigDump{}
if err := unmarshal(buffer.String(), msg); err != nil {
return nil, err
}
return msg, nil
}

func doMosnGet(path string, adminPort uint32) (*bytes.Buffer, error) {
requestURL := fmt.Sprintf("http://127.0.0.1:%d/%s", adminPort, path)
buffer, err := proxy.DoHTTPGet(requestURL)
if err != nil {
return nil, err
}
return buffer, nil
}

func doMosnPost(path, contentType, body string, adminPort uint32) (*bytes.Buffer, error) {
requestURL := fmt.Sprintf("http://127.0.0.1:%d/%s", adminPort, path)
buffer, err := proxy.DoHTTPPost(requestURL, contentType, body)
if err != nil {
return nil, err
}
return buffer, nil
}

func unmarshal(jsonString string, msg proto.Message) error {
u := jsonpb.Unmarshaler{
AllowUnknownFields: true,
}
return u.Unmarshal(strings.NewReader(jsonString), msg)
}
Loading