Browse Source

add coonection Close, add default port on Network Validate

Pedro Carmezim 4 years ago
parent
commit
1f07096404
2 changed files with 9 additions and 5 deletions
  1. 8 4
      pkg/utils/utils.go
  2. 1 1
      pkg/utils/utils_test.go

+ 8 - 4
pkg/utils/utils.go

@@ -140,19 +140,23 @@ func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.Ser
 
 
 func NetworkValidate(endpoint string, timeout time.Duration) error {
 func NetworkValidate(endpoint string, timeout time.Duration) error {
 	hostname, err := url.Parse(endpoint)
 	hostname, err := url.Parse(endpoint)
+
 	if err != nil {
 	if err != nil {
 		return fmt.Errorf("could not parse url: %w", err)
 		return fmt.Errorf("could not parse url: %w", err)
 	}
 	}
 
 
-	host, port, err := net.SplitHostPort(hostname.Host)
-	if err != nil {
-		return fmt.Errorf("could not find host and port from url: %w", err)
+	host := hostname.Hostname()
+	port := hostname.Port()
+
+	if port == "" {
+		port = "443"
 	}
 	}
 
 
 	url := fmt.Sprintf("%v:%v", host, port)
 	url := fmt.Sprintf("%v:%v", host, port)
-	_, err = net.DialTimeout("tcp", url, timeout)
+	conn, err := net.DialTimeout("tcp", url, timeout)
 	if err != nil {
 	if err != nil {
 		return fmt.Errorf("error accessing external store: %w", err)
 		return fmt.Errorf("error accessing external store: %w", err)
 	}
 	}
+	defer conn.Close()
 	return nil
 	return nil
 }
 }

+ 1 - 1
pkg/utils/utils_test.go

@@ -227,7 +227,7 @@ func TestConvertKeys(t *testing.T) {
 }
 }
 
 
 func TestValidate(t *testing.T) {
 func TestValidate(t *testing.T) {
-	err := NetworkValidate("https://google.com", 10*time.Second)
+	err := NetworkValidate("http://google.com", 10*time.Second)
 	if err != nil {
 	if err != nil {
 		t.Errorf("Connection problem: %v", err)
 		t.Errorf("Connection problem: %v", err)
 	}
 	}