Skip to content

Commit 217b41a

Browse files
committed
Handle company environments
1 parent 9440a60 commit 217b41a

3 files changed

Lines changed: 56 additions & 9 deletions

File tree

api/core/v1alpha1/company_types.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ type Cluster struct {
3939
Description string `json:"description,omitempty"`
4040
}
4141

42+
type Environment struct {
43+
Label string `json:"label"`
44+
Description string `json:"description,omitempty"`
45+
Hosts []struct {
46+
Scheme string `json:"scheme"`
47+
Host string `json:"host"`
48+
IsBackoffice bool `json:"isBackoffice"`
49+
} `json:"hosts,omitempty"`
50+
Cluster struct {
51+
ClusterId string `json:"clusterId"`
52+
Namespace string `json:"namespace"`
53+
KubeContextVariables struct {
54+
KubeUrl string `json:"KUBE_URL"`
55+
KubeToken string `json:"KUBE_TOKEN"`
56+
KubeCAPEM string `json:"KUBE_CA_PEM"`
57+
} `json:"kubeContextVariables"`
58+
} `json:"cluster"`
59+
EnvId string `json:"envId"`
60+
EnvPrefix string `json:"envPrefix"`
61+
}
62+
4263
// CompanySpec defines the desired state of Company.
4364
type CompanySpec struct {
4465
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
@@ -50,8 +71,7 @@ type CompanySpec struct {
5071
CompanyOwners []string `json:"companyOwners,omitempty"`
5172
ConsoleRef NamespacedName `json:"consoleRef"`
5273
Clusters []Cluster `json:"clusters,omitempty"`
53-
// Clusters []string `json:"companyOwners"`
54-
// Envs []string `json:"companyOwners"`
74+
Environments []Environment `json:"environments,omitempty"`
5575
}
5676

5777
// CompanyStatus defines the observed state of Company.

pkg/company-controller/company_controller.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,25 @@ func (r *CompanyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
153153
}
154154
}
155155

156+
var clustersMap map[string]string = make(map[string]string)
156157
for _, cluster := range company.Spec.Clusters {
157-
158158
var serviceAccountTokenSecret v1.Secret
159159
if err := r.Get(ctx, types.NamespacedName{Name: cluster.Connection.ServiceAccountToken.SecretRef, Namespace: company.Namespace}, &serviceAccountTokenSecret); err != nil {
160160
log.Error(err, "unable to fetch cluster service account token Secret for Company")
161161
continue
162162
}
163163
var serviceAccountToken = string(serviceAccountTokenSecret.Data[cluster.Connection.ServiceAccountToken.KeyRef])
164164

165-
if err := consoleClient.AddCompanyCluster(ctx, cluster, companyId, serviceAccountToken); err != nil {
165+
if clusterId, err := consoleClient.AddCompanyCluster(ctx, cluster, companyId, serviceAccountToken); err != nil {
166166
log.Error(err, "failed to add company cluster in Console", "companyName", companyName)
167+
} else {
168+
clustersMap[cluster.ClusterId] = clusterId
169+
log.Info("Cluster added successfully to Company in Console", "companyName", companyName, "clusterId", clusterId)
167170
}
168-
171+
}
172+
log.Info("Clusters map: ", clustersMap)
173+
if addEnvironmentsErr := consoleClient.AddCompanyEnvironments(ctx, company.Spec.Environments, companyId, clustersMap); addEnvironmentsErr != nil {
174+
log.Error(addEnvironmentsErr, "failed to add company environments in Console", "companyName", companyName)
169175
}
170176

171177
company.Status.CompanyId = companyId

pkg/console-client/console_client.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (c *Client) AddCompanyOwners(ctx context.Context, company corev1alpha1.Comp
221221
return errors
222222
}
223223

224-
func (c *Client) AddCompanyCluster(ctx context.Context, cluster corev1alpha1.Cluster, companyId string, serviceAccountToken string) error {
224+
func (c *Client) AddCompanyCluster(ctx context.Context, cluster corev1alpha1.Cluster, companyId string, serviceAccountToken string) (string, error) {
225225

226226
var log = log.Default()
227227
addClusterURL := fmt.Sprintf("/api/tenants/%s/clusters/", companyId)
@@ -243,12 +243,33 @@ func (c *Client) AddCompanyCluster(ctx context.Context, cluster corev1alpha1.Clu
243243
log.Printf("Adding cluster %s to company %s. Calling URL %s", cluster.ClusterId, companyId, addClusterURL)
244244
log.Printf("Payload %s", clusterPayload)
245245

246-
if err := c.PostJSON(ctx, addClusterURL, clusterPayload, nil); err != nil {
246+
var result struct {
247+
ClusterId string `json:"_id"`
248+
}
249+
if err := c.PostJSON(ctx, addClusterURL, clusterPayload, &result); err != nil {
247250
log.Printf("Error %s", err)
248-
return err
251+
return "", err
249252
}
250-
return nil
251253

254+
return result.ClusterId, nil
255+
}
256+
257+
func (c *Client) AddCompanyEnvironments(ctx context.Context, environments []corev1alpha1.Environment, companyId string, clusters map[string]string) error {
258+
var log = log.Default()
259+
var addEnvPath = fmt.Sprintf("/tenants/%s/project-blueprint/environments", companyId)
260+
var environmentsPayload []corev1alpha1.Environment = []corev1alpha1.Environment{}
261+
for _, env := range environments {
262+
clusterId, exists := clusters[env.Cluster.ClusterId]
263+
if exists && clusterId != "" {
264+
env.Cluster.ClusterId = clusterId
265+
environmentsPayload = append(environmentsPayload, env)
266+
} else {
267+
log.Fatal("ClusterId not found for environment ", env.EnvId, " with cluster ", env.Cluster.ClusterId)
268+
}
269+
}
270+
log.Printf("Adding environments to company %s. Calling URL %s", companyId, addEnvPath)
271+
log.Printf("Payload %+v", environmentsPayload)
272+
return c.PostJSON(ctx, addEnvPath, environmentsPayload, nil)
252273
}
253274

254275
// PostJSON is a convenience method that performs POST and unmarshals JSON response

0 commit comments

Comments
 (0)