-
Notifications
You must be signed in to change notification settings - Fork 786
/
kservices.go
56 lines (52 loc) · 1.55 KB
/
kservices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package kserving
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
kserve "knative.dev/serving/pkg/client/clientset/versioned"
)
// FindServiceURL finds the service URL for the given knative service name
func FindServiceURL(client kserve.Interface, kubeClient kubernetes.Interface, namespace string, name string) (string, *v1alpha1.Service, error) {
if client == nil {
return "", nil, nil
}
svc, err := client.ServingV1alpha1().Services(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return "", svc, err
}
answer := GetServiceURL(svc, kubeClient, namespace)
return answer, svc, nil
}
// GetServiceURL returns the URL for the given knative service
func GetServiceURL(service *v1alpha1.Service, kubeClient kubernetes.Interface, namespace string) string {
if service == nil {
return ""
}
domain := service.Status.DeprecatedDomain
if domain == "" {
if service.Status.Address != nil {
domain = service.Status.Address.Hostname
}
}
if domain == "" {
return ""
}
name := service.Status.LatestReadyRevisionName
if name == "" {
name = service.Status.LatestCreatedRevisionName
}
scheme := "http://"
if name != "" {
name = name + "-service"
// lets find the service to determine if its https or http
svc, err := kubeClient.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
if err == nil && svc != nil {
for _, port := range svc.Spec.Ports {
if port.Port == 443 {
scheme = "https://"
}
}
}
}
return scheme + domain
}