From 0a97b4452ba53b078d0f041c375d7a45bb9215ff Mon Sep 17 00:00:00 2001 From: Aaron Prindle Date: Fri, 24 Mar 2023 04:38:24 +0000 Subject: [PATCH] chore: change verify schema from v1.Container to subset of direct primitive types --- docs-v2/content/en/schemas/v4beta4.json | 52 +++++++++++++++++++++++++ pkg/skaffold/schema/latest/config.go | 27 ++++++++++++- pkg/skaffold/verify/k8sjob/verify.go | 28 +++++++++++-- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/docs-v2/content/en/schemas/v4beta4.json b/docs-v2/content/en/schemas/v4beta4.json index ca62e392fa2..8cbd98b7a41 100755 --- a/docs-v2/content/en/schemas/v4beta4.json +++ b/docs-v2/content/en/schemas/v4beta4.json @@ -4275,6 +4275,57 @@ "description": "describes the supported kpt validators.", "x-intellij-html-description": "describes the supported kpt validators." }, + "VerifyContainer": { + "required": [ + "name", + "image" + ], + "properties": { + "args": { + "items": { + "type": "string" + }, + "type": "array", + "description": "arguments to the entrypoint. The container image's CMD is used if this is not provided.", + "x-intellij-html-description": "arguments to the entrypoint. The container image's CMD is used if this is not provided.", + "default": "[]" + }, + "command": { + "items": { + "type": "string" + }, + "type": "array", + "description": "entrypoint array. Not executed within a shell. The container image's ENTRYPOINT is used if this is not provided.", + "x-intellij-html-description": "entrypoint array. Not executed within a shell. The container image's ENTRYPOINT is used if this is not provided.", + "default": "[]" + }, + "image": { + "type": "string", + "description": "container image name.", + "x-intellij-html-description": "container image name." + }, + "name": { + "type": "string", + "description": "of the container.", + "x-intellij-html-description": "of the container." + } + }, + "preferredOrder": [ + "name", + "image", + "command", + "args" + ], + "additionalProperties": false, + "type": "object", + "description": "a list of tests to run on images that Skaffold builds.", + "x-intellij-html-description": "a list of tests to run on images that Skaffold builds." + }, + "VerifyEnvVar": { + "type": "object", + "description": "represents an environment variable present in a Container.", + "x-intellij-html-description": "represents an environment variable present in a Container." + }, "VerifyExecutionModeConfig": { "properties": { "kubernetesCluster": { @@ -4303,6 +4354,7 @@ ], "properties": { "container": { + "$ref": "#/definitions/VerifyContainer", "description": "container information for the verify test.", "x-intellij-html-description": "container information for the verify test." }, diff --git a/pkg/skaffold/schema/latest/config.go b/pkg/skaffold/schema/latest/config.go index ed0b43a510c..2025303a8a1 100644 --- a/pkg/skaffold/schema/latest/config.go +++ b/pkg/skaffold/schema/latest/config.go @@ -603,11 +603,36 @@ type VerifyTestCase struct { // Name is the name descriptor for the verify test. Name string `yaml:"name" yamltags:"required"` // Container is the container information for the verify test. - Container v1.Container `yaml:"container" yamltags:"required"` + Container VerifyContainer `yaml:"container" yamltags:"required"` // ExecutionMode is the execution mode used to execute the verify test case. ExecutionMode VerifyExecutionModeConfig `yaml:"executionMode,omitempty"` } +// VerifyContainer is a list of tests to run on images that Skaffold builds. +type VerifyContainer struct { + // Name of the container. + Name string `yaml:"name" yamltags:"required"` + // Image is the container image name. + Image string `yaml:"image" yamltags:"required"` + // Command is the entrypoint array. Not executed within a shell. + // The container image's ENTRYPOINT is used if this is not provided. + Command []string `yaml:"command,omitempty"` + // Args are the arguments to the entrypoint. + // The container image's CMD is used if this is not provided. + Args []string `yaml:"args,omitempty"` + // Env is the list of environment variables to set in the container. + Env []VerifyEnvVar `json:"env,omitempty"` +} + +// VerifyEnvVar represents an environment variable present in a Container. +type VerifyEnvVar struct { + // Name of the environment variable. Must be a C_IDENTIFIER. + Name string `json:"name" yamltags:"required"` + + // Value of the environment variable + Value string `json:"value"` +} + // RenderConfig contains all the configuration needed by the render steps. type RenderConfig struct { diff --git a/pkg/skaffold/verify/k8sjob/verify.go b/pkg/skaffold/verify/k8sjob/verify.go index abd655bfde9..657d3cf5ac9 100644 --- a/pkg/skaffold/verify/k8sjob/verify.go +++ b/pkg/skaffold/verify/k8sjob/verify.go @@ -327,7 +327,7 @@ func (v *Verifier) TrackContainerAndJobFromBuild(artifact graph.Artifact, contai v.logger.RegisterJob(job.Name) } -func (v *Verifier) createJob(jobName string, container corev1.Container) *batchv1.Job { +func (v *Verifier) createJob(jobName string, container latest.VerifyContainer) *batchv1.Job { job := &batchv1.Job{ TypeMeta: metav1.TypeMeta{ Kind: "Job", @@ -348,7 +348,7 @@ func (v *Verifier) createJob(jobName string, container corev1.Container) *batchv }, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{container}, + Containers: []corev1.Container{verifyContainerToK8sContainer(container)}, RestartPolicy: corev1.RestartPolicyNever, }, }, @@ -357,7 +357,27 @@ func (v *Verifier) createJob(jobName string, container corev1.Container) *batchv return job } -func (v *Verifier) createJobFromManifestPath(jobName string, container corev1.Container, manifestPath string) (*batchv1.Job, error) { +func verifyContainerToK8sContainer(vc latest.VerifyContainer) corev1.Container { + c := corev1.Container{ + Name: vc.Name, + Image: vc.Image, + Command: vc.Command, + Args: vc.Args, + } + if len(vc.Env) > 0 { + cEnv := []corev1.EnvVar{} + for _, env := range vc.Env { + cEnv = append(cEnv, corev1.EnvVar{ + Name: env.Name, + Value: env.Value, + }) + } + c.Env = cEnv + } + return c +} + +func (v *Verifier) createJobFromManifestPath(jobName string, container latest.VerifyContainer, manifestPath string) (*batchv1.Job, error) { var job *batchv1.Job b, err := ioutil.ReadFile(manifestPath) @@ -396,7 +416,7 @@ func (v *Verifier) createJobFromManifestPath(jobName string, container corev1.Co job.Labels = map[string]string{} } job.Labels["skaffold.dev/run-id"] = v.labeller.GetRunID() - job.Spec.Template.Spec.Containers = []corev1.Container{container} + job.Spec.Template.Spec.Containers = []corev1.Container{verifyContainerToK8sContainer(container)} job.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyNever job.Spec.Template.Labels["skaffold.dev/run-id"] = v.labeller.GetRunID()