This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
/
key.go
124 lines (100 loc) · 3.21 KB
/
key.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package key
import (
"context"
"fmt"
"strings"
"github.com/giantswarm/microerror"
valuemodifierpath "github.com/giantswarm/valuemodifier/path"
apiv1 "k8s.io/api/core/v1"
k8sruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/awscnfm/v12/pkg/project"
)
const (
// Credential is the default credential we use for most of our conformance
// test clusters. These credentials define which AWS Account to use.
Credential = "credential-default"
// Organization is the Giant Swarm specific organization we create our
// conformance test clusters in.
Organization = "giantswarm"
)
const (
KiamTestNamespace = "kube-system"
)
const (
draughtsmanNamespace = "draughtsman"
draughtsmanConfigMapName = "draughtsman-values-configmap"
draughtsmanConfigMapDataKey = "values"
draughtsmanConfigMapDockerRegistryKey = "Installation.V1.Registry.Domain"
)
const (
NetPolDefaultNamespaceName = "default"
NetPolTestNamespaceName = "test"
NetPolName = "deny-from-all-namespaces"
NetPolNginxTestPodName = "netpol-nginx-test-pod"
NetPolNginxTestPodAppLabel = "nginx-test-pod"
NetPolNginxSvcName = "netpol-nginx-test-svc"
LabelApp = "app"
LabelManagedBy = "managed-by"
)
func APIEndpoint(id string, base string) string {
return fmt.Sprintf("api.%s.k8s.%s", id, base)
}
func DomainFromHost(h string) string {
h = strings.Replace(h, "https://", "", 1)
h = strings.Replace(h, "g8s.", "", 1)
h = strings.Replace(h, ":443", "", 1)
return h
}
func KiamTestJobName() string {
return fmt.Sprintf("%s-kiam-test", project.Name())
}
func KiamTestNetPolName() string {
return fmt.Sprintf("%s-kiam-test", project.Name())
}
func NetPolCurlTestCommand() string {
return fmt.Sprintf("/usr/bin/curl --connect-timeout 5 %s.%s.svc", NetPolNginxSvcName, NetPolTestNamespaceName)
}
func NetPolTestJobName(namespace string) string {
return fmt.Sprintf("%s-netpol-%s-job", project.Name(), namespace)
}
func RegionFromHost(h string) string {
h = strings.Split(DomainFromHost(h), ".")[1]
// Domain in giraffe is giraffe.pek.aws.k8s.adidas.com.cn which does not follow the convention
if h == "pek" {
h = "cn-north-1"
}
return h
}
func FetchDockerRegistry(ctx context.Context, cpCtrlClient k8sruntimeclient.Client) (string, error) {
var dockerRegistry string
cm := &apiv1.ConfigMap{}
err := cpCtrlClient.Get(
ctx,
k8sruntimeclient.ObjectKey{
Name: draughtsmanConfigMapName,
Namespace: draughtsmanNamespace,
},
cm)
if err != nil {
return "", microerror.Mask(err)
}
var valueReaderService *valuemodifierpath.Service
{
valueReaderConfig := valuemodifierpath.DefaultConfig()
valueReaderConfig.InputBytes = []byte(cm.Data[draughtsmanConfigMapDataKey])
valueReaderService, err = valuemodifierpath.New(valueReaderConfig)
if err != nil {
return "", microerror.Mask(err)
}
value, err := valueReaderService.Get(draughtsmanConfigMapDockerRegistryKey)
if err != nil {
return "", microerror.Mask(err)
}
var ok bool
dockerRegistry, ok = value.(string)
if !ok {
return "", microerror.Maskf(executionFailedError, "Failed to parse DockerRegistry value from draughtsman configmap on CP.")
}
}
return dockerRegistry, nil
}