github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/apis/prowjobs/v1/types.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"time"
    23  
    24  	buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
    25  	corev1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  )
    28  
    29  // ProwJobType specifies how the job is triggered.
    30  type ProwJobType string
    31  
    32  // Various job types.
    33  const (
    34  	// PresubmitJob means it runs on unmerged PRs.
    35  	PresubmitJob ProwJobType = "presubmit"
    36  	// PostsubmitJob means it runs on each new commit.
    37  	PostsubmitJob = "postsubmit"
    38  	// Periodic job means it runs on a time-basis, unrelated to git changes.
    39  	PeriodicJob = "periodic"
    40  	// BatchJob tests multiple unmerged PRs at the same time.
    41  	BatchJob = "batch"
    42  )
    43  
    44  // ProwJobState specifies whether the job is running
    45  type ProwJobState string
    46  
    47  // Various job states.
    48  const (
    49  	// TriggeredState means the job has been created but not yet scheduled.
    50  	TriggeredState ProwJobState = "triggered"
    51  	// PendingState means the job is scheduled but not yet running.
    52  	PendingState = "pending"
    53  	// SuccessState means the job completed without error (exit 0)
    54  	SuccessState = "success"
    55  	// FailureState means the job completed with errors (exit non-zero)
    56  	FailureState = "failure"
    57  	// AbortedState means prow killed the job early (new commit pushed, perhaps).
    58  	AbortedState = "aborted"
    59  	// ErrorState means the job could not schedule (bad config, perhaps).
    60  	ErrorState = "error"
    61  )
    62  
    63  // ProwJobAgent specifies the controller (such as plank or jenkins-agent) that runs the job.
    64  type ProwJobAgent string
    65  
    66  const (
    67  	// KubernetesAgent means prow will create a pod to run this job.
    68  	KubernetesAgent ProwJobAgent = "kubernetes"
    69  	// JenkinsAgent means prow will schedule the job on jenkins.
    70  	JenkinsAgent = "jenkins"
    71  	// BuildAgent means prow will schedule the job via a build-crd resource.
    72  	BuildAgent = "build"
    73  )
    74  
    75  const (
    76  	// DefaultClusterAlias specifies the default cluster key to schedule jobs.
    77  	DefaultClusterAlias = "default"
    78  )
    79  
    80  // +genclient
    81  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    82  
    83  // ProwJob contains the spec as well as runtime metadata.
    84  type ProwJob struct {
    85  	metav1.TypeMeta   `json:",inline"`
    86  	metav1.ObjectMeta `json:"metadata,omitempty"`
    87  
    88  	Spec   ProwJobSpec   `json:"spec,omitempty"`
    89  	Status ProwJobStatus `json:"status,omitempty"`
    90  }
    91  
    92  // ProwJobSpec configures the details of the prow job.
    93  //
    94  // Details include the podspec, code to clone, the cluster it runs
    95  // any child jobs, concurrency limitations, etc.
    96  type ProwJobSpec struct {
    97  	// Type is the type of job and informs how
    98  	// the jobs is triggered
    99  	Type ProwJobType `json:"type,omitempty"`
   100  	// Agent determines which controller fulfills
   101  	// this specific ProwJobSpec and runs the job
   102  	Agent ProwJobAgent `json:"agent,omitempty"`
   103  	// Cluster is which Kubernetes cluster is used
   104  	// to run the job, only applicable for that
   105  	// specific agent
   106  	Cluster string `json:"cluster,omitempty"`
   107  	// Job is the name of the job
   108  	Job string `json:"job,omitempty"`
   109  	// Refs is the code under test, determined at
   110  	// runtime by Prow itself
   111  	Refs *Refs `json:"refs,omitempty"`
   112  	// ExtraRefs are auxiliary repositories that
   113  	// need to be cloned, determined from config
   114  	ExtraRefs []*Refs `json:"extra_refs,omitempty"`
   115  
   116  	// Report determines if the result of this job should
   117  	// be posted as a status on GitHub
   118  	Report bool `json:"report,omitempty"`
   119  	// Context is the name of the status context used to
   120  	// report back to GitHub
   121  	Context string `json:"context,omitempty"`
   122  	// RerunCommand is the command a user would write to
   123  	// trigger this job on their pull request
   124  	RerunCommand string `json:"rerun_command,omitempty"`
   125  	// MaxConcurrency restricts the total number of instances
   126  	// of this job that can run in parallel at once
   127  	MaxConcurrency int `json:"max_concurrency,omitempty"`
   128  
   129  	// PodSpec provides the basis for running the test under
   130  	// a Kubernetes agent
   131  	PodSpec *corev1.PodSpec `json:"pod_spec,omitempty"`
   132  
   133  	// BuildSpec provides the basis for running the test as
   134  	// a build-crd resource
   135  	// https://github.com/knative/build
   136  	BuildSpec *buildv1alpha1.BuildSpec `json:"build_spec,omitempty"`
   137  
   138  	// DecorationConfig holds configuration options for
   139  	// decorating PodSpecs that users provide
   140  	DecorationConfig *DecorationConfig `json:"decoration_config,omitempty"`
   141  
   142  	// RunAfterSuccess are jobs that should be triggered if
   143  	// this job runs and does not fail
   144  	RunAfterSuccess []ProwJobSpec `json:"run_after_success,omitempty"`
   145  }
   146  
   147  // DecorationConfig specifies how to augment pods.
   148  //
   149  // This is primarily used to provide automatic integration with gubernator
   150  // and testgrid.
   151  type DecorationConfig struct {
   152  	// Timeout is how long the pod utilities will wait
   153  	// before aborting a job with SIGINT.
   154  	Timeout time.Duration `json:"timeout,omitempty"`
   155  	// GracePeriod is how long the pod utilities will wait
   156  	// after sending SIGINT to send SIGKILL when aborting
   157  	// a job. Only applicable if decorating the PodSpec.
   158  	GracePeriod time.Duration `json:"grace_period,omitempty"`
   159  	// UtilityImages holds pull specs for utility container
   160  	// images used to decorate a PodSpec.
   161  	UtilityImages *UtilityImages `json:"utility_images,omitempty"`
   162  	// GCSConfiguration holds options for pushing logs and
   163  	// artifacts to GCS from a job.
   164  	GCSConfiguration *GCSConfiguration `json:"gcs_configuration,omitempty"`
   165  	// GCSCredentialsSecret is the name of the Kubernetes secret
   166  	// that holds GCS push credentials
   167  	GCSCredentialsSecret string `json:"gcs_credentials_secret,omitempty"`
   168  	// SSHKeySecrets are the names of Kubernetes secrets that contain
   169  	// SSK keys which should be used during the cloning process
   170  	SSHKeySecrets []string `json:"ssh_key_secrets,omitempty"`
   171  	// SkipCloning determines if we should clone source code in the
   172  	// initcontainers for jobs that specify refs
   173  	SkipCloning bool `json:"skip_cloning,omitempty"`
   174  }
   175  
   176  // UtilityImages holds pull specs for the utility images
   177  // to be used for a job
   178  type UtilityImages struct {
   179  	// CloneRefs is the pull spec used for the clonerefs utility
   180  	CloneRefs string `json:"clonerefs,omitempty"`
   181  	// InitUpload is the pull spec used for the initupload utility
   182  	InitUpload string `json:"initupload,omitempty"`
   183  	// Entrypoint is the pull spec used for the entrypoint utility
   184  	Entrypoint string `json:"entrypoint,omitempty"`
   185  	// sidecar is the pull spec used for the sidecar utility
   186  	Sidecar string `json:"sidecar,omitempty"`
   187  }
   188  
   189  // PathStrategy specifies minutia about how to contruct the url.
   190  // Usually consumed by gubernator/testgrid.
   191  const (
   192  	PathStrategyLegacy   = "legacy"
   193  	PathStrategySingle   = "single"
   194  	PathStrategyExplicit = "explicit"
   195  )
   196  
   197  // GCSConfiguration holds options for pushing logs and
   198  // artifacts to GCS from a job.
   199  type GCSConfiguration struct {
   200  	// Bucket is the GCS bucket to upload to
   201  	Bucket string `json:"bucket,omitempty"`
   202  	// PathPrefix is an optional path that follows the
   203  	// bucket name and comes before any structure
   204  	PathPrefix string `json:"path_prefix,omitempty"`
   205  	// PathStrategy dictates how the org and repo are used
   206  	// when calculating the full path to an artifact in GCS
   207  	PathStrategy string `json:"path_strategy,omitempty"`
   208  	// DefaultOrg is omitted from GCS paths when using the
   209  	// legacy or simple strategy
   210  	DefaultOrg string `json:"default_org,omitempty"`
   211  	// DefaultRepo is omitted from GCS paths when using the
   212  	// legacy or simple strategy
   213  	DefaultRepo string `json:"default_repo,omitempty"`
   214  }
   215  
   216  // ProwJobStatus provides runtime metadata, such as when it finished, whether it is running, etc.
   217  type ProwJobStatus struct {
   218  	StartTime      metav1.Time  `json:"startTime,omitempty"`
   219  	CompletionTime *metav1.Time `json:"completionTime,omitempty"`
   220  	State          ProwJobState `json:"state,omitempty"`
   221  	Description    string       `json:"description,omitempty"`
   222  	URL            string       `json:"url,omitempty"`
   223  
   224  	// PodName applies only to ProwJobs fulfilled by
   225  	// plank. This field should always be the same as
   226  	// the ProwJob.ObjectMeta.Name field.
   227  	PodName string `json:"pod_name,omitempty"`
   228  
   229  	// BuildID is the build identifier vended either by tot
   230  	// or the snowflake library for this job and used as an
   231  	// identifier for grouping artifacts in GCS for views in
   232  	// TestGrid and Gubernator. Idenitifiers vended by tot
   233  	// are monotonically increasing whereas identifiers vended
   234  	// by the snowflake library are not.
   235  	BuildID string `json:"build_id,omitempty"`
   236  
   237  	// JenkinsBuildID applies only to ProwJobs fulfilled
   238  	// by the jenkins-operator. This field is the build
   239  	// identifier that Jenkins gave to the build for this
   240  	// ProwJob.
   241  	JenkinsBuildID string `json:"jenkins_build_id,omitempty"`
   242  }
   243  
   244  // Complete returns true if the prow job has finished
   245  func (j *ProwJob) Complete() bool {
   246  	// TODO(fejta): support a timeout?
   247  	return j.Status.CompletionTime != nil
   248  }
   249  
   250  // SetComplete marks the job as completed (at time now).
   251  func (j *ProwJob) SetComplete() {
   252  	j.Status.CompletionTime = new(metav1.Time)
   253  	*j.Status.CompletionTime = metav1.Now()
   254  }
   255  
   256  // ClusterAlias specifies the key in the clusters map to use.
   257  //
   258  // This allows scheduling a prow job somewhere aside from the default build cluster.
   259  func (j *ProwJob) ClusterAlias() string {
   260  	if j.Spec.Cluster == "" {
   261  		return DefaultClusterAlias
   262  	}
   263  	return j.Spec.Cluster
   264  }
   265  
   266  // Pull describes a pull request at a particular point in time.
   267  type Pull struct {
   268  	Number int    `json:"number,omitempty"`
   269  	Author string `json:"author,omitempty"`
   270  	SHA    string `json:"sha,omitempty"`
   271  
   272  	// Ref is git ref can be checked out for a change
   273  	// for example,
   274  	// github: pull/123/head
   275  	// gerrit: refs/changes/00/123/1
   276  	Ref string `json:"ref,omitempty"`
   277  }
   278  
   279  // Refs describes how the repo was constructed.
   280  type Refs struct {
   281  	// Org is something like kubernetes or k8s.io
   282  	Org string `json:"org,omitempty"`
   283  	// Repo is something like test-infra
   284  	Repo string `json:"repo,omitempty"`
   285  
   286  	BaseRef string `json:"base_ref,omitempty"`
   287  	BaseSHA string `json:"base_sha,omitempty"`
   288  
   289  	Pulls []Pull `json:"pulls,omitempty"`
   290  
   291  	// PathAlias is the location under <root-dir>/src
   292  	// where this repository is cloned. If this is not
   293  	// set, <root-dir>/src/github.com/org/repo will be
   294  	// used as the default.
   295  	PathAlias string `json:"path_alias,omitempty"`
   296  	// CloneURI is the URI that is used to clone the
   297  	// repository. If unset, will default to
   298  	// `https://github.com/org/repo.git`.
   299  	CloneURI string `json:"clone_uri,omitempty"`
   300  }
   301  
   302  func (r Refs) String() string {
   303  	rs := []string{fmt.Sprintf("%s:%s", r.BaseRef, r.BaseSHA)}
   304  	for _, pull := range r.Pulls {
   305  		ref := fmt.Sprintf("%d:%s", pull.Number, pull.SHA)
   306  
   307  		if pull.Ref != "" {
   308  			ref = fmt.Sprintf("%s:%s", ref, pull.Ref)
   309  		}
   310  
   311  		rs = append(rs, ref)
   312  	}
   313  	return strings.Join(rs, ",")
   314  }
   315  
   316  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   317  
   318  // VirtualMachineList is a list of VirtualMachine resources
   319  type ProwJobList struct {
   320  	metav1.TypeMeta `json:",inline"`
   321  	metav1.ListMeta `json:"metadata"`
   322  
   323  	Items []ProwJob `json:"items"`
   324  }