Path: blob/main/components/common-go/kubernetes/probes.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package kubernetes56import (7"context"8"crypto/tls"9"fmt"10"net"11"net/http"12"strings"13"time"1415"github.com/gitpod-io/gitpod/common-go/log"16)1718func NetworkIsReachableProbe(url string) func() error {19log.Infof("creating network check using URL %v", url)20return func() error {21tr := &http.Transport{22TLSClientConfig: &tls.Config{InsecureSkipVerify: true},23Proxy: http.ProxyFromEnvironment,24}25client := &http.Client{26Transport: tr,27Timeout: 5 * time.Second,28// never follow redirects29CheckRedirect: func(*http.Request, []*http.Request) error {30return http.ErrUseLastResponse31},32}3334resp, err := client.Get(url)35if err != nil {36log.Errorf("unexpected error checking URL %v: %v", url, err)37return err38}39resp.Body.Close()4041if resp.StatusCode > 499 {42log.WithField("url", url).WithField("statusCode", resp.StatusCode).WithError(err).Error("NetworkIsReachableProbe: unexpected status code checking URL")43return fmt.Errorf("returned status %d", resp.StatusCode)44}4546return nil47}48}4950func DNSCanResolveProbe(host string, timeout time.Duration) func() error {51log.Infof("creating DNS check for host %v", host)5253// remove port if there is one54host = strings.Split(host, ":")[0]5556resolver := net.Resolver{}57return func() error {58ctx, cancel := context.WithTimeout(context.Background(), timeout)59defer cancel()6061addrs, err := resolver.LookupHost(ctx, host)62if err != nil {63log.WithField("host", host).WithError(err).Error("NetworkIsReachableProbe: unexpected error resolving host")64return err65}6667if len(addrs) < 1 {68return fmt.Errorf("could not resolve host")69}7071return nil72}73}747576