/
resources.go
204 lines (186 loc) · 5.21 KB
/
resources.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package hive
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"fmt"
"os"
hivev1 "github.com/openshift/hive/apis/hive/v1"
hivev1azure "github.com/openshift/hive/apis/hive/v1/azure"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
"github.com/Azure/ARO-RP/pkg/api"
utillog "github.com/Azure/ARO-RP/pkg/util/log"
"github.com/Azure/ARO-RP/pkg/util/pullsecret"
)
// Changing values of these constants most likely would require
// some sort of migration on the Hive cluster for existing clusters.
const (
ClusterDeploymentName = "cluster"
aroServiceKubeconfigSecretName = "aro-service-kubeconfig-secret"
clusterServicePrincipalSecretName = "cluster-service-principal-secret"
)
var (
devEnvVars = []string{
"AZURE_FP_CLIENT_ID",
"AZURE_RP_CLIENT_ID",
"AZURE_RP_CLIENT_SECRET",
"AZURE_SUBSCRIPTION_ID",
"AZURE_TENANT_ID",
"DOMAIN_NAME",
"KEYVAULT_PREFIX",
"LOCATION",
"PROXY_HOSTNAME",
"PULL_SECRET",
"RESOURCEGROUP",
}
prodEnvVars = []string{
"AZURE_FP_CLIENT_ID",
"CLUSTER_MDSD_ACCOUNT",
"CLUSTER_MDSD_CONFIG_VERSION",
"CLUSTER_MDSD_NAMESPACE",
"DOMAIN_NAME",
"GATEWAY_DOMAINS",
"GATEWAY_RESOURCEGROUP",
"KEYVAULT_PREFIX",
"MDSD_ENVIRONMENT",
"ACR_RESOURCE_ID",
}
)
func (hr *clusterManager) resources(sub *api.SubscriptionDocument, doc *api.OpenShiftClusterDocument) ([]kruntime.Object, error) {
namespace := doc.OpenShiftCluster.Properties.HiveProfile.Namespace
clusterSP, err := clusterSPToBytes(sub, doc.OpenShiftCluster)
if err != nil {
return nil, err
}
cd := adoptedClusterDeployment(
namespace,
doc.OpenShiftCluster.Name,
doc.ID,
doc.OpenShiftCluster.Properties.InfraID,
doc.OpenShiftCluster.Location,
doc.OpenShiftCluster.Properties.NetworkProfile.APIServerPrivateEndpointIP,
)
err = utillog.EnrichHiveWithCorrelationData(cd, doc.CorrelationData)
if err != nil {
return nil, err
}
err = utillog.EnrichHiveWithResourceID(cd, doc.OpenShiftCluster.ID)
if err != nil {
return nil, err
}
return []kruntime.Object{
aroServiceKubeconfigSecret(namespace, doc.OpenShiftCluster.Properties.AROServiceKubeconfig),
clusterServicePrincipalSecret(namespace, clusterSP),
cd,
}, nil
}
func aroServiceKubeconfigSecret(namespace string, kubeConfig []byte) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: aroServiceKubeconfigSecretName,
Namespace: namespace,
},
Data: map[string][]byte{
"kubeconfig": kubeConfig,
},
Type: corev1.SecretTypeOpaque,
}
}
func clusterServicePrincipalSecret(namespace string, secret []byte) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: clusterServicePrincipalSecretName,
Namespace: namespace,
},
Data: map[string][]byte{
"osServicePrincipal.json": secret,
},
Type: corev1.SecretTypeOpaque,
}
}
func envSecret(namespace string, isDevelopment bool) *corev1.Secret {
stringdata := map[string]string{}
if isDevelopment {
for _, i := range devEnvVars {
stringdata["ARO_"+i] = os.Getenv(i)
}
} else {
for _, i := range prodEnvVars {
stringdata["ARO_"+i] = os.Getenv(i)
}
}
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: envSecretsName,
},
StringData: stringdata,
}
}
func adoptedClusterDeployment(namespace, clusterName, clusterID, infraID, location, APIServerPrivateEndpointIP string) *hivev1.ClusterDeployment {
return &hivev1.ClusterDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: ClusterDeploymentName,
Namespace: namespace,
},
Spec: hivev1.ClusterDeploymentSpec{
BaseDomain: "",
ClusterName: clusterName,
Installed: true,
ClusterMetadata: &hivev1.ClusterMetadata{
AdminKubeconfigSecretRef: corev1.LocalObjectReference{
Name: aroServiceKubeconfigSecretName,
},
ClusterID: clusterID,
InfraID: infraID,
},
Platform: hivev1.Platform{
Azure: &hivev1azure.Platform{
BaseDomainResourceGroupName: "",
Region: location,
CredentialsSecretRef: corev1.LocalObjectReference{
Name: clusterServicePrincipalSecretName,
},
},
},
ControlPlaneConfig: hivev1.ControlPlaneConfigSpec{
APIServerIPOverride: APIServerPrivateEndpointIP,
},
PreserveOnDelete: true,
ManageDNS: false,
},
}
}
func pullsecretSecret(namespace string, oc *api.OpenShiftCluster) (*corev1.Secret, error) {
pullSecret, err := pullsecret.Build(oc, string(oc.Properties.ClusterProfile.PullSecret))
if err != nil {
return nil, err
}
for _, key := range []string{"cloud.openshift.com"} {
pullSecret, err = pullsecret.RemoveKey(pullSecret, key)
if err != nil {
return nil, err
}
}
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: pullsecretSecretName,
},
StringData: map[string]string{
".dockerconfigjson": pullSecret,
},
}, nil
}
func installConfigCM(namespace string, location string) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: installConfigName,
},
StringData: map[string]string{
"install-config.yaml": fmt.Sprintf(installConfigTemplate, location),
},
}
}