-
Notifications
You must be signed in to change notification settings - Fork 149
/
get.go
46 lines (38 loc) · 1.26 KB
/
get.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
package session
import (
"context"
"fmt"
"strings"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func Get(kubeClient client.Client, name string, namespace string) (*InternalSession, error) {
var result *InternalSession
statefulSet := appsv1.StatefulSet{}
if err := kubeClient.Get(context.Background(), client.ObjectKey{
Namespace: namespace,
Name: name,
}, &statefulSet); err != nil {
if errors.IsNotFound(err) {
return nil, fmt.Errorf("session %s/%s not found", namespace, name)
}
return nil, err
}
labels := statefulSet.GetLabels()
if labels != nil {
if labels["app"] != "vcluster" || labels["app.kubernetes.io/part-of"] != "gitops-run" {
return nil, fmt.Errorf("%s/%s is an invalid GitOps Run session", namespace, name)
}
}
annotations := statefulSet.GetAnnotations()
result = &InternalSession{
SessionName: statefulSet.Name,
SessionNamespace: statefulSet.Namespace,
Command: annotations["run.weave.works/command"],
CliVersion: annotations["run.weave.works/cli-version"],
PortForward: strings.Split(annotations["run.weave.works/port-forward"], ","),
Namespace: annotations["run.weave.works/namespace"],
}
return result, nil
}