-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 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,69 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gitpod-io/gitpod/common-go/log" | ||
) | ||
|
||
func NetworkIsReachableProbe(url string) func() error { | ||
log.Infof("creating network check using URL %v", url) | ||
return func() error { | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
client := &http.Client{ | ||
Transport: tr, | ||
Timeout: 1 * time.Second, | ||
// never follow redirects | ||
CheckRedirect: func(*http.Request, []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
|
||
resp, err := client.Get(url) | ||
if err != nil { | ||
log.Errorf("unexpected error checking URL %v: %v", url, err) | ||
return err | ||
} | ||
resp.Body.Close() | ||
|
||
if resp.StatusCode > 399 { | ||
log.WithField("url", url).WithField("statusCode", resp.StatusCode).Error("NetworkIsReachableProbe: unexpected status code checking URL") | ||
return fmt.Errorf("returned status %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func DNSCanResolveProbe(host string, timeout time.Duration) func() error { | ||
log.Infof("creating DNS check for host %v", host) | ||
|
||
resolver := net.Resolver{} | ||
return func() error { | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
addrs, err := resolver.LookupHost(ctx, host) | ||
if err != nil { | ||
log.WithField("host", host).Error("NetworkIsReachableProbe: unexpected error resolving host") | ||
return err | ||
} | ||
|
||
if len(addrs) < 1 { | ||
return fmt.Errorf("could not resolve host") | ||
} | ||
|
||
return nil | ||
} | ||
} |