package hubregistry
import (
"context"
"strings"
"time"
rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1"
"github.com/goodrain/rainbond-operator/util/constants"
"github.com/goodrain/rainbond/builder/sources/registry"
"github.com/goodrain/rainbond/config/configs"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/goodrain/rainbond/pkg/component/k8s"
"github.com/goodrain/rainbond/pkg/gogo"
utils "github.com/goodrain/rainbond/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/types"
)
var defaultRegistryComponent *RegistryComponent
type RegistryComponent struct {
RegistryCli *registry.Registry
ServerConfig *configs.ServerConfig
}
func New() *RegistryComponent {
defaultRegistryComponent = &RegistryComponent{
RegistryCli: new(registry.Registry),
ServerConfig: configs.Default().ServerConfig,
}
return defaultRegistryComponent
}
func (r *RegistryComponent) Start(ctx context.Context) error {
var cluster rainbondv1alpha1.RainbondCluster
err := clients.K8SClientInitClient(k8s.Default().Clientset, k8s.Default().RestConfig)
if err != nil {
logrus.Errorf("k8s client init rainbondClient failure: %v", err)
return err
}
if err := clients.RainbondKubeClient.Get(context.Background(), types.NamespacedName{Namespace: utils.GetenvDefault("RBD_NAMESPACE", constants.Namespace), Name: "rainbondcluster"}, &cluster); err != nil {
return errors.Wrap(err, "get configuration from rainbond cluster")
}
registryConfig := cluster.Spec.ImageHub
domain := registryConfig.Domain
if strings.Contains(registryConfig.Domain, ":") {
domain = strings.Split(registryConfig.Domain, ":")[0]
}
if domain == "goodrain.me" {
registryConfig.Domain = r.ServerConfig.RbdHub
}
gogo.Go(func(ctx context.Context) error {
for {
registryCli, err := registry.NewInsecure(registryConfig.Domain, registryConfig.Username, registryConfig.Password)
if err == nil {
*r.RegistryCli = *registryCli
logrus.Infof("create hub client success")
return nil
}
logrus.Errorf("create hub client failed, try time is %d,%s", 10, err.Error())
time.Sleep(10 * time.Second)
}
})
return nil
}
func (r *RegistryComponent) CloseHandle() {
}
func Default() *RegistryComponent {
return defaultRegistryComponent
}