Skip to content
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

Bug 2048537: improve exposed registry connection check (#1297) #1299

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/apis/migration/v1alpha1/migcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ const (
OperatorVersionKey = "OPERATOR_VERSION"
RegistryReadinessProbeTimeout = "REGISTRY_READINESS_TIMEOUT"
RegistryLivenessProbeTimeout = "REGISTRY_LIVENESS_TIMEOUT"
RegistryValidationSubpath = "REGISTRY_VALIDATION_SUBPATH"
)

// constants
const (
RegistryDefaultProbeTimeout = 300
RegistryDefaultProbeTimeout = 300
RegistryDefaultHealthcheckSubpath = "/v2/_catalog?n=5"
)

// MigClusterSpec defines the desired state of MigCluster
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/migration/v1alpha1/migplan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func (r *MigPlan) UpdateRegistryDeployment(storage *MigStorage, deployment *apps
LivenessProbe: &kapi.Probe{
Handler: kapi.Handler{
HTTPGet: &kapi.HTTPGetAction{
Path: "/v2/_catalog?n=5",
Path: RegistryDefaultHealthcheckSubpath,
Port: intstr.IntOrString{IntVal: 5000},
},
},
Expand All @@ -517,7 +517,7 @@ func (r *MigPlan) UpdateRegistryDeployment(storage *MigStorage, deployment *apps
ReadinessProbe: &kapi.Probe{
Handler: kapi.Handler{
HTTPGet: &kapi.HTTPGetAction{
Path: "/v2/_catalog?n=5",
Path: RegistryDefaultHealthcheckSubpath,
Port: intstr.IntOrString{IntVal: 5000},
},
},
Expand Down
36 changes: 34 additions & 2 deletions pkg/controller/migcluster/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migcluster
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -306,8 +307,29 @@ func (r ReconcileMigCluster) validateRegistryRoute(ctx context.Context, cluster
func checkRegistryConnection(cluster *migapi.MigCluster, kclient k8sclient.Client) (int, error, error) {
var statusCode int

url := "https://" + cluster.Spec.ExposedRegistryPath + "/v2/"
validationSubPath := migapi.RegistryDefaultHealthcheckSubpath
clusterClient, err := cluster.GetClient(kclient)
if err != nil {
return statusCode, nil, err
}
clusterConfig, err := cluster.GetClusterConfigMap(clusterClient)
if err != nil {
return statusCode, nil, err
}
if val, exists := clusterConfig.Data[migapi.RegistryValidationSubpath]; exists {
log.V(4).Info("using configured validation subpath", "path", val)
validationSubPath = val
}

registryURI := "https://" + cluster.Spec.ExposedRegistryPath + validationSubPath
if _, err := url.ParseRequestURI(registryURI); err != nil {
log.Error(err, "failed to parse exposed registry validation url", "url", registryURI)
return statusCode, fmt.Errorf("invalid healthcheck url %s", registryURI), nil
}
restConfig, err := cluster.BuildRestConfig(kclient)
if err != nil {
return statusCode, nil, err
}
token := restConfig.BearerToken
// Construct transport using default values from http lib
defaultTransport := http.DefaultTransport.(*http.Transport)
Expand All @@ -324,7 +346,7 @@ func checkRegistryConnection(cluster *migapi.MigCluster, kclient k8sclient.Clien
}

client := &http.Client{Transport: transport}
req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequest("GET", registryURI, nil)
if err != nil {
return statusCode, nil, err
}
Expand All @@ -333,7 +355,17 @@ func checkRegistryConnection(cluster *migapi.MigCluster, kclient k8sclient.Clien
res, regErr := client.Do(req)
if regErr == nil && res != nil {
statusCode = res.StatusCode
if validationSubPath == migapi.RegistryDefaultHealthcheckSubpath {
type catalog struct {
Repositories []string `json:"repositories"`
}
if err := json.NewDecoder(res.Body).Decode(&catalog{}); err != nil {
return statusCode,
fmt.Errorf("invalid reply from registry url %s", registryURI), nil
}
}
}

return statusCode, regErr, nil
}

Expand Down