Skip to content

Commit

Permalink
[Misc] CTOP: Additional env. variables added
Browse files Browse the repository at this point in the history
For all TenantOperations, we now add:
CAPOP_TENANT_TYPE - provider or consumer
CAPOP_APP_NAME - btp app name
CAPOP_GLOBAL_ACCOUNT_ID,
CAPOP_PROVIDER_TENANT_ID & CAPOP_PROVIDER_SUBDOMAIN

These are also present on relevant initContainers.
  • Loading branch information
Pavan-SAP committed Aug 29, 2024
1 parent d01f417 commit 442ad5a
Show file tree
Hide file tree
Showing 32 changed files with 368 additions and 51 deletions.
81 changes: 32 additions & 49 deletions internal/controller/reconcile-captenantoperation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -20,7 +18,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)

type ProvisioningPayload struct {
Expand Down Expand Up @@ -416,19 +413,21 @@ func (c *Controller) initiateJobForCAPTenantOperationStep(ctx context.Context, c
})

params := &jobCreateParams{
namePrefix: relatedResources.CAPTenant.Name + "-" + workload.Name + "-",
labels: labels,
annotations: annotations,
vcapSecretName: vcapSecretName,
imagePullSecrets: convertToLocalObjectReferences(relatedResources.CAPApplicationVersion.Spec.RegistrySecrets),
version: relatedResources.CAPApplicationVersion.Spec.Version,
namePrefix: relatedResources.CAPTenant.Name + "-" + workload.Name + "-",
labels: labels,
annotations: annotations,
vcapSecretName: vcapSecretName,
imagePullSecrets: convertToLocalObjectReferences(relatedResources.CAPApplicationVersion.Spec.RegistrySecrets),
version: relatedResources.CAPApplicationVersion.Spec.Version,
appName: relatedResources.CAPApplication.Spec.BTPAppName,
globalAccountId: relatedResources.CAPApplication.Spec.GlobalAccountId,
providerTenantId: relatedResources.CAPApplication.Spec.Provider.TenantId,
providerSubdomain: relatedResources.CAPApplication.Spec.Provider.SubDomain,
tenantType: relatedResources.CAPTenant.Labels[LabelTenantType],
}

var job *batchv1.Job
if ctop.Spec.Steps[*ctop.Status.CurrentStep-1].Type == v1alpha1.JobTenantOperation {
if params.xsuaaInstanceName, err = getXSUAAInstanceName(consumedServiceInfos, relatedResources, c.kubeClient); err != nil {
return
}
job, err = c.createTenantOperationJob(ctx, ctop, workload, params)
} else { // custom tenant operation
job, err = c.createCustomTenantOperationJob(ctx, ctop, workload, params)
Expand All @@ -454,26 +453,14 @@ type jobCreateParams struct {
vcapSecretName string
imagePullSecrets []corev1.LocalObjectReference
version string
xsuaaInstanceName string
appName string
globalAccountId string
providerTenantId string
providerSubdomain string
tenantType string
}

func (c *Controller) createTenantOperationJob(ctx context.Context, ctop *v1alpha1.CAPTenantOperation, workload *v1alpha1.WorkloadDetails, params *jobCreateParams) (*batchv1.Job, error) {
// prepare payload request
var (
payload []byte
err error
)
if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeProvisioning {
payload, err = json.Marshal(ProvisioningPayload{SubscribedSubdomain: ctop.Spec.SubDomain, EventType: "CREATE"})
} else if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeUpgrade {
payload, err = json.Marshal(UpgradePayload{Tenants: []string{ctop.Spec.TenantId}, AutoUnDeploy: true})
} else { // deprovisioning
payload, err = json.Marshal(struct{}{})
}
if err != nil {
return nil, err
}

derivedWorkload := deriveWorkloadForTenantOperation(workload)

// create job for tenant operation (provisioning / upgrade / deprovisioning)
Expand All @@ -496,8 +483,8 @@ func (c *Controller) createTenantOperationJob(ctx context.Context, ctop *v1alpha
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
ImagePullSecrets: params.imagePullSecrets,
Containers: getContainers(payload, ctop, derivedWorkload, workload, params),
InitContainers: *updateInitContainers(derivedWorkload.initContainers, getCTOPEnv(params.version, ctop), params.vcapSecretName),
Containers: getContainers(ctop, derivedWorkload, workload, params),
InitContainers: *updateInitContainers(derivedWorkload.initContainers, getCTOPEnv(params, ctop), params.vcapSecretName),
Volumes: derivedWorkload.volumes,
ServiceAccountName: derivedWorkload.serviceAccountName,
SecurityContext: derivedWorkload.podSecurityContext,
Expand All @@ -516,12 +503,12 @@ func (c *Controller) createTenantOperationJob(ctx context.Context, ctop *v1alpha
return c.kubeClient.BatchV1().Jobs(ctop.Namespace).Create(ctx, job, metav1.CreateOptions{})
}

func getContainers(payload []byte, ctop *v1alpha1.CAPTenantOperation, derivedWorkload tentantOperationWorkload, workload *v1alpha1.WorkloadDetails, params *jobCreateParams) []corev1.Container {
func getContainers(ctop *v1alpha1.CAPTenantOperation, derivedWorkload tentantOperationWorkload, workload *v1alpha1.WorkloadDetails, params *jobCreateParams) []corev1.Container {
container := &corev1.Container{
Name: workload.Name,
Image: derivedWorkload.image,
ImagePullPolicy: derivedWorkload.imagePullPolicy,
Env: append(getCTOPEnv(params.version, ctop), derivedWorkload.env...),
Env: append(getCTOPEnv(params, ctop), derivedWorkload.env...),
EnvFrom: getEnvFrom(params.vcapSecretName),
VolumeMounts: derivedWorkload.volumeMounts,
Resources: derivedWorkload.resources,
Expand Down Expand Up @@ -641,15 +628,15 @@ func (c *Controller) createCustomTenantOperationJob(ctx context.Context, ctop *v
Name: workload.Name,
Image: workload.JobDefinition.Image,
ImagePullPolicy: workload.JobDefinition.ImagePullPolicy,
Env: append(getCTOPEnv(params.version, ctop), workload.JobDefinition.Env...),
Env: append(getCTOPEnv(params, ctop), workload.JobDefinition.Env...),
EnvFrom: getEnvFrom(params.vcapSecretName),
VolumeMounts: workload.JobDefinition.VolumeMounts,
Command: workload.JobDefinition.Command,
Resources: workload.JobDefinition.Resources,
SecurityContext: workload.JobDefinition.SecurityContext,
},
},
InitContainers: *updateInitContainers(workload.JobDefinition.InitContainers, getCTOPEnv(params.version, ctop), params.vcapSecretName),
InitContainers: *updateInitContainers(workload.JobDefinition.InitContainers, getCTOPEnv(params, ctop), params.vcapSecretName),
},
},
},
Expand All @@ -659,18 +646,6 @@ func (c *Controller) createCustomTenantOperationJob(ctx context.Context, ctop *v
return c.kubeClient.BatchV1().Jobs(ctop.Namespace).Create(ctx, job, metav1.CreateOptions{})
}

func getXSUAAInstanceName(consumedServiceInfos []v1alpha1.ServiceInfo, relatedResources *cros, kubeClient kubernetes.Interface) (string, error) {
info := util.GetXSUAAInfo(consumedServiceInfos, relatedResources.CAPApplication)
if info.Secret == "" {
return "", errors.New("missing XSUAA service information")
}
entry, err := util.CreateVCAPEntryFromSecret(info, relatedResources.CAPApplication.Namespace, kubeClient)
if err != nil {
return "", err
}
return entry["name"].(string), nil
}

func addCAPTenantOperationLabels(ctop *v1alpha1.CAPTenantOperation, cat *v1alpha1.CAPTenant) (updated bool) {
appMetadata := appMetadataIdentifiers{
ownerInfo: &ownerInfo{
Expand Down Expand Up @@ -702,8 +677,16 @@ func addCAPTenantOperationLabels(ctop *v1alpha1.CAPTenantOperation, cat *v1alpha
return updated
}

func getCTOPEnv(version string, ctop *v1alpha1.CAPTenantOperation) []corev1.EnvVar {
func getCTOPEnv(params *jobCreateParams, ctop *v1alpha1.CAPTenantOperation) []corev1.EnvVar {
return []corev1.EnvVar{
{Name: EnvCAPOpAppVersion, Value: version}, {Name: EnvCAPOpTenantID, Value: ctop.Spec.TenantId}, {Name: EnvCAPOpTenantOperation, Value: string(ctop.Spec.Operation)}, {Name: EnvCAPOpTenantSubDomain, Value: string(ctop.Spec.SubDomain)},
{Name: EnvCAPOpAppVersion, Value: params.version},
{Name: EnvCAPOpTenantId, Value: ctop.Spec.TenantId},
{Name: EnvCAPOpTenantOperation, Value: string(ctop.Spec.Operation)},
{Name: EnvCAPOpTenantSubDomain, Value: string(ctop.Spec.SubDomain)},
{Name: EnvCAPOpTenantType, Value: params.tenantType},
{Name: EnvCAPOpAppName, Value: params.appName},
{Name: EnvCAPOpGlobalAccountId, Value: params.globalAccountId},
{Name: EnvCAPOpProviderTenantId, Value: params.providerTenantId},
{Name: EnvCAPOpProviderSubDomain, Value: params.providerSubdomain},
}
}
7 changes: 6 additions & 1 deletion internal/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ const RouterHttpCookieName = "CAPOP_ROUTER_STICKY"

const (
EnvCAPOpAppVersion = "CAPOP_APP_VERSION"
EnvCAPOpTenantID = "CAPOP_TENANT_ID"
EnvCAPOpTenantId = "CAPOP_TENANT_ID"
EnvCAPOpTenantSubDomain = "CAPOP_TENANT_SUBDOMAIN"
EnvCAPOpTenantOperation = "CAPOP_TENANT_OPERATION"
EnvCAPOpTenantType = "CAPOP_TENANT_TYPE"
EnvCAPOpAppName = "CAPOP_APP_NAME"
EnvCAPOpGlobalAccountId = "CAPOP_GLOBAL_ACCOUNT_ID"
EnvCAPOpProviderTenantId = "CAPOP_PROVIDER_TENANT_ID"
EnvCAPOpProviderSubDomain = "CAPOP_PROVIDER_SUBDOMAIN"
EnvCAPOpSubscriptionPayload = "CAPOP_SUBSCRIPTION_PAYLOAD"
EnvVCAPServices = "VCAP_SERVICES"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ spec:
value: provisioning
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: CAPOP_SUBSCRIPTION_PAYLOAD
valueFrom:
secretKeyRef:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: foo
value: bar
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: close
value: encounter
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: close
value: encounter
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: close
value: encounter
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: flow
value: glow
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: flow
value: glow
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: close
value: encounter
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: close
value: encounter
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ spec:
value: upgrade
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: flow
value: glow
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ spec:
value: deprovisioning
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ spec:
value: deprovisioning
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ spec:
value: deprovisioning
- name: CAPOP_TENANT_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_TYPE
value: provider
- name: CAPOP_APP_NAME
value: test-cap-01
- name: CAPOP_GLOBAL_ACCOUNT_ID
value: btp-glo-acc-id
- name: CAPOP_PROVIDER_TENANT_ID
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
Expand Down
Loading

0 comments on commit 442ad5a

Please sign in to comment.