Skip to content

Commit

Permalink
add container args
Browse files Browse the repository at this point in the history
  • Loading branch information
skrishnan-sap committed Oct 15, 2024
1 parent 9784914 commit 4034b4d
Show file tree
Hide file tree
Showing 28 changed files with 202 additions and 47 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
20 changes: 9 additions & 11 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 @@ -85,11 +85,6 @@ spec:
type: string
workloads:
items:
oneOf:
- required:
- deploymentDefinition
- required:
- jobDefinition
properties:
annotations:
additionalProperties:
Expand Down Expand Up @@ -540,6 +535,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 @@ -1378,11 +1377,6 @@ spec:
monitoring:
properties:
deletionRules:
oneOf:
- required:
- metrics
- required:
- expression
properties:
expression:
type: string
Expand Down Expand Up @@ -3044,6 +3038,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
52 changes: 52 additions & 0 deletions hack/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

if ! which go >/dev/null; then
echo "Error: go not found in path"
exit 1
fi

if ! which jq >/dev/null; then
echo "Error: jq not found in path"
exit 1
fi

cd $(dirname "${BASH_SOURCE[0]}")/..

if [ ! -f go.mod ]; then
echo "Error: this is not a go module, is it?"
exit 1
fi

if [ -z "${CODEGEN_PKG:-}" ]; then
if [ -d ./vendor/k8s.io/code-generator ]; then
CODEGEN_PKG=./vendor/k8s.io/code-generator
else
CODEGEN_PKG=$(go mod download -json k8s.io/code-generator | jq -r '.Dir //empty')
fi
fi

if [ -z "${GEN_PKG_PATH:-}" ]; then
GEN_PKG_PATH=$(go list -m)/pkg
fi

echo "PWD: ${PWD}"
echo "CODEGEN_PKG: ${CODEGEN_PKG}"
echo "GEN_PKG_PATH: ${GEN_PKG_PATH}"

source "${CODEGEN_PKG}/kube_codegen.sh"

kube::codegen::gen_helpers \
--boilerplate ./hack/LICENSE_BOILERPLATE.txt \
./pkg/apis

kube::codegen::gen_client \
--with-watch \
--with-applyconfig \
--output-dir "./pkg/client" \
--output-pkg "${GEN_PKG_PATH}"/client \
--boilerplate ./hack/LICENSE_BOILERPLATE.txt \
./pkg/apis
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
47 changes: 28 additions & 19 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,25 @@ 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}
container.Command = []string{"node"}
container.Args = []string{"./node_modules/@sap/cds-mtxs/bin/cds-mtx", `$(` + EnvCAPOpTenantMtxsOperation + `)`, 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.Args = append(container.Args, "--body", `$(`+EnvCAPOpSubscriptionPayload+`)`)
}
}

Expand Down Expand Up @@ -570,6 +562,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 +621,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 +671,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 +683,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"]
args: ["./node_modules/@sap/cds-mtxs/bin/cds-mtx", "$(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"]
args: ["./node_modules/@sap/cds-mtxs/bin/cds-mtx", "$(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"]
args: ["./node_modules/@sap/cds-mtxs/bin/cds-mtx", "$(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"]
args: ["./node_modules/@sap/cds-mtxs/bin/cds-mtx", "$(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
args:
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- subscribe
- $(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
args:
- ./node_modules/@sap/cds-mtxs/bin/cds-mtx
- unsubscribe
- $(CAPOP_TENANT_MTXS_OPERATION)
- tenant-id-for-provider
image: docker.image.repo/srv/server:latest
imagePullPolicy: Always
Expand Down
Loading

0 comments on commit 4034b4d

Please sign in to comment.