Skip to content

Commit

Permalink
fix: get grafana hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Aug 4, 2022
1 parent de5d19c commit facb27b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
54 changes: 8 additions & 46 deletions api-server/services/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net"
"strings"

"github.com/ghodss/yaml"
Expand All @@ -27,6 +26,8 @@ import (

"github.com/bentoml/grafana-operator/api/integreatly/v1alpha1"

"github.com/bentoml/yatai-common/system"

"github.com/bentoml/yatai-schemas/modelschemas"
"github.com/bentoml/yatai/api-server/config"
"github.com/bentoml/yatai/api-server/models"
Expand Down Expand Up @@ -379,55 +380,16 @@ func (s *clusterService) GetDockerRegistryRef(ctx context.Context, cluster *mode
return ref, nil
}

func (s *clusterService) GetIngressIp(ctx context.Context, cluster *models.Cluster) (string, error) {
var ip string
if cluster.Config != nil {
ip = cluster.Config.IngressIp
}
if ip == "" {
cliset, _, err := s.GetKubeCliSet(ctx, cluster)
if err != nil {
return "", errors.Wrap(err, "get kube cli set")
}
servicesCli := cliset.CoreV1().Services(consts.KubeNamespaceYataiComponents)
svcName := "yatai-ingress-controller-ingress-nginx-controller"
svc, err := servicesCli.Get(ctx, svcName, metav1.GetOptions{})
if err != nil {
return "", errors.Wrap(err, "get ingress service")
}
if len(svc.Status.LoadBalancer.Ingress) == 0 {
return "", errors.Errorf("the external ip of service %s on namespace %s is empty!", svcName, consts.KubeNamespaceYataiComponents)
}

ing := svc.Status.LoadBalancer.Ingress[0]

ip = ing.IP
if ip == "" {
ip = ing.Hostname
}
}
if ip == "" {
return "", errors.Errorf("please specify the ingress ip or hostname in cluster %s", cluster.Name)
}
if net.ParseIP(ip) == nil {
addr, err := net.LookupIP(ip)
if err != nil {
return "", errors.Wrapf(err, "lookup ip from ingress hostname %s in cluster %s", ip, cluster.Name)
}
if len(addr) == 0 {
return "", errors.Errorf("cannot lookup ip from ingress hostname %s in cluster %s", ip, cluster.Name)
}
ip = addr[0].String()
}
return ip, nil
}

func (s *clusterService) GenerateGrafanaHostname(ctx context.Context, cluster *models.Cluster) (string, error) {
ip, err := ClusterService.GetIngressIp(ctx, cluster)
clientset, _, err := s.GetKubeCliSet(ctx, cluster)
if err != nil {
return "", errors.Wrap(err, "get kube cli set")
}
domainSuffix, err := system.GetDomainSuffix(ctx, clientset)
if err != nil {
return "", err
}
return fmt.Sprintf("grafana-yatai-infra-external-%s.apps.yatai.dev", strings.ReplaceAll(ip, ".", "-")), nil
return fmt.Sprintf("grafana-yatai-infra-external.%s", domainSuffix), nil
}

func (s *clusterService) GetGrafanaRootPath(ctx context.Context, cluster *models.Cluster) (string, error) {
Expand Down
9 changes: 7 additions & 2 deletions api-server/services/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
networkingtypev1 "k8s.io/client-go/kubernetes/typed/networking/v1"
"k8s.io/client-go/rest"

"github.com/bentoml/yatai-common/system"
"github.com/bentoml/yatai-schemas/modelschemas"
"github.com/bentoml/yatai/api-server/models"
"github.com/bentoml/yatai/common/consts"
Expand Down Expand Up @@ -602,11 +603,15 @@ func (s *deploymentService) GenerateDefaultHostname(ctx context.Context, deploym
if err != nil {
return "", err
}
ip, err := ClusterService.GetIngressIp(ctx, cluster)
clientset, _, err := ClusterService.GetKubeCliSet(ctx, cluster)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-yatai-%s.apps.yatai.dev", deployment.Name, strings.ReplaceAll(ip, ".", "-")), nil
domainSuffix, err := system.GetDomainSuffix(ctx, clientset)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s.%s", deployment.Name, deployment.KubeNamespace, domainSuffix), nil
}

func (s *deploymentService) GetURLs(ctx context.Context, deployment *models.Deployment) ([]string, error) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/aws/aws-sdk-go v1.43.26 // indirect
github.com/bentoml/yatai-common v0.0.0-20220801050420-8e725fdff9f6
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiU
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bentoml/grafana-operator v1.4.1-0.20210927064226-14795530b647 h1:fKWmU8aM6tda4yThxj8SGKWJYCxjLW9gSM6WIIFFgvo=
github.com/bentoml/grafana-operator v1.4.1-0.20210927064226-14795530b647/go.mod h1:w8sk7HbPMnOlZislz8Ch3pRw15FNMY6o1Q+53fepnhc=
github.com/bentoml/yatai-common v0.0.0-20220801050420-8e725fdff9f6 h1:XB2vKCbtYtO4/mSydE/op8fnL+3g7CZbwdN1oUel/SA=
github.com/bentoml/yatai-common v0.0.0-20220801050420-8e725fdff9f6/go.mod h1:evYb3j9/fzKsKFxU5l3DD/erGc0aEYPRlKTjXlnX1do=
github.com/bentoml/yatai-deployment-operator v0.0.0-20220509150025-8aae33c8b98b h1:FTDJ+5TMgBWGB8EYRvLyJKtYDnWKvE6CqqYnhY+TEe4=
github.com/bentoml/yatai-deployment-operator v0.0.0-20220509150025-8aae33c8b98b/go.mod h1:vu1jltSCPUq+nKmO/ixQxAVKrYEcT/XXGIhluCMtLs8=
github.com/bentoml/yatai-schemas v0.0.0-20220425180144-46a9d8e948f8/go.mod h1:OT5T7rrtdjJIkJpUazp1fQBbAVzl55YXyUJNVRLqskw=
Expand Down

0 comments on commit facb27b

Please sign in to comment.