From 8656fc530b792b80f627caa5fb3aabfecd219fd2 Mon Sep 17 00:00:00 2001 From: wangcanfeng Date: Wed, 16 Sep 2020 10:02:55 +0800 Subject: [PATCH] fix(status): fix ready condition in status. refactor GetHealth method, using in-cluster service to get health. --- controllers/harbor/health.go | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/controllers/harbor/health.go b/controllers/harbor/health.go index 413a07c55..4b1f0e3e4 100644 --- a/controllers/harbor/health.go +++ b/controllers/harbor/health.go @@ -4,14 +4,12 @@ import ( "context" "encoding/json" "fmt" + "io/ioutil" + "net/http" + goharborv1alpha1 "github.com/goharbor/harbor-operator/api/v1alpha1" "github.com/opentracing/opentracing-go" "github.com/pkg/errors" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/runtime/serializer" - "k8s.io/client-go/rest" - - goharborv1alpha1 "github.com/goharbor/harbor-operator/api/v1alpha1" ) const ( @@ -64,33 +62,20 @@ func (r *Reconciler) GetHealth(ctx context.Context, harbor *goharborv1alpha1.Har span, ctx := opentracing.StartSpanFromContext(ctx, "check") defer span.Finish() - config := rest.CopyConfig(r.RestConfig) - config.APIPath = "api" - config = rest.AddUserAgent(config, fmt.Sprintf("%s(%s)", r.GetName(), r.GetVersion())) - config.NegotiatedSerializer = serializer.NewCodecFactory(r.Scheme) - config.GroupVersion = &corev1.SchemeGroupVersion - - client, err := rest.UnversionedRESTClientFor(config) + // access in-cluster service + url := fmt.Sprintf("http://%s.%s%s", harbor.NormalizeComponentName(goharborv1alpha1.CoreName), harbor.GetNamespace(), HarborHealthEndpoint) + resp, err := http.Get(url) if err != nil { - return nil, errors.Wrap(err, "cannot get rest client") + return nil, errors.Wrap(err, "cannot get health response") } - // https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-services/#manually-constructing-apiserver-proxy-urls - - result, err := client.Get(). - Context(ctx). - Resource("services"). - Namespace(harbor.GetNamespace()). - Name(harbor.NormalizeComponentName(goharborv1alpha1.CoreName)). - SubResource("proxy"). - Suffix(HarborHealthEndpoint). - DoRaw() + body, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil, errors.Wrap(err, "cannot get health response") + return nil, errors.Wrap(err, "cannot get health response body.") } health := &APIHealth{} - err = json.Unmarshal(result, health) + err = json.Unmarshal(body, health) return health, errors.Wrap(err, "unexpected health response") }