Skip to content

Commit

Permalink
Seperate YurtHubHost & YurtHubProxyHost
Browse files Browse the repository at this point in the history
  • Loading branch information
luc99hen committed Aug 18, 2022
1 parent 26465b0 commit 1ce3a71
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
10 changes: 6 additions & 4 deletions cmd/yurthub/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/openyurtio/openyurt/cmd/yurthub/app/options"
"github.com/openyurtio/openyurt/pkg/projectinfo"
ipUtils "github.com/openyurtio/openyurt/pkg/util/ip"
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/filter"
"github.com/openyurtio/openyurt/pkg/yurthub/filter/discardcloudservice"
Expand Down Expand Up @@ -118,8 +119,8 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
restMapperManager := meta.NewRESTMapperManager(storageManager)

hubServerAddr := net.JoinHostPort(options.YurtHubHost, options.YurtHubPort)
proxyServerAddr := net.JoinHostPort(options.YurtHubHost, options.YurtHubProxyPort)
proxySecureServerAddr := net.JoinHostPort(options.YurtHubHost, options.YurtHubProxySecurePort)
proxyServerAddr := net.JoinHostPort(options.YurtHubProxyHost, options.YurtHubProxyPort)
proxySecureServerAddr := net.JoinHostPort(options.YurtHubProxyHost, options.YurtHubProxySecurePort)
proxyServerDummyAddr := net.JoinHostPort(options.HubAgentDummyIfIP, options.YurtHubProxyPort)
proxySecureServerDummyAddr := net.JoinHostPort(options.HubAgentDummyIfIP, options.YurtHubProxySecurePort)
workingMode := util.WorkingMode(options.WorkingMode)
Expand All @@ -137,10 +138,11 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
}

// use dummy ip and bind ip as cert IP SANs
certIPs := []net.IP{
certIPs := ipUtils.RemoveDupIPs([]net.IP{
net.ParseIP(options.HubAgentDummyIfIP),
net.ParseIP(options.YurtHubHost),
}
net.ParseIP(options.YurtHubProxyHost),
})

cfg := &YurtHubConfiguration{
LBMode: options.LBMode,
Expand Down
7 changes: 5 additions & 2 deletions cmd/yurthub/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const (
// YurtHubOptions is the main settings for the yurthub
type YurtHubOptions struct {
ServerAddr string
YurtHubHost string
YurtHubHost string // YurtHub server host (e.g.: expose metrics API)
YurtHubProxyHost string // YurtHub proxy server host
YurtHubPort string
YurtHubProxyPort string
YurtHubProxySecurePort string
Expand Down Expand Up @@ -77,6 +78,7 @@ type YurtHubOptions struct {
func NewYurtHubOptions() *YurtHubOptions {
o := &YurtHubOptions{
YurtHubHost: "127.0.0.1",
YurtHubProxyHost: "127.0.0.1",
YurtHubProxyPort: util.YurtHubProxyPort,
YurtHubPort: util.YurtHubPort,
YurtHubProxySecurePort: util.YurtHubProxySecurePort,
Expand Down Expand Up @@ -131,8 +133,9 @@ func (options *YurtHubOptions) Validate() error {

// AddFlags returns flags for a specific yurthub by section name
func (o *YurtHubOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.YurtHubHost, "bind-address", o.YurtHubHost, "the IP address on which to listen for the --serve-port port.")
fs.StringVar(&o.YurtHubHost, "bind-address", o.YurtHubHost, "the IP address of YurtHub Server")
fs.StringVar(&o.YurtHubPort, "serve-port", o.YurtHubPort, "the port on which to serve HTTP requests(like profiling, metrics) for hub agent.")
fs.StringVar(&o.YurtHubProxyHost, "bind-proxy-address", o.YurtHubProxyHost, "the IP address of YurtHub Proxy Server")
fs.StringVar(&o.YurtHubProxyPort, "proxy-port", o.YurtHubProxyPort, "the port on which to proxy HTTP requests to kube-apiserver")
fs.StringVar(&o.YurtHubProxySecurePort, "proxy-secure-port", o.YurtHubProxySecurePort, "the port on which to proxy HTTPS requests to kube-apiserver")
fs.StringVar(&o.ServerAddr, "server-addr", o.ServerAddr, "the address of Kubernetes kube-apiserver,the format is: \"server1,server2,...\"")
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ func JoinIPStrings(ips []net.IP) string {
}
return strings.Join(strs, ",")
}

// RemoveDupIPs removes duplicate ips from the ip list and returns a new created list
func RemoveDupIPs(ips []net.IP) []net.IP {
results := make([]net.IP, 0, len(ips))
temp := map[string]bool{}
for _, ip := range ips {
if _, ok := temp[string(ip)]; !ok {
temp[string(ip)] = true
results = append(results, ip)
}
}
return results
}
45 changes: 45 additions & 0 deletions pkg/util/ip/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package ip

import (
"net"
"reflect"
"testing"
)

Expand All @@ -42,3 +44,46 @@ func TestGetLoopbackIP(t *testing.T) {
}
}
}

func TestRemoveDupIPs(t *testing.T) {
tests := []struct {
name string
originalIps []net.IP
expectedIps []net.IP
}{
{
"no duplication",
[]net.IP{[]byte("1.1.1.1")},
[]net.IP{[]byte("1.1.1.1")},
},
{
"empty list",
[]net.IP{},
[]net.IP{},
},
{
"dup list",
[]net.IP{[]byte("1.1.1.1"), []byte("1.1.1.1")},
[]net.IP{[]byte("1.1.1.1")},
},
{
"nil list",
nil,
[]net.IP{},
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
t.Logf("\tTestCase: %s", test.name)
{
get := RemoveDupIPs(test.originalIps)
if !reflect.DeepEqual(get, test.expectedIps) {
t.Errorf("\texpect %v, but get %v", test.expectedIps, get)
}
}
})
}
}

0 comments on commit 1ce3a71

Please sign in to comment.