-
Notifications
You must be signed in to change notification settings - Fork 8
/
podreference.go
52 lines (42 loc) · 1.42 KB
/
podreference.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubedef
import (
"fmt"
"strings"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
)
func (cpr *ContainerPodReference) UniqueID() string {
if cpr.Container == "" {
return fmt.Sprintf("%s/%s", cpr.Namespace, cpr.PodName)
}
return fmt.Sprintf("%s/%s/%s", cpr.Namespace, cpr.PodName, cpr.Container)
}
func MakePodRef(ns, name, containerName string, obj runtime.Deployable) *runtimepb.ContainerReference {
cpr := &ContainerPodReference{
Namespace: ns,
PodName: name,
Container: containerName,
}
return &runtimepb.ContainerReference{
UniqueId: cpr.UniqueID(),
HumanReference: cpr.Container,
Kind: decideKind(obj, containerName),
Opaque: protos.WrapAnyOrDie(cpr),
}
}
func decideKind(obj runtime.Deployable, containerName string) runtimepb.ContainerKind {
if obj == nil {
return runtimepb.ContainerKind_CONTAINER_KIND_UNSPECIFIED
}
if ServerCtrName(obj) == containerName {
return runtimepb.ContainerKind_PRIMARY
}
return runtimepb.ContainerKind_SUPPORT
}
func ServerCtrName(obj runtime.Deployable) string {
return strings.ToLower(obj.GetName()) // k8s doesn't accept uppercase names.
}