Skip to content

Commit

Permalink
[FEAT] add container args (#145)
Browse files Browse the repository at this point in the history
CHANGES:
- added `args` to containers (includes CRD changes)
- added an environment variable `CAPOP_TENANT_MTXS_OPERATION ` for
tenant operations to supply the `mtxs` command. All tenant operation
related environment variables, including subscription payload, are now
available in the corresponding `initContainers`

---------

Co-authored-by: Pavan <[email protected]>
  • Loading branch information
skrishnan-sap and Pavan-SAP authored Oct 15, 2024
1 parent 9784914 commit b363b96
Show file tree
Hide file tree
Showing 27 changed files with 155 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crds/sme.sap.com_capapplications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.2
controller-gen.kubebuilder.io/version: v0.16.4
name: capapplications.sme.sap.com
spec:
group: sme.sap.com
Expand Down
18 changes: 13 additions & 5 deletions crds/sme.sap.com_capapplicationversions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.2
controller-gen.kubebuilder.io/version: v0.16.4
name: capapplicationversions.sme.sap.com
spec:
group: sme.sap.com
Expand Down Expand Up @@ -540,6 +540,10 @@ spec:
x-kubernetes-list-type: atomic
type: object
type: object
args:
items:
type: string
type: array
command:
items:
type: string
Expand Down Expand Up @@ -1379,10 +1383,10 @@ spec:
properties:
deletionRules:
oneOf:
- required:
- metrics
- required:
- expression
- required:
- metrics
- required:
- expression
properties:
expression:
type: string
Expand Down Expand Up @@ -3044,6 +3048,10 @@ spec:
x-kubernetes-list-type: atomic
type: object
type: object
args:
items:
type: string
type: array
backoffLimit:
format: int32
type: integer
Expand Down
2 changes: 1 addition & 1 deletion crds/sme.sap.com_captenantoperations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.2
controller-gen.kubebuilder.io/version: v0.16.4
name: captenantoperations.sme.sap.com
spec:
group: sme.sap.com
Expand Down
2 changes: 1 addition & 1 deletion crds/sme.sap.com_captenantoutputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.2
controller-gen.kubebuilder.io/version: v0.16.4
name: captenantoutputs.sme.sap.com
spec:
group: sme.sap.com
Expand Down
2 changes: 1 addition & 1 deletion crds/sme.sap.com_captenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.2
controller-gen.kubebuilder.io/version: v0.16.4
name: captenants.sme.sap.com
spec:
group: sme.sap.com
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/reconcile-capapplicationversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func newContentDeploymentJob(cav *v1alpha1.CAPApplicationVersion, workload *v1al
Image: workload.JobDefinition.Image,
ImagePullPolicy: workload.JobDefinition.ImagePullPolicy,
Command: workload.JobDefinition.Command,
Args: workload.JobDefinition.Args,
Env: append([]corev1.EnvVar{
{Name: EnvCAPOpAppVersion, Value: cav.Spec.Version},
}, workload.JobDefinition.Env...),
Expand Down Expand Up @@ -754,6 +755,7 @@ func getContainer(params *DeploymentParameters) []corev1.Container {
Image: params.WorkloadDetails.DeploymentDefinition.Image,
ImagePullPolicy: params.WorkloadDetails.DeploymentDefinition.ImagePullPolicy,
Command: params.WorkloadDetails.DeploymentDefinition.Command,
Args: params.WorkloadDetails.DeploymentDefinition.Args,
Env: getEnv(params),
EnvFrom: getEnvFrom(params.VCAPSecretName),
VolumeMounts: params.WorkloadDetails.DeploymentDefinition.VolumeMounts,
Expand Down
53 changes: 29 additions & 24 deletions internal/controller/reconcile-captenantoperation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type tentantOperationWorkload struct {
image string
imagePullPolicy corev1.PullPolicy
command []string
args []string
env []corev1.EnvVar
volumeMounts []corev1.VolumeMount
volumes []corev1.Volume
Expand Down Expand Up @@ -484,7 +485,7 @@ func (c *Controller) createTenantOperationJob(ctx context.Context, ctop *v1alpha
RestartPolicy: corev1.RestartPolicyNever,
ImagePullSecrets: params.imagePullSecrets,
Containers: getContainers(ctop, derivedWorkload, workload, params),
InitContainers: *updateInitContainers(derivedWorkload.initContainers, getCTOPEnv(params, ctop), params.vcapSecretName),
InitContainers: *updateInitContainers(derivedWorkload.initContainers, getCTOPEnv(params, ctop, v1alpha1.JobTenantOperation), params.vcapSecretName),
Volumes: derivedWorkload.volumes,
ServiceAccountName: derivedWorkload.serviceAccountName,
SecurityContext: derivedWorkload.podSecurityContext,
Expand All @@ -508,34 +509,21 @@ func getContainers(ctop *v1alpha1.CAPTenantOperation, derivedWorkload tentantOpe
Name: workload.Name,
Image: derivedWorkload.image,
ImagePullPolicy: derivedWorkload.imagePullPolicy,
Env: append(getCTOPEnv(params, ctop), derivedWorkload.env...),
Env: append(getCTOPEnv(params, ctop, v1alpha1.JobTenantOperation), derivedWorkload.env...),
EnvFrom: getEnvFrom(params.vcapSecretName),
VolumeMounts: derivedWorkload.volumeMounts,
Resources: derivedWorkload.resources,
SecurityContext: derivedWorkload.securityContext,
}

var operation string

if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeProvisioning {
operation = "subscribe"
} else if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeUpgrade {
operation = "upgrade"
} else { // deprovisioning
operation = "unsubscribe"
}

appendCommand := false
if derivedWorkload.command != nil {
container.Command = derivedWorkload.command
container.Args = derivedWorkload.args
} else {
container.Command = []string{"node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx", operation, ctop.Spec.TenantId}
appendCommand = true
}
if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeProvisioning {
container.Env = append(container.Env, corev1.EnvVar{Name: EnvCAPOpSubscriptionPayload, ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: ctop.Annotations[AnnotationSubscriptionContextSecret]}, Key: SubscriptionContext}}})
if appendCommand {
container.Command = append(container.Command, "--body", `$(`+EnvCAPOpSubscriptionPayload+`)`)
container.Command = []string{"node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx"} // Use entrypoint for mtxs as the command
container.Args = []string{`$(` + EnvCAPOpTenantMtxsOperation + `)`, ctop.Spec.TenantId}
if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeProvisioning {
container.Args = append(container.Args, "--body", `$(`+EnvCAPOpSubscriptionPayload+`)`)
}
}

Expand Down Expand Up @@ -570,6 +558,7 @@ func deriveWorkloadForTenantOperation(workload *v1alpha1.WorkloadDetails) tentan
result.image = workload.JobDefinition.Image
result.imagePullPolicy = workload.JobDefinition.ImagePullPolicy
result.command = workload.JobDefinition.Command
result.args = workload.JobDefinition.Args
result.env = workload.JobDefinition.Env
result.volumeMounts = workload.JobDefinition.VolumeMounts
result.volumes = workload.JobDefinition.Volumes
Expand Down Expand Up @@ -628,15 +617,16 @@ func (c *Controller) createCustomTenantOperationJob(ctx context.Context, ctop *v
Name: workload.Name,
Image: workload.JobDefinition.Image,
ImagePullPolicy: workload.JobDefinition.ImagePullPolicy,
Env: append(getCTOPEnv(params, ctop), workload.JobDefinition.Env...),
Env: append(getCTOPEnv(params, ctop, v1alpha1.JobCustomTenantOperation), workload.JobDefinition.Env...),
EnvFrom: getEnvFrom(params.vcapSecretName),
VolumeMounts: workload.JobDefinition.VolumeMounts,
Command: workload.JobDefinition.Command,
Args: workload.JobDefinition.Args,
Resources: workload.JobDefinition.Resources,
SecurityContext: workload.JobDefinition.SecurityContext,
},
},
InitContainers: *updateInitContainers(workload.JobDefinition.InitContainers, getCTOPEnv(params, ctop), params.vcapSecretName),
InitContainers: *updateInitContainers(workload.JobDefinition.InitContainers, getCTOPEnv(params, ctop, v1alpha1.JobCustomTenantOperation), params.vcapSecretName),
},
},
},
Expand Down Expand Up @@ -677,8 +667,8 @@ func addCAPTenantOperationLabels(ctop *v1alpha1.CAPTenantOperation, cat *v1alpha
return updated
}

func getCTOPEnv(params *jobCreateParams, ctop *v1alpha1.CAPTenantOperation) []corev1.EnvVar {
return []corev1.EnvVar{
func getCTOPEnv(params *jobCreateParams, ctop *v1alpha1.CAPTenantOperation, stepType v1alpha1.JobType) []corev1.EnvVar {
env := []corev1.EnvVar{
{Name: EnvCAPOpAppVersion, Value: params.version},
{Name: EnvCAPOpTenantId, Value: ctop.Spec.TenantId},
{Name: EnvCAPOpTenantOperation, Value: string(ctop.Spec.Operation)},
Expand All @@ -689,4 +679,19 @@ func getCTOPEnv(params *jobCreateParams, ctop *v1alpha1.CAPTenantOperation) []co
{Name: EnvCAPOpProviderTenantId, Value: params.providerTenantId},
{Name: EnvCAPOpProviderSubDomain, Value: params.providerSubdomain},
}

if stepType == v1alpha1.JobTenantOperation {
var operation string
if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeProvisioning {
operation = "subscribe"
env = append(env, corev1.EnvVar{Name: EnvCAPOpSubscriptionPayload, ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: ctop.Annotations[AnnotationSubscriptionContextSecret]}, Key: SubscriptionContext}}})
} else if ctop.Spec.Operation == v1alpha1.CAPTenantOperationTypeUpgrade {
operation = "upgrade"
} else { // deprovisioning
operation = "unsubscribe"
}
env = append(env, corev1.EnvVar{Name: EnvCAPOpTenantMtxsOperation, Value: operation})
}

return env
}
1 change: 1 addition & 0 deletions internal/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const (
EnvCAPOpTenantId = "CAPOP_TENANT_ID"
EnvCAPOpTenantSubDomain = "CAPOP_TENANT_SUBDOMAIN"
EnvCAPOpTenantOperation = "CAPOP_TENANT_OPERATION"
EnvCAPOpTenantMtxsOperation = "CAPOP_TENANT_MTXS_OPERATION"
EnvCAPOpTenantType = "CAPOP_TENANT_TYPE"
EnvCAPOpAppName = "CAPOP_APP_NAME"
EnvCAPOpGlobalAccountId = "CAPOP_GLOBAL_ACCOUNT_ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ spec:
secretKeyRef:
name: test-cap-01-gen
key: subscriptionContext
- name: CAPOP_TENANT_MTXS_OPERATION
value: subscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx", "subscribe", "tenant-id-for-provider", "--body", "$(CAPOP_SUBSCRIPTION_PAYLOAD)"]
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx"]
args: ["$(CAPOP_TENANT_MTXS_OPERATION)", "tenant-id-for-provider", "--body", "$(CAPOP_SUBSCRIPTION_PAYLOAD)"]
image: docker.image.repo/srv/server:latest
name: mtx
imagePullSecrets:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ spec:
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_MTXS_OPERATION
value: upgrade
- name: foo
value: bar
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-cap-backend-gen
optional: true
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx", "upgrade", "tenant-id-for-provider"]
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx"]
args: ["$(CAPOP_TENANT_MTXS_OPERATION)", "tenant-id-for-provider"]
image: docker.image.repo/srv/server:v2
name: cap-backend
imagePullSecrets:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ spec:
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_MTXS_OPERATION
value: upgrade
- name: flow
value: glow
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-ten-op-gen
optional: true
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx", "upgrade", "tenant-id-for-provider"]
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx"]
args: ["$(CAPOP_TENANT_MTXS_OPERATION)", "tenant-id-for-provider"]
image: docker.image.repo/srv/server:latest
name: ten-op
imagePullSecrets:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ spec:
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_MTXS_OPERATION
value: unsubscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx", "unsubscribe", "tenant-id-for-provider"]
command: ["node", "./node_modules/@sap/cds-mtxs/bin/cds-mtx"]
args: ["$(CAPOP_TENANT_MTXS_OPERATION)", "tenant-id-for-provider"]
image: docker.image.repo/srv/server:latest
name: mtx
imagePullSecrets:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ spec:
secretKeyRef:
name: test-cap-01-gen
key: subscriptionContext
- name: CAPOP_TENANT_MTXS_OPERATION
value: subscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command:
- node
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- subscribe
args:
- $(CAPOP_TENANT_MTXS_OPERATION)
- tenant-id-for-provider
- --body
- $(CAPOP_SUBSCRIPTION_PAYLOAD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,17 @@ spec:
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_MTXS_OPERATION
value: unsubscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command:
- node
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- unsubscribe
args:
- $(CAPOP_TENANT_MTXS_OPERATION)
- tenant-id-for-provider
image: docker.image.repo/srv/server:latest
imagePullPolicy: Always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,17 @@ spec:
value: tenant-id-for-provider
- name: CAPOP_PROVIDER_SUBDOMAIN
value: my-provider
- name: CAPOP_TENANT_MTXS_OPERATION
value: upgrade
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command:
- node
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- upgrade
args:
- $(CAPOP_TENANT_MTXS_OPERATION)
- tenant-id-for-provider
image: docker.image.repo/srv/server:latest
imagePullPolicy: Always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ spec:
secretKeyRef:
name: test-cap-01-gen
key: subscriptionContext
- name: CAPOP_TENANT_MTXS_OPERATION
value: subscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command:
- node
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- subscribe
args:
- $(CAPOP_TENANT_MTXS_OPERATION)
- tenant-id-for-provider
- --body
- $(CAPOP_SUBSCRIPTION_PAYLOAD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ spec:
secretKeyRef:
name: test-cap-01-gen
key: subscriptionContext
- name: CAPOP_TENANT_MTXS_OPERATION
value: subscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
optional: true
command:
- node
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- subscribe
args:
- $(CAPOP_TENANT_MTXS_OPERATION)
- tenant-id-for-provider
- --body
- $(CAPOP_SUBSCRIPTION_PAYLOAD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ spec:
secretKeyRef:
name: test-cap-01-gen
key: subscriptionContext
- name: CAPOP_TENANT_MTXS_OPERATION
value: subscribe
envFrom:
- secretRef:
name: test-cap-01-provider-abcd-mtx-gen
Expand Down
Loading

0 comments on commit b363b96

Please sign in to comment.